3252 4 Ids
3252 4 Ids
EXPERIMENT - IV
The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second
has index 1 etc.
3D Array: To access elements from 3-D arrays we can use comma separated integers representing the
dimensions and the index of the element.
Negative Indexing: Use negative indexing to access an array from the end.
Slicing : Slicing in python means taking elements from one given index to another given index.
import numpy as np
print(self.arr[0:3])
Print(“2D array”)
Print(self.tarr)
Print(self.tarr[1:])
Import numpy as np
Print(self.tarr.reshape(1,2,2)
Print(self.tarr[0:2])
d. Negative Indexing
Print(self.tarr[-1 :-3])
Code:
import numpy as np
class Array:
def createArray(self):
l=[]
for i in range(0,n):
l.append (Elements)
def displayArray(self):
print("elements are:")
print(self.arr)
print(self.arr[0:3])
def slicing1Dto2D(self):
self.tarr=self.arr.reshape(2,2)
print(self.tarr)
print(self.tarr[1:])
def slicing2Dto3D(self):
print(self.tarr.reshape(1,2,2))
print(self.tarr[0:2])
print(self.tarr[-1:-3])
a=Array()
a.createArray()
a.displayArray()
a.slicing1Dto2D()
a.slicing2Dto3D()
Enter size :4
Enter elements :1
Enter elements :2
Enter elements :3
Enter elements :4
elements are:
[1 2 3 4]
after slicing 1D array
[1 2 3]
2D array is
[[1 2]
[3 4]]
after slicing 2D array
[[3 4]]
3D array is
[[[1 2]
[3 4]]]
after silicing 3D array
[[1 2]
[3 4]]
[]
Result:
Verified Different ways to create numpy arrays.
Signature of Faculty: Grade: