Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

confusion matrix from two columns pandas dataframe

import pandas as pd

def confusion_matrix(df: pd.DataFrame, col1: str, col2: str):
    """
    Given a dataframe with at least
    two categorical columns, create a 
    confusion matrix of the count of the columns
    cross-counts
    
    use like:
    
    >>> confusion_matrix(test_df, 'actual_label', 'predicted_label')
    """
    return (
            df
            .groupby([col1, col2])
            .size()
            .unstack(fill_value=0)
            )
Comment

PREVIOUS NEXT
Code Example
Python :: pandas create a column from index 
Python :: python rsi trading strategy 
Python :: yesno django 
Python :: check if a value in dataframe is nan 
Python :: how to factorise an expression in python 
Python :: zermelo python 
Python :: train test split python 
Python :: python generate list alphabet 
Python :: import crypto python 
Python :: dataframe x y to geodataframe 
Python :: perimeter of semicircle formula 
Python :: pandas rename column name 
Python :: resize multiple images to same size python 
Python :: python read file in string list 
Python :: matplotlib bold 
Python :: export a dataframe from rstudio as csv 
Python :: frequency of occurrence of that element in the list and the positions 
Python :: find common words in two lists python 
Python :: how to see if a proxy is up in python 
Python :: plotly scatter markers size 
Python :: adaptive thresholding python 
Python :: tkinter text in canvas 
Python :: convert letters to numbers in python 
Python :: switch columns and rows python 
Python :: export sklearn.metrics.classification_report as csv 
Python :: pandas dataframe get number of columns 
Python :: mode code python 
Python :: python check if string is number 
Python :: nlargest 
Python :: random forrest plotting feature importance function 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =