我试图使用ExtraTreesClassifier
与稀疏数据,根据the documentation,但我确实得到一个运行时间TypeError
要求密集的数据。这是scikit学习0.17.1,以下我从文档报价:带有稀疏训练数据的ExtraTreesClassifier?
Parameters: X : array-like or sparse matrix of shape = [n_samples, n_features]
的代码非常简单:
import pandas as pd
from scipy.sparse import coo_matrix, csr_matrix, hstack
from sklearn.ensemble import ExtraTreesClassifier
import numpy as np
from scipy import *
features = array([[1, 0], [0, 1], [3, 4]])
sparse_features = csr_matrix(features)
labels = array([0, 1, 0])
classifier = ExtraTreesClassifier()
classifier.fit(sparse_features, labels)
这里的例外:TypeError: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.
。这在传递features
时工作正常。
看起来文档已经过时,或者上面的代码有问题吗?
任何帮助将不胜感激。谢谢。
在文档中:“稀疏矩阵提供给稀疏csc_matrix”,请尝试使用csc_matrix。 – Alleo
另外,我已经在sklearn == 0.17.1上运行你的代码,并且它可以正常工作(使用csc和csr matricex)。 – Alleo
谢谢你,我已经尝试使用csc_matrix,它确实工作。我很抱歉,我对文档的理解是,如果它是一个稀疏矩阵,则X会在内部转换为该格式。请添加您的输入作为答案,我会结束这个问题。谢谢。 – user2916547