<jupyter_start><jupyter_text>1. prepare data<jupyter_code>from sklearn import linear_model
from sklearn import datasets


boston = datasets.load_boston()
x = boston.data[:,0:1]
y = boston.target

x_train = x[0:400,:]
y_train = y[0:400]

x_test = x[400:,:]
y_test = y[400:]

print('x_train.shape={}, y_train.shape={}'.format(x_train.shape, y_train.shape))
print('x_test.shape={}, y_test.shape={}'.format(x_test.shape, y_test.shape))<jupyter_output>x_train.shape=(400, 1), y_train.shape=(400,)
x_test.shape=(106, 1), y_test.shape=(106,)<jupyter_text>2. draw train & test data<jupyter_code>%matplotlib inline

import matplotlib
import matplotlib.pyplot as plt

plt.scatter(x_train, y_train,  color='black')
plt.scatter(x_test, y_test,  color='blue')<jupyter_output><empty_output><jupyter_text>3. Linear Regression<jupyter_code>from sklearn import  linear_model
import numpy as np

# Create linear regression object
regr = linear_model.LinearRegression()

# Train the model using the training sets
regr.fit(x_train, y_train)

# The mean squared error
print("Mean squared error: {}".format(np.mean((regr.predict(x_test) - y_test) ** 2)))

plt.scatter(x_train, y_train,  color='black')
plt.scatter(x_test, y_test,  color='blue')
plt.plot(x_train, regr.predict(x_train), color='black', linewidth=3)
w = regr.coef_
b = regr.intercept_

print('y = wx+b. w={}, b={}'.format(w,b)) # 线性方程的解<jupyter_output>y = wx+b. w=[-0.40649927], b=25.04622292117041
{'fraction_non_alphanumeric': 0.16111111111111112, 'fraction_numerical': 0.041666666666666664, 'mean_word_length': 6.789189189189189, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '273013', 'license': 'MIT', 'n_tokens_mistral': 633, 'n_tokens_neox': 588, 'n_words': 108, 'path': 'regression/Linear_Regression.ipynb', 'repo_name': 'ybdesire/machinelearning', 'size': '1440'}