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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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