0% found this document useful (0 votes)
7 views2 pages

Steps For Simple Regression Analysis

m

Uploaded by

vinayakarora165
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Steps For Simple Regression Analysis

m

Uploaded by

vinayakarora165
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Step 1.

Import pandas, numpy, matplot libs

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

Step 2. Import data file

Example:
df= pd.read_csv(r"C:\Yogesh Bhatt\9. Lectures\C. GEU\A. Course 8_Predictive
Analytics\2. EDA\placement.csv")
Example:
df= pd.read_excel(r"C:\Yogesh Bhatt\9. Lectures\C. GEU\A. Course 8_Predictive
Analytics\3. Regression Analysis\Attitude and Behaviour.xlsx")

Step 3: Take a glance at the content of data set

df.head()

Step 4: # Developing scatter plot among variables

plt.scatter(df['kWh'],df['MT/Day'])
plt.xlabel('Energy Consumption (1000 xkWh)')
plt.ylabel('Production (MT/Day)')

Step 5: Defining input/output columns

X = df.iloc[:,0:1]
y = df.iloc[:,-1]

Step 6: Training the data set and arranging data in four arrays

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,random_state=2)

Step 7: Import a class called linear Regression

from sklearn.linear_model import LinearRegression

Step 8: Create an object for linear regression

lr = LinearRegression()

Step 9: Train the module

lr.fit(X_train,y_train)

Step 10. Glance at the content of training data set

X_test
y_test

Step 11. Prediction


lr.predict(X_test.iloc[0].values.reshape(1,1))

Step 12. Plot regression line on the scatter plot

plt.scatter(df['kWh'],df['MT/Day'])
plt.plot(X_train.values,lr.predict(X_train), color='red')
plt.xlabel('Energy Consumption (1000 xkWh)')
plt.ylabel('Production (MT/Day)')

Step 13. Slope of line

m = lr.coef_
m

Step 14. Intercept of the line

# Intercept of the line

b = lr.intercept_
b

Step 15. Regression metrices

from sklearn.metrics import mean_absolute_error,mean_squared_error,r2_score


y_pred = lr.predict(X_test)

print("MAE",mean_absolute_error(y_test,y_pred))
print("MSE",mean_squared_error(y_test,y_pred))
print("RMSE",np.sqrt(mean_squared_error(y_test,y_pred)))
print("R2",r2_score(y_test,y_pred))

You might also like