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 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
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 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,long with the axis. If axis is not explicitly passed, it is taken as 0.
Splitting NumPy Arrays
o Splitting is 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:
print(arr.shape)
import numpy as np [[ 1 2 3]
import numpy as np 1
for x in arr: 3
print(x)
PP Sem I/ 2024-2025
K. J. Somaiya College of Engineering, Mumbai-77
print(arr)
newarr = np.array_split(arr, 3)
print(newarr)
import numpy as np
x = np.where(arr == 4)
print(x)
import numpy as np
print(np.sort(arr))
import numpy as np
newarr = arr[x]
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, 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:
PP Sem I/ 2024-2025