[PYTHON] Output the output result of sklearn.metrics.classification_report as a CSV file

I think there is a desire to output the classification result to a CSV file using sklearn.metrics.classification_report. I will. That's why I created a sample code to output a CSV file, so please have a look.

Sample code

classification_report2csv.py


import numpy as np
import pandas as pd
from sklearn.metrics import classification_report

# https://scikitlearn.org/stable/modules/generated/sklearn.metrics.classification_report.html
y_true = np.array([0, 1, 2, 2, 2])
y_pred = np.array([0, 0, 2, 2, 1])
target_names = ['class 0', 'class 1', 'class 2']

report = classification_report(y_pred=y_pred, y_true=y_true,
                               target_names=target_names, output_dict=True)

#Transposed to make the metrics name header
report_df = pd.DataFrame(report).T
report_df
"""
	precision	recall	f1-score	support
class 0	0.5	1.000000	0.666667	1.0
class 1	0.0	0.000000	0.000000	1.0
class 2	1.0	0.666667	0.800000	3.0
accuracy	0.6	0.600000	0.600000	0.6
macro avg	0.5	0.555556	0.488889	5.0
weighted avg	0.7	0.600000	0.613333	5.0
"""

# index=Note that if set to False, the label name will disappear.
report_df.to_csv("report.csv")

pd.read_csv("report.csv", index_col=0)
"""
	precision	recall	f1-score	support
class 0	0.5	1.000000	0.666667	1.0
class 1	0.0	0.000000	0.000000	1.0
class 2	1.0	0.666667	0.800000	3.0
accuracy	0.6	0.600000	0.600000	0.6
macro avg	0.5	0.555556	0.488889	5.0
weighted avg	0.7	0.600000	0.613333	5.0
"""

reference

Recommended Posts

Output the output result of sklearn.metrics.classification_report as a CSV file
How to output the output result of the Linux man command to a file
Output the result of gradient descent method as matplotlib animation
Set the output destination of the execution result to Vim started as a modeless window
Save the result of the life game as a gif with python
Download Pandas DataFrame as a CSV file
Extract the TOP command result with USER and output it as CSV
How to put a line number at the beginning of a CSV file
Output in the form of a python array
Speed evaluation of CSV file output in Python
Various ways to read the last line of a csv file in Python
Save the setting conditions as a CSV file using UDF Manager in OCTA
Get the variable name of the variable as a character string.
When reading a csv file with read_csv of pandas, the first column becomes index
Scraping the result of "Schedule-kun"
Get the output value of the command (as received by xargs)
As a result of mounting and tuning with POH! Lite
Change the standard output destination to a file in Python
[Note] Import of a file in the parent directory in Python
Extract the value of dict or list as a string
Output the key list included in S3 Bucket to a file
Process the contents of the file in order with a shell script
Conditional branch due to the existence of a shell script file
Output to csv file with Python
On Linux, the time stamp of a file is a little past.
Write standard output to a file
Read the standard output of a subprocess line by line in Python
Format the CSV file of "National Holiday" of the Cabinet Office with pandas
Process the result of% time,% timeit
Normalize the file that converted Excel to csv as it is.
[Python] How to scrape a local html file and output it as CSV using Beautiful Soup
The story of the "hole" in the file
[Python] How to store a csv file as one-dimensional array data
Output search results of posts to a file using Mattermost API
Output the result of morphological analysis with Mecab to a WEB browser compatible with Sakura server / UTF-8
The idea of feeding the config file with a python file instead of yaml
Try using Elasticsearch as the foundation of a question answering system
Dig the directory and create a list of directory paths + file names
Get the formula in an excel file as a string in Python
[Python scraping] Output the URL and title of the site containing a specific keyword to a text file
The story of writing a program
Understand the number of input / output parameters of a convolutional neural network
What is the XX file at the root of a popular Python project?
If you want a singleton in python, think of the module as a singleton
Test & Debug Tips: Create a file of the specified size in Python
[Python] How to read a csv file (read_csv method of pandas module)
Push the hash list into VT and receive the detection result as a list
I made a program to check the size of a file in Python
I tried to verify the result of A / B test by chi-square test
Change the length of Python csv strings
Measure the relevance strength of a crosstab
AHC task (3) Read CSV as a dictionary
[python] [meta] Is the type of python a type?
A memo explaining the axis specification of axis
[Python] Get the character code of the file
Get the filename of a directory (glob)
The story of blackjack A processing (python)
Notice the completion of a time-consuming command
[Python3] Understand the basics of file operations
How to pass the execution result of a shell command in a list in Python
A Python script that reads a SQL file, executes BigQuery and saves the csv