Forked from animesh-agarwal/Linear Regression using sckit-learn.py
Created
March 30, 2019 12:04
-
-
Save mathiasfls/4fafb79e0780666f8514fcad7172e2c2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # imports | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from sklearn.linear_model import LinearRegression | |
| from sklearn.metrics import mean_squared_error, r2_score | |
| # generate random data-set | |
| np.random.seed(0) | |
| x = np.random.rand(100, 1) | |
| y = 2 + 3 * x + np.random.rand(100, 1) | |
| # sckit-learn implementation | |
| # Model initialization | |
| regression_model = LinearRegression() | |
| # Fit the data(train the model) | |
| regression_model.fit(x, y) | |
| # Predict | |
| y_predicted = regression_model.predict(x) | |
| # model evaluation | |
| rmse = mean_squared_error(y, y_predicted) | |
| r2 = r2_score(y, y_predicted) | |
| # printing values | |
| print('Slope:' ,regression_model.coef_) | |
| print('Intercept:', regression_model.intercept_) | |
| print('Root mean squared error: ', rmse) | |
| print('R2 score: ', r2) | |
| # plotting values | |
| # data points | |
| plt.scatter(x, y, s=10) | |
| plt.xlabel('x') | |
| plt.ylabel('y') | |
| # predicted values | |
| plt.plot(x, y_predicted, color='r') | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment