Page 255 - Robotics and AI class 10
P. 255
90
82
95
b. (i) Python code that demonstrates numpy array slicing import numpy as np
# Creating a numpy array
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# Slicing the array to get elements from index 2 to 5
sliced_arr = arr[2:6]
print("Sliced Array:", sliced_arr)
Output:
Sliced Array: [3 4 5 6]
c. (i) Python code for getting numpy array shape and reshaping them.
import numpy as np
# Creating a numpy array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Getting the shape of the array
shape = arr.shape
# Reshaping the array to a 1D array
reshaped_arr = arr.reshape(-1)
print("Shape:", shape)
print("Reshaped Array:", reshaped_arr)
Output:
Shape: (3, 3)
Reshaped Array: [1 2 3 4 5 6 7 8 9]
d. (i) Python code for iterating a numpy array.
import numpy as np
# Creating a numpy array
arr = np.array([10, 20, 30, 40, 50])
# Iterating through the array
for element in arr:
print(element)
Output:
10
20
30
40
50
Assignment 253

