DML
DML
#Program to show use of random() and seed() functions from random module
from random import random,seed
#The seed() sets the random number seed
seed(222)
print("The random number generated is: ",random())
#Using different seed value for displaying a random number
seed(500)
print("The random number generated using different seed value is: ",random())
OUTPUT:
import numpy as np
arr=np.array([[1,2,3,4],[5,6,7,8],[9,0,1,2],[3,4,5,6]])
print("ORIGINAL ARRAY IS: \n",arr)
print("TRANSPOSED ARRAY IS: \n",np.transpose(arr))
print("DIAGONAL WITH OFFSET 0 IS: \n",arr.diagonal(0))
print("DIAGONAL WITH OFFSET -1 (LOWER DIAGONAL) IS: \n",arr.diagonal(-1))
print("DIAGONAL WITH OFFSET 1 (UPPER DIAGONAL) IS: \n",arr.diagonal(1))
OUTPUT:
[8 rows x 11 columns]
The first five records of the dataset is:
model mpg cyl disp hp ... qsec vs am gear carb
0 Mazda RX4 21.0 6 160.0 110 ... 16.46 0 1 4 4
1 Mazda RX4 Wag 21.0 6 160.0 110 ... 17.02 0 1 4 4
2 Datsun 710 22.8 4 108.0 93 ... 18.61 1 1 4 1
3 Hornet 4 Drive 21.4 6 258.0 110 ... 19.44 1 0 3 1
4 Hornet Sportabout 18.7 8 360.0 175 ... 17.02 0 0 3 2
[5 rows x 12 columns]
The first three records of the dataset is:
model mpg cyl disp hp drat wt qsec vs am gear carb
0 Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
1 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
2 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
The first three records of the selected columns from the dataset is:
mpg hp
0 21.0 110
1 21.0 110
2 22.8 93
The last two records of the dataset is:
model mpg cyl disp hp drat wt qsec vs am gear carb
30 Maserati Bora 15.0 8 301.0 335 3.54 3.57 14.6 0 1 5 8
31 Volvo 142E 21.4 4 121.0 109 4.11 2.78 18.6 1 1 4 2
The last two records of the selected columns from the dataset is:
mpg hp
30 15.0 335
31 21.4 109
The values corresponding to gear column are:
[4 4 4 3 3 3 3 4 4 4 4 3 3 3 3 3 3 4 4 4 3 3 3 3 3 4 5 5 5 5 5 4]
PROGRAM 11
PROGRAM THAT PLOT MARKS OBTAINED BY TWO STUDENTS IN SIX SUBJECTS USING
BARGRAPH
import numpy as np
import matplotlib.pyplot as plt
marks_Jia=(83,79,80,83,85,89)
marks_Gokul=(95,84,89,99,100,91)
index=np.array([1,2,3,4,5,6])
width=0.30
plt.bar(index,marks_Jia,width,label="Marks obtained by Jia")
plt.bar(index+width,marks_Gokul,width,label="Marks obtained by Gokul")
plt.ylabel=('Marks')
plt.title=("Comparing marks of Jia and Gokul")
plt.xticks(index+width/2,('subject1','subject2','subject3','subject4','subject5','subject6'))
plt.legend(loc='best')
plt.show()
OUTPUT:
PROGRAM 12
PROGRAM FOR CREATING A LINE CHART WITH SETTING LIMIT OF AXES
Physics Maths
0 62 89
1 47 87
2 55 67
3 74 55
4 31 47
5 55 72
6 85 76
7 63 79
8 42 44
9 32 92
10 71 99
11 55 47
import scipy.stats
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1234)
sample=np.random.lognormal(mean=1.,sigma=.4,size=10000)
shape,loc,scale=scipy.stats.lognorm.fit(sample,floc=0)
num_bins=100
c="red"
counts,edges,patches=plt.hist(sample,bins=num_bins,color=c)
centers=0.5*(edges[:-1]+edges[1:])
cdf=scipy.stats.lognorm.cdf(edges,shape,loc=loc,scale=scale)
prob=np.diff(cdf)
plt.plot(centers,sample.size*prob,'k-',linewidth=2)
plt.title('Normal Distribution')
OUTPUT:
PROGRAM 16
PROGRAM TO IMPLEMENT LINEAR REGRESSION FOR A DATASET
# Import libraries
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import numpy as np
# Import dataset
dataset = pd.read_csv("C:/Users/user/Downloads/Salary Data.csv")
print(dataset)
# Split into training and testing dataset
x = dataset.iloc[:, [0]].values
y = dataset.iloc[:, [1]].values
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=0)
# Linear regression
regressor = LinearRegression()
regressor.fit(x_train, y_train)
y_pred = regressor.predict(x_test)
# Print variance score
print('\n\n Variance score: %.2f' % r2_score(y_test, y_pred))
# Plot Training Set
plt.scatter(x_train, y_train, color="red")
plt.plot(x_train, regressor.predict(x_train), color="blue")
plt.title("Salary vs Years of Experience (Training Set)")
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.show()
# Plot Test Set
plt.scatter(x_test, y_test, color="red") # Use test data
plt.plot(x_train, regressor.predict(x_train), color="blue") # Line remains from training data
plt.title("Salary vs Years of Experience (Test Set)")
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.show()
# Predict a new value
y_pred = regressor.predict(np.array([[13.5]])) # Reshapes to 2D array
print("\n\n Linear Regression \n Given new x value = 13.5")
print("Predicted Y value =", y_pred.round(2))
OUTPUT:
Features:
[[ 9.77075874 3.27621022]
[ -9.71349666 11.27451802]
[ -6.91330582 -9.34755911]
[-10.86185913 -10.75063497]
[ -8.50038027 -4.54370383]]
True Labels:
[1 0 2 2 2]
Scaled Features:
[[ 2.13082109 0.25604351]
[-1.52698523 1.41036744]
[-1.00130152 -1.56583175]
[-1.74256891 -1.76832509]
[-1.29924521 -0.87253446]]
K-Means Inertia = 74.57960106819854
Centers: [[ 1.19539276 0.13158148]
[-0.91941183 -1.18551732]
[-0.25813925 1.05589975]]
K-Means Labels: [0 2 1 1 1]