vertopal.com_unit2
vertopal.com_unit2
array([1, 2, 3, 4, 5])
tuple
np.array((6,7,8,9,10))
array([ 6, 7, 8, 9, 10])
array([0, 2, 4, 6, 8])
linspace
np.linspace(0,1,5)
zeros
np.zeros((2,3))
ones
np.ones((3,2))
array([[1., 1.],
[1., 1.],
[1., 1.]])
eye
np.eye(3)
Random Functions
random integers
np.random.randint(0,10,size=(3,3))
array([[4, 7, 0],
[1, 0, 1],
[0, 6, 4]])
normal distribution
np.random.randn(3,3)
uniform distribution
np.random.uniform(1,10,size=(2,2))
array([[1.87983737, 3.09166869],
[1.89303338, 7.17404002]])
Manipulation of NumPy
indexing
arr = np.array([1,2,3,4,5,6,7,8])
arr[2]
arr[-1]
slicing
arr[1:5]
array([2, 3, 4, 5])
arr[1:6:2]
array([2, 4, 6])
reshaping
arr.reshape(2,4)
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
arr.reshape(2,2,2)
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
joining
arr1 = [9,10,11,12,13,14,15,16]
np.concatenate((arr,arr1))
np.vstack((arr,arr1))
array([[ 1, 2, 3, 4, 5, 6, 7, 8],
[ 9, 10, 11, 12, 13, 14, 15, 16]])
np.hstack((arr,arr1))
np.split(arr,4)
array([5, 7, 9])
np.subtract(x,y)
np.multiply(x,y)
np.divide(x,y)
np.power(x,y)
trignometric functions
np.sin(x)
np.cos(x)
array([ 0.54030231, -0.41614684, -0.9899925 ])
np.tan(x)
np.arcsin(x)
C:\Users\pasup\AppData\Local\Temp\ipykernel_20464\1524129805.py:1:
RuntimeWarning: invalid value encountered in arcsin
np.arcsin(x)
np.arccos(x)
C:\Users\pasup\AppData\Local\Temp\ipykernel_20464\396561874.py:1:
RuntimeWarning: invalid value encountered in arccos
np.arccos(x)
np.arctan(x)
np.log(x)
np.log10(x)
np.log2(x)
array([0. , 1. , 1.5849625])
statistical functions
np.mean(x)
2.0
np.std(x)
0.816496580927726
np.var(x)
0.6666666666666666
np.median(x)
2.0
comparison functions
np.maximum(x,y)
array([4, 5, 6])
np.minimum(x,y)
array([1, 2, 3])
np.greater(x,y)
np.less(x,y)
Mathematical Methods
sum
np.sum(x)
np.sum(x,axis=0)
product
np.prod(x)
cumulative operations
np.cumsum(x)
array([1, 3, 6])
np.cumprod(x)
array([1, 2, 6])
dot product
np.dot(x,y)
32
32
data = {'c1':[10,20,30,40,50],
'c2':[15,25,35,45,55],
'c3':[5,np.nan,15,25,35]}
df = pd.DataFrame(data)
statistical operations
df.mean()
c1 30.0
c2 35.0
c3 20.0
dtype: float64
df.std()
c1 15.811388
c2 15.811388
c3 12.909944
dtype: float64
df.sum()
c1 150.0
c2 175.0
c3 80.0
dtype: float64
df.corr()
c1 c2 c3
c1 1.000000 1.000000 0.982708
c2 1.000000 1.000000 0.982708
c3 0.982708 0.982708 1.000000
comparison operations
df['c1']>df['c2']
0 False
1 False
2 False
3 False
4 False
dtype: bool
df.isnull().sum()
c1 0
c2 0
c3 1
dtype: int64
load
image = Image.open('OIP (1).jpg')
image
image_array = np.array(image)
image_array
array([[[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
...,
[ 3, 3, 3],
[ 3, 3, 3],
[ 3, 3, 3]],
[[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
...,
[ 3, 3, 3],
[ 3, 3, 3],
[ 3, 3, 3]],
[[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
...,
[ 3, 3, 3],
[ 3, 3, 3],
[ 3, 3, 3]],
...,
[[ 7, 7, 9],
[ 7, 7, 9],
[ 7, 7, 9],
...,
[ 7, 7, 9],
[ 7, 7, 9],
[ 7, 7, 9]],
crop
cropped_image = image_array[50:200,50:200]
Image.fromarray(cropped_image)
flip
flipped_horizontal = cropped_image[:,::-1] #left to right
Image.fromarray(flipped_horizontal)
flipped_verical = cropped_image[::-1,:] #top to bottom
Image.fromarray(flipped_verical)
save
flipped_image = Image.fromarray(flipped_horizontal)
flipped_image.save('flipped_image.jpg')
d1 = [10,20,20,30,30,30,40,50]
d2 = [5.5,7.1,7.1,6.8,9.0,10.2,7.1]
d3 = [100,200,300,400,500]
for d1
d = np.array(d1)
np.mean(d)
28.75
np.median(d)
30.0
stats.mode(d)[0]
30
np.std(d)
11.659223816361019
np.var(d)
135.9375
for d2
import statistics as st
d=np.array(d2)
st.mean(d)
7.542857142857143
st.variance(d)
2.4161904761904758
st.stdev(d)
1.5544100090357356
st.median(d)
7.1
st.mode(d)
7.1
for d3