```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
from sklearn.datasets import make_classification
```


```python
y_true = np.round(np.random.sample(10))
y_pred = np.round(np.random.sample(10))
equals = 0
for true, pred in zip(y_true, y_pred):
  if true == pred: equals +=1
equals
```




    5




```python
# Alternative way of 0-1 sample generation
n, p = 1, .5  # number of trials, probability of each trial
s = np.random.binomial(n, p, size=10)
s
```




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



Try ours from the quiz



```python
y_true = np.array([1, 1, 0, 1, 1, 1])
y_pred = np.array([1, 1, 1, 1, 0, 0])

```


```python
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>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>y_true</th>
      <td>1</td>
      <td>1</td>
      <td>0</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
    </tr>
    <tr>
      <th>y_pred</th>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
    </tr>
  </tbody>
</table>
</div>




```python
allCount = len(y_true)
oneCount = int(np.sum(y_true))
zeroCount = allCount - oneCount
allCount, oneCount, zeroCount
```




    (6, 5, 1)




```python

```


```python
cm = metrics.confusion_matrix(y_true, y_pred)
cm
```




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




```python
display = metrics.ConfusionMatrixDisplay(cm, ["0's or negative", "1's or positive"])
display.plot()
plt.show()
```




```python
metrics.accuracy_score(y_true, y_pred)
```




    0.5



## By hand, in Python terminology (non-statistical)
Basic cells of confusion matrix. 


```python
(TN, FP), (FN, TP) = cm
print("TN =", TN)
print("FP =", FP)
print("FN =", FN)
print("TP =", TP)
```

    TN = 0
    FP = 1
    FN = 2
    TP = 3


Marginals


```python

```


```python
diagonal = TP +  TN
print("diagonal =", diagonal)
all = TP + TN + FP + FN
print("all =", all)
accByFormula = diagonal / all
print("accByFormula =", accByFormula)
```

    diagonal = 3
    all = 6
    accByFormula = 0.5





```python
metrics.precision_score(y_true, y_pred)
```




    0.75






```python
metrics.recall_score(y_true, y_pred)
```




    0.6




```python
%%script false
plt.plot(*metrics.precision_recall_curve(y_true, y_pred))
plt.show()
```




```python
metrics.f1_score(y_true, y_pred)
```




    0.6666666666666665




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

                  precision    recall  f1-score   support
    
               0       0.00      0.00      0.00         1
               1       0.75      0.60      0.67         5
    
        accuracy                           0.50         6
       macro avg       0.38      0.30      0.33         6
    weighted avg       0.62      0.50      0.56         6
    



```python

```

------------------


```python
y_true = np.array([1, 1, 0, 1, 1, 1])
y_pred = np.array([1, 1, 1, 1, 0, 0])
```


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

                  precision    recall  f1-score   support
    
               0       0.00      0.00      0.00         1
               1       0.75      0.60      0.67         5
    
        accuracy                           0.50         6
       macro avg       0.38      0.30      0.33         6
    weighted avg       0.62      0.50      0.56         6
    


--------------------------


```python
y_true = np.array([1, 1, 0, 1, 1, 1])
y_pred = np.array([1, 0, 0, 1, 1, 1])
```


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

                  precision    recall  f1-score   support
    
               0       0.50      1.00      0.67         1
               1       1.00      0.80      0.89         5
    
        accuracy                           0.83         6
       macro avg       0.75      0.90      0.78         6
    weighted avg       0.92      0.83      0.85         6
    



```python

```

#ROC


```python
import matplotlib.pyplot as plt  
from sklearn import datasets, metrics, model_selection, svm
X, y = datasets.make_classification(random_state=0)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, random_state=0)
clf = svm.SVC(random_state=0)
clf.fit(X_train, y_train)

metrics.plot_roc_curve(clf, X_test, y_test)  
plt.show()  
```


```python
import numpy as np
from sklearn import metrics
#y = np.array([1, 1, 2, 2])
y = np.array([0, 0, 1, 1])
scores = np.array([0.1, 0.4, 0.35, 0.8])
fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=1)
fpr

```




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




```python
tpr
```




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




```python
thresholds
```




    array([1.8 , 0.8 , 0.4 , 0.35, 0.1 ])




```python
plt.plot(fpr, tpr)
```


```python
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
ax.plot(fpr, tpr, thresholds)
```


```python
from plotly import graph_objects as go
```


```python
line = go.Scatter3d(x=fpr, y=tpr, z=thresholds, mode="lines")
fig = go.Figure(data=line)
fig.show()
```


<html>
<head><meta charset="utf-8" /></head>
<body>
    <div>
            
                
            
            <div id="4bd06129-5cfa-45a8-be3d-1479a02fe096" class="plotly-graph-div" style="height:525px; width:100%;"></div>
            
        </div>
</body>
</html>


8d7cd029873dd6ddcee911c5d1abca95f0834ed6
ipynb
Jupyter Notebook
Chapter 2/python/classification metrics/2-class metrics.ipynb
borisgarbuzov/schulich_data_science_1
fd05ec2bbbe35408f90ebfcf10bb4ca588e7871c
MIT
Chapter 2/python/classification metrics/2-class metrics.ipynb
borisgarbuzov/schulich_data_science_1
fd05ec2bbbe35408f90ebfcf10bb4ca588e7871c
MIT
Chapter 2/python/classification metrics/2-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