- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个简单的推荐器,它会根据推文的相似性推荐其他用户。我使用 tfidf 对所有文本进行矢量化,并且能够将数据放入 MultinomialNB 中,但在尝试预测时不断出现错误
我尝试将数据重新整形为数组,但出现错误无法将字符串转换为 float 。我什至可以对这些数据使用这个算法吗?我尝试了不同的列来查看是否得到结果,但位置错误相同。
ValueError Traceback (most recent call last)
<ipython-input-39-a982bc4e1f49> in <module>
20 nb_mul.fit(train_idf,y_train)
21 user_knn = UserUser(10, min_sim = 0.4, aggregate='weighted-average')
---> 22 nb_mul.predict(y_test)
23 #nb_mul.predict(np.array(test['Tweets'], test['Sentiment']))
24 #TODO: find a way to predict with test data
~/anaconda2/lib/python3.6/site-packages/sklearn/naive_bayes.py in predict(self, X)
64 Predicted target values for X
65 """
---> 66 jll = self._joint_log_likelihood(X)
67 return self.classes_[np.argmax(jll, axis=1)]
68
~/anaconda2/lib/python3.6/site-packages/sklearn/naive_bayes.py in _joint_log_likelihood(self, X)
728 check_is_fitted(self, "classes_")
729
--> 730 X = check_array(X, accept_sparse='csr')
731 return (safe_sparse_dot(X, self.feature_log_prob_.T) +
732 self.class_log_prior_)
~/anaconda2/lib/python3.6/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)
525 try:
526 warnings.simplefilter('error', ComplexWarning)
--> 527 array = np.asarray(array, dtype=dtype, order=order)
528 except ComplexWarning:
529 raise ValueError("Complex data not supported\n"
~/anaconda2/lib/python3.6/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: '["b\'RT @Avalanche: Only two cities have two teams in the second round of the playoffs...\\\\n\\\\nDenver and Boston!\\\\n\\\\n#MileHighBasketball #GoAvsGo http\\\\xe2\\\\x80\\\\xa6\'"]'
for train, test in xf.partition_users(final_test[['user','Tweets','Sentiment']],5, xf.SampleFrac(0.2)):
x_train = []
for index, row in train.iterrows():
x_train.append(row['Tweets'])
y_train = np.array(train['Sentiment'])
y_test = np.array([test['user'],test['Tweets']])
#print(y_train)
tfidf = TfidfVectorizer(min_df=5, max_df = 0.8, sublinear_tf=True, use_idf=True,stop_words='english', lowercase=False)
train_idf = tfidf.fit(x_train)
train_idf = train_idf.transform(x_train)
nb_mul = MultinomialNB()
nb_mul.fit(train_idf,y_train)
user_knn = UserUser(10, min_sim = 0.4, aggregate='weighted-average')
nb_mul.predict(y_test)
数据看起来像这样
user Tweets \
0 2287418996 ["b'RT @HPbasketball: This stuff is 100% how K...
1 2287418996 ["b'@KeuchelDBeard I may need to rewatch Begin...
2 2287418996 ["b'@keithlaw Is that the stated reason for th...
3 2287418996 ['b"@keithlaw @Yanks23242 I definitely don\'t ...
4 2287418996 ["b'@Yanks23242 @keithlaw Sorry, please sub Jo...
Sentiment Score
0 neu 0.815
1 neu 0.744
2 neu 1.000
3 neu 0.863
4 neu 0.825
同样,我希望插入用户的推文和情绪,并根据相似性在数据中推荐另一个用户。
最佳答案
您不应将推文直接提供给分类器。您需要使用合适的 TfidfVectorizer
将文本转换为矢量。
进行以下更改
nb_mul.predict(tfidf.transform(test['Tweets']))
了解该模型只会给出测试数据推文的情绪。
如果您的目的是推荐,请尝试使用其他推荐方法。
关于python - 如何使用用户、文本数据调用 MultinomialNB.predict()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55940318/
给定一个包含 100 行的数据表,例如: Place | Text | Value | Text_Two europe | some random text | 3.
好的,我正在学习 Andrew Ng 的机器学习类(class)。我正在阅读 this chapter并想使用 SKLearn 和 Python 为自己尝试多项式朴素贝叶斯(第 12 页底部)。所以
我已经能够制作一个 MultinomialNB 分类器并将其保存到 pickle 文件中以备后用(感谢 youtube 视频:https://www.youtube.com/watch?v=0kPRa
我有一个形状为 (100000, 56000) 的 BoW 向量,并且我想使用 scikit-learn 中的 MultinomialNB 来执行分类任务。 MultinomialNB 是否采用稀疏矩
我目前正在尝试为分类特征推出我自己的朴素贝叶斯分类器,以确保我理解它们。现在我想将它们与 sklearns MultinomialNB 进行比较。但由于某种原因,我无法让 skearn 版本正确运行。
这是一个关于功能集的非常基本的问题。 假设我有一群具有各种特征的人,我想向他们提出建议。他们还写了一段自由格式的文本,这对于我需要向他们推荐的内容非常重要。 我可以理解如何矢量化他们的示例文本,但我不
我需要创建一个模型,根据变量对记录进行准确分类。例如,如果记录具有预测变量 A 或 B,我希望将其分类为具有预测值 X。实际数据是这样的形式: Predicted Predictor
在我提出这个问题之前,我不得不说我已经彻底阅读了这个论坛上超过 15 个类似的主题,每个主题都有不同的建议,但所有这些都无法让我正确理解。 好的,所以我将我的“垃圾邮件”文本数据(最初为 csv 格式
我正在使用 sklearn 多项朴素贝叶斯分类器对 20NewsGroup 数据进行分类。代码如下: import numpy as np import operator from sklearn i
我正在创建一个简单的推荐器,它会根据推文的相似性推荐其他用户。我使用 tfidf 对所有文本进行矢量化,并且能够将数据放入 MultinomialNB 中,但在尝试预测时不断出现错误 我尝试将数据重新
我正在尝试使用 sci kit learn 在 python 中的一系列示例上运行多项式朴素贝叶斯。我一直将所有例子都归类为负面。训练集有点偏向负数 P(负数) ~.75。我翻阅了documentat
如何计算多类文本分类的 FPR、TPR、AUC、roc_curve。 我使用了以下代码: from sklearn.model_selection import train_test_split X_
在 sklearn 中运行 MultinomialNB 之前,我正在标准化我的文本输入: vectorizer = TfidfVectorizer(max_df=0.5, stop_words='en
我正在尝试让我的 MultinomialNB 工作。我在训练和测试集上使用 CountVectorizer,当然两个集中都有不同的单词。所以我明白了,为什么会出现错误 ValueError: dime
我目前正在尝试使用 Python 和 Scikit-learn 构建一些文本分类工具。 我的文本不是英文的,因此不受词干分解或其他基于英文的降维的通常处理。 结果,TfIdf矩阵变得非常大(150,0
# Note: The runnable code example is at the end of this question #### # Assume X_train contains clea
问题:将用户产品分类为禁止或批准。 如果商品包含“濒临灭绝的物种”、“虎皮”等 输入:产品标题+描述 相应标签:已批准/禁止 我使用监督学习算法MultinomialNB对产品进行分类,它的二元组准确
我正在尝试比较多项式、二项式和伯努利分类器的性能,但出现错误: TypeError: float() argument must be a string or a number, not 'set'
我是一名优秀的程序员,十分优秀!