【发布时间】:2020-04-04 00:44:54
【问题描述】:
我正在尝试编写检测假新闻的代码。不幸的是,我不断收到相同的错误消息。请有人能解释我哪里出错了吗?我从https://data-flair.training/blogs/advanced-python-project-detecting-fake-news/ 获得了一些代码行,从https://www.datacamp.com/community/tutorials/text-analytics-beginners-nltk 获得了一些代码行。当我尝试组合两个不同的代码(通过删除重复代码)时,我收到一条错误消息。
代码
%matplotlib inline
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import itertools
import json
import csv
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn import metrics
df = pd.read_csv(r"C:\Users\johnrambo\Downloads\fake_news(1).csv", sep=',', header=0, engine='python', escapechar='\\')
X_train, X_test, y_train, y_test = train_test_split(df['headline'], is_sarcastic_1, test_size = 0.2, random_state = 7)
clf = MultinomialNB().fit(X_train, y_train)
predicted = clf.predict(X_test)
print("MultinomialNB Accuracy:", metrics.accuracy_score(y_test, predicted))
错误
ValueError Traceback (most recent call last)
<ipython-input-8-e1f11a702626> in <module>
21 X_train, X_test, y_train, y_test = train_test_split(df['headline'], is_sarcastic_1, test_size = 0.2, random_state = 7)
22
---> 23 clf = MultinomialNB().fit(X_train, y_train)
24
25 predicted = clf.predict(X_test)
~\Anaconda\lib\site-packages\sklearn\naive_bayes.py in fit(self, X, y, sample_weight)
586 self : object
587 """
--> 588 X, y = check_X_y(X, y, 'csr')
589 _, n_features = X.shape
590
~\Anaconda\lib\site-packages\sklearn\utils\validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
717 ensure_min_features=ensure_min_features,
718 warn_on_dtype=warn_on_dtype,
--> 719 estimator=estimator)
720 if multi_output:
721 y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,
~\Anaconda\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
494 try:
495 warnings.simplefilter('error', ComplexWarning)
--> 496 array = np.asarray(array, dtype=dtype, order=order)
497 except ComplexWarning:
498 raise ValueError("Complex data not supported\n"
~\Anaconda\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
536
537 """
--> 538 return array(a, dtype, copy=False, order=order)
539
540
~\Anaconda\lib\site-packages\pandas\core\series.py in __array__(self, dtype)
946 warnings.warn(msg, FutureWarning, stacklevel=3)
947 dtype = "M8[ns]"
--> 948 return np.asarray(self.array, dtype)
949
950 # ----------------------------------------------------------------------
~\Anaconda\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
536
537 """
--> 538 return array(a, dtype, copy=False, order=order)
539
540
~\Anaconda\lib\site-packages\pandas\core\arrays\numpy_.py in __array__(self, dtype)
164
165 def __array__(self, dtype=None):
--> 166 return np.asarray(self._ndarray, dtype=dtype)
167
168 _HANDLED_TYPES = (np.ndarray, numbers.Number)
~\Anaconda\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
536
537 """
--> 538 return array(a, dtype, copy=False, order=order)
539
540
ValueError: could not convert string to float: 'experts caution new car loses 90% of value as soon as you drive it off cliff'
前几行数据
这是我输入 df.head().to_dict() 时得到的:
{'is_sarcastic': {0:1, 1:0, 2:0, 3:1, 4:1}, 'headline': {0: '三十多岁的科学家揭开了脱发的末日时钟', 1:'dem代表。完全说明了为什么国会在性别、种族平等方面做得不够”, 2:“吃你的蔬菜:9 种不同的美味食谱”, 3:“恶劣的天气使骗子无法上班”, 4:“妈妈几乎可以正确使用‘流媒体’这个词了”}, 'article_link': {0: 'https://www.theonion.com/thirtysomething-scientists-unveil-doomsday-clock-of-hai-1819586205', 1: 'https://www.huffingtonpost.com/entry/donna-edwards-inequality_us_57455f7fe4b055bb1170b207', 2: 'https://www.huffingtonpost.com/entry/eat-your-veggies-9-delici_b_8899742.html', 3: 'https://local.theonion.com/inclement-weather-prevents-liar-from-getting-to-work-1819576031', 4:'https://www.theonion.com/mother-comes-pretty-close-to-using-word-streaming-cor-1819575546'}}
【问题讨论】:
-
请将您的数据分享为
df.head().to_dict()并尝试生成mcve -
MultinomialNB().fit期望什么样的输入(numpy 数组、dtype、形状)?检查X_train、y_train的相同属性。 -
你好 rpanai,我已经编辑了我的帖子以显示添加代码 df.head().to_dict() 时显示的内容。
标签: python pandas numpy scikit-learn