Getting started with NumPy in Data Analytics
Getting started with NumPy in Data Analytics
Module-5
Using Python for Data Science
for i in range(0,4):
print(arr[i]) #1234
17-04-2025 Dr. V. Srilakshmi 10
NumPy Array Indexing
Access 2-D Arrays:
• To access elements from 2-D arrays we can use comma separated integers
representing the dimension and the index of the element.
• Example:
import numpy as np
print('2nd element on 1st row: ', arr1[0, 1]) # 2nd element on 1st row: 2
print(arr[1:5]) #2345
print(arr[4:]) #567
[[ 7 8]
[ 9 10]
[11 12]]]
Note : We can reshape an 8 elements 1D array into 4 elements in 2 rows 2D array but we
cannot reshape it into a 3 elements 3 rows 2D array as that would require 3x3 = 9
elements.
17-04-2025 Dr. V. Srilakshmi 19
NumPy Array Reshaping
• You are allowed to have one "unknown" dimension.
• Meaning that you do not have to specify an exact number for one of the
dimensions in the reshape method.
• Pass -1 as the value, and NumPy will calculate this number for you.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
newarr = arr.reshape(2, 2, -1) #observe that more than one -1 can’t be given
print(newarr)
Output:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
print('First array:’)
print(arr)
print('\nSecond array:’)
print(arr1)