'CatBoostRegressor' object has no attribute 'feature_name'

Hi,
Could someone help -me with this error?

Chapter: 12_gradient_boosting_machines
Notebook: 08_making_out_of_sample_predictions

for position in range(10):
params = get_cb_params(catboost_ic_avg,
                t=lookahead,
                best=position)

params = params.to_dict()

for p in ['max_depth', 'min_child_samples']:
    params[p] = int(params[p])
train_length = int(params.pop('train_length'))
test_length = int(params.pop('test_length'))
num_boost_round = int(params.pop('boost_rounds'))
params['task_type'] = 'CPU'

print(f'\nPosition: {position:02}')

# 1-year out-of-sample period
n_splits = int(YEAR / test_length)
cv = MultipleTimeSeriesCV(n_splits=n_splits,
                          test_period_length=test_length,
                          lookahead=lookahead,
                          train_period_length=train_length)

predictions = []
start = time()
for i, (train_idx, test_idx) in enumerate(cv.split(X=data), 1):
    print(i, end=' ', flush=True)
    train_set = catboost_data.slice(train_idx.tolist())

    model = CatBoostRegressor(**params)
    model.fit(X=train_set,
              verbose_eval=False)

    test_set = data.iloc[test_idx, :]
    y_test = test_set.loc[:, label].to_frame('y_test')
    y_pred = model.predict(test_set.loc[:, model.feature_name()])
    predictions.append(y_test.assign(prediction=y_pred))

if position == 0:
    test_predictions = (pd.concat(predictions)
                        .rename(columns={'prediction': position}))
else:
    test_predictions[position] = pd.concat(predictions).prediction

by_day = test_predictions.groupby(level='date')
for position in range(10):
    if position == 0:
        ic_by_day = by_day.apply(lambda x: spearmanr(x.y_test, x[position])[0]).to_frame()
    else:
        ic_by_day[position] = by_day.apply(lambda x: spearmanr(x.y_test, x[position])[0])
print(ic_by_day.describe())
test_predictions.to_hdf(store, f'catboost/test/{lookahead:02}')

AttributeError Traceback (most recent call last)
in
34 test_set = data.iloc[test_idx, :]
35 y_test = test_set.loc[:, label].to_frame(‘y_test’)
—> 36 y_pred = model.predict(test_set.loc[:, model.feature_name()])
37 predictions.append(y_test.assign(prediction=y_pred))
38

AttributeError: ‘CatBoostRegressor’ object has no attribute ‘feature_name’

Thanks

I tried change attribute from model.feature_name() to model.feature_names_(), but show follow error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-33-014f1234dd7f> in <module>
     34         test_set = data.iloc[test_idx, :]
     35         y_test = test_set.loc[:, label].to_frame('y_test')
---> 36         y_pred = model.predict(test_set.loc[:, model.feature_names_()])
     37         predictions.append(y_test.assign(prediction=y_pred))
     38 

TypeError: 'list' object is not callable

The attribute is simply feature_names_ and it’s not a method, hence the error. Just remove the parenthesis, I’ll update the code shortly. See catboost docs.

2 Likes