案例:
from sklearn.datasets import load_boston
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
def linear_model():
bos=load_boston()
print(bos)
x1,x2,y1,y2=train_test_split(bos.data,bos.target,test_size=0.2,)
# 3.特征工程-标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x1)
x_test = transfer.fit_transform(x2)
# 4.机器学习-线性回归(正规方程)
estimator = LinearRegression()
estimator.fit(x_train, y1)
# 5.模型评估
# 5.1 获取系数等值
y_predict = estimator.predict(x_test)
print("预测值为:\n", y_predict)
print("模型中的系数为:\n", estimator.coef_)
print("模型中的偏置为:\n", estimator.intercept_)
def linear_model2():
bos=load_boston()
print(bos)
x1,x2,y1,y2=train_test_split(bos.data,bos.target,test_size=0.2,)
# 3.特征工程-标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x1)
x_test = transfer.fit_transform(x2)
# 4.机器学习-线性回归(正规方程)
# estimator = LinearRegression()
estimator = SGDRegressor(max_iter=1000)
estimator.fit(x_train, y1)
# 5.模型评估
# 5.1 获取系数等值
y_predict = estimator.predict(x_test)
print("预测值为:\n", y_predict)
print("模型中的系数为:\n", estimator.coef_)
print("模型中的偏置为:\n", estimator.intercept_)
if __name__ == '__main__':
linear_model2()