Numpy
Numpy
The numpy library in Python is a fundamental package for numerical computing. It provides support for large,
multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these
arrays efficiently. Here are some key aspects of the numpy library:
In [1]:
#imports the numpy library into your Python script or interactive session. After importi
ng numpy, you gain access to all of its functions, classes, and submodules.
import numpy as np
In [2]:
arr = np.array([25,41,63,66,85,74])
arr
Out[2]:
In [3]:
numpy.ndarray
In [4]:
Out[4]:
6
In [5]:
In [6]:
Out[6]:
1
1
In [7]:
arr = np.array([25,41,63,66,85,74,2.01])
arr
Out[7]:
In [8]:
#Every numpy array has a data type (dtype) that specifies the type of elements stored in
the array.
arr.dtype
Out[8]:
dtype('float64')
In [9]:
2.0.0
In [10]:
arr = np.array([25,41,63,66,85,74,2.01,'krati',True])
arr
Out[10]:
In [11]:
arr.dtype
Out[11]:
dtype('<U32')
In [12]:
ls = [25,41,63,66,85,74]
arr = np.array(ls)
arr
Out[12]:
In [13]:
#Used to access the last element of Array
arr[-1]
Out[13]:
np.int64(74)
arr[start:end]: Returns elements starting from index start up to, but not including, index end.
In [14]:
# arr[2:4] refers to slicing an array arr to extract elements from index 2 up to, but no
t including, index 4.
arr[2:4]
Out[14]:
array([63, 66])
In [15]:
# arr[2:] is a slicing operation that retrieves elements from index 2 to the end of the
array arr.
arr[2:]
Out[15]:
In [16]:
#arr[:4] is a slicing operation that retrieves elements from the beginning of the array
arr up to, but not including, index 4.
arr[:4]
Out[16]:
In [17]:
#arr[start:end:step]: Returns elements starting from index start up to, but not includin
g, index end, with a step size of step.
#arr[::] in numpy creates a shallow copy of the entire array arr, returning all elements
in the same order.
arr[::] #starting--> 0, stoping--> end, jump-->1
Out[17]:
In [18]:
arr[-1] = 500
In [19]:
arr
Out[19]:
array([ 25, 41, 63, 66, 85, 500])
In [20]:
#performing arr + 2 is a basic arithmetic operation that adds the scalar value 2 to each
element in the numpy array arr.
arr + 2
Out[20]:
array([ 27, 43, 65, 68, 87, 502])
In [21]:
#arr * 2 will multiply each element of the array arr by 2.
arr*2
Out[21]:
array([ 50, 82, 126, 132, 170, 1000])
Creation of 2D array You can create a 2D array by passing a nested list to numpy.array:
In [22]:
In [22]:
#The variable ls represents a nested list in Python:
ls = [[1,2,3],[4,5,6],[7,8,9]]
In [23]:
#This will display the nested list ls as it is defined. Each sublist corresponds to a ro
w in the 2D structure.
ls
Out[23]:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In [24]:
# Used to check the type of list elements
type(ls)
Out[24]:
list
In [25]:
#ls[0] gives you the first sublist [1, 2, 3] from the nested list ls.
ls[0]
Out[25]:
[1, 2, 3]
In [26]:
#ls[-1] gives you the last sublist [7, 8, 9] from the nested list ls.
ls[-1]
Out[26]:
[7, 8, 9]
In [27]:
#ls[0][1] accesses the element at index 1 within the sublist ls[0].
ls[0][1] #ls[0,1]
Out[27]:
2
In [28]:
# convert the nested list ls into a NumPy array using np.array(ls) & assign to arr2
arr2 = np.array(ls)
In [29]:
arr2
Out[29]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [30]:
#The ndim attribute in NumPy arrays returns the number of dimensions (or axes) of the ar
ray. For a 2D array, it will return 2.
arr2.ndim
Out[30]:
2
2
In [31]:
#The shape attribute in NumPy arrays returns a tuple that represents the size of each di
mension of the array. For a 2D array (matrix), the shape tuple will have two elements: t
he number of rows and the number of columns.
arr2.shape
Out[31]:
(3, 3)
In [32]:
arr2[1]
Out[32]:
array([4, 5, 6])
In [33]:
#The slice 0:2 selects rows from index 0 to 1 (exclusive of index 2), which gives us the
first two rows of arr2.
arr2[0:2]
Out[33]:
array([[1, 2, 3],
[4, 5, 6]])
In [34]:
#arr2[1][1:] further slices the second row (arr2[1]), starting from index 1 to the end.
In this case, it selects elements [5, 6] from the second row [4, 5, 6].
arr2[1][1:]
# or
#arr2[1,1:]
Out[34]:
array([5, 6])
In [35]:
arr2[1:,1:]
Out[35]:
array([[5, 6],
[8, 9]])
In [36]:
#result contains the subarray starting from the second-to-last row and second-to-last co
lumn of arr2, which includes elements [5, 6] and [8, 9]
arr2[-2:,-2:]
Out[36]:
array([[5, 6],
[8, 9]])
In [37]:
In [38]:
arr2
Out[38]:
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 90]])
Built-in function
In [39]:
# Generate a NumPy array with 500 random integers between 1 and 199
arr = np.random.randint(1,200,500)# 500-> no. of items, 1,200-> in b/w 1 to 200
arr # single d array
Out[39]:
array([ 89, 140, 41, 188, 50, 20, 43, 121, 28, 130, 106, 175, 179,
80, 122, 173, 172, 197, 40, 176, 23, 139, 175, 37, 152, 41,
124, 55, 129, 9, 34, 138, 58, 84, 10, 193, 81, 193, 120,
17, 133, 195, 183, 119, 74, 148, 94, 52, 161, 117, 152, 11,
165, 14, 7, 96, 197, 71, 190, 158, 196, 148, 137, 29, 33,
76, 106, 26, 116, 51, 142, 166, 136, 153, 95, 76, 20, 27,
89, 159, 148, 160, 100, 27, 114, 20, 163, 110, 119, 90, 58,
15, 49, 167, 43, 124, 33, 52, 162, 175, 17, 107, 88, 184,
195, 71, 143, 80, 141, 46, 18, 8, 2, 37, 167, 19, 182,
171, 97, 25, 191, 77, 197, 164, 18, 80, 67, 25, 55, 102,
1, 48, 115, 66, 146, 88, 121, 197, 34, 1, 90, 13, 145,
83, 80, 148, 71, 198, 90, 198, 80, 61, 57, 160, 73, 197,
147, 31, 50, 80, 103, 105, 61, 13, 92, 150, 108, 143, 111,
172, 178, 55, 17, 147, 6, 118, 59, 139, 162, 20, 42, 143,
67, 31, 79, 116, 188, 30, 128, 163, 154, 107, 83, 197, 32,
6, 114, 38, 134, 65, 175, 76, 43, 15, 133, 170, 52, 123,
118, 74, 163, 145, 62, 103, 55, 65, 102, 65, 196, 134, 31,
63, 43, 38, 188, 45, 128, 56, 6, 197, 45, 118, 120, 123,
181, 91, 86, 194, 73, 167, 149, 96, 101, 177, 65, 68, 41,
82, 70, 182, 48, 112, 117, 195, 64, 145, 114, 5, 184, 163,
21, 195, 35, 113, 181, 168, 188, 160, 100, 47, 195, 15, 180,
65, 29, 41, 101, 56, 19, 129, 137, 27, 60, 131, 130, 170,
69, 138, 50, 53, 10, 180, 140, 64, 85, 113, 125, 183, 25,
57, 69, 195, 177, 65, 106, 178, 12, 98, 83, 131, 197, 137,
29, 67, 10, 141, 55, 43, 48, 181, 29, 197, 29, 110, 15,
68, 160, 21, 139, 139, 181, 174, 70, 157, 158, 2, 152, 98,
103, 175, 30, 144, 52, 110, 28, 193, 172, 32, 25, 53, 166,
96, 147, 193, 11, 46, 138, 73, 67, 45, 162, 131, 58, 184,
67, 8, 70, 146, 50, 119, 54, 95, 61, 85, 42, 3, 169,
27, 130, 128, 117, 24, 165, 84, 153, 1, 87, 3, 7, 16,
41, 44, 50, 17, 59, 196, 80, 49, 17, 89, 179, 135, 60,
40, 103, 39, 51, 57, 4, 107, 195, 118, 73, 145, 152, 54,
194, 191, 133, 43, 108, 187, 154, 20, 30, 174, 114, 166, 179,
184, 158, 30, 80, 87, 60, 177, 116, 60, 161, 157, 173, 51,
81, 38, 133, 168, 129, 197, 172, 98, 164, 66, 58, 13, 33,
75, 186, 146, 178, 122, 193, 81, 29, 123, 189, 184, 168, 124,
151, 35, 193, 12, 66, 190, 8, 197, 139, 114, 168, 139, 7,
54, 23, 62, 60, 155, 14, 37, 136, 125, 57, 79, 103, 162,
161, 81, 46, 191, 197, 102], dtype=int32)
In [40]:
c=0
for i in arr:
if (i<100):
c=c+1
print(c)
250
In [41]:
# Count elements less than or equal to 100
arr[arr<=100].size
Out[41]:
252
OR
In [42]:
len(arr[arr<=100])
Out[42]:
252
In [43]:
#filtered_arr.shape retrieves the shape of the resulting filtered array.
arr[arr<=100].shape
Out[43]:
(252,)
In [44]:
## Generate a NumPy array with shape (10, 6) containing random integers between 1 and 19
9
arr2 = np.random.randint(1,200,(10,6)) # 10-> row, 5-> col
arr2
Out[44]:
array([[ 4, 136, 178, 177, 34, 17],
[ 84, 56, 40, 188, 61, 60],
[ 28, 101, 144, 175, 147, 125],
[ 7, 134, 187, 108, 61, 2],
[155, 37, 182, 96, 78, 163],
[ 63, 68, 6, 100, 61, 194],
[ 35, 83, 197, 186, 195, 170],
[ 15, 122, 105, 96, 97, 65],
[ 20, 98, 25, 105, 135, 183],
[158, 110, 164, 39, 103, 83]], dtype=int32)
In [45]:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
In [46]:
arr.dtype
Out[46]:
dtype('float64')
In [47]:
In [48]:
# Create a NumPy array with 10 elements initialized to one
arr = np.ones(10)
arr
Out[48]:
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [49]:
arr = np.ones((10,5))
arr
Out[49]:
In [50]:
ls = [1,2,3,4,5,6,7,8,9]
ls
Out[50]:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
In [51]:
#The code ls = list(range(10)) creates a Python list ls containing integers from 0 to 9.
ls = list(range(10))
ls
Out[51]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [52]:
#np.arange(10) creates a NumPy array that starts at 0 and stops before 10,
np.arange(10) #create single dimentional array
Out[52]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [53]:
#np.arange(5, 100) creates a NumPy array that starts at 5 and stops before 100, resultin
g in elements from 5 up to 99.
np.arange(5,100)
Out[53]:
array([ 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, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
In [54]:
np.arange(10)
Out[54]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [55]:
arr = np.arange(60)
arr #single dimentional array
Out[55]:
In [56]:
# 2D , 60
# 10,6
Out[56]:
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],
[36, 37, 38, 39, 40, 41],
[42, 43, 44, 45, 46, 47],
[48, 49, 50, 51, 52, 53],
[54, 55, 56, 57, 58, 59]])
In [57]:
arr.reshape(10,7)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[57], line 1
----> 1 arr.reshape(10,7)
In [ ]:
arr.reshape(3,20)
Out[ ]:
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,
36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59]])
In [ ]:
arr = arr.reshape(10,6)
In [ ]:
arr
Out[ ]:
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],
[36, 37, 38, 39, 40, 41],
[42, 43, 44, 45, 46, 47],
[48, 49, 50, 51, 52, 53],
[54, 55, 56, 57, 58, 59]])
creation of 3D array
In [ ]:
arr1 = [1,2,3]
arr2 = [[1,2,3],[1,2,3],[1,2,3]] #[2d,2d,2d]
arr3 = [[[1,2,3],[1,2,3],[1,2,3]],[[1,2,3],[1,2,3],[1,2,3]]]
In [ ]:
arr = np.array(arr3)
arr
Out[ ]:
array([[[1, 2, 3],
[1, 2, 3],
[1, 2, 3]],
[[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]])
In [ ]:
#Check the dimention
arr.ndim
Out[ ]:
In [ ]:
# Generate a 3D array with shape (3, 5, 3) filled with random integers between 1 and 199
arr3 = np.random.randint(1,200,(3,5,3)) #table row col
arr3
Out[ ]:
In [ ]:
Out[ ]:
(3, 5, 3)
In [ ]:
arr3[1,4,0:]
Out[ ]:
array([ 89, 173, 57], dtype=int32)
In [ ]:
arr3[2,0:2,1:]
Out[ ]:
array([[ 48, 156],
[172, 49]], dtype=int32)
or
In [ ]:
arr3[:,0,:]
Out[ ]:
OR
In [ ]:
#arr3[:, -5, :] accesses a specific subset of arr3:
#:: Selects all matrices (pages) along the first dimension.
#-5: Selects the fifth row from the end along the second dimension (5 is the total numbe
r of rows, so -5 selects the first row).
#:: Selects all elements along the third dimension.
arr3[:,-5,:]
Out[ ]:
In [ ]:
arr = np.array([9,7,8])
arr
Out[ ]:
array([9, 7, 8])
In [ ]:
min(arr)
Out[ ]:
np.int64(7)
In [ ]:
Out[ ]:
np.int64(9)
In [ ]:
mean(arr)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[58], line 1
----> 1 mean(arr)
In [ ]:
Out[ ]:
np.float64(8.0)
In [ ]:
np.sum(arr)
Out[ ]:
np.int64(24)
In [ ]:
arr
Out[ ]:
array([9, 7, 8])
In [ ]:
Out[ ]:
np.int64(1)
In [ ]:
# Find the index of the minimum value
np.argmax(arr)
Out[ ]:
np.int64(0)
In [ ]:
In [ ]:
arr.sort() # by default sort in asc. order
In [ ]:
arr
Out[ ]:
array([7, 8, 9])
In [ ]:
array([9, 8, 7])
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: