<jupyter_start><jupyter_text>尝试使用线性回归<jupyter_code>from sklearn import datasets
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

%matplotlib inline
# load数据
loaded_data = datasets.load_boston()

# 获取特征数据
data_x = loaded_data.data
# 获取标签数据
data_y = loaded_data.target

data_x[:4,:]
data_y[:4]
# 定义model

model = LinearRegression()
model.fit(data_x,data_y)
print model.predict(data_x[:4, :])
print data_y[:4]<jupyter_output>[30.00384338 25.02556238 30.56759672 28.60703649]
[24.  21.6 34.7 33.4]<jupyter_text>创建虚拟数据<jupyter_code>x, y = datasets.make_regression(n_samples=100, n_features=1, n_targets=1, noise=10)
plt.scatter(x, y)
plt.show()<jupyter_output><empty_output><jupyter_text>参数和评分对于 LinearRegressor 这个模型，这两个属性分别输出模型的斜率和截距（与y轴的交点）<jupyter_code>print(model.coef_)
print(model.intercept_)
# 输入定义的参数
print(model.get_params())
# 输出模型的评分
print(model.score(data_x, data_y))<jupyter_output>0.7406426641094094
{'fraction_non_alphanumeric': 0.12420382165605096, 'fraction_numerical': 0.08386411889596602, 'mean_word_length': 8.822916666666666, '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': '478237', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 493, 'n_tokens_neox': 465, 'n_words': 56, 'path': 'ml/04-sklearn/03-Linear-Regression.ipynb', 'repo_name': 'sdyz5210/python', 'size': '942'}