gpt4 book ai didi

python - 了解 scikit-learn 中的数据格式

转载 作者:行者123 更新时间:2023-11-28 22:49:02 26 4
gpt4 key购买 nike

我正在尝试使用 Python 3.x 中的 scikit-learn 处理多标签文本分类。我有使用 load_svmlight_file 模块加载的 libsvm 格式的数据。数据格式是这样的。

  • 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

每一行对应一个文档。前三个数字是标签,接下来的条目是特征数字及其值。每个特征对应一个词。

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

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  

我是 scikit 的新手。如果能在这方面提供一些帮助,我将不胜感激。

最佳答案

这与多标签分类本身无关。您从 load_svmlight_file 获得的特征矩阵 XSciPy CSR matrix ,如文档中所述,并且以相当不幸的格式打印:

>>> 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

关于python - 了解 scikit-learn 中的数据格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24380426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com