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_negative 0
๋ฏผ๊ฐ๋ / ํ์ ๋ ฅ 1.0 ํน์ด์ฑ 1.0 ์ ๋ฐ๋ 1.0
ํด๋์ค ๋ ์ด๋ธ 1 T_positive 11 F_positive 1 T_negative 18 F_negative 0
๋ฏผ๊ฐ๋ / ํ์ ์จ 1.0 ํน์ด๋ 0.9473684210526315 ์ ๋ฐ๋ 0.9166666666666666
ํด๋์ค ๋ ์ด๋ธ 2 T_positive 11 F_positive 0 T_negative 18 F_negative 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 ์ถ์ ์์ธก ๊ฐ์ผ๋ก ๊ตฌ์ฑ๋์ด ์๋ค๊ณ ๊ฐ์ ํฉ๋๋ค. ์ด๊ฒ์ Wikipedia์์ ์ฌ์ฉ ๋ ๊ตฌ์กฐ์ ํผ๋ ํ๋ ฌ ํจ์์ ๋ํ ๋ฌธ์์์ ์ฐธ์กฐ ๋ ๊ตฌ์กฐ์ ๋์ผํฉ๋๋ค.
๋์กฐ์ ์ผ๋ก ์ด๊ฒ์ sklearn.metrics import classification_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 (๊ธฐ๋ณธ๊ฐ, 2020 ๋
1 ์ 8 ์ผ, 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
Python ์ข
์์ฑ :
ํ : 20.0.2
setuptools : 38.2.5
sklearn : 0.22.1
numpy : 1.18.1
scipy : 1.4.1
Cython : ์์
ํ๋ค : 1.0.1
matplotlib : 3.1.3
joblib : 0.14.1
OpenMP๋ก ๊ตฌ์ถ : True
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
์
๋๋ค. ์ด๊ฒ์ ์ ๋ฐ๋์ ๋ฆฌ์ฝ์ ๋ค์ง์ ๊ฒ์
๋๋ค.
classification_report๋ฅผ ์ฐธ์กฐํ์ญ์์ค.
ํธ์ง : ํผ๋ ํ๋ ฌ๋ ๊ฑฐ๊พธ๋ก๋์ด ์์ง๋ง 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 ํ์ 1 ๊ฐ์ ๊ด์ธก์น๊ฐ ์๊ณ 1 ์ด์ 0์ด ์์ผ๋ฏ๋ก ํ์ ์ค์ธก ๊ฐ์ด๊ณ ์ด์ ์์ธก์
๋๋ค. ๋ฐ๋ผ์ confusion_matrix์ ํ์๋ C[i, j]
ํ๊ธฐ๋ฒ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ค๋ช ํด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ์ํคํผ๋์ ์ฐธ๊ณ ์๋ฃ๊ฐ ํผ๋ ์ค๋ฌ์ ์ต๋๋ค!
๋ฌธ์ ์์ต๋๋ค. Wikipedia์์ ์์ ๋ฅผ 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
์ ๋๋ค. ์ด๊ฒ์ ์ ๋ฐ๋์ ๋ฆฌ์ฝ์ ๋ค์ง์ ๊ฒ์ ๋๋ค.classification_report๋ฅผ ์ฐธ์กฐํ์ญ์์ค.
ํธ์ง : ํผ๋ ํ๋ ฌ๋ ๊ฑฐ๊พธ๋ก๋์ด ์์ง๋ง sklearn์ ํผ๋ ํ๋ ฌ์ด ์ํคํผ๋์์์ ๊ฑฐ๊พธ๋ก๋์ด ์๊ธฐ ๋๋ฌธ์ ์๋ํฉ๋๋ค.
1 ํ์ 1 ๊ฐ์ ๊ด์ธก์น๊ฐ ์๊ณ 1 ์ด์ 0์ด ์์ผ๋ฏ๋ก ํ์ ์ค์ธก ๊ฐ์ด๊ณ ์ด์ ์์ธก์ ๋๋ค. ๋ฐ๋ผ์ confusion_matrix์ ํ์๋
C[i, j]
ํ๊ธฐ๋ฒ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.