Get the index of each element of the confusion matrix in Python

Because confusion-matrix of scikit-learn could not get each index. (Maybe you can?)

conditions

For example, when determining whether a cat is a cat, 1 is a cat and 0 is not a cat. Predict preds and use labels as correct data.

procedure

Convert to numpy matrix

import numpy as np

preds = [0., 1., 1., ..., 0., 1., 0.]  #A suitable list of 0 or 1
labels = [0., 1., 1., ..., 0., 1., 0.]  #A suitable list of 0 or 1

preds = np.array(preds)
labels = np.array(labels)

Index the cat

one_ind_p = preds == 1
one_ind_l = labels == 1
zero_ind_p = np.logical_not(one_ind_p)  #Inversion of bool
zero_ind_l = np.logical_not(one_ind_l)
# array([False,  True,  True, ..., False,  True, False])It becomes data like

Get the index of each element of the confusion matrix

tp = np.argwhere(one_ind_l & one_ind_p)
fp = np.argwhere(zero_ind_l & one_ind_p)
fn = np.argwhere(one_ind_l & zero_ind_p)
tn = np.argwhere(zero_ind_l & zero_ind_p)

appendix This is the confusion matrix スクリーンショット 2019-12-20 15.13.31.png http://ibisforest.org/index.php?F%E5%80%A4

Recommended Posts

Get the index of each element of the confusion matrix in Python
Get the number of occurrences for each element in the list
Get the caller of a function in Python
Match the distribution of each group in Python
How to get the number of digits in Python
[python] Get the list of classes defined in the module
Get the size (number of elements) of UnionFind in Python
Get the URL of the HTTP redirect destination in Python
Does the confusion matrix also need the ratio of each element to the row total?
Compare the sum of each element in two lists with the specified value in Python
Get the last element of the array by splitting the string in Python and PHP
Check the operation of Python for .NET in each environment
Get the desktop path in Python
Get the script path in Python
Get the number of specific elements in a python list
Get the first element of queryset
Get index of nth largest / smallest value in list in Python
Get the desktop path in Python
How to count the number of occurrences of each element in the list in Python with weight
Find the eigenvalues of a real symmetric matrix in Python
Get index of nth largest / smallest value in list in Python
Get the value of a specific key up to the specified index in the dictionary list in Python
How to determine the existence of a selenium element in Python
[python] Get the rank of the values in List in ascending / descending order
python note: map -do the same for each element of the list
Get the title and delivery date of Yahoo! News in Python
Get the number of readers of a treatise on Mendeley in Python
Check the behavior of destructor in Python
The result of installing python in Anaconda
I can't get the element in Selenium!
The basics of running NoxPlayer in Python
In search of the fastest FizzBuzz in Python
Get the EDINET code list in Python
Get rid of DICOM images in Python
Status of each Python processing system in 2020
python memo: enumerate () -get index and element of list at the same time and turn for statement
Get a capture of the entire web page in Selenium Python VBA
Get a datetime instance at any time of the day in Python
Get the key for the second layer migration of JSON data in python
Get the contents of git diff from python
Output the number of CPU cores in Python
[Python] Get the files in a folder with Python
Get the weather in Osaka via WebAPI (python)
[Python] Get / edit the scale label of the figure
[Python] Get the main topics of Yahoo News
View the result of geometry processing in Python
Get the X Window System window title in Python
Find the divisor of the value entered in python
[Python] Get the last updated date of the website
Find the solution of the nth-order equation in python
Pre-process the index in Python using Solr's ScriptUpdateProcessor
[Python numpy] Dynamically specify the index of the array
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
Get a glimpse of machine learning in Python
[Python] Find the transposed matrix in a comprehension
[Python] Get the day of the week (English & Japanese)
Get the update date of the Python memo file.