Informatics Practices: Numpy - Array
Informatics Practices: Numpy - Array
Informatics Practices: Numpy - Array
Informatics
Practices
Class XII ( As per
CBSE Board)
Numpy -
Array
New
Syllabus
2019-20
e.g.program
import numpy as np
a = np.array([500, 200, 300]) # Create a 1D Array
print(type(a)) # Prints "<class 'numpy.ndarray'>"
print(a.shape) # Prints "(3,)" means dimension of array
print(a[0], a[1], a[2]) # Prints "500 200 300"
a[0] = 150 # Change an element of the array
print(a)
import numpy as np
x = np.array([1, 2, 3])
y=x
z = np.copy(x)
x[0] = 10
print(x)
print(y)
print(z)
import numpy as np
data = np.array([5,2,7,3,9])
print (data[:]) #print [5 2 7 3 9]
print(data[1:3]) #print [2 7]
print(data[:2]) #print [5 2]
print(data[-2:]) #print [3 9]
e.g.program
import numpy as np
a = np.array([1, 2, 3])
b = np.array([5, 6])
c=np.concatenate([a,b,a])
print(c) #print [1 2 3 5 6 1 2 3]
e.g.program
import numpy as np
a = np.array([[3, 2, 1],[1, 2, 3]]) # Create a 2D Array
print(type(a)) # Prints "<class 'numpy.ndarray'>"
print(a.shape) # Prints (2, 3)
print(a[0][1]) # Prints 2
a[0][1] = 150 # Change an element of the array
print(a) # prints [[ 3 150 1] [ 1 2 3]]
e.g. program
import numpy as np
A = np.array([1,2,3,4,5,6])
B = np.reshape(A, (2, 3))
print(B)
OUTPUT
[[1 2 3]
[4 5 6]]
import numpy as np
A = np.array([[7, 5, 9, 4],
[ 7, 6, 8, 8],
[ 1, 6, 7, 7]])
print(A[:2, :3]) #print elements of 0,1 rows and 0,1,2 columns
print(A[:3, ::2]) #print elements of 0,1,2 rows and alternate column
position
print(A[::-1, ::-1]) #print elements in reverse order
print(A[:, 0]) #print all elements of 0 column
print(A[0, :]) #print all elements of 0 rows
print(A[0]) #print all elements of 0 row
import numpy as np
A = np.array([[7, 5],
[1, 6]])
# concatenate along the first axis OUTPUT
print(np.concatenate([A, A])) [[7 5]
# concatenate along the second [1 6]
axis (zero-indexed) [7 5]
[1 6]]
import numpy as np
a = np.array([[7, 5, 9], OUTPUT
[ 2, 6, 8]]) [[7 5 9]
print(a) [2 6 8]]
c=np.add(a,2)
print(c) [[ 9 7 11]
c=np.subtract(a,2) [ 4 8 10]]
print(c)
[[5 3 7]
[0 4 6]]
c=np.multiply(a,2)
print(c) [[14 10 18]
[ 4 12 16]]
c=np.divide(a,2)
print(c) [[3.5 2.5 4.5]
[1. 3. 4. ]]
E.G.PROGRAM
import numpy as np
a = np.array([[7.333, 5.223],
[ 2.572, 6.119]]) OUTPUT
print(np.power(a,2)) [[53.772889 27.279729]
[ 6.615184 37.442161]]
e.g. program
import numpy as np
b = np.array([600,470,170,430,300])
print(b.mean()) # print 394.0
print(np.var(b,ddof=0)) # print 21704.0
Example
variance variance
YEAR ITEM1 PRICE ITEM 2 PRICE A B AxB
2015 1000 130 -266.667 10 -2666.67
2016 1200 110 -66.6667 -10 666.6667
2017 1600 120 333.3333 10 3333.333
1266.666667 120 1333.333
covariance= 666.6667
import numpy as np
a = np.array([1000,1200,1600])
b = np.array([130,110,120])
print(np.cov(a,b,bias=True)[0,1])
OUTPUT
-666.6666666666666
Formula
import numpy as np
a = np.array([1000,1200,1600])
b = np.array([130,110,120])
print(np.corrcoef(a, b))
OUTPUT
[[ 1. -0.32732684]
[-0.32732684 1. ]]
import numpy as np
import matplotlib.pyplot as plt
def estcoefficients(x,y):
n=np.size(x)
meanx, meany = np.mean(x), np.mean(y)
sy = np.sum(y*x - n*meany*meanx)
sx = np.sum(x*x - n*meanx*meanx)
a=sx/sy
b=meany-a*meanx
return(a,b)
def plotregline(x,y,b):
plt.scatter(x,y,color="r",marker="o",s=30)
ypred=b[0]+b[1]*x
plt.plot(x,ypred,color="g")
plt.xlabel('SIZE')
plt.ylabel('COST')
plt.show()
x=np.array([10,20,30,40,50]) # independent variable
y=np.array([400,800,1100,1700,2100]) # dependent variable
b=estcoefficients(x,y)
plotregline(x,y,b)