确保pandas.crosstab返回正方形矩阵
我目前正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
计算
crosstab
后,您可以Reindex
沿索引和列轴的数据框架。After calculating
crosstab
you canreindex
the dataframe along both index and columns axis.一部分数组
您可以创建
ZEROS
所需形状的数组,然后用crosstab
输出替换 但是,如果这种方法在“中间”中,则该方法将起作用 - 例如,例如,例如,
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 thecrosstab
Output
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]
andtrue_data = [1, 2, 3, 4]