قد يُبلغ sklearn.metrics.classification عن قيم مقلوبة للدقة والاسترجاع؟
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.neighbors import KNeighborsClassifier
from sklearn import datasets
def calc_precision_recall(conf_matrix, class_labels):
# for each class
for i in range(len(class_labels)):
# calculate true positives
true_positives =(conf_matrix[i, i])
# false positives
false_positives = (conf_matrix[i, :].sum() - true_positives)
# false negatives
false_negatives = 0
for j in range(len(class_labels)):
false_negatives += conf_matrix[j, i]
false_negatives -= true_positives
# and finally true negatives
true_negatives= (conf_matrix.sum() - false_positives - false_negatives - true_positives)
# print calculated values
print(
"Class label", class_labels[i],
"T_positive", true_positives,
"F_positive", false_positives,
"T_negative", true_negatives,
"F_negative", false_negatives,
"\nSensitivity/recall", true_positives / (true_positives + false_negatives),
"Specificity", true_negatives / (true_negatives + false_positives),
"Precision", true_positives/(true_positives+false_positives), "\n"
)
return
# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, 0:3] # we only take the first two features.
y = iris.target
# Random_state parameter is just a random seed that can be used to reproduce these specific results.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=27)
# Instantiate a K-Nearest Neighbors Classifier:
KNN_model = KNeighborsClassifier(n_neighbors=2)
# Fit the classifiers:
KNN_model.fit(X_train, y_train)
# Predict and store the prediction:
KNN_prediction = KNN_model.predict(X_test)
# Generate the confusion matrix
conf_matrix = confusion_matrix(KNN_prediction, y_test)
# Print the classification report
print(classification_report(KNN_prediction, y_test))
# Dummy class labels for the three iris classes
class_labels = [0,1,2]
# Own function to calculate precision and recall from the confusion matrix
calc_precision_recall(conf_matrix, class_labels)
تقوم وظيفتي بإرجاع ما يلي لكل فئة:
تصنيف الفئة 0 T_positive 7 F_positive 0 T_negative 23 F_Segative 0
الحساسية / الاسترجاع 1.0 الخصوصية 1.0 الدقة 1.0
تصنيف الفئة 1 T_positive 11 F_positive 1 سلبي T 18 سلبي 0
الحساسية / الاسترجاع 1.0 الخصوصية 0.9473684210526315 الدقة 0.9166666666666666
تصنيف الفئة 2 T_positive 11 F_positive 0 T_negative 18 F_Segative 1
الحساسية / الاسترجاع 0.9166666666666666 الخصوصية 1.0 الدقة 1.0
precision recall
0 1.00 1.00
1 0.92 1.00
2 1.00 0.92
تفترض وظيفتي أن مصفوفة الارتباك مبنية على القيم الفعلية على المحور x العلوي والقيم المتوقعة أسفل المحور y الأيسر. هذا هو نفس الهيكل المستخدم في ويكيبيديا والمشار إليه في وثائق دالة مصفوفة الارتباك.
في المقابل ، هذه هي النتائج التي تم الإبلاغ عنها بواسطة sklearn.metrics import rating_report
precision recall f1-score support
0 1.00 1.00 1.00 7
1 1.00 0.92 0.96 12
2 0.92 1.00 0.96 11
نظام:
python: 3.8.1 (افتراضي ، 8 كانون الثاني (يناير) 2020 ، 22:29:32) [GCC 7.3.0]
قابل للتنفيذ: / home / will / anaconda3 / envs / ElStatLearn / bin / python
الجهاز: Linux-4.15.0-91-generic-x86_64-with-glibc2.10
تبعيات بايثون:
النقطة: 20.0.2
أدوات الإعداد: 38.2.5
sklearn: 0.22.1
numpy: 1.18.1
scipy: 1.4.1
سايثون: لا شيء
الباندا: 1.0.1
matplotlib: 3.1.3
سجل العمل: 0.14.1
بنيت مع OpenMP: صحيح
أعتقد أن y_test
يجب أن يأتي أولاً في print(classification_report(KNN_prediction, y_test))
.
لذلك: print(classification_report(y_test, KNN_prediction))
.
الدالة sklearn.metrics.classification_report(y_true, y_pred, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn')
لها y_true
كوسيطة أولى. هذا من شأنه أن يقلب الدقة ويتذكر.
انظر تصنيف_تقرير .
تحرير: مصفوفة الارتباك الخاصة بك عكسية أيضًا ، لكنها تعمل لأن مصفوفة الارتباك الخاصة بـ sklearn مقلوبة من ويكيبيديا.
>>> from sklearn.metrics import confusion_matrix
>>> y_true = [2, 0, 2, 2, 0, 1]
>>> y_pred = [0, 0, 2, 2, 0, 2]
>>> confusion_matrix(y_true, y_pred)
array([[2, 0, 0],
[0, 0, 1],
[1, 0, 2]])
يمكنك أن ترى أن هناك ملاحظة واحدة في الصف 1 و 0 في العمود 1 ، وبالتالي فإن الصفوف هي الحقيقة الأساسية والأعمدة عبارة عن تنبؤات. لذا يمكنك استخدام الترميز C[i, j]
المعروض في confusion_matrix
شكراً جزيلاً لتوضيح ذلك - لقد أربكتني إشارة ويكيبيديا!
لا توجد مشكلة ، ربما يجب أن تجعل ويكيبيديا تغير مثالها إلى اتجاه sklearn.
التعليق الأكثر فائدة
أعتقد أن
y_test
يجب أن يأتي أولاً فيprint(classification_report(KNN_prediction, y_test))
.لذلك:
print(classification_report(y_test, KNN_prediction))
.الدالة
sklearn.metrics.classification_report(y_true, y_pred, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn')
لهاy_true
كوسيطة أولى. هذا من شأنه أن يقلب الدقة ويتذكر.انظر تصنيف_تقرير .
تحرير: مصفوفة الارتباك الخاصة بك عكسية أيضًا ، لكنها تعمل لأن مصفوفة الارتباك الخاصة بـ sklearn مقلوبة من ويكيبيديا.
يمكنك أن ترى أن هناك ملاحظة واحدة في الصف 1 و 0 في العمود 1 ، وبالتالي فإن الصفوف هي الحقيقة الأساسية والأعمدة عبارة عن تنبؤات. لذا يمكنك استخدام الترميز
C[i, j]
المعروض في confusion_matrix