Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Machine Learning

WEEK-7

NAME : G Sai hari shashank varma DATE : 27-3-2024

HTNO : 22R25A6607 BRANCH : CSM- B

PROBLEM STATEMENT:

1. Build a multiple linear regression model using python for a particular data set by

a) Splitting Training data and Test data.

b) Evaluate the model (intercept and slope).

c) Visualize the training set and testing set

d) predicting the test set result

e) compare actual output values with predicted values

PYTHON PROGRAM:

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression

file = "/content/50_Startups.csv"

df = pd.read_csv(file)

print(df,'\n')

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

y = df.iloc[:, -1].values

from sklearn.compose import ColumnTransformer

from sklearn.preprocessing import OneHotEncoder


ct=ColumnTransformer(transformers=[('encoder',OneHotEncoder(),
[3])],remainder='passthrough')

X=np.array(ct.fit_transform(x))

print(X)

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

print(X_train,'\n')

print(X_test,'\n')

print(y_train,'\n')

print(y_test,'\n')

model=LinearRegression()

model.fit(X_train,y_train)

#Evaluate the model

print(model.intercept_,'\n')

print(model.coef_,'\n')

#predicting the test set result

y_pred_train=model.predict(X_train)

y_pred_test=model.predict(X_test)

#Compare actual o/p with predicted o/p

dataset=pd.DataFrame({'Actual':y_test,'Predicted':y_pred_test})

print(dataset,'\n')

#Visualization

plt.figure(figsize=(10,5))

plt.subplot(1,1,1)

plt.scatter(y_train,y_pred_train,color='blue')

plt.plot(y_train,y_train,color='red',linestyle='--')

plt.title('Training Set: Actual vs Predicted')

plt.xlabel('Actual')
plt.ylabel('Predicted')

plt.grid()

Output:

You might also like