Skip to content

Instantly share code, notes, and snippets.

@masayang
Created October 9, 2012 01:03
Show Gist options
  • Save masayang/3855945 to your computer and use it in GitHub Desktop.
Save masayang/3855945 to your computer and use it in GitHub Desktop.
配列でデータが来る場合のLinear Regression
import numpy as np
from sklearn import linear_model
import matplotlib.pyplot as plt
array = np.array([[0, 0.1], [1, 1.1], [2, 1.8], [3, 2.7]])
print array
X = array[:, 0:1]
Y = array[:, 1]
print X
print Y
regr = linear_model.LinearRegression()
regr.fit(X, Y)
print 'Coefficients: %f' % regr.coef_
print 'Residual sum of squares: %f' % np.mean((regr.predict(X) - Y) ** 2)
print 'Variance score %f' % regr.score(X, Y)
plt.scatter(X, Y)
plt.plot(X, regr.predict(X), color = 'blue', linewidth = 3)
plt.savefig("twodim.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment