0% found this document useful (0 votes)
3 views2 pages

Num Py

The document provides a tutorial on using NumPy, covering key operations such as importing the library, creating ndarrays, and manipulating arrays. It includes examples of creating 3-D arrays, accessing elements, slicing, joining, and splitting arrays. Each operation is accompanied by code snippets and expected output.

Uploaded by

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

Num Py

The document provides a tutorial on using NumPy, covering key operations such as importing the library, creating ndarrays, and manipulating arrays. It includes examples of creating 3-D arrays, accessing elements, slicing, joining, and splitting arrays. Each operation is accompanied by code snippets and expected output.

Uploaded by

gowthamraj1174
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

NumPy (Numerical Python)

1.Importing NumPy:
Import NumPy
2.Create a NumPy ndarray Object:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
OUTPUT:
[1 2 3 4 5]
<class 'numpy.ndarray'>
3.Creating 3-D Arrays:
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)
OUTPUT:
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
4. Get the first element from the following array:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[0])
OUTPUT: 1

5. Slice elements from index 1 to index 5 from the following array:


import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5])
OUTPUT:
[2 3 4 5]
6. Join two arrays:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
print(arr)
OUTPUT:
[1,2,3,4,5,6]
7. Join two 2-D arrays along rows (axis=1):
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr)
OUTPUT:
[[1 2 5 6]
[3 4 7 8]]
8. Splitting NumPy Arrays:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr)
OUTPUT:
[array([1, 2]), array([3, 4]), array([5, 6])]

You might also like