Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

7/21/2021 numpy_functions - Jupyter Notebook

=> numpy stands for numerical python


=> fundamental pacakge for numerical computations in python
=> supports n-dimensional array objects that can be used for processing the
mutidimensional data. It supports differnt data   types
=> using numpy we can perform
#mathematical and logical operations on arrays
#fourier transformations
#Linera algebra operations
#Random number generation

In [1]:

import numpy as np

In [10]:

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

In [15]:

arr_2D=np.array([[1,2],[3,4],[5,6]])

In [16]:

arr_2D.ndim

Out[16]:

In [17]:

arr_2D.shape

Out[17]:

(3, 2)

In [18]:

arr_2D.size

Out[18]:

In [19]:

arr_2D.dtype

Out[19]:

dtype('int32')

localhost:8888/notebooks/numpydemo/numpy_functions.ipynb 1/9
7/21/2021 numpy_functions - Jupyter Notebook

In [22]:

arr_ones=np.ones((3,3),dtype=int)

In [23]:

print(arr_ones)

[[1 1 1]

[1 1 1]

[1 1 1]]

In [30]:

arr_zeros=np.zeros((3,3),dtype=int)

In [31]:

print(arr_zeros)

[[0 0 0]

[0 0 0]

[0 0 0]]

In [37]:

#By default numpy empty() funciton assigns float64 bit random value to all the elements . T
#we can change the type of the elements using dtype attribute.
arr_empty=np.empty((2,4))

In [38]:

print(arr_empty)

[[0.00000000e+000 0.00000000e+000 0.00000000e+000 0.00000000e+000]

[0.00000000e+000 6.75881804e-321 9.65508085e-312 6.95187041e-310]]

In [66]:

arr=np.arange(1,13)

In [67]:

print(arr)

[ 1 2 3 4 5 6 7 8 9 10 11 12]

In [57]:

arr_lin=np.linspace(1,5,10)

localhost:8888/notebooks/numpydemo/numpy_functions.ipynb 2/9
7/21/2021 numpy_functions - Jupyter Notebook

In [55]:

print(arr_lin)

[1. 1.44444444 1.88888889 2.33333333 2.77777778 3.22222222

3.66666667 4.11111111 4.55555556 5. ]

In [62]:

arr_re=np.reshape(arr,(3,4))

In [63]:

print(arr_re)

[[ 1 2 3 4]

[ 5 6 7 8]

[ 9 10 11 12]]

In [68]:

arr_2D=np.array([[1,2],[3,4],[5,6]])

In [70]:

print(arr_2D)

[[1 2]

[3 4]

[5 6]]

In [72]:

arr_2D.shape

Out[72]:

(3, 2)

In [73]:

arr_con=arr_2D.flatten()

In [74]:

print(arr_con)

[1 2 3 4 5 6]

In [75]:

#Basic Operations
arr_2D=np.array([[1,2],[3,4],[5,6]])

localhost:8888/notebooks/numpydemo/numpy_functions.ipynb 3/9
7/21/2021 numpy_functions - Jupyter Notebook

In [76]:

nt(arr_2D)

[[1 2]

[3 4]

[5 6]]

In [80]:

arr_2D.min()

Out[80]:

In [79]:

arr_2D.max()

Out[79]:

In [81]:

arr_2D.sum()

Out[81]:

21

In [82]:

arr_2D.sum(axis=0)

Out[82]:

array([ 9, 12])

In [83]:

arr_2D.sum(axis=1)

Out[83]:

array([ 3, 7, 11])

In [84]:

np.sqrt(arr_2D)

Out[84]:

array([[1. , 1.41421356],

[1.73205081, 2. ],

[2.23606798, 2.44948974]])

localhost:8888/notebooks/numpydemo/numpy_functions.ipynb 4/9
7/21/2021 numpy_functions - Jupyter Notebook

In [86]:

np.std(arr_2D)

Out[86]:

1.707825127659933

In [87]:

#Matrix operations

In [88]:

a=np.array([[1,2],[3,4]])
b=np.array([[5,6],[7,8]])

In [90]:

print(a)

[[1 2]

[3 4]]

In [91]:

print(b)

[[5 6]

[7 8]]

In [97]:

a*b #scaler mulitiplication of matirices.(mutplying the corresponding elements)

Out[97]:

array([[ 5, 12],

[21, 32]])

In [96]:

a.dot(b) #multiplication can be performed by using dot() function

Out[96]:

array([[19, 22],

[43, 50]])

In [98]:

a=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])

localhost:8888/notebooks/numpydemo/numpy_functions.ipynb 5/9
7/21/2021 numpy_functions - Jupyter Notebook

In [99]:

print(a)

[[ 1 2 3 4]

[ 5 6 7 8]

[ 9 10 11 12]]

In [100]:

a.transpose()

Out[100]:

array([[ 1, 5, 9],

[ 2, 6, 10],

[ 3, 7, 11],

[ 4, 8, 12]])

slicing /stacking
In [102]:

a=np.array([6,7,8])
a[0:2]

Out[102]:

array([6, 7])

In [103]:

a=np.array([[6,7,8],[1,2,3],[9,3,2]])

In [104]:

print(a)

[[6 7 8]

[1 2 3]

[9 3 2]]

In [105]:

a[1,2]

Out[105]:

localhost:8888/notebooks/numpydemo/numpy_functions.ipynb 6/9
7/21/2021 numpy_functions - Jupyter Notebook

In [106]:

a[0:2,2]

Out[106]:

array([8, 3])

In [107]:

a[:,0:2]

Out[107]:

array([[6, 7],

[1, 2],

[9, 3]])

In [110]:

a[-1,1:]

Out[110]:

array([3, 2])

stacking of arrays
In [113]:

a=np.arange(6).reshape(3,2)

In [114]:

print(a)

[[0 1]

[2 3]

[4 5]]

In [125]:

b=np.arange(6,12).reshape(3,2)

In [127]:

print(b)

[[ 6 7]

[ 8 9]

[10 11]]

localhost:8888/notebooks/numpydemo/numpy_functions.ipynb 7/9
7/21/2021 numpy_functions - Jupyter Notebook

In [126]:

np.hstack((a,b))

Out[126]:

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

[ 2, 3, 8, 9],

[ 4, 5, 10, 11]])

In [128]:

np.vstack((a,b))

Out[128]:

array([[ 0, 1],

[ 2, 3],

[ 4, 5],

[ 6, 7],

[ 8, 9],

[10, 11]])

In [130]:

a=np.arange(30).reshape(2,15)

In [131]:

print(a)

[[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]

[15 16 17 18 19 20 21 22 23 24 25 26 27 28 29]]

In [132]:

ls=np.hsplit(a,3)

In [134]:

ls

Out[134]:

[array([[ 0, 1, 2, 3, 4],

[15, 16, 17, 18, 19]]),

array([[ 5, 6, 7, 8, 9],

[20, 21, 22, 23, 24]]),

array([[10, 11, 12, 13, 14],

[25, 26, 27, 28, 29]])]

In [136]:

ls=np.vsplit(a,2)

localhost:8888/notebooks/numpydemo/numpy_functions.ipynb 8/9
7/21/2021 numpy_functions - Jupyter Notebook

In [137]:

1 ls

Out[137]:

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

array([[15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])]

In [ ]:

localhost:8888/notebooks/numpydemo/numpy_functions.ipynb 9/9

You might also like