Machine Learning | (10) 回归算法-岭回归

Machine Learning | 机器学习简介


Machine Learning | (1) Scikit-learn与特征工程


Machine Learning | (2) sklearn数据集与机器学习组成


Machine Learning | (3) Scikit-learn的分类器算法-k-近邻


Machine Learning | (4) Scikit-learn的分类器算法-逻辑回归


Machine Learning | (5) Scikit-learn的分类器算法-朴素贝叶斯


Machine Learning | (6) Scikit-learn的分类器算法-性能评估


Machine Learning | (7) Scikit-learn的分类器算法-决策树(Decision Tree)


Machine Learning | (8) Scikit-learn的分类器算法-随机森林(Random Forest)


Machine Learning | (9) 回归算法-线性回归


Machine Learning | (10) 回归算法-岭回归


回归算法之岭回归

具有L2正则化的线性最小二乘法。岭回归是一种专用于共线性数据分析的有偏估计回归方法,实质上是一种改良的最小二乘估计法,通过放弃最小二乘法的无偏性,以损失部分信息、降低精度为代价获得回归系数更为符合实际、更可靠的回归方法,对病态数据的拟合要强于最小二乘法。当数据集中存在共线性的时候,岭回归就会有用。


sklearn.linear_model.Ridge

class sklearn.linear_model.Ridge(alpha=1.0, fit_intercept=True, normalize=False, copy_X=True, max_iter=None, tol=0.001, solver='auto', random_state=None)**
  """
  :param alpha:float类型,正规化的程度
  """
from sklearn.linear_model import Ridge
clf = Ridge(alpha=1.0)
clf.fit([[0, 0], [0, 0], [1, 1]], [0, .1, 1]))

方法

score(X, y, sample_weight=None)

clf.score()

属性

coef_

clf.coef_
array([ 0.34545455,  0.34545455])


intercept_

clf.intercept_
0.13636...

案例分析

 
 
def linearmodel():
    """
    线性回归对波士顿数据集处理
    :return: None
    """
 
    # 1、加载数据集
 
    ld = load_boston()
 
    x_train,x_test,y_train,y_test = train_test_split(ld.data,ld.target,test_size=0.25)
 
    # 2、标准化处理
 
    # 特征值处理
    std_x = StandardScaler()
    x_train = std_x.fit_transform(x_train)
    x_test = std_x.transform(x_test)
 
 
    # 目标值进行处理
 
    std_y  = StandardScaler()
    y_train = std_y.fit_transform(y_train)
    y_test = std_y.transform(y_test)
 
    # 3、估计器流程
 
    # LinearRegression
    lr = LinearRegression()
 
    lr.fit(x_train,y_train)
 
    # print(lr.coef_)
 
    y_lr_predict = lr.predict(x_test)
 
    y_lr_predict = std_y.inverse_transform(y_lr_predict)
 
    print("Lr预测值:",y_lr_predict)
 
 
    # SGDRegressor
    sgd = SGDRegressor()
 
    sgd.fit(x_train,y_train)
 
    # print(sgd.coef_)
 
    y_sgd_predict = sgd.predict(x_test)
 
    y_sgd_predict = std_y.inverse_transform(y_sgd_predict)
 
    print("SGD预测值:",y_sgd_predict)
 
    # 带有正则化的岭回归
 
    rd = Ridge(alpha=0.01)
 
    rd.fit(x_train,y_train)
 
    y_rd_predict = rd.predict(x_test)
 
    y_rd_predict = std_y.inverse_transform(y_rd_predict)
 
    print(rd.coef_)
 
    # 两种模型评估结果
 
    print("lr的均方误差为:",mean_squared_error(std_y.inverse_transform(y_test),y_lr_predict))
 
    print("SGD的均方误差为:",mean_squared_error(std_y.inverse_transform(y_test),y_sgd_predict))
 
    print("Ridge的均方误差为:",mean_squared_error(std_y.inverse_transform(y_test),y_rd_predict))
 
    return None
上一篇:类似通讯录分组的Android PinnedSectionListView,且分组标签悬停滑入滑出


下一篇:艾伟:[WCF的Binding模型]之五:绑定元素(Binding Element)