“bagaimana mendapatkan matriks kebingungan di python” Kode Jawaban

Python matriks kebingungan

from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(y_test, y_pred)
sns.heatmap(conf_mat, square=True, annot=True, cmap='Blues', fmt='d', cbar=False)
Adventurous Addax

Python matriks kebingungan

By definition, entry i,j in a confusion matrix is the number of 
observations actually in group i, but predicted to be in group j. 
Scikit-Learn provides a confusion_matrix function:

from sklearn.metrics import confusion_matrix
y_actu = [2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2]
y_pred = [0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2]
confusion_matrix(y_actu, y_pred)
# Output
# array([[3, 0, 0],
#        [0, 1, 2],
#        [2, 1, 3]], dtype=int64)
Bored Coder

Kode Python matriks kebingungan

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_predicted)
cm
# after creating the confusion matrix, for better understaning plot the cm.
import seaborn as sn
plt.figure(figsize = (10,7))
sn.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')
Clumsy Cowfish

Python matriks kebingungan

df_confusion = pd.crosstab(y_actu, y_pred, rownames=['Actual'], colnames=['Predicted'], margins=True)
Bad Bison

bagaimana mendapatkan matriks kebingungan di python

from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(y_test, y_pred)
Colorful Copperhead

Hitung matriks kebingungan menggunakan python

import numpy as np

currentDataClass = [1, 3, 3, 2, 5, 5, 3, 2, 1, 4, 3, 2, 1, 1, 2]
predictedClass = [1, 2, 3, 4, 2, 3, 3, 2, 1, 2, 3, 1, 5, 1, 1]

def comp_confmat(actual, predicted):

    classes = np.unique(actual) # extract the different classes
    matrix = np.zeros((len(classes), len(classes))) # initialize the confusion matrix with zeros

    for i in range(len(classes)):
        for j in range(len(classes)):

            matrix[i, j] = np.sum((actual == classes[i]) & (predicted == classes[j]))

    return matrix

comp_confmat(currentDataClass, predictedClass)

array([[3., 0., 0., 0., 1.],
       [2., 1., 0., 1., 0.],
       [0., 1., 3., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 1., 1., 0., 0.]])

Frail Fowl

Jawaban yang mirip dengan “bagaimana mendapatkan matriks kebingungan di python”

Pertanyaan yang mirip dengan “bagaimana mendapatkan matriks kebingungan di python”

Lebih banyak jawaban terkait untuk “bagaimana mendapatkan matriks kebingungan di python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya