我是 sklearn 的新手。我正在使用管道在文本挖掘问题中同时使用矢量化器和分类器。这是我的代码:
def create_ngram_model():
tfidf_ngrams = TfidfVectorizer(ngram_range=(1, 3),
analyzer="word", binary=False)
clf = GaussianNB()
pipeline = Pipeline([('vect', tfidf_ngrams), ('clf', clf)])
return pipeline
def get_trains():
data=open('../cleaning data/cleaning the sentences/cleaned_comments.csv','r').readlines()[1:]
lines=len(data)
features_train=[]
labels_train=[]
for i in range(lines):
l=data[i].split(',')
labels_train+=[int(l[0])]
a=l[2]
features_train+=[a]
return features_train,labels_train
def train_model(clf_factory,features_train,labels_train):
features_train,labels_train=get_trains()
features_train, features_test, labels_train, labels_test = cross_validation.train_test_split(features_train, labels_train, test_size=0.1, random_state=42)
clf=clf_factory()
clf.fit(features_train,labels_train)
pred = clf.predict(features_test)
accuracy = accuracy_score(pred,labels_test)
return accuracy
X,Y=get_trains()
print train_model(create_ngram_model,X,Y)
从 get_trains() 返回的特征是字符串。我收到此错误。
clf.fit(features_train,labels_train)
File "C:Python27libsite-packagessklearnpipeline.py", line 130, in fit
self.steps[-1][-1].fit(Xt, y, **fit_params)
File "C:Python27libsite-packagessklearnnaive_bayes.py", line 149, in fit
X, y = check_arrays(X, y, sparse_format='dense')
File "C:Python27libsite-packagessklearnutilsvalidation.py", line 263, in check_arrays
raise TypeError('A sparse matrix was passed, but dense '
TypeError: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.
我多次遇到此错误。然后,我只是将功能更改为 features_transformed.toarray(),但由于在这里,我使用的是管道,我无法这样做,因为转换后的功能会自动返回。我还尝试创建一个返回 features_transformed.toarray() 的新类,但这也引发了相同的错误。我搜索了很多,但没有得到它。请帮忙!!
有 2 个选项:
-
使用稀疏数据兼容分类器。例如,文档说伯努利朴素贝叶斯和多项朴素贝叶斯支持
fit
的稀疏输入。 -
向管道添加"增密器"。显然,你弄错了,这个对我有用(当我需要在此过程中增密我的稀疏数据时):
class Densifier(object): def fit(self, X, y=None): pass def fit_transform(self, X, y=None): return self.transform(X) def transform(self, X, y=None): return X.toarray()
确保在分类器之前将其放入管道中。