2012-03-06 00:18:05 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-05-26 18:03:52 +08:00
|
|
|
"""
|
2012-03-06 00:18:05 +08:00
|
|
|
=========================================================
|
|
|
|
|
Linear Regression Example
|
|
|
|
|
=========================================================
|
2020-09-07 16:42:52 +08:00
|
|
|
The example below uses only the first feature of the `diabetes` dataset,
|
|
|
|
|
in order to illustrate the data points within the two-dimensional plot.
|
|
|
|
|
The straight line can be seen in the plot, showing how linear regression
|
|
|
|
|
attempts to draw a straight line that will best minimize the
|
|
|
|
|
residual sum of squares between the observed responses in the dataset,
|
|
|
|
|
and the responses predicted by the linear approximation.
|
|
|
|
|
|
|
|
|
|
The coefficients, residual sum of squares and the coefficient of
|
|
|
|
|
determination are also calculated.
|
2010-11-02 18:38:06 +08:00
|
|
|
|
2021-10-22 21:33:22 +08:00
|
|
|
"""
|
2012-03-23 00:12:36 +08:00
|
|
|
|
2012-03-06 00:18:05 +08:00
|
|
|
# Code source: Jaques Grobler
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2012-03-06 00:18:05 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2012-03-06 00:18:05 +08:00
|
|
|
import numpy as np
|
|
|
|
|
from sklearn import datasets, linear_model
|
2017-02-28 06:06:26 +08:00
|
|
|
from sklearn.metrics import mean_squared_error, r2_score
|
2012-03-06 00:18:05 +08:00
|
|
|
|
|
|
|
|
# Load the diabetes dataset
|
2019-08-25 11:17:01 +08:00
|
|
|
diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True)
|
2012-03-23 00:12:36 +08:00
|
|
|
|
2012-03-06 00:18:05 +08:00
|
|
|
# Use only one feature
|
2019-08-25 11:17:01 +08:00
|
|
|
diabetes_X = diabetes_X[:, np.newaxis, 2]
|
2010-05-26 18:03:52 +08:00
|
|
|
|
2012-03-06 00:18:05 +08:00
|
|
|
# Split the data into training/testing sets
|
2015-06-15 14:17:28 +08:00
|
|
|
diabetes_X_train = diabetes_X[:-20]
|
|
|
|
|
diabetes_X_test = diabetes_X[-20:]
|
2010-05-26 18:03:52 +08:00
|
|
|
|
2012-03-06 00:18:05 +08:00
|
|
|
# Split the targets into training/testing sets
|
2019-08-25 11:17:01 +08:00
|
|
|
diabetes_y_train = diabetes_y[:-20]
|
|
|
|
|
diabetes_y_test = diabetes_y[-20:]
|
2012-03-06 00:18:05 +08:00
|
|
|
|
|
|
|
|
# Create linear regression object
|
|
|
|
|
regr = linear_model.LinearRegression()
|
|
|
|
|
|
|
|
|
|
# Train the model using the training sets
|
2012-04-28 18:04:36 +08:00
|
|
|
regr.fit(diabetes_X_train, diabetes_y_train)
|
2012-03-06 00:18:05 +08:00
|
|
|
|
2017-02-28 06:06:26 +08:00
|
|
|
# Make predictions using the testing set
|
|
|
|
|
diabetes_y_pred = regr.predict(diabetes_X_test)
|
|
|
|
|
|
2012-03-06 00:18:05 +08:00
|
|
|
# The coefficients
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Coefficients: \n", regr.coef_)
|
2015-12-04 05:17:58 +08:00
|
|
|
# The mean squared error
|
2017-02-28 06:06:26 +08:00
|
|
|
print("Mean squared error: %.2f" % mean_squared_error(diabetes_y_test, diabetes_y_pred))
|
2019-08-06 01:19:55 +08:00
|
|
|
# The coefficient of determination: 1 is perfect prediction
|
|
|
|
|
print("Coefficient of determination: %.2f" % r2_score(diabetes_y_test, diabetes_y_pred))
|
2012-03-06 00:18:05 +08:00
|
|
|
|
2012-04-28 18:04:36 +08:00
|
|
|
# Plot outputs
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.scatter(diabetes_X_test, diabetes_y_test, color="black")
|
2017-02-28 06:06:26 +08:00
|
|
|
plt.plot(diabetes_X_test, diabetes_y_pred, color="blue", linewidth=3)
|
2010-05-26 18:03:52 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xticks(())
|
|
|
|
|
plt.yticks(())
|
2012-03-06 00:18:05 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|