在Python机器学习(三) - 机器学习之魂---算法中我们列出了11中常用的回归问题的算法,基于现有的数据,从sklearn里逐一搬运出各算法相应的代码,并对比了各种算法的拟合效果。
学习小组的杨小羊同学研究出了一套一招见血的代码,搬来一试,果然灵。一小段代码各种算法(默认参数)的拟合度一目了然,省去了挨个逐个算法分别写代码的麻烦,主要代码如下:
#1. 导入各种算法
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import BaggingRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.ensemble import GradientBoostingRegressor
import xgboost as xgb
from sklearn.svm import SVR
from sklearn.neural_network import MLPRegressor
#2. 将各算法存放在models字典中
models = {}
models['LinearRegression'] = LinearRegression()
models['BaggingRegressor'] = BaggingRegressor()
models['KNeighborsRegressor'] = KNeighborsRegressor()
models['DecisionTreeRegressor'] = DecisionTreeRegressor()
models['RandomForestRegressor'] = RandomForestRegressor()
models['ExtraTreesRegressor'] = ExtraTreesRegressor()
models['AdaboostRegressor'] = AdaBoostRegressor()
models['GradientBoostingRegressor'] = GradientBoostingRegressor()
models['xgboost'] = xgb.XGBRegressor()
models['SVM'] = SVR()
models['MLPRegressor']=MLPRegressor()
print(models) #可获得各种算法的默认参数
#3. 各算法评估
from sklearn.cross_validation import cross_val_score
results = []
for key in models:
cv_results=cross_val_score(models[key],x_train,y_train, cv=5)
print('%s: %.4f(%.4f)' %(key,cv_results.mean(),cv_results.std()))
#4. 运行结果
LinearRegression: 0.75(0.04)
BaggingRegressor: 0.81(0.04)
KNeighborsRegressor: 0.30(0.10)
DecisionTreeRegressor: 0.64(0.20)
RandomForestRegressor: 0.80(0.06)
ExtraTreesRegressor: 0.89(0.04)
AdaboostRegressor: 0.80(0.03)
GradientBoostingRegressor: 0.86(0.06)
xgboost: 0.83(0.05)
SVM: -0.02(0.04)
MLPRegressor: -353778.72(370539.08)
真是没有对比就没有伤害,有些算法居然和这套数据这么八字不合,让我扶墙哭一会儿。不过讲真,这只是按照算法默认的参数进行回归的结果,有些算法(尤其是集成算法可以通过调参,获得更佳的拟合效果)。还有最后一个MLPRegressor神经网络是不是特别抢眼,拟合成这样这明显是在耍大小姐脾气。没错,人家早就说过了,神经网络算法对数据敏感,必须进行标准化才能做拟合,不然就神经给你看。
领取专属 10元无门槛券
私享最新 技术干货