Error: "ValueError: cannot use sparse input in 'SVR' trained on dense data"?

Error: "ValueError: cannot use sparse input in 'SVR' trained on dense data"?

from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer()
vector = vectorizer.fit_transform(X_train).toarray()
print(vector.shape)
print(type(vector))
print(vector)    
vector

这给出 class 'numpy.ndarray' 形状 (97, 370)

from sklearn.svm import SVR
from sklearn.model_selection import cross_val_score
clf = SVR(gamma='auto',cache_size=12000,max_iter=-1)
print("Training the data set...")
clf = clf.fit(vector, y_train)
print("Training Completed")
test_vectors = vectorizer.transform(X_test)
test_vectors

这给出 <108x370 类型的稀疏矩阵 '' 以压缩稀疏行格式存储 1212 个元素>

clf.predict(test_vectors)

这给出了错误 "ValueError: cannot use sparse input in 'SVR' trained on dense data"

这里有什么问题?我该如何解决?谢谢!

您应该调用 .toarray(),就像您对火车数据所做的那样:

test_vectors = vectorizer.transform(X_test).toarray()