Skip to content

Instantly share code, notes, and snippets.

@mGalarnyk
Last active February 23, 2017 02:54
Show Gist options
  • Save mGalarnyk/5e9cce90236caa0713bf95846dbd1fc6 to your computer and use it in GitHub Desktop.
Save mGalarnyk/5e9cce90236caa0713bf95846dbd1fc6 to your computer and use it in GitHub Desktop.
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
# Read in csv file
# File: https://github.com/mGalarnyk/Python_Tutorials/blob/master/Python_Basics/Linear_Regression/linear.csv
raw_data = pd.read_csv("linear.csv")
# Removes rows with NaN in them
filtered_data = raw_data[~np.isnan(raw_data["y"])]
x_y = np.array(filtered_data)
x, y = x_y[:,0], x_y[:,1]
# Reshaping
x, y = x.reshape(-1,1), y.reshape(-1, 1)
# Linear Regression Object
lin_regression = LinearRegression()
# Fitting linear model to the data
lin_regression.fit(x,y)
# Get slope of fitted line
m = lin_regression.coef_
# Get y-Intercept of the Line
b = lin_regression.intercept_
# Get Predictions for original x values
# you can also get predictions for new data
predictions = lin_regression.predict(x)
# following slope intercept form
print "formula: y = {0}x + {1}".format(m, b)
# Plot the Original Model (Black) and Predictions (Blue)
plt.scatter(x, y, color='black')
plt.plot(x, predictions, color='blue',linewidth=3)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment