pandas – Logistic Regression with scikit-learn giving key error while printing output as : KeyError: ‘Logistic Regression’

plt.figure(figsize = (12,7))
ax = sns.barplot(x = models,y = accuracy_list)
plt.xlabel("Classifiers",fontsize = 15)
plt.ylabel("Accuracy (%)", fontsize =15)
for p in ax.patches:
    width = p.get_width()
    height = p.get_height()
    x= p.get_x()
    y = p.get_y()
    ax.annotate(f"{height} %",(x + width/2 , y + height*1.01),ha = "center")
plt.show()

while i’m runing the above code i got this error….

> plt.figure(figsize = (12,7))
ax = sns.barplot(x = models,y = accuracy_list)
plt.xlabel("Classifiers",fontsize = 15)
plt.ylabel("Accuracy (%)", fontsize =15)
for p in ax.patches:
    width = p.get_width()
    height = p.get_height()
    x= p.get_x()
    y = p.get_y()
    ax.annotate(f"{height} %",(x + width/2 , y + height*1.01),ha = "center")
plt.show()

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In [50], line 2
      1 plt.figure(figsize = (12,7))
----> 2 ax = sns.barplot(x = models,y = accuracy_list)
      3 plt.xlabel("Classifiers",fontsize = 15)
      4 plt.ylabel("Accuracy (%)", fontsize =15)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\seaborn\categorical.py:2755, in barplot(data, x, y, hue, order, hue_order, estimator, errorbar, n_boot, units, seed, orient, color, palette, saturation, width, errcolor, errwidth, capsize, dodge, ci, ax, **kwargs)
   2752 if estimator is len:
   2753     estimator = "size"
-> 2755 plotter = _BarPlotter(x, y, hue, data, order, hue_order,
   2756                       estimator, errorbar, n_boot, units, seed,
   2757                       orient, color, palette, saturation,
   2758                       width, errcolor, errwidth, capsize, dodge)
   2760 if ax is None:
   2761     ax = plt.gca()

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\seaborn\categorical.py:1530, in _BarPlotter.__init__(self, x, y, hue, data, order, hue_order, estimator, errorbar, n_boot, units, seed, orient, color, palette, saturation, width, errcolor, errwidth, capsize, dodge)
   1525 def __init__(self, x, y, hue, data, order, hue_order,
   1526              estimator, errorbar, n_boot, units, seed,
   1527              orient, color, palette, saturation, width,
   1528              errcolor, errwidth, capsize, dodge):
   1529     """Initialize the plotter."""
-> 1530     self.establish_variables(x, y, hue, data, orient,
   1531                              order, hue_order, units)
   1532     self.establish_colors(color, palette, saturation)
   1533     self.estimate_statistic(estimator, errorbar, n_boot, seed)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\seaborn\categorical.py:594, in _CategoricalPlotter.establish_variables(self, x, y, hue, data, orient, order, hue_order, units)
    591 group_names = categorical_order(groups, order)
    593 # Group the numeric data
--> 594 plot_data, value_label = self._group_longform(vals, groups,
    595                                               group_names)
    597 # Now handle the hue levels for nested ordering
    598 if hue is None:

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\seaborn\categorical.py:641, in _CategoricalPlotter._group_longform(self, vals, grouper, order)
    638     vals = pd.Series(vals, index=index)
    640 # Group the val data
--> 641 grouped_vals = vals.groupby(grouper)
    642 out_data = []
    643 for g i

n order:


File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\series.py:2076, in Series.groupby(self, by, axis, level, as_index, sort, group_keys, squeeze, observed, dropna)
   2073     raise TypeError("You have to supply one of 'by' and 'level'")
   2074 axis = self._get_axis_number(axis)
-> 2076 return SeriesGroupBy(
   2077     obj=self,
   2078     keys=by,
   2079     axis=axis,
   2080     level=level,
   2081     as_index=as_index,
   2082     sort=sort,
   2083     group_keys=group_keys,
   2084     squeeze=squeeze,
   2085     observed=observed,
   2086     dropna=dropna,
   2087 )

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\groupby\groupby.py:959, in GroupBy.__init__(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, squeeze, observed, mutated, dropna)
    956 if grouper is None:
    957     from pandas.core.groupby.grouper import get_grouper
--> 959     grouper, exclusions, obj = get_grouper(
    960         obj,
    961         keys,
    962         axis=axis,
    963         level=level,
    964         sort=sort,
    965         observed=observed,
    966         mutated=self.mutated,
    967         dropna=self.dropna,
    968     )
    970 self.obj = obj
    971 self.axis = obj._get_axis_number(axis)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\groupby\grouper.py:888, in get_grouper(obj, key, axis, level, sort, observed, mutated, validate, dropna)
    886         in_axis, level, gpr = False, gpr, None
    887     else:
--> 888         raise KeyError(gpr)
    889 elif isinstance(gpr, Grouper) and gpr.key is not None:
    890     # Add key to exclusions
    891     exclusions.add(gpr.key)

KeyError: 'Logistic Regression'

<Figure size 1200x700 with 0 Axes>

Read more here: Source link