Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
94 views

Phython Practical Notebook

The document provides 10 code examples demonstrating various linear algebra and statistical operations in Python including: 1) basic arithmetic on numbers, 2) creating arrays of zeros, ones, and random values, 3) removing rows and columns from 2D arrays, 4) adding and subtracting matrices, 5) multiplying matrices, 6) calculating matrix norms, 7) transposing matrices, 8) calculating eigenvectors of matrices, 9) implementing simple linear regression, and 10) performing elementary statistical operations like mean, median, mode, standard deviation and variance on datasets. For each example, the code and output are shown.

Uploaded by

babu roy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Phython Practical Notebook

The document provides 10 code examples demonstrating various linear algebra and statistical operations in Python including: 1) basic arithmetic on numbers, 2) creating arrays of zeros, ones, and random values, 3) removing rows and columns from 2D arrays, 4) adding and subtracting matrices, 5) multiplying matrices, 6) calculating matrix norms, 7) transposing matrices, 8) calculating eigenvectors of matrices, 9) implementing simple linear regression, and 10) performing elementary statistical operations like mean, median, mode, standard deviation and variance on datasets. For each example, the code and output are shown.

Uploaded by

babu roy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

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=np.ones((2,3))
Print(“Array of all ones:\n”,ones)
Zeros=np.zeros((2,3))
Print(“Array of all zeros:\n”,zeros)
random=np.random.random((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=np.array([[2,3,4],[4,6,8],[8,0,1]])
a=int(input(“enter the row you want to delete”))
arr1=np.delete(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 = np.array ([ [1,2] , [3,4] ] )
# creating second matrix
B = np.array ( [ [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 (np.add (A. B) )
Output:
Page |5

ii)Subtracting elements of matrices


# importing numpy as np
Import numpy as np

# creating first matri


A = np.array ([ [1,2], [3,4] ] )
#creating second matrix
B=np.array ([ [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 (np.subtract(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 =np.array([[1,2,3],[4,5,6],[7,8,9]])
print(“Matrix:\n’’,matrix)
print(“Matrix norm:”,np.linalg.norm(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= np.array([1,2],
[2,3]])
Print(“ Printing the orginal square array:\n”,m)
# finding elegenvalues
W= np.linalg.eig(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 matplotlib.pylot as plt

def estimate_code(x, y):


# number of observation/points
n = np.size(x)

# mean of x and y vector


m_x = np.mean(x)
m_y = np.mean(y)

# calculating crops – deviation and devation about x


SS_xy = np.sum(x*y) – n*m_y*m_x
SS_xx = np.sum(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


Plt.scatter(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
Plt.xlabel(‘x’)
Plt.ylabel(‘y’)

# function to show plot


Plt.show ()

Def main() :
# observation / data
X = np.array ( [ 0,1,2,3,4,5,6,7,8,9 ] )
Y = np.array ( [ 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:”,np.mean(data))
print(“Median:”,np.median(data))
print(“Mode:”,stats.model(data))
print(“Mode:”,stats.model(data))
print(“Standard deviation:”,np.std(data))
print(“Variance:”,np.var(data))

output:

You might also like