XGBRegressor.fit()引发TypeError:预期的序列或类似数组的类,得到<class'xgboost.core.dmatrix'=“”>

时间:2019-04-04 06:44:48

标签: python xgboost

当尝试适合我的XGBRegressor模型时,尽管TypeError: Expected sequence or array-like, got <class 'xgboost.core.DMatrix'>类型为x,但我在numpy.ndarray参数上得到了from xgboost import XGBRegressor from sklearn.metrics import r2_score model = XGBRegressor(max_depth=5, learning_rate=0.001, n_estimators=5000) eval_set = [(X_test, y_test)] model.fit(X_train, y_train, eval_set=eval_set, eval_metric=r2_score, early_stopping_rounds=20, verbose=True)

--------------------------------------------------------------------------- TypeError                                 Traceback (most recent call last) <ipython-input-50-4bb3ebe8ef00> in <module>
----> 1 model.fit(X_train, y_train, eval_set=eval_set, eval_metric=r2_score, early_stopping_rounds=20, verbose=True)

~/anaconda3/envs/ds/lib/python3.6/site-packages/xgboost/sklearn.py in fit(self, X, y, sample_weight, eval_set, eval_metric, early_stopping_rounds, verbose, xgb_model, sample_weight_eval_set, callbacks)
    376                               evals_result=evals_result, obj=obj, feval=feval,
    377                               verbose_eval=verbose, xgb_model=xgb_model,
--> 378                               callbacks=callbacks)
    379 
    380         if evals_result:

~/anaconda3/envs/ds/lib/python3.6/site-packages/xgboost/training.py in train(params, dtrain, num_boost_round, evals, obj, feval, maximize, early_stopping_rounds, evals_result, verbose_eval, xgb_model, callbacks, learning_rates)
    214                            evals=evals,
    215                            obj=obj, feval=feval,
--> 216                            xgb_model=xgb_model, callbacks=callbacks)
    217 
    218 

~/anaconda3/envs/ds/lib/python3.6/site-packages/xgboost/training.py in
_train_internal(params, dtrain, num_boost_round, evals, obj, feval, xgb_model, callbacks)
     82         # check evaluation result.
     83         if len(evals) != 0:
---> 84             bst_eval_set = bst.eval_set(evals, i, feval)
     85             if isinstance(bst_eval_set, STRING_TYPES):
     86                 msg = bst_eval_set

~/anaconda3/envs/ds/lib/python3.6/site-packages/xgboost/core.py in eval_set(self, evals, iteration, feval)    1175         if feval is not None:    1176             for dmat, evname in evals:
-> 1177                 feval_ret = feval(self.predict(dmat), dmat)    1178                 if isinstance(feval_ret, list):    1179           for name, val in feval_ret:

~/anaconda3/envs/ds/lib/python3.6/site-packages/sklearn/metrics/regression.py in r2_score(y_true, y_pred, sample_weight, multioutput)
    532     """
    533     y_type, y_true, y_pred, multioutput = _check_reg_targets(
--> 534         y_true, y_pred, multioutput)
    535     check_consistent_length(y_true, y_pred, sample_weight)
    536 

~/anaconda3/envs/ds/lib/python3.6/site-packages/sklearn/metrics/regression.py in _check_reg_targets(y_true, y_pred, multioutput)
     73 
     74     """
---> 75     check_consistent_length(y_true, y_pred)
     76     y_true = check_array(y_true, ensure_2d=False)
     77     y_pred = check_array(y_pred, ensure_2d=False)

~/anaconda3/envs/ds/lib/python3.6/site-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)
    229     """
    230 
--> 231     lengths = [_num_samples(X) for X in arrays if X is not None]
    232     uniques = np.unique(lengths)
    233     if len(uniques) > 1:

~/anaconda3/envs/ds/lib/python3.6/site-packages/sklearn/utils/validation.py in <listcomp>(.0)
    229     """
    230 
--> 231     lengths = [_num_samples(X) for X in arrays if X is not None]
    232     uniques = np.unique(lengths)
    233     if len(uniques) > 1:

~/anaconda3/envs/ds/lib/python3.6/site-packages/sklearn/utils/validation.py in _num_samples(x)
    136         else:
    137             raise TypeError("Expected sequence or array-like, got %s" %
--> 138                             type(x))
    139     if hasattr(x, 'shape'):
    140         if len(x.shape) == 0:

TypeError: Expected sequence or array-like, got <class 'xgboost.core.DMatrix'>

错误消息:

const imgUrls = [
    1,2,3
];

class ImageSlide extends Component {

render() {
    const { url } = this.props;
    const Text=...
    return (          
        <div>
            <div className={`pic${url}`}>
                <p className="p1_1">{Text.p1_1}</p>
            </div>
        </div>      
    );
}

2 个答案:

答案 0 :(得分:1)

# XGBoost Classifier

from xgboost import XGBClassifier

xgb_classifier = XGBClassifier()

xgb_classifier.fit(X_train, y_train)

y_pred_xgb = xgb_classifier.predict(X_test)

accuracy_score(y_test, y_pred_xgb)

答案 1 :(得分:0)

我了解了导致此错误的原因。这是因为我设置了错误的<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@color/black_1_50"/> <!--shadow Color--> </shape> </item> <item android:left="0dp" android:right="0dp" android:top="20dp" android:bottom="0dp"> <shape android:shape="oval"> <solid android:color="@color/white_1_60"/> </shape> </item> </layer-list>