2016-10-18 2 views
0

Im 아래 코드를 실행하면 두 번째 그래프의 숫자가 두 자리 정밀도 (0.3333 -> 33.33 %)로 백분율 형식으로 표시됩니다. 나는 배열의 람다 함수에서 '{percent, .2 %}'형식()을 사용하는 다른 버전의 톤을 시도했지만 모든 방법을 모릅니다. 모든 입력은 appriciated입니다! 사전에파이썬 그래프의 요소로 백분율 형식

import seaborn as sns 
import matplotlib.pyplot as plt 
import matplotlib 
import numpy as np 
from sklearn.tree import DecisionTreeClassifier 
from sklearn import datasets 

%matplotlib inline 

iris = datasets.load_iris() 
x = iris['data'] 
y = iris['target'] 

x = iris_x[:, :2] 

clf_tree = DecisionTreeClassifier(random_state = 1) 
fit_clf = clf_tree.fit(x, y) 

y_pred_proba = fit_clf.predict_proba(x) 
y_pred = fit_clf.predict(x) 

conf_mat = confusion_matrix(y_true = y, y_pred = y_pred) 

fig, ax = plt.subplots(figsize = (15, 9)) 
ax.matshow(conf_mat, cmap = plt.cm.Blues, alpha = 0.3) 

for i in range(conf_mat.shape[0]): 
    for j in range(conf_mat.shape[1]): 
     ax.text(x = j, y = i, 
       s = conf_mat[i, j], 
       va = 'center', 
       ha = 'center') 

plt.xlabel('Predicted') 
plt.ylabel('Actual') 
plt.show() 


conf_mat_prc = conf_mat/len(y) 

fig, ax = plt.subplots(figsize = (15, 9)) 
ax.matshow(conf_mat_prc, cmap = plt.cm.Blues, alpha = 0.3) 


for i in range(conf_mat_prc.shape[0]): 
    for j in range(conf_mat_prc.shape[1]): 
     ax.text(x = j, y = i, 
       s = conf_mat_prc[i, j], 
       va = 'center', 
       ha = 'center') 

plt.xlabel('Predicted % dist') 
plt.ylabel('Actual % dist') 
plt.show() 

많은 감사,

--swepab

답변

0

코드에서 (적어도) 두 가지 문제가 있습니다

  • line 14에서 iris_x 무엇입니까? 난 당신이 대신 0 (INT)의 부동 얻을 conf_mat_prc = conf_mat/float(len(y)) 대신 conf_mat_prc = conf_mat/len(y)로 정의되어야한다 iris_x[:, :2]

  • conf_mat_prc 대신 x[:, :2]을 의미 생각합니다.

마지막 번째 그래프 (라인 48)에 대해 정밀도 부동 소수점 숫자의 개수를 정의하는 str(round(conf_mat_prc[i, j]*100,precision)) + "%"를 사용한다. 여기에 새 두 번째 그래프의

import seaborn as sns 
import matplotlib.pyplot as plt 
import matplotlib 
import numpy as np 
from sklearn.tree import DecisionTreeClassifier 
from sklearn import datasets 
from sklearn.metrics import confusion_matrix 

# %matplotlib inline 

iris = datasets.load_iris() 
x = iris['data'] 
y = iris['target'] 

x = x[:, :2] 

clf_tree = DecisionTreeClassifier(random_state = 1) 
fit_clf = clf_tree.fit(x, y) 

y_pred_proba = fit_clf.predict_proba(x) 
y_pred = fit_clf.predict(x) 

conf_mat = confusion_matrix(y_true = y, y_pred = y_pred) 

fig, ax = plt.subplots(figsize = (15, 9)) 
ax.matshow(conf_mat, cmap = plt.cm.Blues, alpha = 0.3) 

for i in range(conf_mat.shape[0]): 
    for j in range(conf_mat.shape[1]): 
     ax.text(x = j, y = i, 
       s = conf_mat[i, j], 
       va = 'center', 
       ha = 'center') 

plt.xlabel('Predicted') 
plt.ylabel('Actual') 
plt.show() 



conf_mat_prc = conf_mat/float(len(y)) 

fig, ax = plt.subplots(figsize = (15, 9)) 
ax.matshow(conf_mat_prc, cmap = plt.cm.Blues, alpha = 0.3) 


precision = 2 
for i in range(conf_mat_prc.shape[0]): 
    for j in range(conf_mat_prc.shape[1]): 
     ax.text(x = j, y = i, 
       s = str(round(conf_mat_prc[i, j]*100,precision)) + "%", 
       va = 'center', 
       ha = 'center') 

plt.xlabel('Predicted % dist') 
plt.ylabel('Actual % dist') 
plt.show() 

:

여기에 새로운 코드의

enter image description here

+0

죄송합니다, 코드에 붙여 넣을 때 그 날의 실수였다. 내가 원하는 것은 모든 셀의 모든 요소에 대한 백분율 형식입니다. 예를 들어 셀 (0,0)에서 0.33 대신 33.33 %를 얻을 수 있습니다. – swepab

+0

@swepab 지금 확인하십시오 – Yugi

+0

승! 고마워, 도움을 많이! – swepab

관련 문제