0% found this document useful (0 votes)
7 views6 pages

3252 4 Ids

The document provides a detailed explanation of indexing and slicing in NumPy arrays, covering 1-D, 2-D, and 3-D arrays. It includes code examples for creating and slicing arrays, as well as using negative indexing. The output section demonstrates the results of the operations performed on the arrays.

Uploaded by

nbkr115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

3252 4 Ids

The document provides a detailed explanation of indexing and slicing in NumPy arrays, covering 1-D, 2-D, and 3-D arrays. It includes code examples for creating and slicing arrays, as well as using negative indexing. The output section demonstrates the results of the operations performed on the arrays.

Uploaded by

nbkr115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Student ID:23kb1a3252

EXPERIMENT - IV

Department of Computer Science and Engineering Page | 21


Student ID:23kb1a3252
Indexing and Slicing a NumPy Array
a. Slicing 1-D Numpy array
b. Slicing 2-D Numpy array
c. Slicing 3-D Numpy array

Description: Array indexing is the same as accessing an array element.

You can access an array element by referring to its index number.

The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second
has index 1 etc.

1D Array: arr[i] accesses the element at index i.

2D Array: arr[i, j] accesses the element at row i and column j.

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.

We pass slice instead of index like this: [start:end].

We can also define the step, like this: [start:end:step].

If we don't pass start its considered 0

If we don't pass end its considered length of array in that dimension

If we don't pass step its considered 1

a. Slicing 1-D Numpy Array

Create Slice Array

import numpy as np

print(self.arr[0:3])

b. Slicing 2-D Numpy Array

Create Slice Array

Department of Computer Science and Engineering Page | 22


Student ID:23kb1a3252
import numpy as np

self.tarr = self.arr.reshape (2,2)

Print(“2D array”)

Print(self.tarr)

Print(“after slicing 2D array”)

Print(self.tarr[1:])

c. Slicing 3-D Numpy Array

Create Slice Array

Import numpy as np

Print(“3D array is”)

Print(self.tarr.reshape(1,2,2)

Print(“after slicing3D array”)

Print(self.tarr[0:2])

d. Negative Indexing

Print(self.tarr[-1 :-3])

Code:
import numpy as np

class Array:

def createArray(self):

l=[]

n=int(input("Enter size :"))

for i in range(0,n):

Elements=int(input("Enter elements :"))

l.append (Elements)

Department of Computer Science and Engineering Page | 23


Student ID:23kb1a3252
self.arr=np.array(l)

def displayArray(self):

print("elements are:")

print(self.arr)

print("after slicing 1D array")

print(self.arr[0:3])

def slicing1Dto2D(self):

self.tarr=self.arr.reshape(2,2)

print("2D array is")

print(self.tarr)

print("after slicing 2D array")

print(self.tarr[1:])

def slicing2Dto3D(self):

print("3D array is")

print(self.tarr.reshape(1,2,2))

print("after silicing 3D array")

print(self.tarr[0:2])

print(self.tarr[-1:-3])

a=Array()
a.createArray()
a.displayArray()
a.slicing1Dto2D()
a.slicing2Dto3D()

Department of Computer Science and Engineering Page | 24


Student ID:23kb1a3252
Output :

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]]
[]

Department of Computer Science and Engineering Page | 25


Student ID:23kb1a3252

Result:
Verified Different ways to create numpy arrays.
Signature of Faculty: Grade:

Department of Computer Science and Engineering Page | 26

You might also like