Python AttributeError:未找到稠密的
任务:使用Python AttributeError:未找到稠密的,python,machine-learning,scikit-learn,svm,tf-idf,Python,Machine Learning,Scikit Learn,Svm,Tf Idf,任务:使用CountVectorizer和TfidfTransformer使用SVC进行文档分类 我已经使用pickle对模型进行了训练、测试并保存了模型以及CountVectorizer和TfidfTransformer 现在,当我加载并使用它预测时,它给出了AttributeError:densed not found 我在培训和测试阶段都检查了输入的形状。都一样。但我认为形状不是问题 在培训和通过加载来使用它之间没有版本更新。所有内容都在同一版本上scikit learn==0.23.2
CountVectorizer
和TfidfTransformer
使用SVC
进行文档分类
我已经使用pickle
对模型进行了训练、测试并保存了模型以及CountVectorizer和TfidfTransformer
现在,当我加载并使用它预测时,它给出了AttributeError:densed not found
我在培训和测试阶段都检查了输入的形状。都一样。但我认为形状不是问题
在培训和通过加载来使用它之间没有版本更新。所有内容都在同一版本上scikit learn==0.23.2
这是我的密码:
#Training
X_train = train["Text"]
Y_train = train["Class"]
count = CountVectorizer(lowercase=False)
X_count_vector = count.fit_transform(X_train)
tfidf = TfidfTransformer(smooth_idf=True, use_idf=True)
X_train = tfidf.fit_transform(X_count_vector)
classifier = SVC()
classifier.fit(X_train.todense(), Y_train) #Training as dense matrix
#Testing
cvec = count.transform(test["Text"])
predable = tfidf.transform(cvec)
pred = classifier.predict(predable.todense()) #This line works, it gived the predicted values as expected.
#Saving the model
pickle.dump(classifier, open("classifier.txt", 'wb'))
pickle.dump(count, open("countVec.txt", 'wb'))
pickle.dump(tfidf, open("tfidf.txt", 'wb'))
#Loading the model
classifiernew = pickle.load(open("classifier.txt", 'rb'))
countVectornew = pickle.load(open("countVec.txt", 'rb'))
tfidfnew = pickle.load(open("tfidf.txt", 'rb'))
#Using the Loaded Model
newInp = test["Text"]
countVec = countVectornew.transform(newInp)
X_test = tfidfnew.transform(countVec)
#Prediction, this is where the Error enters in
predd = classifiernew.predict(X_test.todense()) #The error causing line
以下是完整的回溯:
res = clf.predict(testable.dense())
Traceback (most recent call last):
File "<ipython-input-74-d4714966beb3>", line 1, in <module>
res = clf.predict(testable.dense())
File "...\env\lib\site-packages\scipy\sparse\base.py", line 687, in __getattr__
raise AttributeError(attr + " not found")
AttributeError: dense not found
res=clf.predict(testable.dense())
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
res=clf.predict(testable.dense())
文件“…\env\lib\site packages\scipy\sparse\base.py”,第687行,在uu getattr中__
提升属性错误(属性+“未找到”)
AttributeError:未找到稠密的
好吧,在重新打开这个问题后,我可以发布这个问题的解决方法。
由于某种原因,.dense()
方法不起作用,我使用了.toarray()
来代替它,它非常适合我
愉快的建模。我建议发布一篇文章。@Davis我已经添加了代码,其中包含负责该问题的行。如果您需要任何详细信息,请告诉我。无法发布答案,但我找到了解决此问题的方法。出于某种原因,.dense()方法不起作用,而我使用了.toarray()方法,该方法对我来说非常有效。谢谢