Professional Documents
Culture Documents
Ans:
road(mumbai,indore,601).
road(indore,gwalior,505).
road(gwalior,agra,121).
1
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
road(agra,delhi,222).
road(mumbai,surat,300).
road(surat,vadodara,154).
road(vadodara,jodhpur,554).
road(jodhpur,jaipur,357).
route(X,Y,Distance):-
road(X,Y,Distance),
write(Distance),write(' km.'),nl.
route(X,Y,Distance):-
road(X,Z,Distance1),
route(Z,Y,Distance2),
Distance is Distance1+Distance2,!,
write(Distance),write(' km.'),nl.
Output :
2
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
3
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
Ans:
(i) import necessary libraries and read the data from the file
#(i) import necessary libraries and read the data from the file
from mpl_toolkits.mplot3d import Axes3D
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
%matplotlib inline
4
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
data.describe()
5
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
(vi) Apply splitting on the dataset as 80% for training and 20% for testing.
#(vi) Apply splitting on the dataset as 80% for training and 20% for testing.
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
X=data['Head Size(cm^3)'].values
Y=data['Brain Weight(grams)'].values
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2)
#splitting the data into training and test
msk = np.random.rand(len(data)) < 0.8
6
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
train = data[msk]
test = data[~msk]
print(train)
print('-------------------------------------')
print(test)
7
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
(vii) Assigning ‘X’ as independent variable and ‘y’ as dependent variable. Use input as Head Size and
output Brain weight
#(vii) Assigning ‘X’ as independent variable and ‘y’ as dependent variable. Use input
as Head Size and output Brain weight
X=data['Head Size(cm^3)'].values
Y=data['Brain Weight(grams)'].values
sns.scatterplot(data=data, x="Head Size(cm^3)", y="Brain Weight(grams)")
plt.show()
8
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
for i in range(len(X)):
pred_y.append(c + m * X[i])
return(pred_y)
def r2score(y_obs,y_pred):
yhat = np.mean(y_obs)
ss_res = 0.0
ss_tot = 0.0
for i in range(len(y_obs)):
ss_tot += (y_obs[i]-yhat)**2
ss_res += (y_obs[i]-y_pred[i])**2
r2 = 1 - (ss_res/ss_tot)
return r2
9
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
(ix) Train the model
10
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
(x)Display rmse, r2_score
11