正规方程:
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
x_train,x_test,y_train,y_test=train_test_split(diab.data,diab.target,random_state=22)
transfer=StandardScaler()
x_train=transfer.fit_transform(x_train)
x_test=transfer.transform(x_test)
estimatar=LinearRegression()
estimatar.fit(x_train,y_train)
print('正规方程权重系数为:',estimatar.coef_)
print('正规方程偏置为:',estimatar.intercept_)
y_predict=estimatar.predict(x_test)
error=mean_squared_error(y_test,y_predict)
print('正规方程的均方误差:',error)
梯度下降:
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression,SGDRegressor
from sklearn.metrics import mean_squared_error
x_train,x_test,y_train,y_test=train_test_split(diab.data,diab.target,random_state=22)
transfer=StandardScaler()
x_train=transfer.fit_transform(x_train)
x_test=transfer.transform(x_test)
estimatar.fit(x_train,y_train)
print('梯度下降权重系数为:',estimatar.coef_)
print('梯度下降偏置为:',estimatar.intercept_)
y_predict=estimatar.predict(x_test)
error=mean_squared_error(y_test,y_predict)
print('梯度下降的均方误差:',error)