传递了一个稀疏矩阵,但需要密集数据.使用 X.toarray() 转换为密集的 numpy 数组 [英] A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array

查看:39
本文介绍了传递了一个稀疏矩阵,但需要密集数据.使用 X.toarray() 转换为密集的 numpy 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下,我正在尝试将训练数据用于 GBRT 回归树,相同的数据适用于其他分类器,但给出了 GBRT 的上述错误.请帮忙:

Code goes as follows, I am trying to use training data for GBRT regression trees, same data works good for other classifiers but gives above error for GBRT. please help :

dataset = load_files('train')
vectorizer = TfidfVectorizer(encoding='latin1')
X_train = vectorizer.fit_transform((open(f).read() for f in dataset.filenames)) 
assert sp.issparse(X_train)     
print("n_samples: %d, n_features: %d" % X_train.shape)
y_train = dataset.target
def benchmark(clf_class, params, name):
    clf = clf_class(**params).fit(X_train, y_train)

推荐答案

我在尝试使用 load_svmlight_files 加载的数据训练 GradientBoostingClassifier 时遇到了同样的问题.通过将稀疏矩阵转换为 numpy 数组来解决.

I came accross the same problem trying to train a GradientBoostingClassifier using the data loaded by load_svmlight_files. Solved by transforming a sparse matrix to a numpy array.

X_train.todense()

这篇关于传递了一个稀疏矩阵,但需要密集数据.使用 X.toarray() 转换为密集的 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆