我正在尝试使用XGBoost开发一个预测模型。我的基本想法是开发一个自动化的预测模型,该模型使用从数据集中提取的前10个重要特征(数据集有700多行和90多列)来预测值。
输入数据每周更新,因此下一周的预测应使用当前周的值进行。我已经从XGBoost模型中提取了重要特征,但由于错误无法实现自动化。
import xgboost as xgbfrom sklearn.metrics import mean_squared_errorfrom sklearn.metrics import accuracy_scorefrom sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=100)eval_set = [(X_train, y_train), (X_test, y_test)]xg_reg = MyXGBRegressor(objective ='reg:squarederror', colsample_bytree = 0.3, learning_rate = 0.01,max_depth = 6, reg_alpha = 15, n_estimators = 1000, subsample = 0.5)predictions = xg_reg.fit(X_train,y_train, early_stopping_rounds=30, eval_metric=["rmse", "mae"], eval_set=eval_set, verbose=True)
上述代码帮助我运行回归器并预测值。以下代码会引发错误。
import xgboost as xgbfrom xgboost import XGBRegressorclass MyXGBRegressor(XGBRegressor): @property def coef_(self): return Nonethresholds = np.sort(xg_reg.feature_importances_)from sklearn.feature_selection import SelectFromModelfor thresh in thresholds: selection = SelectFromModel(xg_reg, threshold=thresh, prefit = True) selected_dataset = selection.transform(X_test) feature_idx = selection.get_support() feature_name = X.columns[feature_idx] selected_dataset = pd.DataFrame(selected_dataset) selected_dataset.columns = feature_name
错误如下:
---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-11-a42c3ed80da2> in <module> 3 for thresh in thresholds: 4 selection = SelectFromModel(xg_reg, threshold=thresh, prefit = True)----> 5 selected_dataset = selection.transform(X_test) 6 7 feature_idx = selection.get_support()~\Anaconda3\lib\site-packages\sklearn\feature_selection\_base.py in transform(self, X) 86 force_all_finite=not _safe_tags(self, key="allow_nan"), 87 )---> 88 mask = self.get_support() 89 if not mask.any(): 90 warn("No features were selected: either the data is"~\Anaconda3\lib\site-packages\sklearn\feature_selection\_base.py in get_support(self, indices) 50 values are indices into the input feature vector. 51 """---> 52 mask = self._get_support_mask() 53 return mask if not indices else np.where(mask)[0] 54 ~\Anaconda3\lib\site-packages\sklearn\feature_selection\_from_model.py in _get_support_mask(self) 186 ' "prefit=True" while passing the fitted' 187 ' estimator to the constructor.')--> 188 scores = _get_feature_importances( 189 estimator=estimator, getter=self.importance_getter, 190 transform_func='norm', norm_order=self.norm_order)~\Anaconda3\lib\site-packages\sklearn\feature_selection\_base.py in _get_feature_importances(estimator, getter, transform_func, norm_order) 189 return importances 190 elif transform_func == "norm":--> 191 if importances.ndim == 1: 192 importances = np.abs(importances) 193 else:AttributeError: 'NoneType' object has no attribute 'ndim'
回答:
问题在于MyXGBRegressor
的coef_
属性被设置为None
。如果你使用XGBRegressor
而不是MyXGBRegressor
,那么SelectFromModel
将使用XGBRegressor
的feature_importances_
属性,你的代码将会正常工作。
import numpy as npfrom xgboost import XGBRegressorfrom sklearn.datasets import make_regressionfrom sklearn.model_selection import train_test_splitfrom sklearn.feature_selection import SelectFromModel# 生成一些数据X, y = make_regression(n_samples=1000, n_features=5, random_state=100)# 分割数据X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=100)# 实例化模型model = XGBRegressor(objective="reg:squarederror", colsample_bytree=0.3, learning_rate=0.01, max_depth=6, reg_alpha=15, n_estimators=1000, subsample=0.5)# 拟合模型model.fit(X_train, y_train, early_stopping_rounds=30, eval_metric=["rmse", "mae"], eval_set=[(X_train, y_train), (X_test, y_test)], verbose=True)# 提取特征重要性thresholds = np.sort(model.feature_importances_)# 选择特征selection = SelectFromModel(model, threshold=thresholds[2], prefit=True)feature_idx = selection.get_support()print(feature_idx)# array([ True, True, True, False, False])selected_dataset = selection.transform(X_test)print(selected_dataset.shape)# (200, 3)