NumPy
NumPy:
NumPy is a Python library used to work with arrays.
It also provides functions for mathematics, statistics, linear algebra, reshaping, and
more.
Arrays in NumPy are called ndarray (n-dimensional array).
Array: array is a collection of homogeneous elements.
Priority list of data types in NumPy: (Least to higher priority)
1. Boolean (True, False)
2. Int8, int16 (small integers)
3. Int32, int64 (large integers)
4. float16, float32 (floating point numbers)
5. float 64 (double precision float)
6. complex64, complex128
importing NumPy:
import numpy as np
Creating an array in NumPy:
Var=np.array([10,20,30,40]) 1D array
Var=np.array([[10,20], [30,40]]) 2D array
Var =np.array([[[10,20], [30,40]], [[50,60], [70,80]]]) 3D array
the arrays inside 2d array and 3d array must be of same length i.e., number of the
elements inside the array must be same.
If we want to convert the array into any data type we can use dtype.
3D array is like keeping an 2D array inside 1D array.
Attributes of NumPy:
There are 4 main attributes in NumPy.
1. Shape
2. Size
3. ndim
4. dtype
Shape: Gives a tuple showing the number of elements along each dimension., (rows,
columns).
Size: gives the total number of elements in the array (all values in all dimensions).
ndim: gives the number of dimensions (also called axes) of the NumPy array.
dtype: shows the data type of the elements stored in the array (like int, float, bool, etc.).ad
also convert one data type to another.
Create an array using range:
Syntax: np.arange(start value, end value, updation) (only for 1D array)
Create an array using linspace:
Syntax: np.linspace(start value, end value, how many values we need)
If we don’t mention how many values, we need it just takes 50 as a default value. It will divide
into equal spaces between the mentioned numbers.
Identity:
It creates an identity matrix
Syntax: np.identity(n) nnumber of rows
Reshape:
It is used to change the dimensions of the array i.e., from 1D to 2D or 3D. while
converting from 1D to 2D we should mention (rows, columns) i.,e product of row and column
number must be equal to number of elements present in the array. For 3D array we have to
mention (row, column, number of element in one set).
Syntax: var.reshape(rows, columns)
Var.reshape(rows, columns, N)
Flattening: it is the process of converting 2D and 3D array into 1D array.
Syntax: var.reshape(-1)
Random array:
Creates an array with random values between the starting and ending value.
Syntax: arr=np.random.randint(starting value, ending value, N)
For elements between 0 and 1:
Syntax: arr=np.random.rand(n)
For elements between -♾️ to +♾️:
Syntax: arr=np.random.randn(n)
Indexing:
Used to fetch the elements using index values.
For 1D array arr[index]
For 2D array arr [row_index, col_index]
For 3D array arr [row_index, col_index, value_index]
Operators:
All arithmetic and bitwise operators can be used
Array with array [1,2,3] + [1,2,3] = [2,4,6]
Array with constant[2,4,5] + 2= [4,6,7]
Array with inbuilt methods like
mean
max
min
variance