<jupyter_start><jupyter_text>Linear Regression`LinearRegression` fits a linear model to the dataset by adjusting a set of parameters in order to make the sum of the squared residuals of the model as small as possible An example Load `diabetes` dataset:<jupyter_code>import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt

diabetes = datasets.load_diabetes()
diabetes.data.shape
# Use only one feature
diabetes_X = diabetes.data[:, np.newaxis, 2]

## Divide the dataset into train and test set
diabetes_X_train = diabetes_X[:-20]
diabetes_X_test = diabetes_X[-20:]
diabetes_y_train = diabetes.target[:-20]
diabetes_y_test = diabetes.target[-20:]
from sklearn import linear_model
regr = linear_model.LinearRegression()
regr.fit(diabetes_X_train, diabetes_y_train)
# Get parameters
regr.get_params()
print(regr.coef_)
# the mean square error
np.mean((regr.predict(diabetes_X_test)-diabetes_y_test) ** 2)
# variance score: 1 is perfect prediction, 0 means that there is no linear relationship between X and y
regr.score(diabetes_X_test, diabetes_y_test)
# Plot output
plt.scatter(diabetes_X_test, diabetes_y_test, color='black')
plt.plot(diabetes_X_test, regr.predict(diabetes_X_test), color = 'blue', linewidth=3)

plt.xticks(())
plt.yticks(())

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09480122324159021, 'fraction_numerical': 0.009938837920489297, 'mean_word_length': 6.654970760233918, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '280299', 'license': 'MIT', 'n_tokens_mistral': 465, 'n_tokens_neox': 432, 'n_words': 126, 'path': 'ML Algorithm - Linear Regression.ipynb', 'repo_name': 'machlearn/ipython-notebooks', 'size': '1308'}