了解scikit-learn中的数据格式 [英] Understanding format of data in scikit-learn
问题描述
我正在尝试使用Python 3.x中的scikit-learn处理多标签文本分类.我有使用load_svmlight_file
模块加载的libsvm格式的数据.数据格式是这样的.
I am trying to work with multi-label text classification using scikit-learn in Python 3.x. I have data in libsvm format which I am loading using load_svmlight_file
module. The data format is like this.
- 314523,165538,76255 1:1 2:1 3:1 4:1 5:1 6:1 7:1 8:1 9:1 10:1 11:1 12:2 13:1
- 410523,230296,368303,75145 8:1 19:2 22:1 24:1 29:1 63:1 68:1 69:3 76:1 82:1 83:1 84:1
- 314523,165538,76255 1:1 2:1 3:1 4:1 5:1 6:1 7:1 8:1 9:1 10:1 11:1 12:2 13:1
- 410523,230296,368303,75145 8:1 19:2 22:1 24:1 29:1 63:1 68:1 69:3 76:1 82:1 83:1 84:1
每行对应一个文档.前三个数字是标签,接下来的条目是具有其值的要素编号.每个功能都对应一个单词.
Each of these lines corresponds to one document. The first three numbers are the labels, and the next entries are feature numbers with their values. Each feature corresponds to a word.
我正在使用此脚本加载数据.
I am loading the data using this script.
from sklearn.datasets import load_svmlight_file
X,Y = load_svmlight_file("train.csv", multilabel = True, zero_based = True)
我的问题是,当我通过执行print (X[0])
来查看数据格式时,会得到此输出.
My question is, that when I see the format of data by doing for example, print (X[0])
, I get this output.
(0,1)1.0
(0, 1) 1.0
(0,2)1.0
(0,3)1.0
(0,4)1.0
(0,5)1.0
(0,6)1.0
(0,7)1.0
(0,8)1.0
(0,9)1.0
(0,10)1.0
(0,11)1.0
(0,12)2.0
(0,13)1.0
我不了解此格式的含义.格式不应该是这样的.
I don't understand the meaning of this format. Shouldn't the format be something like this.
> 1 2 3 4 5 6 7 8 9 10 11 12 13
> 1 1 1 1 1 1 1 1 1 1 1 2 1
我是scikit的新手.在这方面,我将提供一些帮助.
I am new to scikit. I would appreciate some help in this regard.
推荐答案
这与多标签分类本身无关.从load_svmlight_file
获得的特征矩阵X
是
This has nothing to do with multilabel classification per se. The feature matrix X
that you get from load_svmlight_file
is a SciPy CSR matrix, as explained in the docs, and those print in a rather unfortunate format:
>>> from scipy.sparse import csr_matrix
>>> X = csr_matrix([[0, 0, 1], [2, 3, 0]])
>>> X
<2x3 sparse matrix of type '<type 'numpy.int64'>'
with 3 stored elements in Compressed Sparse Row format>
>>> X.toarray()
array([[0, 0, 1],
[2, 3, 0]])
>>> print(X)
(0, 2) 1
(1, 0) 2
(1, 1) 3
这篇关于了解scikit-learn中的数据格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!