简体   繁体   中英

Percentage format in elements of python graph

Im executing the below code and I would like the numbers in the second graph to be percentage format with a two digit precision (0.3333 --> 33.33%). I have tried a ton of different version where I use '{percent, .2%}'.format() in lambda functions on the arrays, etc, but I dont get it all the way. All input is 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()

Many thanks in advance,

--swepab

There are (at least) two problems in your code:

  • What is iris_x in line 14 ? I think you meant x[:, :2] instead of iris_x[:, :2]

  • conf_mat_prc should be defined as conf_mat_prc = conf_mat/float(len(y)) instead of conf_mat_prc = conf_mat/len(y) to get float instead of 0 (int).

Finally, for the second graph (line 48) use str(round(conf_mat_prc[i, j]*100,precision)) + "%" in which precision defines the number of floating point digits.

Here's the new code:

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()

Here's the new second graph :

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM