def confusion_matrix[pred,original]: #pass predicted and original labels to this function
matrix=np.zeros[[2,2]] # form an empty matric of 2x2
for i in range[len[pred]]: #the confusion matrix is for 2 classes: 1,0
#1=positive, 0=negative
if int[pred[i]]==1 and int[original[i]]==0:
matrix[0,0]+=1 #True Positives
elif int[pred[i]]==-1 and int[original[i]]==1:
matrix[0,1]+=1 #False Positives
elif int[pred[i]]==0 and int[original[i]]==1:
matrix[1,0]+=1 #False Negatives
elif int[pred[i]]==0 and int[original[i]]==0:
matrix[1,1]+=1 #True Negatives
precision=matrix[0,0]/[matrix[0,0]+matrix[0,1]]
print["Precision:",precision]
recall=matrix[0,0]/[matrix[0,0]+matrix[1,0]]
print["Recall:",recall]
specificity=matrix[1,1]/[matrix[0,1]+matrix[1,1]]
print["Specificity:",specificity]
negative_pred_value=matrix[1,1]/[matrix[1,0]+matrix[1,1]]
print["Negative Predicted Value:",negative_pred_value]
f1=2[precision*recall]/[precision+recall]
print["F1 score:",f1]
#the above code adds up the frequencies of the tps,tns,fps,fns and a matrix is formed
return matrix
Ma trận nhầm lẫn là một cách để hình dung có bao nhiêu mẫu từ mỗi nhãn được dự đoán chính xác. Vẻ đẹp của ma trận nhầm lẫn là nó thực sự cho phép chúng ta xem mô hình thất bại ở đâu và mô hình thành công ở đâu, đặc biệt là khi các nhãn bị mất cân bằng. Nói cách khác, chúng ta có thể nhìn xa hơn độ chính xác của mô hình.
P.S. Một số người sử dụng các giá trị dự đoán trên các hàng và các giá trị thực trên các cột, chỉ là chuyển vị của ma trận này. Một số người bắt đầu bởi lớp tiêu cực trước tiên sau đó là lớp tích cực. Đây chỉ là những cách khác nhau để vẽ ma trận nhầm lẫn, và tất cả đều truyền đạt cùng một điều.
Sai dương [lỗi loại 1] so với các tiêu cực sai [lỗi loại 2] vs False Negatives [FN-Type 2 error]
nguồn
Ma trận nhầm lẫn trong Python
Hãy thử tạo một ma trận nhầm lẫn trong Python
In [1]:
import random import numpy as np
In [4]:
# first 50 values are positive-labels [1], second 50 values are negative-labels [0] actual_values = [1] * 50 + [0] * 50 predicted_values = random.choices[[0, 1], k=100] # randomly generate 0 and 1 labels predicted_values[0:5]
Chúng ta có thể tính toán sau đó mỗi trong số 4 kết quả có thể có trong ma trận nhầm lẫn bằng cách so sánh từng giá trị trong
fp = 0 fn = 0 tp = 0 tn = 0 for actual_value, predicted_value in zip[actual_values, predicted_values]: # let's first see if it's a true [t] or false prediction [f] if predicted_value == actual_value: # t? if predicted_value == 1: # tp tp += 1 else: # tn tn += 1 else: # f? if predicted_value == 1: # fp fp += 1 else: # fn fn += 1 our_confusion_matrix = [ [tn, fp], [fn, tp] ] # we convert it to numpy array to be printed properly as a matrix our_confusion_matrix = np.array[our_confusion_matrix] our_confusion_matrix0 với giá trị tương ứng của nó trong
fp = 0 fn = 0 tp = 0 tn = 0 for actual_value, predicted_value in zip[actual_values, predicted_values]: # let's first see if it's a true [t] or false prediction [f] if predicted_value == actual_value: # t? if predicted_value == 1: # tp tp += 1 else: # tn tn += 1 else: # f? if predicted_value == 1: # fp fp += 1 else: # fn fn += 1 our_confusion_matrix = [ [tn, fp], [fn, tp] ] # we convert it to numpy array to be printed properly as a matrix our_confusion_matrix = np.array[our_confusion_matrix] our_confusion_matrix1
In [5]:
fp = 0 fn = 0 tp = 0 tn = 0 for actual_value, predicted_value in zip[actual_values, predicted_values]: # let's first see if it's a true [t] or false prediction [f] if predicted_value == actual_value: # t? if predicted_value == 1: # tp tp += 1 else: # tn tn += 1 else: # f? if predicted_value == 1: # fp fp += 1 else: # fn fn += 1 our_confusion_matrix = [ [tn, fp], [fn, tp] ] # we convert it to numpy array to be printed properly as a matrix our_confusion_matrix = np.array[our_confusion_matrix] our_confusion_matrix
Out[5]:
array[[[24, 26], [24, 26]]]
Chúng ta có thể nhận được cùng một ma trận nhầm lẫn bằng hàm
fp = 0 fn = 0 tp = 0 tn = 0 for actual_value, predicted_value in zip[actual_values, predicted_values]: # let's first see if it's a true [t] or false prediction [f] if predicted_value == actual_value: # t? if predicted_value == 1: # tp tp += 1 else: # tn tn += 1 else: # f? if predicted_value == 1: # fp fp += 1 else: # fn fn += 1 our_confusion_matrix = [ [tn, fp], [fn, tp] ] # we convert it to numpy array to be printed properly as a matrix our_confusion_matrix = np.array[our_confusion_matrix] our_confusion_matrix2
In [6]:
from sklearn.metrics import confusion_matrix
In [7]:
confusion_matrix[actual_values, predicted_values]
Out[7]:
array[[[24, 26], [24, 26]]]
Sự chính xác
Chúng ta dự đoán chính xác có bao nhiêu giá trị? Có bao nhiêu dự đoán thực sự trong số tất cả các mẫu?
In [6]:
accuracy = [tp + tn]/100 accuracy
In [7]:
# or from sklearn.metrics import accuracy_score accuracy_score[actual_values, predicted_values]
Chính xác so với nhớ lại
Độ chính xác
Độ chính xác tính toán tỷ lệ phần trăm của số lần dự đoán là chính xác trong tổng số dự đoán được đưa ra. Ví dụ - nếu bạn dự đoán rằng 100 bệnh nhân sẽ bắt Covid -19, nhưng chỉ có 90 bệnh nhân thực sự có Covid, thì độ chính xác của bạn là 90%. Vì vậy, trong tất cả các tích cực được dự đoán [tích cực thực sự và dương tính giả] có bao nhiêu thực sự là tích cực thực sự [TP]?
In [8]:
import random import numpy as np0
In [9]:
import random import numpy as np1
In [10]:
import random import numpy as np2
In [11]:
import random import numpy as np3
Hồi tưởng
Trong số tất cả các mẫu tích cực thực tế, bạn đã phát hiện bao nhiêu mẫu? Ví dụ, nếu có 100 bệnh nhân Covid-19 và tổng cộng bạn chỉ dự đoán 50 người trong số họ bị nhiễm bệnh [dương tính], vì vậy việc thu hồi của bạn là 50%. Vì vậy, trong tất cả các tích cực thực tế [TP và FN], có bao nhiêu được dự đoán là tích cực [TP].detect? For example, if there are 100 covid-19 patients, and in total you predicted only 50 of them as infected [positive], so your recall is 50%. So out of all actual positives [tp and fn], how many are predicted to be positive [tp].
In [12]:
import random import numpy as np4
In [13]:
import random import numpy as np5
In [14]:
import random import numpy as np6
In [15]:
import random import numpy as np7
Tầm quan trọng của độ chính xác và thu hồi
Giả sử tập dữ liệu của bạn chỉ có 10 mẫu dương và 90 mẫu âm. Nếu bạn sử dụng một trình phân loại phân loại mọi thứ là tiêu cực, độ chính xác của nó sẽ là 90%, điều này là sai lệch. Nhưng phân loại thực sự khá ngu ngốc! Vì vậy, hãy tính toán độ chính xác và thu hồi cho một mô hình như vậy
In [16]:
import random import numpy as np8
import random import numpy as np9
# first 50 values are positive-labels [1], second 50 values are negative-labels [0] actual_values = [1] * 50 + [0] * 50 predicted_values = random.choices[[0, 1], k=100] # randomly generate 0 and 1 labels predicted_values[0:5]0
Sklearn đang cảnh báo chúng tôi về một bộ phận không? Đó là ở đâu. Đó là trong độ chính xác của lớp tích cực. Chúng ta nên chia rẽ bởi tất cả các tích cực dự đoán, nhưng mô hình không đưa ra dự đoán tích cực, vì vậy đó là số không! Quan trọng hơn, việc thu hồi tích cực cũng bằng không, bởi vì mô hình không phát hiện bất kỳ mẫu tích cực nào, vì nó được phân loại một cách ngây thơ mọi thứ là tiêu cực.detect any of the positive samples, as it is naively classifying everything as negative.
F1-score
Để thống nhất độ chính xác và thu hồi thành một biện pháp, chúng tôi lấy ý nghĩa hài hòa của chúng, được gọi là điểm F1
In [17]:
# first 50 values are positive-labels [1], second 50 values are negative-labels [0] actual_values = [1] * 50 + [0] * 50 predicted_values = random.choices[[0, 1], k=100] # randomly generate 0 and 1 labels predicted_values[0:5]1
# first 50 values are positive-labels [1], second 50 values are negative-labels [0] actual_values = [1] * 50 + [0] * 50 predicted_values = random.choices[[0, 1], k=100] # randomly generate 0 and 1 labels predicted_values[0:5]2
In [18]:
# first 50 values are positive-labels [1], second 50 values are negative-labels [0] actual_values = [1] * 50 + [0] * 50 predicted_values = random.choices[[0, 1], k=100] # randomly generate 0 and 1 labels predicted_values[0:5]3
In [19]:
# first 50 values are positive-labels [1], second 50 values are negative-labels [0] actual_values = [1] * 50 + [0] * 50 predicted_values = random.choices[[0, 1], k=100] # randomly generate 0 and 1 labels predicted_values[0:5]4
Báo cáo phân loại Sklearn
Trong Sklearn, bạn có thể hiển thị tất cả các kết quả này trong một bảng kết hợp! và cũng cho hơn hai lớp.
In [20]:
# first 50 values are positive-labels [1], second 50 values are negative-labels [0] actual_values = [1] * 50 + [0] * 50 predicted_values = random.choices[[0, 1], k=100] # randomly generate 0 and 1 labels predicted_values[0:5]5
In [21]:
# first 50 values are positive-labels [1], second 50 values are negative-labels [0] actual_values = [1] * 50 + [0] * 50 predicted_values = random.choices[[0, 1], k=100] # randomly generate 0 and 1 labels predicted_values[0:5]6
# first 50 values are positive-labels [1], second 50 values are negative-labels [0] actual_values = [1] * 50 + [0] * 50 predicted_values = random.choices[[0, 1], k=100] # randomly generate 0 and 1 labels predicted_values[0:5]7
Hỗ trợ: Các cột này cho bạn biết có bao nhiêu mẫu trong mỗi lớp.: This columns tells you how many samples are in each class.
Macro AVG
Đối với một vấn đề phân loại đa lớp, ngoài điểm số thu hồi, độ chính xác và F1 khôn ngoan của lớp, chúng tôi kiểm tra điểm thu hồi trung bình và trung bình có trọng số, độ chính xác và điểm F1 của toàn bộ mô hình. Những điểm số này giúp lựa chọn mô hình tốt nhất cho nhiệm vụ trong tay.
Trong ma trận nhầm lẫn ở trên, nếu chúng ta làm trung bình của cột chính xác, chúng ta sẽ nhận được 0,23 như hình dưới đây. Tương tự, trung bình của các cột khác có thể được tìm thấy.
In [8]:
# first 50 values are positive-labels [1], second 50 values are negative-labels [0] actual_values = [1] * 50 + [0] * 50 predicted_values = random.choices[[0, 1], k=100] # randomly generate 0 and 1 labels predicted_values[0:5]8
AVG có trọng số
Trung bình có trọng số là trung bình của điểm số có trọng số của mỗi cột. Ví dụ, điểm trung bình có trọng số cột chính xác được tính bằng cách nhân giá trị chính xác với số mẫu tương ứng và sau đó lấy trung bình như hình dưới đây.
In [12]:
# first 50 values are positive-labels [1], second 50 values are negative-labels [0] actual_values = [1] * 50 + [0] * 50 predicted_values = random.choices[[0, 1], k=100] # randomly generate 0 and 1 labels predicted_values[0:5]9