Here you would be taught Numpy Tutorial for Python from basic to advance. There are no such prerequisites for you but you must have knowledge about what an array is and basic python syntax before we start Numpy Tutorial for Python from basic to advance.
What is Numpy?
Numpy is a Package in Python used for Scientific computations using which you can perform a variety of operations like:-
- Operations on arrays.
- Random number generation.
- Shape manipulation.
- Linear algebra Operations.
- Open CMD in Windows.
- Change directory to \Python.
- Then type pip list . A list appears to show what all packages have already been installed.
- Then type pip install numpy. Numpy will be installed.
Basics
Numpy is mainly used for multidimensional array manipulations.
It is a table of same type of data.
In numpy dimensions are called Axes.
Let's take an example:
[1,2,3] = It has 3 elements and 1 row. Hence its length would be 3 and number of axes would be 1.
Let an array ndarray=[4,2,3,1,5,6].
Some other attributes of numpy are:-
- ndarray.dim: returns the number of dimensions of an array. In this case it will return 1.
- ndarray.shape: For a matrix with 'n' rows and 'm' columns shape will be (n,m). In this case ity will be (1,6).
- ndarray.size: returns total number of elements from the array. In this case it will be 6.
- ndarray.dtype: returns the datatype of the elements stored in array. For int it returns numpy.int32, for float it returns numpy.float64. In this case it will return numpy.int32.
- ndarray.itemsize: it returns the size in bytes of each element of the array. For int it will be (32/number of elements) , for float it will be (64/number of elements). In the above case it will be 32/6=5.
- ndarray.data: used to access the data of our array.
How to create array using numpy?
>>> import numpy as np
>>> a=np.array([3,4,5])
>>>a
array([3,4,5])
For two-dimensional array:-
>>>import numpy as np
>>>b=np.array([(3,4,5),(6,7,8)])
>>>b
array([[3,4,5],
[6,7,8]])
We can also specify the type of array at the time of creation:-
>>>a=np.array([[1,2],[3,4], dtype=complex )
>>>c
array([[1.+0.j, 2.+0.j],
[3.+0.j, 4.+0.j]])
Some commonly used functions:-
1. zeros() :- This function creates an array full of zeroes.
>>>np.zeros((3,4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
2. ones() :- This function creates an array of ones.
>>>np.ones((2,3), dtype=np.int16)
array([[1, 1, 1]
[1, 1, 1]])
3. empty() :- This function creates an array of random numbers.
>>>np.empty((2,3))
array([[3.7360547e-234, 3.64266367e-342, 6.5534264e-456]
[5.2435674e-233, 2.43335671e-232,2.3394311e-66655]])
4. arange() :- Returns array with evenly spaced elements as per the intervals specified.
i.e - arange(start, stop, step, dtype)
>>>np.arange(4,20,3)
array([4, 7, 10, 13, 16, 19])
*** This function cannot be used with floating point numbers. We have another function for floating numbers.
i.e - arange(start, stop, step, dtype)
>>>np.arange(4,20,3)
array([4, 7, 10, 13, 16, 19])
*** This function cannot be used with floating point numbers. We have another function for floating numbers.
5. linspace() :- Returns array with evenly spaced elements as per the interval for float numbers.
i.e - np.linspace(start, stop, num, endpoint, retstep, dtype)
>>>np.linspace(0, 2, 10)
array([0. , 0.220345 , 0.4299024 , 0.618345 , 0.776345 , 0.8967454 , 0.971232 , 0.999434])
6. sin() :- Used to calculate trigonometric values of sin.
>>> np.sin(x)
Returns array with all sine values for 'x' array entered by user.
7. shape :- Used to get the current shape of our array, but can also be used to re-shape the array.
>>>x=np.arange([1,2,3,4])
>>>x.shape
(4)
>>>y=np.zeros((2,3,4))
>>>y.shape
(2,3,4)
8. random.rand() :- Returns an array with random float values between 0 and 1 not including 1.
>>>x=[0, 0, 0, 0, 0]
>>>x=np.random.rand(5)
>>>x=np.random.rand(5)
array([0.8453 , 0.6157 , 0.7619 , 0.3455 , 0.4011])
9. random.randint() :- Used to generate random integers from low to high not including high.
>>>x=[0, 0, 0, 0, 0, 0]
>>>x=np.random.randint(1,5)
array([1,4,2,4,3,3])
10. random.normal() :- Used to generate random normal distribution with mean and standard deviation.
>>>x=[0,0,0]
>>>x=np.random.normal(100,20)
array([99.6042354, 120.4923474, 106.86734356])
11. exp() :- Used to calculate the exponential of all the elements of array.
>>>b=np.arange(3)
>>>print(np.exp(b))
array([1. , 2.71828182, 7.389056])
12. sqrt() :- Used to find square roots of the elements of an array.
>>>b=np.arange(3)
>>>print(sqrt(b))
array([0. ,1. ,1.41421])
13. add() :- Used to add the elements of two arrays.
>>>b=[0, 1 , 2]
>>>c=[2, -1, 4]
>>>print(add(b,c))
array([2, 0 ,6])
Functions in numpy to solve matrices
>>>x=[0, 0, 0, 0, 0, 0]
>>>x=np.random.randint(1,5)
array([1,4,2,4,3,3])
10. random.normal() :- Used to generate random normal distribution with mean and standard deviation.
>>>x=[0,0,0]
>>>x=np.random.normal(100,20)
array([99.6042354, 120.4923474, 106.86734356])
11. exp() :- Used to calculate the exponential of all the elements of array.
>>>b=np.arange(3)
>>>print(np.exp(b))
array([1. , 2.71828182, 7.389056])
12. sqrt() :- Used to find square roots of the elements of an array.
>>>b=np.arange(3)
>>>print(sqrt(b))
array([0. ,1. ,1.41421])
13. add() :- Used to add the elements of two arrays.
>>>b=[0, 1 , 2]
>>>c=[2, -1, 4]
>>>print(add(b,c))
array([2, 0 ,6])
Functions in numpy to solve matrices
1. transpose() :- Used to calculate transpose of a matrix.
>>>import numpy as np
>>>a=np.array([[1.0 , 2.0] , [3.0 , 4.0]])
>>>a.transpose()
[ [ 1.0 , 3.0 ],
[ 2.0 , 4.0 ] ]
2. linalg.inv() :- Used to calculate inverse of a matrix.
>>>import numpy as np
>>>a=np.array([[1.0 , 2.0] , [3.0 , 4.0]])
>>>np.linalg.inv(a)
[ [-2.0 , 1.0 ],
[ 1.5, -0.5 ] ]
[ 1.5, -0.5 ] ]
3. eye() :- Used to represent identity Matrix.
>>>u=np.eye(2)
>>>u
[ [ 1.0 , 0. ],
[ 0.0 , 1.0 ] ]
4. @ :- Used to calculate product of two matrices.
>>>j = np.array([[ 0.0 , -1.0 ],[ 1.0 , 0.0]])
>>>j@j
[[ -1.0 , 0.0 ],
[ 0.0 , -1.0 ]]
5. trace() :- Used to calculate trace( sum of all main diagonal elements) of a matrix.
>>>import numpy as np
>>>a=np.array([[1.0 , 2.0] , [3.0 , 4.0]])
>>>np.trace(a)
5.0
6. linalg.solve() :- Used to solve a linear matrix equation.
>>>import numpy as np
>>>a=np.array([[1.0 , 2.0] , [3.0 , 4.0]])
>>>y=np.array([[5.0] , [7.1]])
>>>np.linalg.solve(a,y)
array([[-3.0] , [4.0 ]])
Iterating, indexing and slicing of arrays
1. Iterating : Iterating means to move from beginning of the array till its end.
For 1-D array-
>>>a=np.arange(5)**3
>>>print(a)
array([ 0 , 1 , 8 , 27 , 64 , 125 ])
>>>for i in a:
>>> print(i**(1/3))
0.0
1.0
For 1-D array-
>>>a=np.arange(5)**3
>>>print(a)
array([ 0 , 1 , 8 , 27 , 64 , 125 ])
>>>for i in a:
>>> print(i**(1/3))
0.0
1.0
2.0
3.0
4.0
5.0
For 2-D array-
>>>b=array([ 0 , 1 , 2 , 3 ],
[ 4 , 5 , 6 , 7 ],
[ 8 , 9 , 10, 11])
>>>print(b)
array([ [ 0, 1 ,2 ,3 ],
[ 4, 5 ,6 , 7],
[ 8, 9, 10 ,11]])
2. Indexing :- Used to find the index of corresponding elements of an array.
For 1-D array-
>>>a=np.arange(5)**3
>>>a[2]
8
For 2-D array-
>>>b=array([ 0 , 1 , 2 , 3 ],
[ 4 , 5 , 6 , 7 ],
[ 8 , 9 , 10, 11])
>>>b[1][2]
6
3.0
4.0
5.0
For 2-D array-
>>>b=array([ 0 , 1 , 2 , 3 ],
[ 4 , 5 , 6 , 7 ],
[ 8 , 9 , 10, 11])
>>>print(b)
array([ [ 0, 1 ,2 ,3 ],
[ 4, 5 ,6 , 7],
[ 8, 9, 10 ,11]])
2. Indexing :- Used to find the index of corresponding elements of an array.
For 1-D array-
>>>a=np.arange(5)**3
>>>a[2]
8
For 2-D array-
>>>b=array([ 0 , 1 , 2 , 3 ],
[ 4 , 5 , 6 , 7 ],
[ 8 , 9 , 10, 11])
>>>b[1][2]
6
3. Slicing :- Used to slice an array and print the elements present in that part of array.
For 1-D array-
>>>a=np.arange(5)**3
>>>print(a)
array([ 0 , 1 , 8 , 27 , 64 , 125 ])
For 1-D array-
>>>a=np.arange(5)**3
>>>print(a)
array([ 0 , 1 , 8 , 27 , 64 , 125 ])
>>>a[2 : 5]
array([ 8 , 27 ,36])
For 2-D array- >>>b=array([ 0 , 1 , 2 , 3 ],
[ 4 , 5 , 6 , 7 ],
[ 8 , 9 , 10, 11])
>>>b[0:2 , 1]
array([1 , 5])
[ 4 , 5 , 6 , 7 ],
[ 8 , 9 , 10, 11])
>>>b[0:2 , 1]
array([1 , 5])
Changing shapes of an array
We can change shape of an array using some functions of numpy , but these functions
only the modified array and does not changes the original array.
1. ravel() :- Used to flatten an array. However if user makes any changes in array then those changes are reflected to original array.
>>>import numpy as np
>>>a=np.arange(6).reshape(2,3)
>>>a.ravel()
array([ 0, 1, 2, 3, 4, 5 ])
**Our multi-dimensional array is converted into a one -dimensional array.
2. flatten() :- Used to flatten an array but changes are not reflected back to the original array .
>>>import numpy as np
>>>a=np.arange(6).reshape(2,3)
>>>a.flatten()
array([ 0, 1, 2, 3, 4, 5 ])
3. shape() :- Used to specify shape of an array.
>>>a=np.arange(9)
>>>a.shape= (3,3)
>>>print (a)
array([[0 ,1 ,2],
[3 ,4 ,5],
[6 ,7 ,8]])
4. T :- Used to transpose an array(same as transpose()).
>>>a=np.arange(9)
>>>a.shape(3,3)
>>>print(a.T)
array([[0 ,3 ,6],
[1 ,4 ,7],
[2 ,5 ,8]])
5. resize() :- Used to resize our original array .
>>>import numpy as np
>>>a=np.arange(6).reshape(3,3)
>>>print(np.resize(a,(6,6))
array([[0 1 2 3 4 5]
[6 7 8 0 1 2]
[3 4 5 6 7 8]
[0 1 2 3 4 5]
[6 7 8 0 1 2]
[3 4 5 6 7 8]])
1. ravel() :- Used to flatten an array. However if user makes any changes in array then those changes are reflected to original array.
>>>import numpy as np
>>>a=np.arange(6).reshape(2,3)
>>>a.ravel()
array([ 0, 1, 2, 3, 4, 5 ])
**Our multi-dimensional array is converted into a one -dimensional array.
2. flatten() :- Used to flatten an array but changes are not reflected back to the original array .
>>>import numpy as np
>>>a=np.arange(6).reshape(2,3)
>>>a.flatten()
array([ 0, 1, 2, 3, 4, 5 ])
3. shape() :- Used to specify shape of an array.
>>>a=np.arange(9)
>>>a.shape= (3,3)
>>>print (a)
array([[0 ,1 ,2],
[3 ,4 ,5],
[6 ,7 ,8]])
4. T :- Used to transpose an array(same as transpose()).
>>>a=np.arange(9)
>>>a.shape(3,3)
>>>print(a.T)
array([[0 ,3 ,6],
[1 ,4 ,7],
[2 ,5 ,8]])
5. resize() :- Used to resize our original array .
>>>import numpy as np
>>>a=np.arange(6).reshape(3,3)
>>>print(np.resize(a,(6,6))
array([[0 1 2 3 4 5]
[6 7 8 0 1 2]
[3 4 5 6 7 8]
[0 1 2 3 4 5]
[6 7 8 0 1 2]
[3 4 5 6 7 8]])
This was all about Numpy Tutorial for Python from basic to advance. Hope you might have got a grasp on Numpy . Tell me about this article to me by sharing your reviews .
Thank You!!
great , thanks for the help
ReplyDeleteclick here