Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 理解scikit学习中的数据格式_Python_Numpy_Machine Learning_Scipy_Scikit Learn - Fatal编程技术网

Python 理解scikit学习中的数据格式

Python 理解scikit学习中的数据格式,python,numpy,machine-learning,scipy,scikit-learn,Python,Numpy,Machine Learning,Scipy,Scikit Learn,我正在尝试使用Python3.x中的scikit learn处理多标签文本分类。我使用load\u svmlight\u文件模块加载libsvm格式的数据。数据格式如下 > 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 31452316553876255 1:12:13:14:15:16:17:18:19:110:1112:213:1 4105232302963683

我正在尝试使用Python3.x中的scikit learn处理多标签文本分类。我使用
load\u svmlight\u文件
模块加载libsvm格式的数据。数据格式如下

> 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
  • 31452316553876255 1:12:13:14:15:16:17:18:19:110:1112:213:1
  • 41052323029636830375145 8:11922:1241:129:163:168:169:376:182:183:184:1
每一行对应一个文档。前三个数字是标签,下一个条目是要素编号及其值。每个特征对应一个单词

我正在使用此脚本加载数据

from sklearn.datasets import load_svmlight_file

X,Y = load_svmlight_file("train.csv", multilabel = True, zero_based = True)
我的问题是,当我看到数据的格式时,例如,
print(X[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

我不明白这种格式的含义。格式不应该是这样的吗

> 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 > 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
我是新来的。我希望在这方面能得到一些帮助。

这与多标签分类本身无关。从
load\u svmlight\u file
中获得的特征矩阵
X
是一个,如文档中所述,这些特征矩阵以非常糟糕的格式打印:

>>> 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
>>从scipy.sparse导入csr\u矩阵
>>>X=csr_矩阵([[0,0,1],[2,3,0]]
>>>X
>>>十、toarray()
数组([[0,0,1],
[2, 3, 0]])
>>>打印(X)
(0, 2)    1
(1, 0)    2
(1, 1)    3

感谢您指出这一点。我在scikit学习文档中找不到这个。我仍然无法理解CSR矩阵是如何构建的。我能把它转换回普通数组吗?@Sasha,它在数组中。您可以使用
X.toarray()
转换为数组,如我在示例中所示。非常感谢。我现在明白了。