In [ ]:
from sklearn import linear_model
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [ ]:
x_train = np.random.normal(10, 20, 100)
noise = np.random.normal(1, 10, 100)
y_train = x_train * 2.0 + 1.0 + noise
plt.scatter(x_train, y_train)
In [ ]:
x_train = x_train.reshape((100, 1))
In [ ]:
x_train.shape
In [ ]:
linear = linear_model.LinearRegression() # Initialize model
linear.fit(x_train, y_train) # fit data
print('Score: \n', linear.score(x_train, y_train)) # Evaluate the model
print('Coefficient: \n', linear.coef_)
print('Intercept: \n', linear.intercept_)
#Predict Output
predicted= linear.predict(x_train)
In [ ]:
plt.scatter(x_train, y_train)
x_space = np.linspace(*plt.xlim())
plt.plot(x_space, linear.coef_*x_space + linear.intercept_)
In [ ]:
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x_train, y_train, random_state=1)
In [ ]:
# default split is 75% for training and 25% for testing
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)
In [ ]:
linear = linear_model.LinearRegression()
linear.fit(X_train, y_train)
print('Score with train data: \n', linear.score(X_train, y_train))
print('Coefficient: \n', linear.coef_)
print('Intercept: \n', linear.intercept_)
#Predict Output
print('Score with test data: \n', linear.score(X_test, y_test))
In [ ]:
from IPython.core.display import HTML
HTML("<iframe src=http://scikit-learn.org/stable/ width=800 height=350></iframe>")
In [ ]: