0% found this document useful (0 votes)
2 views3 pages

Dav Practicenumpy Kaustubh

The document provides examples of using NumPy for creating and manipulating arrays, including 1D, 2D, and 3D arrays. It demonstrates array slicing, indexing, and sorting, showcasing how to access specific elements, rows, and columns. The code snippets illustrate practical applications of these concepts with output results.

Uploaded by

daxaxot965
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)
2 views3 pages

Dav Practicenumpy Kaustubh

The document provides examples of using NumPy for creating and manipulating arrays, including 1D, 2D, and 3D arrays. It demonstrates array slicing, indexing, and sorting, showcasing how to access specific elements, rows, and columns. The code snippets illustrate practical applications of these concepts with output results.

Uploaded by

daxaxot965
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/ 3

dav-practicenumpy-kaustubh

October 1, 2023

[31]: import numpy as np #Kaustubh's code

[32]: nums = [1, 2, 3, 4, 5]


nums = np.array(nums)
print(nums)

[1 2 3 4 5]

[13]: # Create a 2D array


twoD = [[1, 2, 3], [4, 5, 6]]
print(np.array(twoD))

[[1 2 3]
[4 5 6]]

[35]: # Create a 3D array


threeD = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
print(np.array(threeD)) #Kaustubh's code

[[[1 2]
[3 4]]

[[5 6]
[7 8]]]

[30]: # Slicing of arrays


np.random.seed(123)
arr = np.random.randint(1, 100, 10)
print("Original array:")
print(arr)
print("Even elements in the array:")
print(arr[arr % 2 == 0]) #Kaustubh's code

Original array:
[67 93 99 18 84 58 87 98 97 48]
Even elements in the array:
[18 84 58 98 48]

1
[20]: arr = np.arange(1, 21)
print("Original Array:")
print(arr)

Original Array:
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]

[21]: sub_arr = arr[5:15]


print("Sliced Subarray:")
print(sub_arr)

Sliced Subarray:
[ 6 7 8 9 10 11 12 13 14 15]

[22]: step_arr = arr[1:10:2]


print("Sliced Array with Step:")
print(step_arr)

Sliced Array with Step:


[ 2 4 6 8 10]

[33]: arr = np.arange(1, 11)


print("Original array:")
print(arr)
print("Even elements in the array:") #Kaustubh's code
print(arr[arr % 2 == 0])

Original array:
[ 1 2 3 4 5 6 7 8 9 10]
Even elements in the array:
[ 2 4 6 8 10]

[23]: # Indexing 2D Arrays


matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("Original 2D Array:")
print(matrix)

Original 2D Array:
[[1 2 3]
[4 5 6]
[7 8 9]]

[24]: # Accessing a single element


element = matrix[1, 2]
print("Element at row 1, column 2:")
print(element)

Element at row 1, column 2:

2
6

[25]: # Accessing a row


row = matrix[1]
print("Row at index 1:")
print(row)

Row at index 1:
[4 5 6]

[26]: # Sorting Arrays


unsorted_arr = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3])
print("Unsorted Array:")
print(unsorted_arr)

Unsorted Array:
[3 1 4 1 5 9 2 6 5 3]

[34]: # Sorting in ascending order


sorted_arr = np.sort(unsorted_arr)
print("Sorted Array (Ascending):")
print(sorted_arr) #Kaustubh's code

Sorted Array (Ascending):


[1 1 2 3 3 4 5 5 6 9]

[28]: # Sorting in descending order


sorted_desc_arr = np.sort(unsorted_arr)[::-1]
print("Sorted Array (Descending):")
print(sorted_desc_arr)

Sorted Array (Descending):


[9 6 5 5 4 3 3 2 1 1]

[29]: # Accessing a column


column = matrix[:, 1]
print("Column at index 1:")
print(column)

Column at index 1:
[2 5 8]

[ ]:

You might also like