You are on page 1of 8

FACULTY OF TECHNOLOGY

Computer Engineering
01CE0711-Machine Learning-Lab Manual

Experiment 5

HAYMANOT ABAWA 919001031999 1


FACULTY OF TECHNOLOGY
Computer Engineering
01CE0711-Machine Learning-Lab Manual

5.Create a multilinear regression model using scikit learn library based on


house price prediction dataset and
Insurance dataset

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score

df =
pd.read_csv("https://raw.githubusercontent.com/Learn
PythonWithRune/MachineLearningWithPython/main/jupyte
r/final/files/house_prices.csv")

df.head()

HAYMANOT ABAWA 919001031999 2


FACULTY OF TECHNOLOGY
Computer Engineering
01CE0711-Machine Learning-Lab Manual

plt.xlabel("Longitude")
plt.ylabel("Price($)")
plt.scatter(df.Longitude, df['House unit price'])

df.corr()

x = df.iloc[:,:-1]

HAYMANOT ABAWA 919001031999 3


FACULTY OF TECHNOLOGY
Computer Engineering
01CE0711-Machine Learning-Lab Manual

y = df.iloc[:,-1]
x_train, x_test, y_train, y_test =
train_test_split(x,y)

lin = LinearRegression()
lin.fit(x_train,y_train)
y_pred = lin.predict(x_test)

print(y_test,y_pred)

print(r2_score(y_test,y_pred))

HAYMANOT ABAWA 919001031999 4


FACULTY OF TECHNOLOGY
Computer Engineering
01CE0711-Machine Learning-Lab Manual

Experiment 6

6. Create the non- linear regression model by using

HAYMANOT ABAWA 919001031999 5


FACULTY OF TECHNOLOGY
Computer Engineering
01CE0711-Machine Learning-Lab Manual

polynomial regression based on scikit learn library and iris


Dataset
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset =
pd.read_csv("/content/dataset_Pressure.csv")

dataset.head()

X = (dataset.iloc[:,:-1].values)
y = dataset.iloc[:,-1].values

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test =
train_test_split(X,y, test_size = 0.2)

from sklearn.linear_model import LinearRegression


lin_reg = LinearRegression()
lin_reg.fit(X,y)

HAYMANOT ABAWA 919001031999 6


FACULTY OF TECHNOLOGY
Computer Engineering
01CE0711-Machine Learning-Lab Manual

plt.scatter(X, y, color = 'red')


plt.plot(X, lin_reg.predict(X), color = 'blue')
plt.title('(LinearRegression )')
plt.xlabel("temperature")
plt.ylabel('pressure')
plt.show()

from sklearn.preprocessing import PolynomialFeatures

HAYMANOT ABAWA 919001031999 7


FACULTY OF TECHNOLOGY
Computer Engineering
01CE0711-Machine Learning-Lab Manual

poly_reg = PolynomialFeatures(degree = 2)
X_poly = poly_reg.fit_transform(X)
pol_reg = PolynomialFeatures()
pol_reg.fit(X_poly, y)

plt.scatter(X, y, color = 'red')


plt.plot(X,
pol_reg.predict(poly_reg.fit_transform(X)), color =
'blue')
plt.title('(Polymonial Regression )')
plt.xlabel("temperature")
plt.ylabel('pressure')
plt.show()

HAYMANOT ABAWA 919001031999 8

You might also like