我正在尝试在Streamlit.io应用程序上拟合我的模型,但出现上述Value-Error。但这在Jupyter Notebook上不会给出相同的错误,请使用任何更好的方法都将有所帮助。
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
File "c:\users\8470p\anaconda3\lib\site-packages\streamlit\ScriptRunner.py", line 311, in _run_script exec(code, module.__dict__)
File "C:\Users\8470p\app2.py", line 122, in bow_transformer = CountVectorizer(analyzer=text_process).fit(messages['message'])
File "c:\users\8470p\anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 1024, in fit self.fit_transform(raw_documents)
File "c:\users\8470p\anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 1058, in fit_transform self.fixed_vocabulary_)
File "c:\users\8470p\anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 962, in _count_vocab analyze = self.build_analyzer()
File "c:\users\8470p\anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 339, in build_analyzer if self.analyzer == 'char':
File "c:\users\8470p\anaconda3\lib\site-packages\pandas\core\generic.py", line 1555, in __nonzero__ self.__class__.__name__
在此处输入代码
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.metrics import classification_report
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
bow_transformer =
CountVectorizer(analyzer=text_process).fit(messages['message'])
msg_train, msg_test, label_train, label_test =
train_test_split(messages['message'], messages['label'], test_size=0.2)
pipeline = Pipeline([
('bow', CountVectorizer(analyzer=text_process)), # strings to token
integer counts
('tfidf', TfidfTransformer()), # integer counts to weighted TF-IDF scores
('classifier', MultinomialNB()), # train on TF-IDF vectors w/ Naive Bayes
classifier
])
NB_Clasifier = pipeline.fit(msg_train,label_train)
答案 0 :(得分:1)
一个大线索是它可以在Jupyter笔记本电脑中使用,但不能在Streamlit中使用,这表明您的工作环境有所不同。
当未正确比较系列时,您看到的错误是从熊猫发出的。有一个very good explanation of this error on this stackoverflow answer。
但是,由于您的错误隐藏在sklearn中(而不是您自己的代码)中,因此,可以通过将Jupyter中使用的sklearn版本与使用Streamlit时安装的版本进行匹配来解决您遇到的问题。
如果用每种情况下使用的Pandas,SKlearn和Python版本(Jupyter和Streamlit)更新帖子,将更容易帮助您解决这一问题。
将整个追溯(不只是上半部分)以纯文本而不是屏幕快照的形式发布也可能有帮助。
感谢您试用Streamlit!