使用我们自己封装的SGD(随机梯度下降法)¶
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
#生成有100000个样本,有1个特征的数据
m = 100000
x = np.random.normal(size=m)
X = x.reshape(-1,1)
y = 4. *x + 3. + np.random.normal(0,3,size=m)
from playML.LinearRegression import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit_sgd(X,y,n_iters=2)
LinearRegression()
lin_reg.coef_#对应的系数,与我们的设定一致
输出:array([4.02030617])
lin_reg.intercept_#对应的截距,与我们的设定一致
输出:3.0105715023345283
真实使用我们自己的SGD(使用真实数据)
from sklearn import datasets
boston = datasets.load_boston()
X = boston.data
y = boston.target
X = X[y < 50.0]
y = y[y < 50.0]
#将数据分为训练数据集和测试数据集
from playML.model_selection import train_test_split
#将数据分为训练数据集和测试数据集
X_train,X_test,y_train,y_test = train_test_split(X,y,seed=666)
# 对数据进行归一化
from sklearn.preprocessing import StandardScaler
standardScaler = StandardScaler()
#训练数据并将其转换为标准的正态分布
standardScaler.fit(X_train)
X_train_standard = standardScaler.transform(X_train)
X_test_standard = standardScaler.transform(X_test)
from playML.LinearRegression import LinearRegression
lin_reg = LinearRegression()
%time lin_reg.fit_sgd(X_train_standard,y_train,n_iters=2)#随着n_iters的增加,得到的结果越来越接近最好的R方值
lin_reg.score(X_test_standard,y_test)
输出:
Wall time: 7 ms
0.7857275413602652
from playML.LinearRegression import LinearRegression
lin_reg = LinearRegression()
%time lin_reg.fit_sgd(X_train_standard,y_train,n_iters=50)
lin_reg.score(X_test_standard,y_test)
输出:
Wall time: 149 ms
0.808560757055621
from playML.LinearRegression import LinearRegression
lin_reg = LinearRegression()
%time lin_reg.fit_sgd(X_train_standard,y_train,n_iters=100)
lin_reg.score(X_test_standard,y_test)
输出
Wall time: 295 ms
0.8129434245278827
scikit-learn中的SGD
from sklearn.linear_model import SGDRegressor#因为在linear_model包中,所以SGDRegressor只能解决线性模型
sgd_reg = SGDRegressor()
%time sgd_reg.fit(X_train_standard,y_train)
sgd_reg.score(X_test_standard,y_test)
Wall time: 278 ms
0.812244351275594