Numpy
Numpy
Description: NumPy is a Python library used for working with arrays. NumPy stands for
Numerical Python. NumPy aims to provide an array object that is up to 50x faster than
traditional Python lists.
Program:
A) Using a numpy module create an array and check the following:
1. Type of array
2. Axes of array
3. Shape of array
4. Type of elements in array
import numpy as np
arr=np.array([[1,2,3],[4,2,5]])
print("Array is of type:",type(arr))
print("no.of dimensions:",arr.ndim)
print("Shape of array:",arr.shape)
print("Size of array:",arr.size)
print("Array stores elements of type:",arr.dtype)
output:
3. From tuple
4. Random values
import numpy as np
c = np.zeros((3, 4))
d = np.ones((3, 4))
print ("\nAn array initialized with all zeros:\n", c)
print ("\nAn array initialized with all ones:\n", d)
output:
b = np.array((2, 4, 6, 8))
print ("\nArray created from tuple:\n", b)
output:
f = np.random.random((3, 3))
print ("\nA random array:\n", f)
output:
A random array:
[[0.94004498 0.18001717 0.62266526]
[0.57897408 0.55372992 0.44950854]
[0.94546098 0.77436751 0.04605927]]
C) Using a numpy module create array and check the following:
3. Flatten array
Program:
newarr = arr.reshape(2, 2, 3)
[[4 2 1]
[2 0 1]]]
2.#Create a sequence of integers from 0 to 200 with steps
of 10
import numpy as np
f = np.arange(0, 200, 10)
print ("\nA sequential array with steps of 10:\n", f)
output:
A sequential array with steps of 10:
[ 0 10 20 30 40 50 60 70 80 90 100 110 120 130
140 150 160 170 180 190]
Original array:
[[1 2 3]
[4 5 6]]
Fattened array:
[1 2 3 4 5 6]
original matrix:
[[1, 0], [0, 1]]
[[1, 2], [3, 4]]
Result of the said matrix multiplication:
[[1 2]
[3 4]]
output:
original matrix:
[[1, 0], [0, 1]]
[[1, 2], [3, 4]]
Outer product of the said two vectors:
[[1 2 3 4]
[0 0 0 0]
[0 0 0 0]
[1 2 3 4]]