美文网首页
常见回归算法

常见回归算法

作者: zerowl | 来源:发表于2019-05-05 23:46 被阅读0次

    sklearn作为机器学习中一个强大的算法包,内置了许多经典的回归算法。

    线性回归

    线性回归拟合一个带系数的线性模型,以最小化数据中的观测值与线性预测值之间的残差平方和。

    #X_train,X_test二维, y_train一维
    
    #加载线性模型算法库
    from sklearn import linear_model
    # 创建线性回归模型的对象
    regr = linear_model.LinearRegression()
    # 利用训练集训练线性模型
    regr.fit(X_train, y_train)
    # 使用测试集做预测
    y_pred = regr.predict(X_test)
    
    KNN回归

    在数据标签是连续变量而不是离散变量的情况下,可以使用KNN回归。分配给查询点的标签是根据其最近邻居标签的平均值计算的。

    from sklearn.neighbors import KNeighborsRegressor
    neigh = KNeighborsRegressor(n_neighbors=2)
    neigh.fit(X_train, y_train)
    y_pred=neigh.predict(X_test)
    
    决策树回归

    决策树也可以应用于回归问题

    from sklearn.tree import  DecisionTreeRegressor
    clf = DecisionTreeRegressor()
    clf = clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    
    随机森林回归
    from sklearn.ensemble import RandomForestRegressor
    regr = RandomForestRegressor(max_depth=2, random_state=0,
                                 n_estimators=100)
    regr.fit(X_train, y_train)
    pred = regr.predict(X_test)
    
    XGBoost回归

    基本所有的机器学习比赛的冠军方案都使用了XGBoost算法

    import xgboost as xgb
    xgb_model = xgb.XGBRegressor(max_depth = 3,
                                 learning_rate = 0.1,
                                 n_estimators = 100,
                                 objective = 'reg:linear',
                                 n_jobs = -1)
    
    xgb_model.fit(X_train, y_train,
                  eval_set=[(X_train, y_train)],
                  eval_metric='logloss',
                  verbose=100)
    y_pred = xgb_model.predict(X_test)
    
    支持向量回归
    from sklearn.svm import SVR
    #创建SVR回归模型的对象
    clf = SVR()
    # 利用训练集训练SVR回归模型
    clf.fit(X_train, y_train) 
    """
    SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1,
        gamma='auto_deprecated', kernel='rbf', max_iter=-1, shrinking=True,
        tol=0.001, verbose=False)
    """
    clf.predict(X_test)
    
    神经网络
    from sklearn.neural_network import MLPRegressor
    mlp=MLPRegressor()
    mlp.fit(X_train,y_train)
    """
    MLPRegressor(activation='relu', alpha=0.0001, batch_size='auto', beta_1=0.9,
           beta_2=0.999, early_stopping=False, epsilon=1e-08,
           hidden_layer_sizes=(100,), learning_rate='constant',
           learning_rate_init=0.001, max_iter=200, momentum=0.9,
           n_iter_no_change=10, nesterovs_momentum=True, power_t=0.5,
           random_state=None, shuffle=True, solver='adam', tol=0.0001,
           validation_fraction=0.1, verbose=False, warm_start=False)
    """
    y_pred = mlp.predict(X_test)
    
    LightGBM回归

    LightGBM作为另一个使用基于树的学习算法的梯度增强框架。相比于XGBoost,LightGBM有如下优点,训练速度更快,效率更高效;低内存的使用量。

    import lightgbm as lgb
    gbm = lgb.LGBMRegressor(num_leaves=31,
                            learning_rate=0.05,
                            n_estimators=20)
    gbm.fit(X_train, y_train,
            eval_set=[(X_train, y_train)],
            eval_metric='logloss',
            verbose=100)
    y_pred = gbm.predict(X_test)
    

    岭回归

    岭回归通过对系数进行惩罚(L2范式)来解决普通最小二乘法的一些问题,例如,当特征之间完全共线性(有解)或者说特征之间高度相关,这个时候适合用岭回归。

    from sklearn.linear_model import Ridge
    # 创建岭回归模型的对象
    reg = Ridge(alpha=.5)
    # 利用训练集训练岭回归模型
    reg.fit(X_train, y_train)
    pred= reg.predict(X_test)
    
    Lasso回归

    Lasso是一个估计稀疏稀疏的线性模型。它在某些情况下很有用,由于它倾向于选择参数值较少的解,有效地减少了给定解所依赖的变量的数量。Lasso模型在最小二乘法的基础上加入L1范式作为惩罚项。

    from sklearn.linear_model import Lasso
    # 创建Lasso回归模型的对象
    reg = Lasso(alpha=0.1)
    # 利用训练集训练Lasso回归模型
    reg.fit(X_train, y_train)
    """
    Lasso(alpha=0.1, copy_X=True, fit_intercept=True, max_iter=1000,
       normalize=False, positive=False, precompute=False, random_state=None,
       selection='cyclic', tol=0.0001, warm_start=False)
    """
    # 使用测试集做预测
    pred=reg.predict(X_test)
    
    Elastic Net回归

    Elastic Net 是一个线性模型利用L1范式和L2范式共同作为惩罚项。这种组合既可以学习稀疏模型,同时可以保持岭回归的正则化属性.

    from sklearn.linear_model import ElasticNet
    #创建ElasticNet回归模型的对象
    regr = ElasticNet(random_state=0)
    # 利用训练集训练ElasticNet回归模型
    regr.fit(X_train, y_train)
    pred=regr.predict(X_test)
    
    SGD回归

    SGD回归也是一种线性回归, 它通过随机梯度下降最小化正则化经验损失.

    import numpy as np
    from sklearn import linear_model
    n_samples, n_features = 10, 5
    np.random.seed(0)
    clf = linear_model.SGDRegressor(max_iter=1000, tol=1e-3)
    clf.fit(X_train, y_train)
    pred=clf.predict(X_test)
    """
    SGDRegressor(alpha=0.0001, average=False, early_stopping=False,
           epsilon=0.1, eta0=0.01, fit_intercept=True, l1_ratio=0.15,
           learning_rate='invscaling', loss='squared_loss', max_iter=1000,
           n_iter=None, n_iter_no_change=5, penalty='l2', power_t=0.25,
           random_state=None, shuffle=True, tol=0.001, validation_fraction=0.1,
           verbose=0, warm_start=False)
    """
    

    回归竞赛问题以及解决方案

    入门级比赛:

    Kaggle——房价预测
    这个比赛作为最基础的回归问题之一,很适合入门机器学习的小伙伴们。
    网址:https://www.kaggle.com/c/house-prices-advanced-regression-techniques
    经典解决方案:
    XGBoost解决方案: https://www.kaggle.com/dansbecker/xgboost
    Lasso解决方案: https://www.kaggle.com/mymkyt/simple-lasso-public-score-0-12102

    进阶比赛:

    Kaggle——销售量预测
    这个比赛作为经典的时间序列问题之一,目标是为了预测下个月每种产品和商店的总销售额。
    网址:https://www.kaggle.com/c/competitive-data-science-predict-future-sales
    经典解决方案:
    LightGBM: https://www.kaggle.com/sanket30/predicting-sales-using-lightgbm
    XGBoost: https://www.kaggle.com/fabianaboldrin/eda-xgboost
    第一名解决方案:https://www.kaggle.com/c/competitive-data-science-predict-future-sales/discussion/74835#latest-503740

    TOP比赛方案:

    Kaggle——餐厅访客预测
    网址:https://www.kaggle.com/c/recruit-restaurant-visitor-forecasting
    解决方案:
    1st 方案: https://www.kaggle.com/plantsgo/solution-public-0-471-private-0-505
    7th 方案:https://www.kaggle.com/c/recruit-restaurant-visitor-forecasting/discussion/49259#latest-284437
    8th 方案:https://github.com/MaxHalford/kaggle-recruit-restaurant
    12th 方案:https://www.kaggle.com/c/recruit-restaurant-visitor-forecasting/discussion/49251#latest-282765
    Kaggle——CorporaciónFavoritaGrocery销售预测
    网址:https://www.kaggle.com/c/favorita-grocery-sales-forecasting
    解决方案:
    1st 方案: https://www.kaggle.com/c/favorita-grocery-sales-forecasting/discussion/47582#latest-360306
    2st 方案:https://www.kaggle.com/c/favorita-grocery-sales-forecasting/discussion/47568#latest-278474
    3st 方案:https://www.kaggle.com/c/favorita-grocery-sales-forecasting/discussion/47560#latest-302253
    4st 方案:https://www.kaggle.com/c/favorita-grocery-sales-forecasting/discussion/47529#latest-271077
    5st方案:https://www.kaggle.com/c/favorita-grocery-sales-forecasting/discussion/47556#latest-270515
    6st方案:https://www.kaggle.com/c/favorita-grocery-sales-forecasting/discussion/47575#latest-269568

    来源:https://mp.weixin.qq.com/s/n0X4aZTtCrSoTJBT5MM9AA

    相关文章

      网友评论

          本文标题:常见回归算法

          本文链接:https://www.haomeiwen.com/subject/igzkoqtx.html