Scikit-Learn also has a general class, MultiOutputRegressor, which can be used to use a single-output regression model and fit one regressor separately to each target.
Your code would then look something like this (using k-NN as example):
from sklearn.neighbors import KNeighborsRegressor from sklearn.multioutput import MultiOutputRegressor X = np.random.random((10,3)) y = np.random.random((10,2)) X2 = np.random.random((7,3)) knn = KNeighborsRegressor() regr = MultiOutputRegressor(knn) regr.fit(X,y) regr.predict(X2)
來自:
https://stats.stackexchange.com/questions/153853/regression-with-scikit-learn-with-multiple-outputs-svr-or-gbm-possible
我的建議是使用sklearn.multioutput.MultiOutputRegressor作為xgb.XGBRegressor
的包裝。 MultiOutputRegressor
為每個目標培訓一個回歸者,只需要回歸者實施fit
和predict
,這是xgboost恰好支持的。
# get some noised linear data X = np.random.random((1000, 10)) a = np.random.random((10, 3)) y = np.dot(X, a) + np.random.normal(0, 1e-3, (1000, 3)) # fitting multioutputregressor = MultiOutputRegressor(xgb.XGBRegressor(objective='reg:linear')).fit(X, y) # predicting print np.mean((multioutputregressor.predict(X) - y)**2, axis=0) # 0.004, 0.003, 0.005
這可能是使用xgboost因為你不會需要改變你的代碼的任何其他部分(如果原先使用sklearn
API)倒退多維目標的最簡單的方法。
來自:
https://stackoverrun.com/cn/q/10892468
1.12.5. 多輸出回歸
多輸出回歸支持 MultiOutputRegressor
可以被添加到任何回歸器中。這個策略包括對每個目標擬合一個回歸器。因為每一個目標可以被一個回歸器精確地表示,通過檢查對應的回歸器,可以獲取關於目標的信息。 因為 MultiOutputRegressor
對於每一個目標可以訓練出一個回歸器,所以它無法利用目標之間的相關度信息。
sklearn 中文文檔 : http://sklearn.apachecn.org/cn/stable/modules/multiclass.html#id14
sklearn.multioutput
.MultiOutputRegressor
官方文檔:http://scikit-learn.org/stable/modules/generated/sklearn.multioutput.MultiOutputRegressor.html
博客地址:https://blog.csdn.net/luanpeng825485697/article/details/79858186