Simple Linear Regression Program in Python
Simple Linear Regression Program in Python
Here.
There is only one dependent variable and one independent variable.
# Import Packages
import pandas as pd
import numpy as np
import matplotlib.pyplot as mlt
from sklearn import linear_model
# Read the csv File (Input File/Train data)
df = pd.read_csv("D:\canada_per_capita_income.csv")
df
# Plotting the data in graph
%matplotlib inline
mlt.xlabel('Years') # Dependent variable
mlt.ylabel('Per Capita Income') # Independent variable
mlt.scatter(df.year,df.per_capita_income,color='red',marker='.')
# Train the model using Linear Regression
reg = linear_model.LinearRegression()
reg.fit(df[['year']],df.per_capita_income)
reg.predict([[2017]])
# Here Years will be present in CSV file
pred_df = pd.read_csv("D:\Years.csv")
pred_df
# Train the model
future_pred = reg.predict(pred_df)
future_pred
# Output will be printed on the next column in CSV and saved that updated CSV file
pred_df['Per_Capita_income']=future_pred
pred_df
pred_df.to_csv("D:\Linear Reg Output.csv")
Input Data File: https://drive.google.com/file/d/1OSOHdmteHgEfdgckfMbFgm-56kXMuE2Z/view?usp=sharing
Years CSV File: https://drive.google.com/file/d/1v_o4h_4aARZtj5gR4Yvlt16pgUJPRAcf/view?usp=sharing
Output File: https://drive.google.com/file/d/1QLyU1vapK0KGi4Y1MtRGeCkOn_bWi6X-/view?usp=sharing