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

Muhammad Danial

muhammaddanialarain@gmail.com

Numpy

In this notebook, we have covered the intermediate level of numpy.

in the next notebook, we will cover the advanced level of numpy.

And i hope this notebook is very useful for everyone.

Creating Numpy Arrays


1. np.array
2. np.arange
3. np.ones
4. np.zeros
5. np.linspace
6. np.identity
7. np.random

In [2]:

# np.array
import numpy as np
a=np.array([1,2,3,4,5])
print(a)
print(type(a))

[1 2 3 4 5]
<class 'numpy.ndarray'>

In [3]:
# 2D and 3D
b=np.array([[1,2,3],[4,5,6]])
print(b)

[[1 2 3]
[4 5 6]]

In [4]:
c=np.array([[[1,2,3],[4,5,6],[7,8,9]]])
print(c)

[[[1 2 3]
[4 5 6]
[7 8 9]]]

In [6]:
# dtype
f=np.array([1,2,3],dtype='float')
print(f)

[1. 2. 3.]
1. np.arange

In [8]:
np.arange(1,11)

Out[8]:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

2. with reshape
. reshape(rows,columns)

In [16]:

np.arange(1,11).reshape(5,2)
Out[16]:
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10]])

In [10]:
np.arange(1,11).reshape(2,5)
Out[10]:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])

3. np.ones and np.zeros

In [12]:
np.ones((3,4))
Out[12]:
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])

In [43]:
np.ones((4,3,6))
Out[43]:

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


[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]],

[[1., 1., 1., 1., 1., 1.],


[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]],

[[1., 1., 1., 1., 1., 1.],


[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]],

[[1., 1., 1., 1., 1., 1.],


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

In [45]:
# you can change the datatype
np.ones((4,3,6),dtype='int')
Out[45]:
array([[[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1]],

[[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1]],

[[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1]],

[[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1]]])

In [30]:
np.zeros((3,4))
Out[30]:
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])

4. np.random
using np.random method we can make an array of random numbers.

In [51]:
np.random.random(3)
Out[51]:
array([0.18759714, 0.83692415, 0.52941064])

In [47]:
np.random.random((3,4))
Out[47]:
array([[0.54646989, 0.92023329, 0.04073484, 0.26406919],
[0.94948329, 0.73971461, 0.6359286 , 0.73424497],
[0.85442418, 0.49475889, 0.14306405, 0.78723788]])

In [52]:
np.random.random((3,4,3))
Out[52]:

array([[[0.21789225, 0.52809489, 0.6049151 ],


[0.16774637, 0.16056792, 0.8972943 ],
[0.37506615, 0.18179426, 0.0792358 ],
[0.22047776, 0.4789056 , 0.50900953]],

[[0.73891653, 0.49041205, 0.53363445],


[0.90544447, 0.93629079, 0.62679403],
[0.00107555, 0.41568539, 0.77760719],
[0.88591051, 0.87440529, 0.53040491]],

[[0.42777187, 0.19424989, 0.49594228],


[0.74330592, 0.17282806, 0.38881508],
[0.4141158 , 0.23539759, 0.69825243],
[0.38487035, 0.58828143, 0.84049319]]])

In [81]:
np.random.randint((3,4))
Out[81]:
array([1, 2])

5. np.linspace
np.linspace(lower range,upper range,number want to generate)

this generate the number equal distance

In [55]:
np.linspace(1,5,10)
Out[55]:
array([1. , 1.44444444, 1.88888889, 2.33333333, 2.77777778,
3.22222222, 3.66666667, 4.11111111, 4.55555556, 5. ])

6. np.identity
Return a identity matrix i.e. a square matrix with ones on the main diagonal.

In [60]:
np.identity(4)
Out[60]:
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])

Numpy Array Attributes


In [84]:
a1=np.arange(10)
a2=np.arange(12,dtype=float).reshape(3,4)
a3=np.arange(8).reshape(2,2,2)
print("Array 1: ")
print(a1)
print("Array 2: ")
print(a2)
print("Array 3: ")
print(a3)

Array 1:
[0 1 2 3 4 5 6 7 8 9]
Array 2:
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]
[ 8. 9. 10. 11.]]
Array 3:
[[[0 1]
[2 3]]

[[4 5]
[6 7]]]
1. ndim
ndim attribute tells the dimension of an array.

In [85]:
print("dimension of array 1: ",a1.ndim)
print('dimension of array 3: ',a3.ndim)

dimension of array 1: 1
dimension of array 3: 3

2. shape
The "shape" of an array is a tuple with the number of elements per axis (dimension).

In [87]:
print("The shape of array is : ",a1.shape)
print("The shape of array is : ",a3.shape)

The shape of array is : (10,)


The shape of array is : (2, 2, 2)

3. size
count the number of elements

In [93]:
a2
Out[93]:
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])

In [94]:
print("the size of array : ",a2.size)
print("the size of array : ",a3.size)

the size of array : 12


the size of array : 8

4. itemsize
itemsize returns the size (in bytes) of each element of a NumPy array.

In [28]:
print("Item size of array : ",a1.itemsize)
print("Item size of array : ",a3.itemsize)

Item size of array : 4


Item size of array : 4

1. dtype

This gives us information about the type of data.

In [95]:
print(a3.dtype)
print(a3.dtype)

int32

Changing Datatype

1. astype

In [96]:

a3.astype(np.float64)
Out[96]:
array([[[0., 1.],
[2., 3.]],

[[4., 5.],
[6., 7.]]])

Array Operations
In [97]:
a1=np.arange(12).reshape(3,4)
a2=np.arange(12,24).reshape(3,4)

In [99]:
a1
Out[99]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [100]:
a2
Out[100]:
array([[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])

1. Scalar operations

In [101]:
# Arithmetic
## in this we use all mathematical operations
a1*2 # in this 2 is scalar
Out[101]:
array([[ 0, 2, 4, 6],
[ 8, 10, 12, 14],
[16, 18, 20, 22]])

In [102]:
# relational
a2 > 15
Out[102]:

array([[False, False, False, False],


array([[False, False, False, False],
[ True, True, True, True],
[ True, True, True, True]])

2. Vector operations

In [103]:
# Arithmetic
## in vector we use both are numpy array
a1 + a2
Out[103]:
array([[12, 14, 16, 18],
[20, 22, 24, 26],
[28, 30, 32, 34]])

Array Functions
1. np.round()

In [183]:
a1=np.random.random((3,3))
a1=np.round(a1*6)
a1
Out[183]:
array([[2., 5., 4.],
[4., 3., 5.],
[1., 5., 6.]])

In [184]:
# max/min/sum/prod
print(np.max(a1))
print(np.min(a1))
print(np.sum(a1))
print(np.prod(a1))

6.0
1.0
35.0
72000.0

In [185]:
# if u want to find the minimum or maximum number in rows or columns then you can pass th
e axis.
# 0 --> columns and 1 --> rows
np.max(a1,axis=1)
Out[185]:
array([5., 5., 6.])

In [188]:
# mean/median/std/var
print("mean: ",np.mean(a1))
print("median: ",np.median(a1))
print("standard deviation: ",np.std(a1))
print("variance: ",np.var(a1))

mean: 3.888888888888889
median: 4.0
standard deviation: 1.5234788000891208
variance: 2.3209876543209877
variance: 2.3209876543209877

In [189]:
# trignometric function
np.sin(a1)
Out[189]:
array([[ 0.90929743, -0.95892427, -0.7568025 ],
[-0.7568025 , 0.14112001, -0.95892427],
[ 0.84147098, -0.95892427, -0.2794155 ]])

In [200]:
# dot product
a2=np.arange(12).reshape(3,4)
a3=np.arange(12,24).reshape(4,3)
print(a2)
print("*"*20)
print(a3)
print("-"*20)
print("shape of array 1 and array 2: ",a2.shape,a3.shape)
print("-"*20)
print("dot product: ")
np.dot(a2,a3)

[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
********************
[[12 13 14]
[15 16 17]
[18 19 20]
[21 22 23]]
--------------------
shape of array 1 and array 2: (3, 4) (4, 3)
--------------------
dot product:
Out[200]:
array([[114, 120, 126],
[378, 400, 422],
[642, 680, 718]])

In [201]:
# log and exponents
print("log of given array 1")
print(np.log(a1))
print("************************************")
print("exponent of given array 1")
print(np.exp(a1))

log of given array 1


[[0.69314718 1.60943791 1.38629436]
[1.38629436 1.09861229 1.60943791]
[0. 1.60943791 1.79175947]]
************************************
exponent of given array 1
[[ 7.3890561 148.4131591 54.59815003]
[ 54.59815003 20.08553692 148.4131591 ]
[ 2.71828183 148.4131591 403.42879349]]

In [202]:
# round/floor/ceil
aa=np.random.random((2,3))*100

In [203]:
aa
Out[203]:
array([[68.4927388 , 29.54681998, 22.17497348],
[41.60535049, 71.22126439, 24.67596716]])

In [204]:
np.round(aa)
Out[204]:
array([[68., 30., 22.],
[42., 71., 25.]])

In [205]:
np.floor(aa)
Out[205]:
array([[68., 29., 22.],
[41., 71., 24.]])

In [206]:
np.ceil(aa)

Out[206]:
array([[69., 30., 23.],
[42., 72., 25.]])

indexing and slicing


In [207]:
a1=np.arange(10)
a2=np.arange(12).reshape(3,4)
a3=np.arange(8).reshape(2,2,2)

In [208]:
print(a1)
print("***********")
print(a2)
print("***********")
print(a3)

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

[[4 5]
[6 7]]]

indexing

In [209]:
a1
Out[209]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [210]:
a1[-1]
Out[210]:
9

In [211]:
a1[1:5]
Out[211]:
array([1, 2, 3, 4])

In [212]:
a2
Out[212]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [213]:
print(a2[1][2])
print(a2[1,2])

6
6

In [214]:
a3
Out[214]:
array([[[0, 1],
[2, 3]],

[[4, 5],
[6, 7]]])

In [215]:
print(a3[1,0,1])
print(a3[1][0][1])

5
5

slicing

In [216]:
a1
Out[216]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [217]:

a1[0:6:2]
Out[217]:

array([0, 2, 4])

In [218]:
In [218]:

a2
Out[218]:

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

In [219]:
a2[0,:]

Out[219]:
array([0, 1, 2, 3])

In [220]:

a2[:,2]
Out[220]:

array([ 2, 6, 10])

In [221]:
a2[1:,1:3]

Out[221]:

array([[ 5, 6],
[ 9, 10]])

In [222]:
a2[::2,1::2]

Out[222]:
array([[ 1, 3],
[ 9, 11]])

In [225]:

a2[1,::2]
Out[225]:

array([4, 6])

In [134]:
a2[0:2,1:]

Out[134]:
array([[1, 2, 3],
[5, 6, 7]])

In [226]:

a2
Out[226]:

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

In [98]:
a2[::2,::3]

Out[98]:
Out[98]:
array([[ 0, 3],
[ 8, 11]])

In [239]:

a3=np.arange(36).reshape(4,3,3)

In [240]:

a3
Out[240]:

array([[[ 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],


[30, 31, 32],
[33, 34, 35]]])

In [246]:

a3[0::3]
Out[246]:

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

[[27, 28, 29],


[30, 31, 32],
[33, 34, 35]]])

In [141]:

a3[0,1]
Out[141]:

array([3, 4, 5])

In [159]:
a3[1,:,1]

Out[159]:
array([10, 13, 16])

In [162]:
a3[2,1:,1:]

Out[162]:
array([[22, 23],
[25, 26]])

In [176]:

a3[0::2,0,::2]
Out[176]:
Out[176]:

array([[ 0, 2],
[18, 20]])

iterating
In [247]:

a1
for i in a1:
print(i)

0
1
2
3
4
5
6
7
8
9

In [248]:
a2
for i in a2:
print(i)
print("****")

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

In [249]:

a3
for i in a3:
print(i)
print("***")

[[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]
[30 31 32]
[33 34 35]]
***

In [250]:

for i in np.nditer(a3):
print(i)

0
1
2
3
4
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
30
31
32
33
34
35

Reshaping
In [ ]:
# reshape

In [251]:
# Transpose
print(a2)
print("*****")
print(np.transpose(a2))
print("*****")
print(a2.T)

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

In [252]:
# ravel
## it is used to convert any dimension of array into 1d array
print(a2)
print("*****")
print(a2.ravel())
print("*****")
print(a3.ravel())

[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
*****
[ 0 1 2 3 4 5 6 7 8 9 10 11]
*****
[ 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 30 31 32 33 34 35]

Stacking
in this we can merge / combine/ 2 or more arrays

In [253]:

# horizontal stacking
a4=np.arange(12).reshape(3,4)
a5=np.arange(12,24).reshape(3,4)

In [255]:
a4

Out[255]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [256]:

a5
Out[256]:

array([[12, 13, 14, 15],


[16, 17, 18, 19],
[20, 21, 22, 23]])

In [257]:
np.hstack((a4,a5))

Out[257]:
array([[ 0, 1, 2, 3, 12, 13, 14, 15],
[ 4, 5, 6, 7, 16, 17, 18, 19],
[ 8, 9, 10, 11, 20, 21, 22, 23]])

In [258]:
# veritcal stacking
np.vstack((a4,a5))

Out[258]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])

Splitting
just the opposite of Stacking
In [202]:

# horizontal splitting
a4
Out[202]:

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

In [259]:

np.hsplit(a4,2)

Out[259]:
[array([[0, 1],
[4, 5],
[8, 9]]),
array([[ 2, 3],
[ 6, 7],
[10, 11]])]

In [260]:

# vertical splitting
a5
Out[260]:

array([[12, 13, 14, 15],


[16, 17, 18, 19],
[20, 21, 22, 23]])

In [261]:
np.vsplit(a5,3)

Out[261]:
[array([[12, 13, 14, 15]]),
array([[16, 17, 18, 19]]),
array([[20, 21, 22, 23]])]

meshgrid

In [266]:

x=[1,2,3,4]
y=[5,6,7]
xm,ym=np.meshgrid(x,y)

In [267]:

xm
Out[267]:

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

In [268]:
ym

Out[268]:
array([[5, 5, 5, 5],
[6, 6, 6, 6],
[7, 7, 7, 7]])
In [270]:

import matplotlib.pyplot as plt

In [281]:
a = np.arange(-10, 10, 0.1)
b = np.arange(-10, 10, 0.1)
xa, xb = np.meshgrid(a, b)
z = np.tan(xa**2 + xb**2) / (xa**2 + xb**2)
h = plt.contourf(a,b,z)
plt.show()

In [290]:
a = np.linspace(-5, 5, 5)
b = np.linspace(-5, 5, 11)
random_data = np.random.random((11, 5))
xa, xb = np.meshgrid(a, b)
cos = (np.cos(xa**2 + xb**3))/(xa**4 + xb**5)
plt.contourf(xa, xb,cos, cmap = 'jet')
plt.colorbar()
plt.show()

C:\Users\DELL\AppData\Local\Temp/ipykernel_12412/3436719025.py:5: RuntimeWarning: divide


by zero encountered in true_divide
cos = (np.cos(xa**2 + xb**3))/(xa**4 + xb**5)

You might also like