numpy33
numpy33
MIS:612210029
DIV: 1st
Batch:B
ASSIGNMENT NO.1
NUMPY:
NumPy, short for Numerical Python, is a key library for
numerical and scientific computing in Python.
It centres around the ‘ndarray’ object, which handles large
multi-dimensional arrays and matrices efficiently. NumPy
offers a variety of mathematical functions for element-
wise
operations and complex calculations. It includes tools for
linear algebra, such as matrix operations and eigenvalues.
The
library also provides random number generation for
simulations and statistical experiments. Additionally, it
integrates smoothly with other scientific libraries like
SciPy,
Pandas, and Matplotlib.
1.
import numpy as np
a=[2,3,4]
arr=np.array(a,dtype=float)
print(arr)
output:
[2. 3. 4.]
2.
import numpy as np
a=[[4,6,8],[3,7,5],[1,5,9]]
=np.array(a)
print(arr)
output:
[[4 6 8]
[3 7 5]
[1 5 9]]
3.
import numpy as np
a=[2,3,4]
arr=np.array(a,dtype=float)
print(arr)
output:
[2. 3. 4.]
4.
import numpy as np
a=[[1,2,3],[3,4,6]]
arr=np.array(a)
arr
print("shape of array:",arr.shape)
output:
shape of array: (2, 3)
5.
import numpy as np
a=[[1,2,3],[3,4,6]]
arr=np.array(a)
b=arr.reshape(3,2)
print("reshape of array:",b)
print("shape of array b :",b.shape)
output:
reshape of array: [[1 2]
[3 3]
[4 6]]
shape of array b : (3, 2)
6.
import numpy as np
a=[[1,2,3],[3,4,6]]
arr=np.array(a)
arr
print("dimension of array:",arr.ndim)
output:
dimension of array: 2
7.
import numpy as np
a=[[1,2,3],[3,4,6]]
arr=np.array(a)
arr
print("size of array:",arr.size)
output:
size of array: 6
8.
arrange(start,end,difference)
import numpy as np
arr=np.arange(2,18,2)
print("array:",arr)
output:
array: [ 2 4 6 8 10 12 14 16]
9.
import numpy as np
arr=np.arange(28)
print("array:",arr)
print("dimension of array:",arr.ndim)
output:
array: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27]
dimension of array: 1
10.
import numpy as np
arr=np.arange(2,18,2)
print("array:",arr)
print("maximum value of array:",arr.max())
print("minimum value of array:",arr.min())
print("sum of array:",arr.sum())
output:
array: [ 2 4 6 8 10 12 14 16]
maximum value of array: 16
minimum value of array: 2
sum of array: 72
11.
import numpy as np
a=np.array([[1,2,3],[4,5,6]])
b=np.array([[1,2,3],[4,5,6]])
print("addition:",a+b)
print("substraction:",a-b)
print("multiplication:",a*b)
output:
addition: [[ 2 4 6]
[ 8 10 12]]
substraction: [[0 0 0]
[0 0 0]]
multiplication: [[ 1 4 9]
[16 25 36]]
12.
Additional operations(zeros and ones)
import numpy as np
arr=np.zeros((2,2))
arr
output:
array([[0., 0.],
[0., 0.]])
13.
import numpy as np
arr=np.ones((2,2))
arr
output:
array([[1., 1.],
[1., 1.]])
14.
import numpy as np
arr=np.diag((3,2,2))
arr
output:
array([[3, 0, 0],
[0, 2, 0],
[0, 0, 2]])
15.
import numpy as np
r=np.eye(4,dtype=np.int64)
print(a)
output:
[[1 2 3]
[4 5 6]]
16.
from numpy import random
x=random.randint(100)
print(x)
output:
26
17.
import numpy.random as np
x=np.rand()
print(x)
output:
0.5465352725307843