Numpy Complete Notes
Numpy Complete Notes
a python library
by Gangadhar Tiwari
its_dheeruu__
What is Numpy?
NumPy is the fundamental package for scientific computing in Python.
its full form is numerical python
[ 2 4 56 422 32 1]
In [4]: type(a)
Out[4]: numpy.ndarray
In [5]: # 2D Array ( Matrix)
new= np.array([[45,34,22,2],[24,55,3,22]])
print(new)
[[45 34 22 2]
[24 55 3 22]]
In [6]: # 3 D - - - - # Tensor
np.array ( [[2,3,33,4,45],[23,45,56,66,2],[357,523,32,24,2],[32,32,44,33,234]]
dtype
The desired data-type for the array. If not given, then the type will be determined as the
minimum type required to hold the objects in the sequence.
In [8]: np.array([11,23,44] , dtype =bool) # Here True becoz , python treats Non -zero
Out[8]:
array([ True, True, True])
A growing plethora of scientific and mathematical Python-based packages are using NumPy
arrays; though these typically support Python-sequence input, they convert such input to
NumPy arrays prior to processing, and they often output NumPy arrays.
2/29
arange
arange can be called with a varying number of positional arguments same as range in list[ ]
reshape
Both of number products should be equal to number of Items present inside the
array.
In [12]: np .ar ang e( 1,11) .r eshap e( 5,2) # converted 5 rows and 2 columns
In [13]: np .ar ang e( 1,11) .r eshap e( 2,5) # converted 2 rows and 5 columns
np.random.random((4,3))
linspace
The numpy.linspace() function is used to create evenly spaced values over a specified
interval.
In [19]: np.linspace(-2,12,6)
Out[19]:
array([-2. , 0.8, 3.6, 6.4, 9.2, 12. ])
identity
The numpy.identity() function is used to create an identity matrix, which is a square matrix with
ones on the main diagonal and zeros elsewhere.
np.identity(3)
4/29
In [21]: np.identity(6)
Array Attributes
In [22]:
a1 = np.arange(10) # 1D
a1
Out[22]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
[[4, 5],
[6, 7]]])
ndim
To findout given arrays number of dimensions
In [25]: a1.ndim
Out[25]: 1
In [26]: a2.ndim
Out[26]: 2
In [27]: a3.ndim
Out[27]: 3
5/29
shape
gives each item consist of no.of rows and np.of column
Out[28]: (10,)
Out[29]: (3, 4)
In [30]: a3.shape # f i r s t ,2 says i t consists of 2D arrays .2,2 gives no.of rows and c
Out[30]: (2, 2, 2)
size
gives number of items
In [31]: a3
Out[31]: array([[[0,1],
[2, 3]],
[[4, 5],
[6, 7]]])
Out[32]: 8
In [33]: a2
In[34]: a2.size
Out[34]: 12
item size
Memory occupied by the item
6/29
In [35]: a2
Out[36]: 4
Out[37]: 8
Out[38]: 4
dtype
gives data type of the item
In [39]: print(a1.dtype)
print(a2.dtype)
print(a3.dtype)
int32
float64
int32
x = np.array([33,22,2.5]) x
In [41]: x.astype(int)
Out[41]: 2])
array([33, 22,
Array operations
In [42]: z1 = np .ar ang e( 12).r eshape( 3,4)
z2 = np.arange(12,24).reshape(3,4)
7/29
In [43]: z1
In [44]: z2
scalar operations
Scalar operations on Numpy arrays include performing addition or subtraction, or multiplication
on each element of a Numpy array.
In [45]: # arithmetic
z1 + 2
In [46]: # Subtraction
z1 - 2
In [47]: # Multiplication
z1* 2
Out[47]: array([[ 0, 2, 4, 6],
[ 8, 10, 12, 14],
[16, 18, 20, 22]])
In [48]: # power
z1 * * 2
In [49]: ## Modulo
z1 % 2
8/29
relational Operators
The relational operators are also known as comparison operators, their main function is to
return either a true or false based on the value of operands.
In [50]: z2
In [52]: z2 > 20
Out[52]:
array([[False, False, False, False],
[False, False, False, False],
[False, True, True, True]])
Vector Operation
We can apply on both numpy array
In [53]: z1
In [54]:
z2
Out[54]: array([[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])
In [55]: # Arthemetic
z1 +z2 # both numpy array Shape is same , we can add item wise
9/29
In [56]: z1 * z2
In [57]: z1 - z2
In [58]: z1 / z2
Array Functions
In [59]: k1 = np.random.random((3,3)) k1
= np.round(k1*100)
k1
In [60]: #Max
np .m ax(k1)
Out[60]: 98.0
In [61]: #min
np .m in(k1)
Out[61]: 24.0
In [62]: #sum
np.sum(k1)
Out[62]: 462.0
np.prod(k1)
Out[63]: 1297293445324800.0
10/29
In Numpy
0 = column , 1 = row
np.max(k1,axis = 1)
np.max(k1,axis = 0)
np.prod(k1,axis = 0)
In [67]: # mean
k1
In [68]: np .mean(k1)
Out[68]: 51.333333333333336
k1.mean(axis=0)
In [70]: # median
np .median( k1)
Out[70]: 49.0
11/29
In [72]: # Standard deviation
np .std( k1)
Out[72]: 19.89416441516903
In [74]: # variance
np .var( k1)
Out[74]: 395.77777777777777
Trignometry Functions
dot product
The numpy module of Python provides a function to perform the dot product of two arrays.
In [78]: s2 =np.arange(12).reshape(3,4)
s3 =np.arange(12,24).reshape(4,3)
In [79]: s2
Out[79]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
12/29
In [80]: s3
In [82]: np.exp(s2)
1. round
The numpy.round() function rounds the elements of an array to the nearest integer or to the
specified number of decimals.
[1. 3. 4. 5.]
In [84]: #randomly
np.round(np.random.random((2,3))*100)
13/29
2. floor
The numpy.floor() function returns the largest integer less than or equal to each element of an array.
[1. 2. 3. 4.]
3. Ceil
The numpy.ceil() function returns the smallest integer greater than or equal to each element of
an array.
In [90]: arr=np.array([1.2,2.7,3.5,4.9])
ceiled_arr = np.ceil(arr)
print(ceiled_arr)
[2. 3. 4. 5.]
In [91]: p1 = np.arange(10)
p2 = np.arange(12).reshape(3,4)
p3 = np.arange(8).reshape(2,2,2)
In [92]: p1
In [93]: p2
14/29
In [94]: p3
Indexing on 1D array
In [95]: p1
Out[95]: array([0,1,2,3,4,5,6,7,8,9])
p1[ -1]
Out[96]: 9
In [97]: # f e t c h i g f i r s t ietm
p 1[0]
Out[97]: 0
indexing on 2D array
In [98]: p2
Out[98]: array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
Out[100]: 6
Out[101]: 11
15/29
In [102]: # fetching desired element : 4
Out[102]: 4
indexing on 3D ( Tensors)
In [103]: p3
p 3[1,0,1]
Out[106]: 5
EXPLANATION:
p 3[0,1,0]
Out[109]: 2
EXPLANATION: Here firstly we take 0 because our desired is 2, is in first matrix which is 0.
and 2 row so 1 and first column so 0
p 3[0,0,0]
Out[110]: 0
Here first we take 0 because our desired is 0, is in first matrix which is 0. and 1 row so 0 and
first column so 0
p 3[1,1,0]
Out[113]: 6
16/29
EXPLANATION: Here first we take because our desired is 6, is in second matrix which is 1.
and second row so 1 and first column so 0
Slicing
Slicing on 1D
In [114]: p1
p 1[2:5]
EXPLANATION : Here First we take , whatever we need first item ,2 and up last(4) + 1 which
5 .because last element is not included
p 1[2:5:2]
Slicing on 2D
In [121]: p2
p2[0, :]
EXPLANATION :Here 0 represents first row and (:) represnts Total column
17/29
In [124]: # fetching total third column
p 2[:,2]
EXPLANATION :Here we want all rows so (:) , and we want 3rd column so 2
p2
EXPLANATION: Here first[1:3] we slice 2 second row is to third row is not existed which is 2
and Secondly, we take[1:3] which is same as first: we slice 2 second row is to third row is not
included which is 3
EXPLANATION: Here we take(:) because we want all rows, second(:2) for alternate value,
and (:) for all columns and (:3) jump for two steps..
18/29
In [163]: # fetch 1,3 and 9,11
p2
EXPLANATION: Here we take(:) because we want all rows, second(:2) for alternate value,
and (1) for we want from second column and (:2) jump for two steps and ignore middle one
p2
EXPLANATION: Here we take(1) because we want second row, second(:) for total column,
(:3) jump for two steps and ignore middle ones
In [159]:
p2[0:2] # f i r s t fetched rows
19/29
In [156]: p2[0:2 ,1: ] # for column
EXPLANATION: 0:2 selects the rows from index 0 (inclusive) to index 2 (exclusive), which
means it will select the first and second rows of the array., is used to separate row and column
selections. 1::2 selects the columns starting from index 1 and selects every second column. So
it will select the second and fourth columns of the array.
Slicing in 3D
In [172]: p3 = np.arange(27).reshape(3,3,3) p3
p 3[1]
20/29
In [179]: # fetch first and last
p 3[::2]
EXPLANATION: Along the first axis, (::2) selects every second element. This means it will
select the subarrays at indices 0 and 2
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
In [186]: p 3[0,1,:]
EXPLANATION : 0 represnts first matrix , 1 represents second row , (:) means total
21/29
In [187]: # Fetch 2 numpy array ,middle column ---> 10,13,16
p3
In [191]: p 3[1,:,1]
Out[191]:
array([10, 13, 16])
EXPLANATION : 1 respresnts middle column , (:) all columns , 1 represnts middle column
p3
22/29
In [195]: p3[2, 1: ] # last two rows
EXPLANATION: Here we go through 3 stages, where 2 for last array, and(1:) from second
row to total rows , and (1:) is for second column to total columns
In [197]: # Fetch o, 2, 18 , 20
p3
EXPLANATION: Here we take (0::2) first and last column, so we did jump using this, and we
took (0) for first row , and we (::2) ignored middle column
23/29
Iterating
In [208]: p1
for i in p1:
print(i)
0
1
2
3
4
5
6
7
8
9
In [209]: p2
for i in p2:
print(i) # prints rows
[ 0 1 2 3]
[4567]
[ 8 9 10 11]
In [210]: p3
24/29
In [213]: for iin p3:
print(i)
[[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]]
print all items in 3D using nditer ----> first convert in to 1D and applying Loop
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
Reshaping
25/29
In [217]: p2
In [219]: np.transpose(p2)
In [221]: p3
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
In [223]: p3.T
Ravel
26/29
In [225]: p2
In [224]: p2.ravel()
In [226]: p3
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
In [227]: p3.ravel()
Stacking
Stacking is the concept of joining arrays in NumPy. Arrays having the same dimensions can be
stacked
In [231]: w1
In [232]: w2
27/29
using hstack for Horizontal stacking
In [236]: np.hstack((w1,w2))
In [238]: w2
In [239]: np.vstack((w1,w2))
Splitting
In [240]: # Horizontal s p l i t t i n g
w1
28/29
In [241]: np.hsplit(w1,2) # splitting by 2
In [242]: np.hsplit(w1,4) # s p l i t t i n g b y 4
Out[242]: [array([[0],
[4],
8 ]),
array([[1],
[5],
9 ]),
array([[ 2],
[6],
10 ]),
array([[ 3],
[7],
11 ])]
In [244]: # Vertical s p l i t t i n g
w2
29/29
Numpy Arrays Vs Python Sequences
NumPy arrays have a fixed size at creation, unlike Python lists (which can grow dynamically).
Changing the size of an ndarray will create a new array and delete the original.
The elements in a NumPy array are all required to be of the same data type, and thus will be
the same size in memory.
NumPy arrays facilitate advanced mathematical and other types of operations on large
numbers of data. Typically, such operations are executed more efficiently and with less code
than is possible using Python’s built-in sequences.
A growing plethora of scientific and mathematical Python-based packages are using NumPy
arrays; though these typically support Python-sequence input, they convert such input to
NumPy arrays prior to processing, and they often output NumPy arrays.
List
1/19
In [1]: # Element-wise addition
a = [ i f o r i i nrange(10000000)]
b = [i for i in range(10000000,20000000)]
c=[ ]
import time
start = time.time()
for iin range(len(a)):
c.append(a[i] + b[i])
print(time.time()-start)
2.0619215965270996
Numpy
a = np.arange(10000000)
b = np.arange(10000000,20000000)
start = time.time()
c = a+b
print(time.time()-start)
0.1120920181274414
Out[3]: 120.35911871666826
so, Numpy is Faster than Normal Python programming, we can see in above Example.
because Numpy uses C type array
List
import sys
sys.getsizeof(P)
Out[4]: 89095160
2/19
Numpy
In [5]: R = np.arange(10000000)
sys.getsizeof(R)
Out[5]: 40000104
sys.getsizeof(R)
Out[6]: 20000104
w = np.arange(12).reshape(4,3) w
w [1,2]
Out[8]: 5
Fancy Indexing
Fancy indexing allows you to select or modify specific elements based on complex conditions
or combinations of indices. It provides a powerful way to manipulate array data in NumPy.
3/19
In [11]: w
w [[0,2,3]]
z = np.arange(24).reshape(6,4) z
z[[0,2,3,5]]
z[:,[0,2,3]]
4/19
Boolean indexing
It allows you to select elements from an array based on a Boolean condition. This allows you
to extract only the elements of an array that meet a certain condition, making it easy to perform
operations on specific subsets of data.
In [17]: G
G>50
Out[18]:
array([[ True, True, True, False],
[False, True , False, True],
[True, False, False, True],
[True, True, True, False],
[False, False, True, True],
[True, False, True, False]
In [19]: # Where is True , it gives result , everything other that removed.we got value
Out[19]: array([64, 51, 75, 86, 53, 60, 95, 75, 79, 98, 87, 58, 56, 93])
% 2 == 0
5/19
In [21]: # Gives only the even numbers
G[G % 2 == 0]
Out[21]: array([64, 50, 8, 86, 6, 60, 50, 98, 34, 58, 56, 26])
Here we used (&) bitwise Not logical (and), because we are working with boolean values
In [23]: # Result
G % 7 == 0
In [25]: # Result
~ % 7 == 0)] # (~) = Not
G[(G
Out[25]: array([64, 51, 75, 50, 8, 86, 6, 53, 60, 50, 95, 75, 79, 34, 45, 87, 58,
26, 93, 17])
Broadcasting
Used in Vectorization
The term broadcasting describes how NumPy treats arrays with different shapes during
arithmetic operations.
The smaller array is “broadcast” across the larger array so that they have compatible shapes.
6/19
In [26]: # same shape
a = np.arange(6).reshape(2,3)
b = np.arange(6,12).reshape(2,3)
print(a)
print(b)
print(a+b)
[[0 1 2]
[3 4 5]]
[[ 6 7 8]
[ 9 10 11]]
[[ 6 8 10]
[12 14 16]]
[[0 1 2]
[3 4 5]]
[[0 1 2]]
[[0 2 4]
[3 5 7]]
Broadcasting Rules
1. Make the two arrays have the same number of dimensions.
If the numbers of dimensions of the two arrays are different, add new dimensions with size
1 to the head of the array with the smaller dimension.
If the sizes of each dimension of the two arrays do not match, dimensions with size 1 are
stretched to the size of the other array.
If there is a dimension whose size is not 1 in either of the two arrays, it can not be
broadcasted, and an error is raised.
7/19
In [28]: # More examples
a = np.arange(12).reshape(4,3)
b = np.arange(3)
print(a) # 2 D
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
In [29]: print(b) # 1 D
[0 1 2]
[[ 0 2 4]
[ 3 5 7]
[ 6 8 10]
[ 9 11 13]]
EXPLANATION: Arthematic Operation possible because, Here a = (4,3) is 2D and b=(3) is1D
so did converted (3) to (1,3) and streched to (4,3)
8/19
In [31]: # Could not Broadcast
a = np.arange(12).reshape(3,4)
b = np.arange(3)
print(a)
print(b)
print(a+b)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[0 1 2]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9360/470058718.py in <module>
7 print( b)
8
----> print(a +b )
9
ValueError : operands could not be broadcast together with shapes (3,4) (3,)
EXPLANATION : Arthematic Operation not possible because , Here a = (3,4) is 2D and b =(3)
is 1D so did converted (3) to (1,3) and streched to (3,3) but , a is not equals to b . so it got failed
In [32]: a = np.arange(3).reshape(1,3)
b = np.arange(3).reshape(3,1)
print(a)
print(b)
print(a+b)
[[0 1 2]]
[[0]
[1]
[2]]
[[0 1 2]
[1 2 3]
[2 3 4]]
9/19
In [33]: a = np.arange(3).reshape(1,3)
b = np.arange(4).reshape(4,1)
print(a)
print(b)
print(a + b)
[[01 2]]
[[0]
[1]
[2]
[3]]
[[0 1 2]
[1 2 3]
[2 3 4]
[3 4 5]]
In [34]: a = np.array([1])
# shape -> (1,1) streched to 2,2
b = np.arange(4).reshape(2,2)
# shape -> (2,2)
print(a)
print(b)
print(a+b)
[1]
[[0 1]
[2 3]]
[[1 2]
[3 4]]
10/19
In [35]: # doesnt work
a = np.arange(12).reshape(3,4)
b = np.arange(12).reshape(4,3)
print(a)
print(b)
print(a+b)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9360/1200695402.py in <module>
7 print( b)
8
----> print(a +b )
9
ValueError : operands could not be broadcast together with shapes (3,4) (4,3)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 910 11]
[12 13 14 15]]
[[0 1]
[2 3]]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9360/2417388683.py in <module>
6 print( b)
7
----> print(a +b )
8
ValueError : operands could not be broadcast together with shapes (4,4) (2,2)
11/19
EXPLANATION : there is no 1 to convert ,so got failed
In [38]: k
In [39]: np.sum(k)
Out[39]: 45
In [40]: np.sin(k)
sigmoid
12/19
In [45]: k=np.arange(100)
sigmoid(k)
In [47]: actual
Out[47]: array([17, 4, 4, 24, 18, 44, 22, 25, 17, 39, 3, 34, 37, 12, 47, 22, 37,
9, 47, 38, 27, 46, 47, 34, 8])
In [48]: predicted
Out[48]: array([47, 31, 30, 17, 7, 22, 1, 16, 1, 24, 16, 7, 6, 37, 18, 15, 2,
33, 25, 33, 9, 17, 36, 7, 16])
mse( actual,predicted)
Out[50]: 469.0
In [51]: # detailed
actual-predicted
Out[51]: array([-30, -27, -26, 7, 11, 22, 21, 9, 16, 15, -13, 27, 31,
-25, 29, 7, 35, -24, 22, 5, 18, 29, 11, 27, -8])
13/19
In [52]: (actual-predicted)**2
Out[52]: array([ 900, 729, 676, 49, 121, 484, 441, 81, 256, 225, 169,
729, 961, 625, 841, 49, 1225, 576, 484, 25, 324, 841,
121, 729, 64], dtype=int32)
Out[53]: 469.0
S = np.array([1,2,3,4,np.nan,6])
S
In [56]: np.isnan(S)
Out[56]:
array([False, False, False, False, True, False])
14/19
Plotting Graphs
#plottinga2Dplot
In [59]:
# x = y
-
x =np.linspace(10,10,100) x
In [60]: y= x
15/19
In [61]: y
plt.plot(x ,y)
16/19
In [63]: # y = x^2
x = np.linspace(-10,10,100)
y = x ** 2
plt.plot(x,y)
Out[63]: [<matplotlib.lines.Line2D at 0x117324e7310>]
In [64]: # y = sin(x)
x = np.linspace(-10,10,100)
y = np.sin(x)
plt.plot(x,y)
17/19
In [65]: # y = xlog(x)
x = np.linspace(-10,10,100)
y = x * np.log(x)
plt.plot(x,y)
C:\Users\user\AppData\Local\Temp/ipykernel_9360/2564014901.py:3:RuntimeWarning:
invalid value encountered in log
y = x * np.log(x)
In [66]: # sigmoid
x = np.linspace(-10,10,100)
y = 1/(1+np.exp(-x))
plt.plot(x,y)
In [ ]:
18/19
In [1]: import numpy as np
import matplotlib.pyplot as plt
Meshgrid
Meshgrids are a way to create coordinate matrices from coordinate vectors. In NumPy,
the meshgrid function is used to generate a coordinate grid given 1D coordinate arrays. It
produces two 2D arrays representing the x and y coordinates of each point on the grid
x=np.linspace(0,10,100)
In [2]:
y=np.linspace(0,10,100)
In [3]: f = x**2+y**2
Plot
In [4]: plt.figure(figsize=(4,2))
plt.plot(f)
plt.show()
1/20
But f is a 1 dimensional function! How does one generate a surface plot?
In [5]: x=np.arange(3)
y = np.arange(3)
In [6]: x
In [7]: y
Generating a meshgrid:
In [9]: xv
In [10]: yv
2/20
In [11]: P = np.linspace(-4, 4, 9)
V=np.linspace(-5,5,11)
print(P)
print(V)
In [13]: print(P_1)
3/20
Numpy Meshgrid Creates Coordinates for a Grid System
These arrays, xv and yv, each seperately give the x and y coordinates on a 2D grid. You can do
normal numpy operations on these arrays:
In [15]: xv ** 2 + yv**2
In [16]: x = np.linspace(2,2,100)
y = np.linspace(-1,1,100)
xv,yv= np.meshgrid(x,y)
f = np.exp(- xv**2-yv**2)
Note: pcolormesh is typically the preferable function for 2D plotting, as opposed to imshow or
pcolor, which take longer.)
4/20
In [17]: plt.figure(figsize=(6, 3))
plt.pcolormesh(xv,yv,f,shading='auto')
plt.colorbar()
plt.grid()
plt.show()
x = np.linspace(-5, 5, 500)
y= np.linspace(-5,5,500)
xv,yv = np.meshgrid(x,y)
rectangular_mask = f(xv, yv)
plt.pcolormesh(xv,yv,rectangular_mask,shading='auto')
plt.colorbar()
plt.grid()
plt.show()
5/20
In [19]: # numpy.linspace creates an array of # 9
linearly placed elements between # -4
and 4, both inclusive
x = np.linspace(-4, 4, 9)
plt.colorbar()
plt.show()
6/20
In [24]: sine= (np.sin(x_1**2+ y_1**2))/(x_1**2+ y_1**2)
plt.contourf(x_1, y_1, sine, cmap = 'jet')
plt.colorbar()
plt.show()
C:\Users\user\AppData\Local\Temp/ipykernel_3612/3873722910.py:1:RuntimeWarni ng:
invalid value encountered in true_divide
sine = (np.sin(x_1**2 + y_1**2))/(x_1**2 + y_1**2)
We observe that x_1 is arow repeated matrix whereas y_1 is a column repeated matrix. One
row of x_1 and one column of y_1 is enough to determine the positions of all the points as the
other values will get repeated over and over.
In [26]: x_1
Out[26]: array([[-4., -3., -2., -1., 0., 1., 2., 3., 4.]])
In [27]: y_1
Out[27]: array([[-5.],
[-4.],
[-3.],
[-2.],
[-1.],
[ 0.],
[ 1.],
[ 2.],
[ 3.],
[ 4.],
[ 5.]])
7/20
The shape of x_1 changed from (11,9) to (1,9) and that of y_1 changed from (11,9) to (11,1)
The indexing of Matrix is how ever different. Actually, it is the exact opposite of Cartesian
indexing.
np.sort
Return a sorted copy of an array.
Out[28]:
array([46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10, 98])
In [31]: b = np.random.randint(1,100,24).reshape(6,4) # 2D
b
Out[32]: array([10, 12, 15, 33, 39, 44, 46, 53, 60, 66, 68, 74, 76, 87, 98])
Out[36]: array([98, 87, 76, 74, 68, 66, 60, 53, 46, 44, 39, 33, 15, 12, 10])
8/20
np.append
The numpy.append() appends values along the mentioned axis at the end of the array
In [37]: # code
Out[37]: array([46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10, 98])
In [38]: np.append(a,200)
Out[38]: array([46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74,
10, 98, 200])
In [39]: b #on 2D
np.append(b,np.ones((b.shape[0],1)))
Out[42]: array([6., 51., 40., 85.,35.,28., 91., 68., 27., 30., 6., 4., 18.,
48., 48., 15., 35.,45.,99., 17., 42., 29., 88., 31., 1., 1.,
1., 1., 1., 1.])
In [43]: np.append(b,np.ones((b.shape[0],1)),axis=1)
np.append(b,np.random.random((b.shape[0],1)),axis=1)
9/20
np.concatenate
numpy.concatenate() function concatenate a sequence of arrays along an existing axis.
In [45]: # code
c = np .ar ang e( 6).reshape(2,3)
d = np .ar ang e(6,12).reshape( 2,3)
In [46]: c
In [47]: d
np.unique
With the help of np.unique() method, we can get the unique values from an array given as
parameter in np.unique() method.
In [50]: # code
e = np.array([1,1,2,2,3,3,4,4,5,5,6,6])
In [51]: e
In [52]: np.unique(e)
10/20
np.expand_dims
With the help of Numpy.expand_dims() method, we can get the expanded dimensions of an
array
In [53]: #code
a
Out[53]: array([46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10, 98])
In [57]: a.shape # 1 D
Out[57]: (15,)
np.expand_dims(a,axis = 0)
Out[56]: array([[46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10, 98]])
In [60]: = s1)
np .exp and_di m s( a,axi
Out[60]: array([[46],
[53],
[15],
[44],
[33],
[39],
[76],
[60],
[68],
[12],
[87],
[66],
[74],
[10],
[98]])
Out[61]: (15, 1)
11/20
np. where
The numpy.where() function returns the indices of elements in an input array where the given
condition is satisfied.
In [62]: a
Out[62]: array([46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10, 98])
np.where(a> 50)
np.where(a> 50,0,a)
np.argmax
The numpy.argmax() function returns indices of the max element of the array in a particular
axis.
arg = argument
In [68]: # code
a
Out[68]: array([46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10, 98])
Out[69]: 14
12/20
In [71]: b # on 2D
In [75]: # np.argmin
Out[75]: array([46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10, 98])
In [76]: np.argmin(a)
Out[76]: 13
On Statistics:
np.cumsum
numpy.cumsum() function is used when we want to compute the cumulative sum of array
elements over a given axis.
In [77]: a
Out[77]: array([46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10, 98])
In [79]: np.cumsum(a)
Out[79]: array([ 46, 99, 114, 158, 191, 230, 306, 366, 434, 446, 533, 599, 673,
683, 781], dtype=int32)
13/20
In [85]: b
In [86]: np.cumsum(b)
Out[86]: array([ 6, 57, 97, 182, 217, 245, 336, 404, 431, 461, 467, 471, 489,
537, 585, 600, 635, 680, 779, 796, 838, 867, 955, 986], dtype=int32)
Out[88]: array([46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10, 98])
In [89]: np.cumprod(a)
np.percentile
numpy.percentile() function used to compute the nthpercentile of the given data (array
elements) along the specified axis.
14/20
In [90]: a
Out[90]: array([46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10, 98])
Out[91]: 98.0
Out[92]: 10.0
In [94]: np .m edian( a)
Out[94]: 53.0
np.histogram
Numpy has a built-in numpy.histogram() function which represents the frequency of data
distribution in the graphical form.
In [95]: a
Out[95]: array([46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10, 98])
np.corrcoef
Return Pearson product-moment correlation coefficients.
15/20
In [102]: salary
In [103]: experience
Utility functions
np.isin
With the help of numpy.is in() method, we can see that one array having values are checked in
a different numpy array having different elements with different sizes.
In [105]: # code
Out[105]: array([46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10, 98])
np.isin(a,items)
In [108]: a[np.isin(a,items)]
np.flip
Thenumpy.flip()functionreversestheorderofarrayelementsalongthespecifiedaxis,
preserving the shape of the array.
In [109]: # code
Out[109]: array([46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10, 98])
16/20
In [110]: np.flip(a) # reverse
Out[110]: array([98, 10, 74, 66, 87, 12, 68, 60, 76, 39, 33, 44, 15, 53, 46])
In [111]: b
Out[111]:
array([[ 6, 51, 40, 85],
[35, 28, 91, 68],
[27, 30, 6, 4],
[18, 48, 48, 15],
[35, 45, 99, 17],
[42, 29, 88, 31]])
In [112]: np .f l ip(b )
np.put
The numpy.put() function replaces specific elements of an array with given values of p_array.
Array indexed works on flattened array.
In [115]:
# code
a
Out[115]: array([46, 53, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10, 98])
\ 17/20
In [116]: np.put(a,[0,1],[110,530]) # permanent changes
In [117]: a
Out[117]: array([110, 530, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74,
10, 98])
np.delete
The numpy.delete() function returns a new array with the deletion of sub-arrays along with the
mentioned axis.
In [118]: # code
Out[118]: array([110, 530, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74,
10, 98])
Out[119]: array([530, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74, 10,
98])
Set functions
np.union1d
np.intersect1d
np.setdiff1d
np.setxor1d
np.in1d
In [121]: m = np.array([1,2,3,4,5])
n = np.array([3,4,5,6,7])
In [122]: # Union
np.union1d(m,n)
18/20
In [123]: # Intersection
np.intersect1d(m,n)
np.setdiff1d(m,n)
In [127]: np.setdiff1d(n,m)
Out[127]:
array([6, 7])
np.setxor1d(m,n)
np.in1d(m,1)
Out[131]: array([1])
In [130]: np.in1d(m,10)
np.clip
numpy.clip() function is used to Clip (limit) the values in an array.
In [132]: # code
Out[132]: array([110, 530, 15, 44, 33, 39, 76, 60, 68, 12, 87, 66, 74,
10, 98])
Out[133]: array([50, 50, 15, 44, 33, 39, 50, 50, 50, 15, 50, 50, 50, 15, 50])
19/20
it clips the minimum data to 15 and replaces everything below data to 15 and maximum
to 50
np. sw apaxes
In [138]: arr
In [139]: sw ap ped_arr
Out[139]:
array([[1, 4],
[2, 5],
[3, 6]])
In [140]: print("Originalarray:")
print(arr)
In [141]: print("Swappedarray:")
print(swapped_arr)
Swappedarray:
[[1 4]
[2 5]
[3 6]]
In [ ]:
20/20