Galle, LK--:--5/6 sites live

Accuracy score and Confusion Matrix

Accuracy එකට විතරක් රැවටෙන්න පුළුවන් (imbalanced-dataset පාඨම මතක ඇතිනේ). Confusion matrix එකෙන් model එකක් කරන වැරදි කුමන ආකාරයද කියලා හරියටම පෙන්නනවා — වැරදි කවදාවත් cost එකෙන් සමාන නෑ.

Accuracy alone can lie to you (remember the imbalanced-dataset lesson). A confusion matrix shows exactly which mistakes a model makes — and mistakes are rarely equal in cost.

16 May 20266 min readSelf Guide · Stage 16#Evaluating & Choosing Models#Model Evaluation#Machine learning

Lesson 46 of 64 · Part 4 · Evaluating & Choosing Models · Model Evaluation · bilingual notes (English + සිංහල)

Why this matters. Accuracy alone can lie to you (remember the imbalanced-dataset lesson). A confusion matrix shows exactly which mistakes a model makes — and mistakes are rarely equal in cost.

සිංහලෙන්: Accuracy එකට විතරක් රැවටෙන්න පුළුවන් (imbalanced-dataset පාඨම මතක ඇතිනේ). Confusion matrix එකෙන් model එකක් කරන වැරදි කුමන ආකාරයද කියලා හරියටම පෙන්නනවා — වැරදි කවදාවත් cost එකෙන් සමාන නෑ.

What you'll use this for. Before you trust any classifier enough to deploy it, you look at its confusion matrix — this lesson is what makes that reflex automatic.

සිංහලෙන්: Classifier එකක් deploy කරන්න විශ්වාස කරන්නත් කලින්, ඒකේ confusion matrix එක බලනවා — ඒ reflex එක automatic කරන්නෙ මේ පාඩම.

Key principles

  • False positives and false negatives are different failures with different real-world costs. In a cancer-screening model, a false negative (missing real cancer) is far more dangerous than a false positive (an unnecessary follow-up test) — the confusion matrix is what makes that visible.
    False positives සහ false negatives, real-world cost එකෙන් වෙනස් failures දෙකක්

Walkthrough

Importing the Dependencies

ExamplePython
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

Data Collection and Processing

ExamplePython
# loading the csv data to a Pandas DataFrame
heart_data = pd.read_csv('heart.csv')
ExamplePython
# print first 5 rows of the dataset
heart_data.head()
agesexcptrestbpscholfbsrestecg…
0631314523310…
1371213025001…
2410113020400…
3561112023601…
4570012035401…
ExamplePython
# print last 5 rows of the dataset
heart_data.tail()
agesexcptrestbpscholfbsrestecg…
298570014024101…
299451311026401…
300681014419311…
301571013013101…
302570113023600…
ExamplePython
# number of rows and columns in the dataset
heart_data.shape

Output

Output
(303, 14)
ExamplePython
# getting some info about the data
heart_data.info()

Output

Output
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 303 entries, 0 to 302
Data columns (total 14 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   age       303 non-null    int64  
 1   sex       303 non-null    int64  
 2   cp        303 non-null    int64  
 3   trestbps  303 non-null    int64  
 4   chol      303 non-null    int64  
 5   fbs       303 non-null    int64  
 6   restecg   303 non-null    int64  
 7   thalach   303 non-null    int64  
 8   exang     303 non-null    int64  
 9   oldpeak   303 non-null    float64
 10  slope     303 non-null    int64  
 11  ca        303 non-null    int64  
 12  thal      303 non-null    int64  
… (3 more lines)
ExamplePython
# checking for missing values
heart_data.isnull().sum()

Output

Output
age         0
sex         0
cp          0
trestbps    0
chol        0
fbs         0
restecg     0
thalach     0
exang       0
oldpeak     0
slope       0
ca          0
thal        0
target      0
dtype: int64
ExamplePython
# statistical measures about the data
heart_data.describe()
agesexcptrestbpscholfbsrestecg…
count303.000000303.000000303.000000303.000000303.000000303.000000303.000000…
mean54.3663370.6831680.966997131.623762246.2640260.1485150.528053…
std9.0821010.4660111.03205217.53814351.8307510.3561980.525860…
min29.0000000.0000000.00000094.000000126.0000000.0000000.000000…
25%47.5000000.0000000.000000120.000000211.0000000.0000000.000000…
50%55.0000001.0000001.000000130.000000240.0000000.0000001.000000…

… 2 more rows

ExamplePython
# checking the distribution of Target Variable
heart_data['target'].value_counts()

Output

Output
1    165
0    138
Name: target, dtype: int64

1 --> Defective Heart

0 --> Healthy Heart

Splitting the Features and Target

ExamplePython
X = heart_data.drop(columns='target', axis=1)
Y = heart_data['target']
ExamplePython
print(X)

Output

Output
     age  sex  cp  trestbps  chol  fbs  restecg  thalach  exang  oldpeak  \
0     63    1   3       145   233    1        0      150      0      2.3   
1     37    1   2       130   250    0        1      187      0      3.5   
2     41    0   1       130   204    0        0      172      0      1.4   
3     56    1   1       120   236    0        1      178      0      0.8   
4     57    0   0       120   354    0        1      163      1      0.6   
..   ...  ...  ..       ...   ...  ...      ...      ...    ...      ...   
298   57    0   0       140   241    0        1      123      1      0.2   
299   45    1   3       110   264    0        1      132      0      1.2   
300   68    1   0       144   193    1        1      141      0      3.4   
301   57    1   0       130   131    0        1      115      1      1.2   
302   57    0   1       130   236    0        0      174      0      0.0   

     slope  ca  thal  
0        0   0     1  
1        0   0     2  
2        2   0     2  
3        2   0     2  
… (9 more lines)
ExamplePython
print(Y)

Output

Output
0      1
1      1
2      1
3      1
4      1
      ..
298    0
299    0
300    0
301    0
302    0
Name: target, Length: 303, dtype: int64

Splitting the Data into Training data & Test Data

ExamplePython
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, stratify=Y, random_state=2)
ExamplePython
print(X.shape, X_train.shape, X_test.shape)

Output

Output
(303, 13) (242, 13) (61, 13)

Model Training

ExamplePython
model = LogisticRegression(max_iter=1000)
ExamplePython
# training the LogisticRegression model with Training data
model.fit(X_train, Y_train)

Output

Output
LogisticRegression(max_iter=1000)

Model Evaluation

Accuracy Score

ExamplePython
from sklearn.metrics import accuracy_score
ExamplePython
# accuracy on training data
X_train_prediction = model.predict(X_train)
training_data_accuracy = accuracy_score(Y_train, X_train_prediction)
print(training_data_accuracy)

Output

Output
0.8553719008264463
ExamplePython
print('Accuracy on Training data : ', round(training_data_accuracy*100, 2), '%')

Output

Output
Accuracy on Training data :  85.54 %
ExamplePython
# accuracy on test data
X_test_prediction = model.predict(X_test)
test_data_accuracy = accuracy_score(Y_test, X_test_prediction)
print(test_data_accuracy)

Output

Output
0.8032786885245902
ExamplePython
print('Accuracy on Test data : ', round(test_data_accuracy*100, 2), '%')

Output

Output
Accuracy on Test data :  80.33 %

Confuaion Matrix

ExamplePython
from sklearn.metrics import confusion_matrix
ExamplePython
cf_matrix = confusion_matrix(Y_test, X_test_prediction)

print(cf_matrix)

Output

Output
[[22  6]
 [ 6 27]]
ExamplePython
tn, fp, fn, tp = cf_matrix.ravel()

print(tn, fp, fn, tp)

Output

Output
22 6 6 27
ExamplePython
import seaborn as sns
sns.heatmap(cf_matrix, annot=True)

Output

Output
<matplotlib.axes._subplots.AxesSubplot at 0x7f2e02c0fcd0>

Accuracy score and Confusion Matrix — figure 1

confusion matrix for multi class classification: https://colab.research.google.com/drive/17FViiMJbJXQ5s9GOW94emoHcCKUcYr3l?usp=sharing


Keep going: ← Lesson 45 · Lesson 47 → · Track it in the Self Guide