The goal is to create a confusion matrix for a chosen model column and compare it with the true column, by discretizing the values into regions.
I have a large dataset where I have constructed a large number of models and created predictions (modelx), and the true values (true) which resemble the following models:
The values of both the models and the true column are between [0,1]. I want to create a function where I can specify regions (Ex: [0, 0.25, 0.5, 0.75, 1]) and discretize a chosen model (a column) into binary values (unless a categorical string would work), whether the values are within the region or not.
In the example above, I have four regions and from here would like to create a confusion matrix of the chosen model.
Here's one solution - use pd.cut:
import pandas as pd
import
from sklearn.metrics import confusion_matrix
import plotly.express as px
df = pd.DataFrame(np.random.random((100,7)), columns = [j for j in range(6)] + ["true"])
df_binned = pd.DataFrame()
for col in df.columns:
df_binned[col] = pd.cut(df[col], bins=[0,0.25, 0.5, 0.75, 1.0], labels=list("lmhs"))
# generate confusion matrix
cm = confusion_matrix(y_true=df_binned.true, y_pred=df_binned[0])
# plot
px.imshow(cm).show()