0% found this document useful (0 votes)
5 views

Numpy Week 2 Notes New

The document provides an overview of Numpy, including the creation and manipulation of Numpy arrays and matrices, as well as various functions such as np.arange(), np.linspace(), and np.zeros(). It covers mathematical operations, random value generation, and accessing and modifying entries in arrays and matrices. Additionally, it discusses saving and loading Numpy arrays to and from disk.

Uploaded by

gayathri4842
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Numpy Week 2 Notes New

The document provides an overview of Numpy, including the creation and manipulation of Numpy arrays and matrices, as well as various functions such as np.arange(), np.linspace(), and np.zeros(). It covers mathematical operations, random value generation, and accessing and modifying entries in arrays and matrices. Additionally, it discusses saving and loading Numpy arrays to and from disk.

Uploaded by

gayathri4842
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Numpy:

1. Numpy arrays:

2. Creating a list and converting it as Numpy array: Creating a list with one data type as numpy array will not take different data types

3. Converting the above list as Numpy array

4. Data type of the above lists and numpy array – official name for numpy array is nd array
5. Numpy matrix

6. We are not creating a matrix here, just displaying the array in the form of matrix

7. Creating a matrix format in array

Numpy functions:
1. Using np.arange() function:

2. Creating a function with arguments with start and stop value – where the last value is exclusive
3. Adding a step size or incremental value to the above function

4. Using np.linspace() function – This is the only exception function where the stop value is included. Instead of incremental value, it will
create a evenly spaced numbers between the given range. And 50 is the default value.

5. Example 1

6. How the above values are generated

7. Generating a matrix with equally spaced value


8. Using np.zeros() function:

9. Creating a matrix array with bunch of zeroes: 1st element represent rows and 2nd element columns

10. Using np.ones() function – instead of zero we’ll get a matrix with ones

11. Example 1

12. Using np.eye() function: Identity function


a. It creates a square matrix eg: if we enter 5 it gives a 5x5 matrix
b. It contains zero everywhere except diagonal. i.e, (1,1) (2,2) (3,3) (4,4) (5,5) – these element contains 1

13. Creating a matrix using eye()


14. Using a np.reshape() function:

15. Example 1 – First we define an array, then using the reshape method (it is not called a function, it’s called reshape as it is defined inside an
object name. So it doesn’t change the shape of an actual array.

16. Reshaping the array with insufficient elements – Here 2,6 requires 12 elements, whereas the array which we chose contains only 10. So it
throws an error

17. Mathematical operations using numpy

18. Trigonometric function: these functions are available only inside the numpy package. We can also assign list inside this
19. Assigning array inside the trigonometric functions:

20. Exponents and Logarithmic functions: This is the exponential function


Exponent = e ^ 2(or whatever value we give). e = 2.71828

21. We can also assign array to the exponent

22. Log function in numpy

23. Example 1

24. Using log function with an array

25. We can also assign base in log function, if not given, it takes e as default
26. Arithmetic operations on list – It does not sum the values rather it just combine both the lists

27. Arithmetic operations on array – Creating 2 arrays

Inverse is calculated as 1/1, ½, 1/3, ¼, 1/5


28. Trying arithmetic operations on matrices
Step 1: creating 2 matrices

Step 2: Using arithmetic operations with 2 matrices- only in division- where a number is divided by 0 it gives the result as inf (infinity)
29. Linear algebra matrix multiplication – defining 2 matrices and performing linear algebra multiplication – eg: 1*11+1*12+1*13 = 90

30. Transpose of a matrix – transpose just mirrors the matrix. It turns the rows into columns and columns into rows

31. Another method of transpose – T should be in capital

32. Functions to find the minimum and maximum values

33. Using np.random.rand function


34. Generating random values in an array

35. Generating random values in a matrix (it contains numbers between 0 and 1 only)

36. Generating randomly drawn numbers using normal distribution in an array

37. Generating random values in an array

38. Generating random values in a matrix

39. Checking the mean and standard deviation of the above matrix

40. Using random.randint function


41. Generating random values in an array – Here starting value is 1 and ending value is 5 which is exclusive and 10 values to be derived

42. Generating random values and creating a list inside the array – here 1 is the starting 10 is the end which is exclusive, and 5,5 is the matrix

Accessing the entries of a numpy array:


1. Accessing one element from an array

2. Accessing multiple elements from an array

3. Accessing multiple non-consecutive entries using np.arange – it gives us 3 rd, 6th and 9th elements

4. Accessing arrays using logical operations


5. Accessing all the values which are greater than 0 – if we give the function inside the square brackets it will output only trues’

6. Accessing the entries of a matrices

7. Accessing the second row in the matrix – if we give the value inside the square bracket for a matrix it will display the row

8. Accessing 3rd element of the 2nd row – we can enter each element separately inside square bracket or together

9. Accessing first 2 rows with 2nd and 3rd column

10. Accessing matrices using logical operations – but it doesn’t display the output in the matrix structure
11. Modifying the entries of an array

12. Changing the values of an array

13. Modifying entries using logical operations – assigning 65 to the values which are greater than 0

14. Modifying entries of a matrix – 1:3 denotes rows and 3:5 denotes column

15. Changing all the values of the extracted matrices – we might think that, as we have made these changes in the sub mat. It does not
change the values of the original matrix, but it isn’t
16. What happened to the original matrix – the values are changed in the original matrix also

17. Solution to prevent the above scenario

Saving and loading a numpy array


1. Saving numpy objects on the disk

2. Using np.save function

Under the defined path, it will create a file called saved file name and store the mentioned matrix as a reference which can be used
later

3. Using np.savez function –left of the equal sign is the name which we define and right of the equal sign is the actual name of the matrix
4. Loading the saved files – assigning a new variable to the stores path

To access the savez files, we need to give the new variable and the previously defined name

5. Load/save text files – delimiter is nothing but the separator which we’re using inbetween each value. 2 nd output contains values with
points as we have saved it in text format

You might also like