# 【机器学习 非线性回归模型】10分钟了解下8种常见的非线性回归模型

1、SVR

#加载SVR模型算法库
from sklearn.svm import SVR
#训练集
X = [[0, 0], [2, 2]]
y = [0.5, 2.5]
#创建SVR回归模型的对象
clf = SVR()
# 利用训练集训练SVR回归模型
clf.fit(X, y)
"""
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([[1, 1]])


2、决策树回归

from sklearn.tree import  DecisionTreeRegressor
X = [[0, 0], [2, 2]]
y = [0.5, 2.5]
clf = DecisionTreeRegressor()
clf = clf.fit(X, y)
clf.predict([[1, 1]])


3、knn回归

X = [[0], [1], [2], [3]]
y = [0, 0, 1, 1]
from sklearn.neighbors import KNeighborsRegressor
neigh = KNeighborsRegressor(n_neighbors=2)
neigh.fit(X, y)
print(neigh.predict([[1.5]]))


4、RandomForest回归

from sklearn.ensemble import RandomForestRegressor
from sklearn.datasets import make_regression
X, y = make_regression(n_features=4, n_informative=2,
random_state=0, shuffle=False)
regr = RandomForestRegressor(max_depth=2, random_state=0,
n_estimators=100)
regr.fit(X, y)
print(regr.feature_importances_)
print(regr.predict([[0, 0, 0, 0]]))


5、XGBoost回归
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)
print(mean_squared_error(y_test, y_pred))


6、神经网络MLP回归

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,
validation_fraction=0.1, verbose=False, warm_start=False)
"""
y_pred = mlp.predict(X_test)


7、LightGBM回归
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)
print(mean_squared_error(y_test, y_pred))


8、GBDT回归


# GBDT超参数调优
params = {
'n_estimators': 400,
'max_depth': 11,
'learning_rate': 0.06,
'loss': 'ls',
'subsample':0.8
}