Page |1
1) Write a python program to perform Addition,
Subtraction, Multiplication, Division between two numbers
Code-
print (“Enter First Number: “)
numOne = int (input() )
print (“ Enter Second Number: “)
numTwo = int(input() )
res = numne+numtwo
print (“\nAddition Result = “ , res)
res = numOne-numTwo
Print ( “Subtraction Result = “, res)
res = numOne*numTwo
print (“Multiplication Result = “, res )
res = numOne/numTwo
print (“Division Result = “, res)
output:
Page |2
2) Write a Python program to create two dimensional array
of all zeros,ones and random values?
Code-
Import numpy as np
Ones=[Link]((2,3))
Print(“Array of all ones:\n”,ones)
Zeros=[Link]((2,3))
Print(“Array of all zeros:\n”,zeros)
random=[Link]((2,3))
print(“Array of random elements:\n”,random)
output:
Page |3
3) Write a Python program to create a 2D matrix and remove a
particular row and column.
Code –
import numpy as np
arr=[Link]([[2,3,4],[4,6,8],[8,0,1]])
a=int(input(“enter the row you want to delete”))
arr1=[Link](arr,a,0)
print(“Your original array:\n”,arr)
print(“Your array after deleting the row:\n”,arr1)
output:
Page |4
4) Write a Python Program to perform addition and
subtraction between two matrices.
i)Adding elements of the matrix
# importing numpy as np
Import numpy as np
# creating first matrix
A = [Link] ([ [1,2] , [3,4] ] )
# creating second matrix
B = [Link] ( [ [4,5] , [6,7] ] )
Print (“printing elements of first matrix”)
Print(A)
Print (“printing elements of second matrix”)
Print(B)
# adding two matrix
Print (“Addition of two matrix”)
Print ([Link] (A. B) )
Output:
Page |5
ii)Subtracting elements of matrices
# importing numpy as np
Import numpy as np
# creating first matri
A = [Link] ([ [1,2], [3,4] ] )
#creating second matrix
B=[Link] ([ [4,5], [6,7] )
print ( “printing elements of first matrix” )
print(A)
print ( “printing elements of second matrix” )
print(B)
#subtracting two matrix
print (“Subtraction of two matrix” )
print ([Link](A.B) )
output:
Page |6
5) Write a Python Program to perform Multiplication of Two
Matrices.
Code-
# Program to multiply two matrices
#3x3 matrix
X= [ [ 12,7,3],
[4, 5, 6],
[7 ,8 ,9] ]
# 3x4 matrix
Y= [ [5,8,1,2],
[6,7,3,0],
[4,5,9,1] ]
# result is 3x4
Result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
# iterate through rows of x
For i in range(len(x)):
# iterate through columns of y
For j in range (len(Y[0])):
# iterate through rows of y
for k in range(len(Y)):
Page |7
result[i] [j] += X[i] [k] * Y[k] [j]
for r in result:
print(r)
output:
Page |8
6) Write a Python program to calculate the norm of a
matrix.
Code-
import numpy as np
matrix =[Link]([[1,2,3],[4,5,6],[7,8,9]])
print(“Matrix:\n’’,matrix)
print(“Matrix norm:”,[Link](matrix)
output:
Page |9
7) Write a Python Program to Transpose a Matrix.
Code-
#Program to transpose a matrix using a nested loop
X = [[12,7],
[4 ,5]
[3 ,8]]
Result = [[0,0,0],
[0,0,0]]
# iterate through rows
For i in range(len(x)):
# iterate through colums
For j in range(len(x[0])):
Result[j][i] = x[i][j]
For r in result:
Print (r)
Output:
P a g e | 10
8) Write a Python Program to Calculate the Eigen value of
matrix.
Code-
#importing numpy library
Import numpy as np
# create numpy 2d-array
m= [Link]([1,2],
[2,3]])
Print(“ Printing the orginal square array:\n”,m)
# finding elegenvalues
W= [Link](m)
# printing eigenvalues
Print(“Printing the eigen values of the given square array:\n”,w)
Output:
P a g e | 11
9) Write a Python Program to Implement Simple Linear
Regression.
Code-
Import numpy as np
Import [Link] as plt
def estimate_code(x, y):
# number of observation/points
n = [Link](x)
# mean of x and y vector
m_x = [Link](x)
m_y = [Link](y)
# calculating crops – deviation and devation about x
SS_xy = [Link](x*y) – n*m_y*m_x
SS_xx = [Link](x*x) – n*m_x*m_x
# calculating regression cefficients
b_1 = SS_xy / SS_xx
b_0 = m_y – b_1*m_x
return ( b_0 , b_1 )
def plot_regression_line(x,y,b):
P a g e | 12
# plotting the actual points as scatter plot
[Link](x,y,color =”m”, marker = “o”, s = 30)
# predicted response vector
Y_pred = b[0] + b[1]*x
# plotting the regression line
Plt. Plot ( x, y_pred, color = “g”)
# putting labels
[Link](‘x’)
[Link](‘y’)
# function to show plot
[Link] ()
Def main() :
# observation / data
X = [Link] ( [ 0,1,2,3,4,5,6,7,8,9 ] )
Y = [Link] ( [ 1,3,2,5,7,8,8,9,10,12]
# plotting regression line
Plot_ regression_line ( x, y , b )
If _ _ name_ _ == “_ _main_ _*:
main()
P a g e | 13
output:
P a g e | 14
10) Write a Python program to perform Elementary Operation
using any dataset.
Code-
Import numpy as np
Import statistic as stats
data= [1,2,3,4,5,6,7,8,9,10]
print(“Dataset:\n”,data)
print(“Mean:”,[Link](data))
print(“Median:”,[Link](data))
print(“Mode:”,[Link](data))
print(“Mode:”,[Link](data))
print(“Standard deviation:”,[Link](data))
print(“Variance:”,[Link](data))
output: