转换一个一维数组在Python 2D基于类的矩阵 [英] Converting a 1D array into a 2D class-based matrix in python

查看:167
本文介绍了转换一个一维数组在Python 2D基于类的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了多级分类,所以为了测试之后评价它,我需要从分类器predictions( Y_ preD )反对真正的类值进行比较( y_test )。

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 ]

在我总共有46类。

但为了建立ROC曲线(在这里:的http:// scikit- learn.org/stable/auto_examples/plot_roc.html ),我猜我需要的 y_test Y_ preD 以与二进制值的二维矩阵,以下形状:
number_of_test_cases点¯xnumber_of_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.

每列重presents一个类,并重新1 presents的分类识别给定的测试样品行此类的事实。

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

因此​​,考虑上述几个值我发现,我明白我需要y_test看起来是这样的:

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!

是否有任何 numpy的函数来创建从一维数组这样的矩阵?

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

推荐答案

有一个看的 label_binarize 是在例子code引用您的链接功能。

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

这篇关于转换一个一维数组在Python 2D基于类的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆