使用 csr_matrix 列表训练 SGDClassifier

我有一个列表 X_train(超过20000个元素),每个元素都是通过 HashingVectorizer.transform() 创建的稀疏 scipy csr_matrix

我的 HashingVectorizer.transform() 对输入文件进行逐行转换,并将结果追加到列表 X_train 中。

我试图使用 X_train 训练一个 SGDClassifier,但我收到了以下错误:

ValueError: setting an array element with a sequence

如何在不进行CPU或内存密集型操作的情况下训练 SGDClassifier?


回答:

稀疏矩阵的列表,以及将其转换为数组或稀疏矩阵(或不转换)的方法:

In [916]: alist=[sparse.random(1,10,.2, format='csr') for _ in range(3)]In [917]: alistOut[917]: [<1x10 sparse matrix of type '<class 'numpy.float64'>'    with 2 stored elements in Compressed Sparse Row format>, <1x10 sparse matrix of type '<class 'numpy.float64'>'    with 2 stored elements in Compressed Sparse Row format>, <1x10 sparse matrix of type '<class 'numpy.float64'>'    with 2 stored elements in Compressed Sparse Row format>]

创建一个正确的二维稀疏矩阵:

In [918]: sparse.vstack(alist)Out[918]: <3x10 sparse matrix of type '<class 'numpy.float64'>'    with 6 stored elements in Compressed Sparse Row format>

矩阵的对象数组 – 不好

In [919]: np.array(alist)Out[919]: array([ <1x10 sparse matrix of type '<class 'numpy.float64'>'    with 2 stored elements in Compressed Sparse Row format>,       <1x10 sparse matrix of type '<class 'numpy.float64'>'    with 2 stored elements in Compressed Sparse Row format>,       <1x10 sparse matrix of type '<class 'numpy.float64'>'    with 2 stored elements in Compressed Sparse Row format>], dtype=object)

尝试创建一个浮点数数组 – 你的错误

In [920]: np.array(alist, float)---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-920-52d4689fa7b3> in <module>()----> 1 np.array(alist, float)ValueError: setting an array element with a sequence.

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注