Skip to content

Instantly share code, notes, and snippets.

@b00033811
Last active May 24, 2018 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b00033811/fd0f979933d9a2deef00a24d7040098e to your computer and use it in GitHub Desktop.
Save b00033811/fd0f979933d9a2deef00a24d7040098e to your computer and use it in GitHub Desktop.
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
# loading data
x_raw,y_raw=np.loadtxt('data.csv',delimiter=',')
# reshape the data
x=x_raw.reshape(-1,1)
y=y_raw.reshape(-1,1)
# split the model into test/train sets
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=.3,
random_state=1337,
shuffle=True)
# fit the linear model
model=LinearRegression()
model.fit(x_train,y_train)
# get the R2 score
R2=model.score(x_test,y_test)
print ('The R2 score is {0:.2f}'.format (R2))
#get model parameters
coef=model.intercept_,*model.coef_
print (', '.join('a{0}={1:.2f}'.format(i,*a) for i,a in enumerate(coef)))
# get the model predictions &
# visualize the model
predictions=model.predict(x)
fig1=plt.figure('Linear Regression')
plt.plot(x,predictions,color='b',label='Linear model | R2={0:.2f}'.format(R2))
plt.scatter(x_raw,y_raw,color='r',marker='.',label='Input data')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()
plt.legend()
plt.title('Linear Regression Using sklearn')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment