AttributeError: объект RFECV не имеет атрибута Grid_scores_.Python

Программы на Python
Ответить
Anonymous
 AttributeError: объект RFECV не имеет атрибута Grid_scores_.

Сообщение Anonymous »

Проблема, с которой я столкнулся:
Постоянное получение этой ошибки AttributeError: объект 'RFECV' не имеет атрибута 'grid_scores_'. Код приведен ниже:
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import RFECV
from sklearn.model_selection import StratifiedKFold
import warnings
warnings.filterwarnings('ignore')

rfc = RandomForestClassifier(random_state=101)
rfecv = RFECV(estimator=rfc, step=1, cv=StratifiedKFold(5), scoring='accuracy')
rfecv.fit(X_train, y_train)

print('Optimal number of features: {}'.format(rfecv.n_features_))

plt.figure(figsize=(16, 9))
plt.title('Recursive Feature Elimination with Cross-Validation', fontsize=18, fontweight='bold', pad=20)
plt.xlabel('Number of features selected', fontsize=14, labelpad=20)
plt.ylabel('% Correct Classification', fontsize=14, labelpad=20)
plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_, color='#303F9F', linewidth=3)
plt.show()

Ошибки:
AttributeError Traceback (most recent call last)
Cell In[36], line 5
3 plt.xlabel('Number of features selected', fontsize=14, labelpad=20)
4 plt.ylabel('% Correct Classification', fontsize=14, labelpad=20)
----> 5 plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_, color='#303F9F', linewidth=3)
6 plt.show()

AttributeError: 'RFECV' object has no attribute 'grid_scores_'

Что я пробовал
Я пробовал заменить grid_scores_ на cv_results_, как кто-то рекомендовал в Quora , но выскакивает с новой ошибкой. Согласно документации sklearn.feature_selection.RFECV, такого атрибута, как grid_scores_, нет, но код, который я использую для изучения, содержит этот атрибут.
plt.figure(figsize=(16, 9))
plt.title('Recursive Feature Elimination with Cross-Validation', fontsize=18, fontweight='bold', pad=20)
plt.xlabel('Number of features selected', fontsize=14, labelpad=20)
plt.ylabel('% Correct Classification', fontsize=14, labelpad=20)
plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_, color='#303F9F', linewidth=3)
plt.show()

Ошибки:
TypeError Traceback (most recent call last)
Cell In[37], line 5
3 plt.xlabel('Number of features selected', fontsize=14, labelpad=20)
4 plt.ylabel('% Correct Classification', fontsize=14, labelpad=20)
----> 5 plt.plot(range(1, len(rfecv.cv_results_) + 1), rfecv.cv_results_, color='#303F9F', linewidth=3)
6 plt.show()

File ~\anaconda3\Lib\site-packages\matplotlib\pyplot.py:2812, in plot(scalex, scaley, data, *args, **kwargs)
2810 @_copy_docstring_and_deprecators(Axes.plot)
2811 def plot(*args, scalex=True, scaley=True, data=None, **kwargs):
-> 2812 return gca().plot(
2813 *args, scalex=scalex, scaley=scaley,
2814 **({"data": data} if data is not None else {}), **kwargs)

File ~\anaconda3\Lib\site-packages\matplotlib\axes\_axes.py:1688, in Axes.plot(self, scalex, scaley, data, *args, **kwargs)
1445 """
1446 Plot y versus x as lines and/or markers.
1447
(...)
1685 (``'green'``) or hex strings (``'#008000'``).
1686 """
1687 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1688 lines = [*self._get_lines(*args, data=data, **kwargs)]
1689 for line in lines:
1690 self.add_line(line)

File ~\anaconda3\Lib\site-packages\matplotlib\axes\_base.py:311, in _process_plot_var_args.__call__(self, data, *args, **kwargs)
309 this += args[0],
310 args = args[1:]
--> 311 yield from self._plot_args(
312 this, kwargs, ambiguous_fmt_datakey=ambiguous_fmt_datakey)

File ~\anaconda3\Lib\site-packages\matplotlib\axes\_base.py:501, in _process_plot_var_args._plot_args(self, tup, kwargs, return_kwargs, ambiguous_fmt_datakey)
499 self.axes.xaxis.update_units(x)
500 if self.axes.yaxis is not None:
--> 501 self.axes.yaxis.update_units(y)
503 if x.shape[0] != y.shape[0]:
504 raise ValueError(f"x and y must have same first dimension, but "
505 f"have shapes {x.shape} and {y.shape}")

File ~\anaconda3\Lib\site-packages\matplotlib\axis.py:1675, in Axis.update_units(self, data)
1673 neednew = self.converter != converter
1674 self.converter = converter
-> 1675 default = self.converter.default_units(data, self)
1676 if default is not None and self.units is None:
1677 self.set_units(default)

File ~\anaconda3\Lib\site-packages\matplotlib\category.py:105, in StrCategoryConverter.default_units(data, axis)
103 # the conversion call stack is default_units -> axis_info -> convert
104 if axis.units is None:
--> 105 axis.set_units(UnitData(data))
106 else:
107 axis.units.update(data)

File ~\anaconda3\Lib\site-packages\matplotlib\category.py:181, in UnitData.__init__(self, data)
179 self._counter = itertools.count()
180 if data is not None:
--> 181 self.update(data)

File ~\anaconda3\Lib\site-packages\matplotlib\category.py:214, in UnitData.update(self, data)
212 # check if convertible to number:
213 convertible = True
--> 214 for val in OrderedDict.fromkeys(data):
215 # OrderedDict just iterates over unique values in data.
216 _api.check_isinstance((str, bytes), value=val)
217 if convertible:
218 # this will only be called so long as convertible is True.

TypeError: unhashable type: 'dict'


Подробнее здесь: https://stackoverflow.com/questions/768 ... rid-scores
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»