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

vertopal.com_unit2

Uploaded by

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

vertopal.com_unit2

Uploaded by

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

creating numpy arrays form DS,Intrinstic

Numpy objects and Random Functions


import numpy as np

From Data Structures


List
np.array([1,2,3,4,5])

array([1, 2, 3, 4, 5])

tuple
np.array((6,7,8,9,10))

array([ 6, 7, 8, 9, 10])

Intrinsic Numpy Objects


arange
np.arange(0,10,2)

array([0, 2, 4, 6, 8])

linspace
np.linspace(0,1,5)

array([0. , 0.25, 0.5 , 0.75, 1. ])

zeros
np.zeros((2,3))

array([[0., 0., 0.],


[0., 0., 0.]])

ones
np.ones((3,2))
array([[1., 1.],
[1., 1.],
[1., 1.]])

eye
np.eye(3)

array([[1., 0., 0.],


[0., 1., 0.],
[0., 0., 1.]])

Random Functions
random integers
np.random.randint(0,10,size=(3,3))

array([[4, 7, 0],
[1, 0, 1],
[0, 6, 4]])

random float b/w 0 and 1


np.random.rand(4)

array([0.36080479, 0.19946214, 0.82008299, 0.44586926])

normal distribution
np.random.randn(3,3)

array([[ 1.8281684 , 1.78129918, -0.0494379 ],


[-0.21485669, -0.63535532, -0.2181847 ],
[-0.5568903 , -0.61707171, -0.91538607]])

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))

array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,


16])

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))

array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,


16])
splitting
np.split(arr,2)

[array([1, 2, 3, 4]), array([5, 6, 7, 8])]

np.split(arr,4)

[array([1, 2]), array([3, 4]), array([5, 6]), array([7, 8])]

Computation on Numpy Arrays Using Universal


and Mathematical functions
x=np.array([1,2,3])
y=np.array([4,5,6])

Universal Functions (ufuncs)


arithematic operations
np.add(x,y)

array([5, 7, 9])

np.subtract(x,y)

array([-3, -3, -3])

np.multiply(x,y)

array([ 4, 10, 18])

np.divide(x,y)

array([0.25, 0.4 , 0.5 ])

np.power(x,y)

array([ 1, 32, 729])

trignometric functions
np.sin(x)

array([0.84147098, 0.90929743, 0.14112001])

np.cos(x)
array([ 0.54030231, -0.41614684, -0.9899925 ])

np.tan(x)

array([ 1.55740772, -2.18503986, -0.14254654])

np.arcsin(x)

C:\Users\pasup\AppData\Local\Temp\ipykernel_20464\1524129805.py:1:
RuntimeWarning: invalid value encountered in arcsin
np.arcsin(x)

array([1.57079633, nan, nan])

np.arccos(x)

C:\Users\pasup\AppData\Local\Temp\ipykernel_20464\396561874.py:1:
RuntimeWarning: invalid value encountered in arccos
np.arccos(x)

array([ 0., nan, nan])

np.arctan(x)

array([0.78539816, 1.10714872, 1.24904577])

exponential and logarithmic funtions


np.exp(x)

array([ 2.71828183, 7.3890561 , 20.08553692])

np.log(x)

array([0. , 0.69314718, 1.09861229])

np.log10(x)

array([0. , 0.30103 , 0.47712125])

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)

array([False, False, False])

np.less(x,y)

array([ True, True, True])

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

np.matmul(x,y) #matrix product

32

Statistical and Comparison on csv file


import pandas as pd

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

Image load, Crop, Flip Using numpy indexing


from PIL import Image

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]],

[[34, 34, 36],


[ 1, 1, 3],
[11, 11, 13],
...,
[ 8, 8, 10],
[ 8, 8, 10],
[ 8, 8, 10]],

[[47, 47, 49],


[ 9, 9, 11],
[12, 12, 14],
...,
[ 8, 8, 10],
[ 8, 8, 10],
[ 8, 8, 10]]], dtype=uint8)

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')

Statistics for different types of data


from scipy import stats

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

You might also like