-2

Ideal output table format

SR=pd.DataFrame([['Linear Regression', 0.9533333333333334, 0.9747081712062257, 0.8255813953488372],['Ridge Classifier', 0.905, 0.9980544747081712, 0.3488372093023256],     ['Decision Tree Classifier',0.9883333333333333,0.9922178988326849,0.9651162790697675],     ['Random Forest', 0.9916666666666667, 0.9980544747081712, 0.9534883720930233],['XG Boost', 0.9916666666666667, 0.9980544747081712, 0.9534883720930233],['Neural Network', 1.0, 1.0, 1.0]], columns = ['Model', 'Accuracy','Sensitivity','Specificity'])

How do I create the barchart attached (was made on excel) using seaborn with the SR dataframe that I have?

Zephyr
  • 11,891
  • 53
  • 45
  • 80
  • From the [duplicate](https://stackoverflow.com/questions/68469741/how-to-create-grouped-bar-plots-in-a-single-figure-from-a-wide-dataframe): `SR.set_index('Model', inplace=True)` and `SR.plot(kind='bar')` – Trenton McKinney Aug 09 '21 at 16:10

1 Answers1

1

First of all, you should re-shape your dataframe with pandas.melt:

SR = pd.melt(frame = SR,
             id_vars = 'Model',
             var_name = 'Statistic',
             value_name = 'value')

So you get:

                       Model    Statistic     value
0          Linear Regression     Accuracy  0.953333
1           Ridge Classifier     Accuracy  0.905000
2   Decision Tree Classifier     Accuracy  0.988333
3              Random Forest     Accuracy  0.991667
4                   XG Boost     Accuracy  0.991667
5             Neural Network     Accuracy  1.000000
6          Linear Regression  Sensitivity  0.974708
7           Ridge Classifier  Sensitivity  0.998054
8   Decision Tree Classifier  Sensitivity  0.992218
9              Random Forest  Sensitivity  0.998054
10                  XG Boost  Sensitivity  0.998054
11            Neural Network  Sensitivity  1.000000
12         Linear Regression  Specificity  0.825581
13          Ridge Classifier  Specificity  0.348837
14  Decision Tree Classifier  Specificity  0.965116
15             Random Forest  Specificity  0.953488
16                  XG Boost  Specificity  0.953488
17            Neural Network  Specificity  1.000000

Then you can plot the new dataframe with:

sns.barplot(ax = ax, data = SR, x = 'Model', y = 'value', hue = 'Statistic')

Complete Code

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

SR=pd.DataFrame([['Linear Regression', 0.9533333333333334, 0.9747081712062257, 0.8255813953488372],['Ridge Classifier', 0.905, 0.9980544747081712, 0.3488372093023256],     ['Decision Tree Classifier',0.9883333333333333,0.9922178988326849,0.9651162790697675],     ['Random Forest', 0.9916666666666667, 0.9980544747081712, 0.9534883720930233],['XG Boost', 0.9916666666666667, 0.9980544747081712, 0.9534883720930233],['Neural Network', 1.0, 1.0, 1.0]], columns = ['Model', 'Accuracy','Sensitivity','Specificity'])

SR = pd.melt(frame = SR,
             id_vars = 'Model',
             var_name = 'Statistic',
             value_name = 'value')

fig, ax = plt.subplots(figsize = (12, 6))

sns.barplot(ax = ax, data = SR, x = 'Model', y = 'value', hue = 'Statistic')

plt.show()

enter image description here

Zephyr
  • 11,891
  • 53
  • 45
  • 80