```python
from sklearn import metrics
import numpy as np
import sympy
from sympy.matrices import Matrix
import pandas as pd
import matplotlib.pyplot as plt

```

# 3-class




```python
x = np.array([[2, 0],
              [1, 1],
              [0, 2],
              [2, 0],
              [1, 2],
              [0, 0],
              [2, 0]])

df = pd.DataFrame(x, columns = ['y','yHat'])
df
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>y</th>
      <th>yHat</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>2</td>
      <td>0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1</td>
      <td>1</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0</td>
      <td>2</td>
    </tr>
    <tr>
      <th>3</th>
      <td>2</td>
      <td>0</td>
    </tr>
    <tr>
      <th>4</th>
      <td>1</td>
      <td>2</td>
    </tr>
    <tr>
      <th>5</th>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>6</th>
      <td>2</td>
      <td>0</td>
    </tr>
  </tbody>
</table>
</div>




```python
print(metrics.classification_report(df['y'], df['yHat']))
```

                  precision    recall  f1-score   support
    
               0       0.25      0.50      0.33         2
               1       1.00      0.50      0.67         2
               2       0.00      0.00      0.00         3
    
        accuracy                           0.29         7
       macro avg       0.42      0.33      0.33         7
    weighted avg       0.36      0.29      0.29         7
    



```python
metrics.precision_score( df['y'], df['yHat'], average=None)
```




    array([0.25, 1.  , 0.  ])




```python
metrics.precision_score( df['y'], df['yHat'], average="micro")
```




    0.2857142857142857




```python
metrics.precision_score(df['y'], df['yHat'], average="macro")
```




    0.4166666666666667




```python
metrics.precision_score(df['y'], df['yHat'], average="weighted")
```




    0.35714285714285715




```python
metrics.recall_score( df['y'], df['yHat'], average="micro")
```




    0.2857142857142857




```python
metrics.recall_score(df['y'], df['yHat'], average="macro")
```




    0.3333333333333333




```python
metrics.recall_score(df['y'], df['yHat'], average="weighted")
```




    0.2857142857142857




```python
metrics.f1_score( df['y'], df['yHat'], average="micro")
```




    0.2857142857142857




```python
metrics.f1_score(df['y'], df['yHat'], average="macro")
```




    0.3333333333333333




```python
metrics.f1_score(df['y'], df['yHat'], average="weighted")
```




    0.2857142857142857




```python

```


```python
metrics.accuracy_score(df['y'], df['yHat'])
```




    0.2857142857142857




```python
pd.crosstab(index=df['y'], columns=df['yHat'], margins=True)
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>yHat</th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>All</th>
    </tr>
    <tr>
      <th>y</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>0</td>
      <td>1</td>
      <td>2</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0</td>
      <td>1</td>
      <td>1</td>
      <td>2</td>
    </tr>
    <tr>
      <th>2</th>
      <td>3</td>
      <td>0</td>
      <td>0</td>
      <td>3</td>
    </tr>
    <tr>
      <th>All</th>
      <td>4</td>
      <td>1</td>
      <td>2</td>
      <td>7</td>
    </tr>
  </tbody>
</table>
</div>




```python
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(df['y'], df['yHat'])
```


```python
confusion_matrix( df['yHat'],df['y'],)
```




    array([[1, 0, 3],
           [0, 1, 0],
           [1, 1, 0]])




```python
from sklearn.metrics import ConfusionMatrixDisplay
ConfusionMatrixDisplay(cm, display_labels=[0,1,2]).plot()
```


```python
from sklearn.metrics import multilabel_confusion_matrix
mcm = multilabel_confusion_matrix(df['y'], df['yHat'], labels=[0, 1, 2])
mcm
```




    array([[[2, 3],
            [1, 1]],
    
           [[5, 0],
            [1, 1]],
    
           [[2, 2],
            [3, 0]]])




```python
multilabel_confusion_matrix(df['y'], df['yHat'])
```




    array([[[2, 3],
            [1, 1]],
    
           [[5, 0],
            [1, 1]],
    
           [[2, 2],
            [3, 0]]])




```python
#ConfusionMatrixDisplay(mcm, display_labels=[0,1,2]).plot()



# how to plot it
# https://stackoverflow.com/questions/62722416/plot-confusion-matrix-for-multilabel-classifcation-python
```


```python
from math import sqrt

def factorize_2d(value):
  res = []
  for x in range(1, int(sqrt(value)+1)):
    if not (value % x):
      res.append([x,value//x])
  return res

factorize_2d(24)
```




    [[1, 24], [2, 12], [3, 8], [4, 6]]




```python

def plot_mcm(mcm):
  n_classes = len(mcm)
  maybe_shapes = factorize_2d(n_classes)
  shape = maybe_shapes[0]
  for s in maybe_shapes:
    if (s[0] + s[1]) < (shape[0] + shape[1]):
      shape = s
  #proportions = 5*np.array(shape)
  proportions = (15, 6)
  fig, axes = plt.subplots(*shape, figsize=proportions)
  if shape[0] != 1 and shape[1] != 1:
    axes = [item for sublist in axes for item in sublist]
  for matrix, ax, i_class in zip(mcm, axes, range(n_classes)):
    ConfusionMatrixDisplay(matrix, display_labels=["others", i_class]).plot(ax=ax)
  
  plt.tight_layout()
  plt.show()

plot_mcm(mcm)
```


```python

```

#More classes


```python
num_classes = 3
y_true = np.random.randint(0, num_classes, 10)
y_pred = np.random.randint(0, num_classes, 10)
df = pd.DataFrame(data={"y_true": y_true, "y_pred": y_pred})
df.transpose()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      <th>4</th>
      <th>5</th>
      <th>6</th>
      <th>7</th>
      <th>8</th>
      <th>9</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>y_true</th>
      <td>0</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>2</td>
      <td>1</td>
      <td>1</td>
      <td>0</td>
      <td>1</td>
    </tr>
    <tr>
      <th>y_pred</th>
      <td>1</td>
      <td>1</td>
      <td>0</td>
      <td>1</td>
      <td>2</td>
      <td>2</td>
      <td>1</td>
      <td>1</td>
      <td>2</td>
      <td>0</td>
    </tr>
  </tbody>
</table>
</div>




```python
cm = confusion_matrix(y_true, y_pred)
ConfusionMatrixDisplay(cm, display_labels=range(num_classes)).plot()
```


```python
mcm = multilabel_confusion_matrix(y_true, y_pred)
```


```python
plot_mcm(mcm)
```


```python
print(metrics.classification_report( y_true, y_pred))
```

                  precision    recall  f1-score   support
    
               0       0.00      0.00      0.00         2
               1       0.80      0.57      0.67         7
               2       0.33      1.00      0.50         1
    
        accuracy                           0.50        10
       macro avg       0.38      0.52      0.39        10
    weighted avg       0.59      0.50      0.52        10
    


d24947792d5557e8f6b4a9258984bcc4f682a44e
ipynb
Jupyter Notebook
Chapter 2/python/classification metrics/3-class metrics.ipynb
borisgarbuzov/schulich_data_science_1
fd05ec2bbbe35408f90ebfcf10bb4ca588e7871c
MIT
Chapter 2/python/classification metrics/3-class metrics.ipynb
borisgarbuzov/schulich_data_science_1
fd05ec2bbbe35408f90ebfcf10bb4ca588e7871c
MIT
Chapter 2/python/classification metrics/3-class metrics.ipynb
borisgarbuzov/schulich_data_science_1
fd05ec2bbbe35408f90ebfcf10bb4ca588e7871c
MIT
2020-10-25T05:26:50.000Z
2021-07-07T08:25:58.000Z
Qwen/Qwen-72B
1. YES
2. YES