0% found this document useful (0 votes)
20 views

Linear Regression - Jupyter Notebook

The document outlines the process of implementing a linear regression model using Python's scikit-learn library, including data preparation, model fitting, and prediction. It highlights several errors encountered during the execution, primarily due to undefined variables. Additionally, it mentions the evaluation of the model's performance using metrics such as Mean Absolute Error and R-squared.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Linear Regression - Jupyter Notebook

The document outlines the process of implementing a linear regression model using Python's scikit-learn library, including data preparation, model fitting, and prediction. It highlights several errors encountered during the execution, primarily due to undefined variables. Additionally, it mentions the evaluation of the model's performance using metrics such as Mean Absolute Error and R-squared.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Linear Regression

In [11]:

# import libraries
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn import metrics

In [ ]:

# Features
X = df1.drop(['MEDV', 'ID'], axis = 1)
# Target
y = df1['MEDV']

# Create a model
lr = LinearRegression()
# Fit the model
lr.fit(X, y)
# make predictions
pred = lr.predict(X)

In [8]:

print('Intercept:', lr.intercept_)

--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
~\AppData\Local\Temp\ipykernel_2252\1794278346.py in <module>
----> 1 print('Intercept:', lr.intercept_)

NameError: name 'lr' is not defined


In [9]:

coeff_df = pd.DataFrame(lr.coef_, X.columns, columns=['Coefficient'])


coeff_df

--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
~\AppData\Local\Temp\ipykernel_2252\725140125.py in <module>
----> 1 coeff_df = pd.DataFrame(lr.coef_, X.columns, columns=['Coefficien
t'])
2 coeff_df

NameError: name 'lr' is not defined

In [10]:

print('Mean Absolute Error:', metrics.mean_absolute_error(y, pred))


print('Mean Squared Error:', metrics.mean_squared_error(y, pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y, pred)))
print('R2:', np.sqrt(metrics.r2_score(y, pred)))

--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
~\AppData\Local\Temp\ipykernel_2252\2761661720.py in <module>
----> 1 print('Mean Absolute Error:', metrics.mean_absolute_error(y, pre
d))
2 print('Mean Squared Error:', metrics.mean_squared_error(y, pred))
3 print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_err
or(y, pred)))
4 print('R2:', np.sqrt(metrics.r2_score(y, pred)))

NameError: name 'y' is not defined

In [ ]:

#Predicting the value of Target


input = [[0.06, 14, 6, 0, 0.5, 6, 22, 7, 4, 340, 19, 400, 8]]
print('The predicted price:', lr.predict(input)[0])

In [4]:

#Importing a library for MLR Training


from sklearn.model_selection import train_test_split
In [13]:

#Splitting the data into training and testing set


x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=0)

--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
~\AppData\Local\Temp\ipykernel_2252\2436172341.py in <module>
----> 1 x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,r
andom_state=0)

NameError: name 'x' is not defined

In [14]:

#Training the model for Training data


from sklearn.linear_model import LinearRegression

In [15]:

mlt=LinearRegression()

In [16]:

mlt.fit(x_train,y_train)

--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
~\AppData\Local\Temp\ipykernel_2252\2794138465.py in <module>
----> 1 mlt.fit(x_train,y_train)

NameError: name 'x_train' is not defined

In [17]:

#Predict the result


ypred=mlt.predict(x_test)

--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
~\AppData\Local\Temp\ipykernel_2252\2625223154.py in <module>
1 #Predict the result
----> 2 ypred=mlt.predict(x_test)

NameError: name 'x_test' is not defined


In [18]:

print(ypred)

--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
~\AppData\Local\Temp\ipykernel_2252\2197523102.py in <module>
----> 1 print(ypred)

NameError: name 'ypred' is not defined

In [19]:

#Evaluate the model


from sklearn.metrics import r2_score

In [20]:

r2_score(y_test,ypred)

--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
~\AppData\Local\Temp\ipykernel_2252\2748603431.py in <module>
----> 1 r2_score(y_test,ypred)

NameError: name 'y_test' is not defined

In [ ]:

#Plot the rsults


import matplotlib.pyplot as plt
plt.figure(figsize=(15,10))
plt.scatter(y_test,ypred)
plt.xlabel('Actual')
plt.ylabel('Predicted')

You might also like