NUMPY FUNCTIONS

(nishantmunjal.com/python/script/NUMPY.ipynb)

In [ ]:
import numpy as np
In [8]:
#defining array
a= np.array([1,2,3])
In [14]:
#it shows the type of the array here it is numpy array of n dimensionals
type(a)
Out[14]:
numpy.ndarray
In [16]:
b= np.array((3,4,5))
type(b)
Out[16]:
numpy.ndarray
In [32]:
#2d Arrays
x = np.zeros((3,4))
print (x)
type (x.dtype)
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
Out[32]:
numpy.dtype
In [25]:
x.shape
Out[25]:
(3, 4)
In [39]:
#Array of '1's and the datatype is int16
y=np.ones((3,4), dtype=np.int16)
print (y)
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]
In [42]:
#3d Arrays
x=np.zeros((2,3,4),dtype=np.float64)
print(x)
[[[0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]]

 [[0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]]]
In [44]:
#data type can be changed
print (x.dtype)
print (y.dtype)
float64
int16
In [46]:
np.full ((2,4),0.11)
Out[46]:
array([[0.11, 0.11, 0.11, 0.11],
       [0.11, 0.11, 0.11, 0.11]])
In [49]:
np.full ((3,4), np.pi)
Out[49]:
array([[3.14159265, 3.14159265, 3.14159265, 3.14159265],
       [3.14159265, 3.14159265, 3.14159265, 3.14159265],
       [3.14159265, 3.14159265, 3.14159265, 3.14159265]])
In [51]:
#Generates the numbers from 10 to 30 with the gap of 2
np.arange(10,30,2)
Out[51]:
array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28])
In [52]:
%timeit x
23.7 ns ± 0.274 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [56]:
np.arange(0,2,0.5)
Out[56]:
array([0. , 0.5, 1. , 1.5])
In [58]:
x=[
    [
        [1],
        [1],
        [1],
        [1]
    ],
    [
        [1],
        [1],
        [1],
        [1]
    ]
]
In [60]:
np.array(x).shape
Out[60]:
(2, 4, 1)
In [62]:
#Generates 11 numbers between 0 to 100 
np.linspace(0,100,11)
Out[62]:
array([  0.,  10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90., 100.])
In [68]:
np.linspace(0,5/2, 6)
Out[68]:
array([0. , 0.5, 1. , 1.5, 2. , 2.5])
In [71]:
#Genrates a 2x3 float matrix between 0-1
np.random.rand(2,3)
Out[71]:
array([[0.51681266, 0.52157904, 0.85998556],
       [0.23753023, 0.39288135, 0.59807163]])
In [78]:
#By default the difference in each value is one
c=np.arange(2,8)
c
Out[78]:
array([2, 3, 4, 5, 6, 7])
In [80]:
c.dtype
Out[80]:
dtype('int64')
In [81]:
#item size in bytes, so here the size of each element in variable c is 8 bytes
c.itemsize
Out[81]:
8
In [85]:
#by default the range starts from 0 with the gap of 1
d=np.arange(6)
print(d)
[0 1 2 3 4 5]
In [88]:
#reshaping of the array can be done, 
#the above single dimension array converted to two dimensional array
d=d.reshape(2,3)
print(d)
[[0 1 2]
 [3 4 5]]
In [94]:
#This displays the value at 3rd position in the array
#prints complete array when print without any index value
a=np.array([1,5,19,20,34,29,94,40,20])
print (a[3])
print (a)
20
[ 1  5 19 20 34 29 94 40 20]
In [96]:
#Prints the array from position 2 to 5
a[2:5]
Out[96]:
array([19, 20, 34])
In [ ]:
#displays the list of arrays np can produce
dir (np)
In [102]:
print (a)
[ 1  5 19 20 34 29 94 40 20]
In [108]:
#prints from the second position in the gap of 2
print(a[2::2])
print (a[2::3])

#in this the array starts with -1 position starts from the right side, reversing array
print(a[::-1])
print (a[::-2])
[-1 34 94 20]
[-1 29 20]
[20 40 94 29 34 20 -1 -1  1]
[20 94 34 -1  1]
In [106]:
#prints the assigned value from 1st position to the third position
a[1:3]=-1
print (a)
[ 1 -1 -1 20 34 29 94 40 20]
In [114]:
#every second position is converted to the -1
b=np.array([1,3,5,9,11])
b[::2]=-1
print(b)
[-1  3 -1  9 -1]
In [116]:
b=np.array([1,3,5,9,11])
b[1::2]=0
print(b)
[ 1  0  5  0 11]
In [130]:
#It allows to cut the slice from the original array
#change the value of any position to some other value
k=np.array([1,2,5,7,9,88,67])
k_slice = k[1:3]
k_slice[1] = 1000
print(k)
print(k_slice)
[   1    2 1000    7    9   88   67]
[   2 1000]
In [136]:
k=np.array([1,2,5,7,9,88,67])
another_slice= k[2:6].copy()
another_slice[1]=200
print(another_slice) # new slice
print(k)             # original array
[  5 200   9  88]
[ 1  2  5  7  9 88 67]
In [4]:
x=np.random.rand(5,6)
print(x)
[[0.10406157 0.05368971 0.91269767 0.45214983 0.87340885 0.35262691]
 [0.25833383 0.89730421 0.10028129 0.26796917 0.45453734 0.08997416]
 [0.656893   0.77679791 0.85114922 0.63211092 0.98084598 0.30275293]
 [0.23562224 0.04980663 0.50103353 0.39401704 0.26096712 0.56370068]
 [0.78407649 0.40684253 0.9239171  0.49700571 0.58630643 0.49854102]]
In [5]:
print(x[1])
print(x[1,1])
[0.25833383 0.89730421 0.10028129 0.26796917 0.45453734 0.08997416]
0.8973042139057217
In [7]:
#to cut out a specific portion from the big array, range can be used
x[0:2,0:2]
Out[7]:
array([[0.10406157, 0.05368971],
       [0.25833383, 0.89730421]])
In [10]:
y=np.random.rand(5,5,5)
print(y)
[[[0.73015902 0.98973224 0.93477625 0.03360677 0.80698303]
  [0.51499602 0.62340937 0.97849854 0.03194473 0.56381944]
  [0.66592848 0.47658055 0.18384973 0.76837887 0.72405425]
  [0.93305233 0.50185139 0.0394426  0.27218558 0.89016353]
  [0.78061572 0.9603428  0.34023383 0.50753472 0.83125349]]

 [[0.22780347 0.71603912 0.86404257 0.6084267  0.4346713 ]
  [0.48633194 0.08257174 0.49644077 0.25501305 0.73607762]
  [0.47434869 0.1782939  0.87745426 0.50246721 0.55400252]
  [0.16251666 0.34164355 0.00756262 0.57309259 0.42157396]
  [0.49505367 0.31741669 0.18297555 0.56408146 0.1823415 ]]

 [[0.29661565 0.93034075 0.24821185 0.184157   0.73233097]
  [0.88569214 0.74570775 0.51485669 0.37417759 0.36746538]
  [0.47157974 0.83396826 0.79976432 0.26061226 0.14040819]
  [0.22300439 0.45402698 0.8446786  0.59504963 0.44389346]
  [0.45252957 0.95746821 0.5382307  0.67380797 0.52945989]]

 [[0.62061588 0.03494204 0.81507982 0.27227264 0.75352237]
  [0.6968582  0.3754612  0.49193219 0.094736   0.68036831]
  [0.45991946 0.49537959 0.60403077 0.48419247 0.25858103]
  [0.42918033 0.17560046 0.47246742 0.46759024 0.33177646]
  [0.23761079 0.36254315 0.10369021 0.28431101 0.79236993]]

 [[0.98116309 0.76455912 0.29342566 0.49065722 0.23452616]
  [0.10630405 0.41611008 0.84033838 0.64275963 0.60471861]
  [0.38119269 0.11157626 0.70774385 0.72403426 0.83903038]
  [0.98085466 0.50600623 0.26298529 0.6989664  0.54945874]
  [0.05763875 0.31725466 0.94380222 0.62798057 0.1604905 ]]]
In [35]:
y[0:2,2:4,2:4]
Out[35]:
array([[[0.18384973, 0.76837887],
        [0.0394426 , 0.27218558]],

       [[0.87745426, 0.50246721],
        [0.00756262, 0.57309259]]])
In [36]:
a=np.arange(12).reshape(3,4)
a
Out[36]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [58]:
#two matrix of equal size can be add and subtract
a=np.array([20,30,40,50])
b=np.arange(4)
c= a + b
print(a)
print(b)
print(c)

c= a - b
print(c)

c= a * b
print(c)

c= a - b
print(c)
[20 30 40 50]
[0 1 2 3]
[20 31 42 53]
[20 29 38 47]
[  0  30  80 150]
[20 29 38 47]
In [61]:
A = np.array([[2,1],
             [0,1]])

B = np.array([[2,0],
             [3,4]])

C = A*B        

print(C)
[[4 0]
 [0 4]]
In [62]:
#(2*2 + 2*3, 1*0+1*4) like this for 2nd line
print(np.dot(A,B))
[[7 4]
 [3 4]]