0

我有数据框汽车。它的结构描述如下:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 91313 entries, 0 to 93099
Data columns (total 16 columns):
 #   Column           Non-Null Count  Dtype  
---  ------           --------------  -----  
 0   Manufacturer     91313 non-null  string 
 1   Model            91313 non-null  string 
 2   Year             91313 non-null  Int64  
 3   Category         91313 non-null  string 
 4   Mileage          91313 non-null  Int64  
 5   FuelType         91313 non-null  string 
 6   EngineVolume     91313 non-null  float64
 7   DriveWheels      91313 non-null  string 
 8   GearBox          91313 non-null  string 
 9   Doors            91313 non-null  string 
 10  Wheel            91313 non-null  string 
 11  Color            91313 non-null  string 
 12  InteriorColor    91313 non-null  string 
 13  LeatherInterior  91313 non-null  boolean
 14  Price            91313 non-null  Int64  
 15  Clearance        91313 non-null  boolean
dtypes: Int64(3), boolean(2), float64(1), string(10)
memory usage: 11.1 MB

我想制作一个使用 CatBoostRegressor 预测汽车价格的模型。我正在尝试这样:

train_dataset = cb.Pool(X_train, y_train) 
test_dataset = cb.Pool(X_test, y_test)
cat_features = ['Manufacturer','Model','Category','FuelType','DriveWheels','GearBox','Doors','Wheel','Color','InteriorColor','LeatherInterior','Clearance']
model = cb.CatBoostRegressor(loss_function = 'RMSE',eval_metric = 'R2',cat_features = cat_features)
grid = {'iterations': [250, 300, 400],
        'learning_rate': [0.1,0.2],
        'depth': [2, 4, 6, 8],
        'l2_leaf_reg': [0.2, 0.5, 1, 3],
        'cat_features' : cat_features
       }
model.grid_search(grid, train_dataset)

我也尝试将 cat_features 放入模型和网格中。但这两种情况都没有帮助。

TypeError                                 Traceback (most recent call last)
<ipython-input-34-2cb43214da9d> in <module>
----> 1 train_dataset = cb.Pool(X_train, y_train)
      2 test_dataset = cb.Pool(X_test, y_test)
      3 cat_features = ['Manufacturer','Model','Category','FuelType','DriveWheels','GearBox','Doors','Wheel','Color','InteriorColor','LeatherInterior','Clearance']
      4 model = cb.CatBoostRegressor(loss_function = 'RMSE',eval_metric = 'R2',cat_features = cat_features)
      5 grid = {'iterations': [250, 300, 400],

~\anaconda3\lib\site-packages\catboost\core.py in __init__(self, data, label, cat_features, text_features, embedding_features, column_description, pairs, delimiter, has_header, ignore_csv_quoting, weight, group_id, group_weight, subgroup_id, pairs_weight, baseline, feature_names, thread_count)
    586                     )
    587 
--> 588                 self._init(data, label, cat_features, text_features, embedding_features, pairs, weight, group_id, group_weight, subgroup_id, pairs_weight, baseline, feature_names, thread_count)
    589         super(Pool, self).__init__()
    590 

~\anaconda3\lib\site-packages\catboost\core.py in _init(self, data, label, cat_features, text_features, embedding_features, pairs, weight, group_id, group_weight, subgroup_id, pairs_weight, baseline, feature_names, thread_count)
   1100             baseline = np.reshape(baseline, (samples_count, -1))
   1101             self._check_baseline_shape(baseline, samples_count)
-> 1102         self._init_pool(data, label, cat_features, text_features, embedding_features, pairs, weight, group_id, group_weight, subgroup_id, pairs_weight, baseline, feature_names, thread_count)
   1103 
   1104 

_catboost.pyx in _catboost._PoolBase._init_pool()

_catboost.pyx in _catboost._PoolBase._init_pool()

_catboost.pyx in _catboost._PoolBase._init_features_order_layout_pool()

_catboost.pyx in _catboost._set_features_order_data_pd_data_frame()

TypeError: Cannot convert StringArray to numpy.ndarray

我该如何处理这个错误?

4

1 回答 1

0

如果您使用 中的功能名称,您还必须在参数cat_features中提供它们。features_name否则,提供分类特征的索引cat_features就足够了。

在你的情况下是:

cat_features = [0, 1, 3, 5, 7, 8, 9, 10, 11, 12, 13, 15]
于 2021-05-04T18:34:54.713 回答