Numpy
NumPy is a one of the Data Science library for the Python programming language,
adding support for large, multi-dimensional arrays and
matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
The ancestor of NumPy, Numeric, was originally created by Jim Hugunin with contributions from several other developers
Section Goals:
- Understand NumPy
- Create Arrays With NumPy
- Retrieve Information From A NumPy Array Through Slicing And Indexing
- Learn Basic NumPy Operations
What Is NumPy?
- Python Library For Creating N-Dimensional Arrays
- Ability To Quickly Broadcast functions
- Built-in Linear Algebra, Statistical Distributions, Trigonometric And Random Number Capabilities
Why Use NumPy?
- While NumPy Structures Look Similar To Standerf Python Lists, They Are Much More Efficient!!!
- The Broadcasting Capabilities Are Also Exreaely Useful For Quickly Applying Functions To Out Data Structures
Create Array With NumPy:
import numpy as np
myList =[1,2,3,4,5,6,7,8,9,0]
myArray =np.array(myList)
print(myArray)
Output:
[1 2 3 4 5 6 7 8 9 0]
Create N-Dimensional Array With NumPy:
import numpy as np
myArray =np.array([[1,2,3],
[4,5,6], [7,8,9]])
print(myArray)
Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
[4 5 6]
[7 8 9]]
Create Array Using NumPy arange Method:
import numpy as np
myArray1 =np.arange(0, 10)
myArray2 =np.arange(0, 20, 2)
print(myArray1)
print(myArray2)
Output:
[0 1 2 3 4 5 6 7 8 9][ 0 2 4 6 8 10 12 14 16 18]
Create Zeros Array Using NumPy zeros Method:
import numpy as np
myArray1 =np.zeros(10)
myArray2 =np.zeros((10, 5))
print(myArray1)
print(myArray2)
Output:
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
Create Ones Array Using NumPy ones Method:
import numpy as np
myArray1 =np.ones(10)
myArray2 =np.ones((10, 5))
print(myArray1)
print(myArray2)
Output:
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
Create LinSpace Array Using NumPy linspace Method:
import numpy as np
myArray =np.linspace(0, 100, 10)
print(myArray)
Output:
[ 0. 11.11111111 22.22222222 33.33333333 44.44444444
55.55555556 66.66666667 77.77777778 88.88888889 100. ]
Create Squere Matrix Array Using NumPy eye Method:
import numpy as np
myArray =np.eye(3)
print(myArray)
Output:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
[0. 1. 0.]
[0. 0. 1.]]
Create Random Values Of Array Using NumPy Random Method:
import numpy as np
myArray1 =np.random.rand(4)
myArray2 =np.random.randn(2, 3)
myArray3 =np.random.randint(0, 100, 10)
print(myArray1)
print(myArray2)
print(myArray3)
Output:
[0.70705863 0.26303514 0.90838782 0.10973079]
[[ 0.7811629 -1.62630961 1.53149854]
[-1.62526936 1.53977785 1.09274884]]
[65 7 99 13 13 76 28 5 17 53]
[[ 0.7811629 -1.62630961 1.53149854]
[-1.62526936 1.53977785 1.09274884]]
[65 7 99 13 13 76 28 5 17 53]
Find Minimum And Maximum Values From NumPy Array Using min, max Method:
import numpy as np
myArray =np.array([10,20,30,40,50
,60,70,80,90])
print(myArray.min())
print(myArray.max())
Output:
10
90
90
Find Minimum And Maximum Values Index From NumPy Array Using argmin, argmax Method:
import numpy as np
myArray =np.array([45,34,67,82
,12,53,89,40])
print(myArray.argmin())
print(myArray.argmax())
Output:
4
6
Find Type Of Values Using NumPy Array dtype Method:
import numpy as np
myArray1 =np.array([1,2,3,4,
5,6,7,8,9])
myArray2 =np.array([1.4,5.7,
45.2,9.5,23.6])
myArray3 =np.array(['aew','eer',
'ere','rdt','ewe'])
myArray4 =np.array([[3,54,5,45],
[2,4,5,56],[54,3,23,78]])
myArray5 =np.array([True,False,False,
True,False,True, True])
print(myArray1.dtype)
print(myArray2.dtype)
print(myArray3.dtype)
print(myArray4.dtype)
Output:
int64
float64
<U3
int64
float64
<U3
int64
bool
Find Shape And Reshape The Array Using NumPy shape, reshape Method:
import numpy as np
myArray1 =np.array([[1,2,3,4],
[5,6,7,8], [9,10,11,12]])
#3x4=12 and also 6x2=12
myArray2 =myArray1.reshape(6,2)
print(myArray1.shape)
print(myArray2)
Output:
(3, 4)
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]
[11 12]]
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]
[11 12]]
Find Value Using Indexing And Slicing The Array:
import numpy as np
myArray =np.array([10,20,30,40,
50,60,70,80,90])
print(myArray[5])
print(myArray[:4])
print(myArray[3:])
print(myArray[3:6])
myArray[:] =10
print(myArray)
Output:
60
[10 20 30 40]
[40 50 60 70 80 90]
[40 50 60]
[10 10 10 10 10 10 10 10 10]
[10 20 30 40]
[40 50 60 70 80 90]
[40 50 60]
[10 10 10 10 10 10 10 10 10]
Perform Conditions From A Array:
import numpy as np
myArray =np.array([10,20,30,40,
50,60,70,80,90])
print(myArray[myArray>=50])
print(myArray[myArray<=50])
print(myArray>=50)
print(myArray<=50)
print(60 in myArray)
print(46 in myArray)
Output:
[50 60 70 80 90]
[10 20 30 40 50]
[10 20 30 40 50]
[False False False False True True True True True]
[ True True True True True False False False False]
True
False
[ True True True True True False False False False]
True
False
Perform Basic Mathematical Operations In NumPy Array:
import numpy as np
myArray =np.array([10,20,30,40,
50,60,70,80,90])
print(myArray+10)
print(myArray-5)
print(myArray*2)
print(myArray/2)
print(myArray%3)
Output:
[ 20 30 40 50 60 70 80 90 100]
[ 5 15 25 35 45 55 65 75 85]
[ 20 40 60 80 100 120 140 160 180]
[ 5. 10. 15. 20. 25. 30. 35. 40. 45.]
[1 2 0 1 2 0 1 2 0]
[ 5 15 25 35 45 55 65 75 85]
[ 20 40 60 80 100 120 140 160 180]
[ 5. 10. 15. 20. 25. 30. 35. 40. 45.]
[1 2 0 1 2 0 1 2 0]
Perform Advanced Mathematical Operations In NumPy Array:
import numpy as np
myArray =np.array([10,20,30,40,
50,60,70,80,90])
print(np.sin(myArray))
print(np.tan(myArray))
print(np.cos(myArray))
print(np.sqrt(myArray))
print(np.log(myArray))
Output:
[-0.54402111 0.91294525 -0.98803162 0.74511316 -0.26237485 -0.30481062
0.77389068 -0.99388865 0.89399666]
[ 0.64836083 2.23716094 -6.4053312 -1.11721493 -0.27190061 0.32004039
1.22195992 9.00365495 -1.99520041]
[-0.83907153 0.40808206 0.15425145 -0.66693806 0.96496603 -0.95241298
0.6333192 -0.11038724 -0.44807362]
[3.16227766 4.47213595 5.47722558 6.32455532 7.07106781 7.74596669
8.36660027 8.94427191 9.48683298]
[2.30258509 2.99573227 3.40119738 3.68887945 3.91202301 4.09434456
4.24849524 4.38202663 4.49980967
0.77389068 -0.99388865 0.89399666]
[ 0.64836083 2.23716094 -6.4053312 -1.11721493 -0.27190061 0.32004039
1.22195992 9.00365495 -1.99520041]
[-0.83907153 0.40808206 0.15425145 -0.66693806 0.96496603 -0.95241298
0.6333192 -0.11038724 -0.44807362]
[3.16227766 4.47213595 5.47722558 6.32455532 7.07106781 7.74596669
8.36660027 8.94427191 9.48683298]
[2.30258509 2.99573227 3.40119738 3.68887945 3.91202301 4.09434456
4.24849524 4.38202663 4.49980967
Perform Sum Operation Using NumPy sum Method:
import numpy as np
myArray1 =np.array([10,20,30,40,
50,60,70,80,90])
myArray2 =np.array([[1,2,3],
[4,5,6],
[7,8,9]])
print(myArray1.sum())
# sum of column
print(myArray2.sum(axis=0))
# sum of row
print(myArray2.sum(axis=1))
Output:
450
[12 15 18]
[ 6 15 24]
[12 15 18]
[ 6 15 24]
To Be Continue...
Comments
Post a Comment