Matplotlib 혼동 행렬 그리 기
3156 단어 Matplotlib혼동 행렬
코드 는 다음 과 같 습 니 다:
import itertools
import matplotlib.pyplot as plt
import numpy as np
#
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
"""
- cm :
- classes :
- normalize : True: , False:
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print(" :")
np.set_printoptions(formatter={'float': '{: 0.2f}'.format})
print(cm)
else:
print(' :')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
# matplotlib , , , matplotlib ,
plt.ylim(len(classes) - 0.5, -0.5)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
테스트 데이터:
cnf_matrix = np.array([[8707, 64, 731, 164, 45],
[1821, 5530, 79, 0, 28],
[266, 167, 1982, 4, 2],
[691, 0, 107, 1930, 26],
[30, 0, 111, 17, 42]])
attack_types = ['Normal', 'DoS', 'Probe', 'R2L', 'U2R']
첫 번 째 상황:백분율 표시
plot_confusion_matrix(cnf_matrix, classes=attack_types, normalize=True, title='Normalized confusion matrix')
효과:두 번 째 상황:숫자 표시
plot_confusion_matrix(cnf_matrix, classes=attack_types, normalize=False, title='Normalized confusion matrix')
효과:여기 서 Matplotlib 가 헷 갈 리 는 행렬 을 그 리 는 실현 에 관 한 글 은 여기까지 소개 되 었 습 니 다.더 많은 관련 Matplotlib 헷 갈 리 는 행렬 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많은 응원 부 탁 드 리 겠 습 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Python 데이터 시각 화 는 Matplotlib 로 자주 사용 되 는 그래 픽 을 그립 니 다.x 축 좌표 눈금 표시 위치 조정 은 도형 을 그 릴 때 x 데 이 터 를 조정 할 수도 있 고 x 축 눈금 을 그 릴 때 눈금 을 조정 하 는 방법 으로 x 축 눈금 을 두 개의 기둥 모양 그림 사이 에 표시 할 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.