Last active
February 23, 2017 02:54
-
-
Save mGalarnyk/5e9cce90236caa0713bf95846dbd1fc6 to your computer and use it in GitHub Desktop.
Linear Regression using Python for the blog post https://medium.com/@GalarnykMichael/linear-regression-using-python-b29174c3797a#.mxd9tjl4z
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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