确保pandas.crosstab返回正方形矩阵

发布于 2025-02-13 07:19:56 字数 751 浏览 0 评论 0原文

我目前正在使用pandas.crosstab在测试后生成分类器的混淆矩阵。不幸的是,有时我的分类器会失败,并将每个信号分类为一个单个标签(而不是多个标签)。 pandas.crosstab在这种情况下会生成一个向量(或非平方矩阵),而不是方形矩阵。
例如,我的基本真相是

true_data = pandas.Series([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])

,我的预测数据正在

pred_data = pandas.Series([3, 3, 2, 3, 2, 1, 1, 3, 4, 1])

应用pandas.crosstab(true_data,pred_data,dropna = false)给出

col_0  1  2  3  4
row_0
1      0  0  2  0
2      0  1  1  0
3      1  1  0  0
4      1  0  1  0
5      1  0  0  1

有一种方法可以取得

col_0  1  2  3  4  5
row_0
1      0  0  2  0  0
2      0  1  1  0  0
3      1  1  0  0  0
4      1  0  1  0  0
5      1  0  0  1  0

方法缺少0的标签?

I am currently using pandas.crosstab to generate the confusion matrix of my classifiers after testing. Unfortunately, sometimes my classifier fails, and classifies every signal as a single label (instead of multiple labels). pandas.crosstab generates a single vector (or a non-square matrix) in that case instead of a square matrix.
As example, my ground truth would be

true_data = pandas.Series([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])

and my predicted data is

pred_data = pandas.Series([3, 3, 2, 3, 2, 1, 1, 3, 4, 1])

Applying pandas.crosstab(true_data, pred_data, dropna=False) gives

col_0  1  2  3  4
row_0
1      0  0  2  0
2      0  1  1  0
3      1  1  0  0
4      1  0  1  0
5      1  0  0  1

Is there a way to get

col_0  1  2  3  4  5
row_0
1      0  0  2  0  0
2      0  1  1  0  0
3      1  1  0  0  0
4      1  0  1  0  0
5      1  0  0  1  0

instead, i.e. leaving the matrix square and filling the missing labels with 0?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

治碍 2025-02-20 07:19:57

计算crosstab后,您可以Reindex沿索引和列轴的数据框架。

i = df.index.union(df.columns)
df.reindex(index=i, columns=i, fill_value=0)

   1  2  3  4  5
1  0  0  2  0  0
2  0  1  1  0  0
3  1  1  0  0  0
4  1  0  1  0  0
5  1  0  0  1  0

After calculating crosstab you can reindex the dataframe along both index and columns axis.

i = df.index.union(df.columns)
df.reindex(index=i, columns=i, fill_value=0)

   1  2  3  4  5
1  0  0  2  0  0
2  0  1  1  0  0
3  1  1  0  0  0
4  1  0  1  0  0
5  1  0  0  1  0
时光无声 2025-02-20 07:19:57

一部分数组

xtab = pd.crosstab(pred_data, true_data, dropna=False).sort_index(axis=0).sort_index(axis=1)
all_unique_values = sorted(set(true_data) | set(pred_data))
z = np.zeros((len(all_unique_values), len(all_unique_values)))
rows, cols = xtab.shape
z[:rows, :cols] = xtab
square_xtab  = pd.DataFrame(z, columns=all_unique_values, index=all_unique_values) 

您可以创建ZEROS所需形状的数组,然后用crosstab 输出

     1    2    3    4    5
1  0.0  0.0  1.0  1.0  1.0
2  0.0  1.0  1.0  0.0  0.0
3  2.0  1.0  0.0  1.0  0.0
4  0.0  0.0  0.0  0.0  1.0
5  0.0  0.0  0.0  0.0  0.0

替换 但是,如果这种方法在“中间”中,则该方法将起作用 - 例如,例如,例如,pred_data = [1,2,4,5] 2、3、4]

You could create a zeros array of the required shape and then replace a portion of the array with the crosstab

xtab = pd.crosstab(pred_data, true_data, dropna=False).sort_index(axis=0).sort_index(axis=1)
all_unique_values = sorted(set(true_data) | set(pred_data))
z = np.zeros((len(all_unique_values), len(all_unique_values)))
rows, cols = xtab.shape
z[:rows, :cols] = xtab
square_xtab  = pd.DataFrame(z, columns=all_unique_values, index=all_unique_values) 

Output

     1    2    3    4    5
1  0.0  0.0  1.0  1.0  1.0
2  0.0  1.0  1.0  0.0  0.0
3  2.0  1.0  0.0  1.0  0.0
4  0.0  0.0  0.0  0.0  1.0
5  0.0  0.0  0.0  0.0  0.0

I haven't thought / tested yet if this approach will work if the mismatch is in the "middle" - as in, if, e.g., pred_data = [1, 2, 4, 5] and true_data = [1, 2, 3, 4]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文