<jupyter_start><jupyter_text>LinearRegression,SGDRegression,Ridge,Lasso for Boston Prediction  Preprocese data<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
from sklearn import preprocessing
data = load_boston()
data_pd = pd.DataFrame(data.data,columns=data.feature_names)
data_pd['price'] = data.target
# 查看空值
data_pd.isnull().sum()
# 查看数据大小
data_pd.shape
# 查看数据描述
data_pd.describe()
# 显示数据前5行
data_pd.head()<jupyter_output><empty_output><jupyter_text>Coefficient relation<jupyter_code>#计算相关系数
data_pd.corr()['price']
#画出相关系数>0.5特征
corr = data_pd.corr()
corr = corr['price']
corr[abs(corr)>0.5].sort_values().plot.bar()
#各变量与价格之间关系
plt.figure(figsize=(16,4))
for i, key in enumerate(['RM', 'LSTAT', 'PTRATIO']):
    plt.subplot(1, 3, i+1)
    plt.xlabel(key)
    plt.scatter(data_pd[key], data_pd['price'], alpha=0.5)
plt.show()<jupyter_output><empty_output><jupyter_text>Single variable prediction<jupyter_code># #单变量

def linear_single(x,y):
    x_mean = np.mean(x)
    y_mean = np.mean(y)
    xsize = x.size
    zi = (x * y).sum() - xsize * x_mean *y_mean
    mu = (x ** 2).sum() - xsize * x_mean ** 2
    # 参数a b
    a = zi / mu
    b = y_mean - a * x_mean
    # 这里对参数保留两位有效数字
    a = np.around(a,decimals=2);
    b = np.around(b,decimals=2);
    return a,b

Y_single = np.array(data_pd['price'])

X_single = np.array(data_pd['RM'])

a,b=linear_single(X_single,Y_single);
y1 = a*X_single + b
plt.scatter(X_single,Y_single)
plt.plot(X_single,y1,c='r')
print('The weights: ',a,b)


from sklearn.metrics import r2_score,mean_squared_error,mean_absolute_error
#r2_score
print("Linear_Single r2_score:",r2_score(Y_single,y1));
#MSE
print("Linear_Single MSE:",mean_squared_error(ss_y.inverse_transform(Y_single),ss_y.inverse_transform(y1)));
#MAE
print("Linear_Single MAE:",mean_absolute_error(ss_y.inverse_transform(Y_single),ss_y.inverse_transform(y1)));<jupyter_output>The weights:  9.1 -34.67
Linear_Single r2_score: 0.4835235394426868
Linear_Single MSE: 3547.0440507998883
Linear_Single MAE: 40.119537835043744<jupyter_text>Multi-variable prediction<jupyter_code># 制作训练集和测试集的数据
data_pd = data_pd[['LSTAT','PTRATIO','RM','price']]
y = np.array(data_pd['price'])
data_pd=data_pd.drop(['price'],axis=1)
X = np.array(data_pd)
# 分割训练集和测试集
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2)

#2.数据标准化处理
from sklearn.preprocessing import StandardScaler
#分别初始化对特征和目标值的标准化器
ss_X=StandardScaler();
ss_y=StandardScaler();

#分别对训练和测试数据的特征以及目标值进行标准化处理
X_train=ss_X.fit_transform(X_train);
X_test=ss_X.transform(X_test);
y_train=ss_y.fit_transform(y_train.reshape(-1,1));
y_test=ss_y.transform(y_test.reshape(-1,1));
#使用线性回归模型LinearRegression和SGDRegressor分别对波士顿房价数据进行训练及预测
from sklearn.linear_model import LinearRegression
lr=LinearRegression();
#使用训练数据进行参数估计
lr.fit(X_train,y_train);
#回归预测
lr_y_predict=lr.predict(X_test);<jupyter_output><empty_output><jupyter_text>LinearRegression<jupyter_code>#模型评估
#使用LinearRegression模型自带的评估模块，并输出评估结果
print("lr_score:",lr.score(X_test,y_test));
#R-square,MSE,MAE评估LinearRegression回归性能
from sklearn.metrics import r2_score,mean_squared_error,mean_absolute_error
#r2_score
print("r2_score:",r2_score(y_test,lr_y_predict));
#MSE
print("MSE:",mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(lr_y_predict)));
#MAE
print("MAE:",mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(lr_y_predict)));<jupyter_output>lr_score: 0.6891839740632231
r2_score: 0.6891839740632231
MSE: 26.69298427128839
MAE: 3.8102303924910137<jupyter_text>SGDR<jupyter_code>#SGDRegressor
from sklearn.linear_model import SGDRegressor
#默认配置初始化线性回归器
sgdr=SGDRegressor();
#使用训练数据进行参数估计
sgdr.fit(X_train,y_train);
#对测试数据进行回归预测
sgdr_y_predict=sgdr.predict(X_test);
#使用SGDRegressor模型自带的评估模块，并输出评估结果
print("sgdr_score:",sgdr.score(X_test,y_test));
#R-square,MSE,MAE评估SGDRegressor回归性能
from sklearn.metrics import r2_score,mean_squared_error,mean_absolute_error
#r2_score
print("r2_score:",r2_score(y_test,sgdr_y_predict));
#MSE
print("MSE:",mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(sgdr_y_predict)));
#MAE
print("MAE:",mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(sgdr_y_predict)));
#绘图
import matplotlib.pyplot as plt
#Linear
plt.plot(y_test, label='true');
plt.plot(lr_y_predict, label='linear');
plt.legend();


#SGD

plt.plot(sgdr_y_predict, label='SGDR');
plt.legend();

plt.show();<jupyter_output><empty_output><jupyter_text>Ridge and lasso<jupyter_code>#Ridge and lasso
from sklearn.linear_model import Ridge  # 线性回归算法Ridge回归，岭回归
from sklearn.linear_model import Lasso  # 线性回归算法Lasso回归，可用作特征筛选
# 创建学习模型
ridge = Ridge()
lasso = Lasso()


# 训练模型

ridge.fit(X_train, y_train)
lasso.fit(X_train, y_train)

# 预测数据
y_pre_ridge = ridge.predict(X_test)
y_pre_lasso = lasso.predict(X_test)

# 评分，R2 决定系数（拟合优度）。模型越好：r2→1；模型越差：r2→0

from sklearn.metrics import r2_score,mean_squared_error,mean_absolute_error
#r2_score of Ridge
print("The Ridge r2_score:",r2_score(y_test,y_pre_ridge));
#MSE
print("The Ridge MSE:",mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(y_pre_ridge)));
#MAE
print("The Ridge MAE:",mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(y_pre_ridge)));

#r2_score of Lasso
print("The Lasso r2_score:",r2_score(y_test,y_pre_lasso));
#MSE
print("The Lasso MSE:",mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(y_pre_lasso)));
#MAE
print("The Lasso MAE:",mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(y_pre_lasso)));
# 绘图

# Ridge
plt.plot(y_test, label='true')
plt.plot(y_pre_ridge, label='ridge')
plt.legend()
 
# Lasso
plt.plot(y_pre_lasso, label='lasso')
plt.legend()
 
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13640167364016736, 'fraction_numerical': 0.0301255230125523, 'mean_word_length': 9.215384615384615, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '436474', 'license': 'MIT', 'n_tokens_mistral': 2821, 'n_tokens_neox': 2744, 'n_words': 326, 'path': 'Boston_Linear_SGDR_Ridge_Lasso.ipynb', 'repo_name': 'cc5592/Statistical-Learning', 'size': '5975'}