python一维矩阵转数组,转换一个一维数组在Python 2D基于类的矩阵

I'm using a multi-class classifier, so in order to evaluate it after testing, I need the predictions from the classifier (y_pred)to be compared against the true class values (y_test).

But I have them both as 1D arrays, like so:

y_test = [1, 1, 1, 2, 1, 4, 5, 3, ... etc ]

y_pred = [1, 1, 1, 2, 3, 2, 5, 0, ... etc ]

In total I have 46 classes.

But in order to build ROC curves (as in here: http://scikit-learn.org/stable/auto_examples/plot_roc.html), I'm guessing I need the y_test and y_pred to be in a 2D matrix with binary values, of the following shape:

number_of_test_cases x number_of_classes.

Where each column represents one class, and 1 represents the fact that the classifier recognized this class on the given test sample row.

So given the above few values I showed, I understand I need y_test to look something like this:

y_test = [ 1 0 ...

1 0

1 0

0 1

1 0

0 1

0 0

0 0

...

This is what I understand... I hope I'm right!

Is there any numpy function to create such a matrix from a 1D array?

解决方案

Have a look at the label_binarize function that's referenced in the example code in your link.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值