我正在尝试在 Streamlit.io 应用程序上安装我的模型,但我收到了上述值错误。但它不会在 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)
最佳答案
一个重要线索是它可以在 Jupyter Notebook 中运行,但不能在 Streamlit 中运行,这表明您的工作环境存在差异。
当系列未正确比较时,您看到的错误是从 Pandas 发出的。有一个very good explanation of this error on this stackoverflow answer .
但是由于您的错误隐藏在 sklearn(而不是您自己的代码)中,因此您遇到的问题很可能可以通过将 Jupyter 中使用的 sklearn 版本与您使用 Streamlit 时安装的版本进行匹配来解决。
如果您使用您在每种情况下使用的 Pandas、SKlearn 和 Python 版本(Jupyter 和 Streamlit)更新您的帖子,将更容易帮助您解决这个问题。
将整个回溯(不仅仅是上半部分)作为纯文本而不是屏幕截图发布也可能有所帮助。
感谢您尝试 Streamlit!
关于python - Streamlit ValueError : The truth value of a Series is ambiguous. 使用 a.empty、a.bool()、a.item()、a.any() 或 a.all(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58781161/