岭回归算法:

sk-learn实现L2岭回归,对线性回归正则化

sk-learn实现L2岭回归,对线性回归正则化

 

from sklearn.datasets import load_boston
from sklearn.externals import joblib
from sklearn.linear_model import Ridge, RidgeCV
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler



def liner_ridge():
    '''
    岭回归
    :return: 
    '''

    #1.获取数据
    data = load_boston()

    #2.数据集划分
    x_train,x_test,y_train,y_test = train_test_split(data.data,data.target,random_state=20)

    #3.特征工程-标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.fit_transform(x_test)

    #4.机器学习-线性回归(岭回归)
    # estimator = Ridge(alpha = 1)
    # estimator = RidgeCV(alphas=(0.1,1,8,5,11))
    # estimator.fit(x_train,y_train)
    #
    # #模型保存
    # joblib.dump(estimator,"./data/test.pkl")

    estimator = joblib.load("./data/test.pkl")

    #5.模型评估
    #获取系数等值
    y_predict = estimator.predict(x_test)
    print("预测值为:",y_predict)
    print("模型中的系数为:",estimator.coef_)
    print("模型中的偏执为:",estimator.intercept_)
    print(estimator.alpha_)
    print(estimator.alphas)
    #评价模型 均方误差
    error = mean_squared_error(y_test,y_predict)
    print("误差为:",error)


if __name__ == '__main__':
    liner_ridge()

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-22
  • 2021-07-01
  • 2021-05-08
  • 2021-04-25
  • 2021-10-16
猜你喜欢
  • 2021-07-22
  • 2021-12-23
  • 2021-04-24
  • 2021-08-06
  • 2022-02-05
  • 2021-10-19
相关资源
相似解决方案