Experiment No-7
Experiment No-7
Example
import numpy
arr=numpy.array([1, 2, 3, 4, 5])
print(arr)
output:?
PP Sem I/ 2024-2025
K. J. Somaiya College of Engineering, Mumbai-77
import numpy as np
arr=np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
Creating ndarrays:
array = np.array([[0,1,2],[2,3,4]])
output:
[[0 1 2]
[2 3 4]]
array = np.zeros((2,3))
[[0. 0. 0.]
[0. 0. 0.]]
array = np.ones((2,3))
[[1. 1. 1.]
[1. 1. 1.]]
array = np.eye(3)
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Slicing arrays
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].
PP Sem I/ 2024-2025
K. J. Somaiya College of Engineering, Mumbai-77
[[1. 2. 3.]
[4. 5. 6.]]
print(arr * arr)
[[ 1. 4. 9.]
[16. 25. 36.]]
print(arr - arr)
[[0. 0. 0.]
[0. 0. 0.]]
● Shape of an Array
o The shape of an array is the number of elements in each dimension.
● Reshaping arrays
o Reshaping means changing the shape of an array.
o The shape of an array is the number of elements in each dimension.
o By reshaping we can add or remove dimensions or change the number of
elements
o in each dimension.
● Iterating Arrays
o Iterating means going through elements one by one.
o As we deal with multi-dimensional arrays in numpy, we can do this using the
basic
o for loop of Python. If we iterate on a 1-D array it will go through each
element
o one by one.
● Joining NumPy Arrays
o Joining means putting the contents of two or more arrays in a single array.
o In SQL we join tables based on a key, whereas in NumPy we join arrays by
axes.
o We pass a sequence of arrays that we want to join to the concatenate()
function, along with the axis. If the axis is not explicitly passed, it is taken as
0.
● Splitting NumPy Arrays
o Splitting is the reverse operation of Joining.
o Joining merges multiple arrays into one and Splitting breaks one array into
multiple.
o We use array_split() for splitting arrays, we pass it the array we want to split
and the number of splits.
● NumPy Searching Arrays
o You can search an array for a certain value, and return the indexes that get a
match.
o To search an array, use the where() method.
● Sorting Arrays
PP Sem I/ 2024-2025
K. J. Somaiya College of Engineering, Mumbai-77
1. Problem statement:
import numpy as np
print(arr.shape)
import numpy as np
newarr = arr.reshape(4, 3) [ 4 5 6]
print(newarr) [ 7 8 9]
[10 11 12]]
import numpy as np
for x in arr: 2
print(x) 3
print(arr)
PP Sem I/ 2024-2025
K. J. Somaiya College of Engineering, Mumbai-77
print(newarr)
import numpy as np
x = np.where(arr == 4)
print(x)
print(np.sort(arr))
import numpy as np
print(newarr)
2. Write a Python program to calculate the sum of all columns in a 2D NumPy array.
3. Create two NumPy arrays representing monthly high and low temperatures for a year.
Calculate the monthly average temperatures, and the overall average high and low
temperatures, and identify the months with the highest and lowest average temperatures.
PP Sem I/ 2024-2025
K. J. Somaiya College of Engineering, Mumbai-77
Implementation details:
Output(s):
PP Sem I/ 2024-2025
K. J. Somaiya College of Engineering, Mumbai-77
Conclusion:
1. Generate a random integer from 0 to 100 using the NumPy random function.
2. Explain the slicing of 2-D Array
PP Sem I/ 2024-2025