【问题标题】:How to do a single value prediction in NLP如何在 NLP 中进行单值预测
【发布时间】:2019-11-20 14:30:10
【问题描述】:

我的数据集是餐馆评论,有两列评论和点赞。 根据评论显示他们是否喜欢这家餐厅

我在 NLP 中清理数据作为第一步。然后作为第二步使用如下的词袋模型。

from sklearn.feature_extraction.text import CountVectorizer

cv = CountVectorizer(max_features = 1500)

X = cv.fit_transform(corpus).toarray()

y = dataset.iloc[:, 1].values

根据我的数据集,上面给出的 X 为 1500 列,0 和 1,1000 行。

我预测如下

y_pred = classifier.predict(X_test)

所以现在我的评论是“食物很好”,我如何预测他们是否喜欢。一个单一的值来预测。

请您帮帮我。如果需要其他信息,请告诉我。

谢谢

【问题讨论】:

  • 阅读关于情绪分析的词袋模型(正面和负面词)。仅供参考,这个论坛是发布用于调试的代码示例。您可能需要查看其他堆栈交换论坛,例如 datascience.stackexchange.com,以获得更好的建议。

标签: machine-learning nlp


【解决方案1】:

您只需要先申请cv.transform,就像这样:

>>> test = ['Food was good']
>>> test_vec = cv.transform(test)
>>> classifier.predict(test_vec)
# returns predicted class

【讨论】:

  • 非常感谢您帮助我。我试过了。这对我有用。
  • 很高兴我能帮上忙 :)
【解决方案2】:

这里的训练和测试是一个简单的例子:

培训:

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
text = ["This is good place","Hyatt is awesome hotel"]

count_vect = CountVectorizer()
tfidf_transformer = TfidfTransformer()
X_train_counts = count_vect.fit_transform(text)
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)

pd.DataFrame(X_train_tfidf.todense(), columns = count_vect.get_feature_names())
# Now apply any classification u want to on top of this data-set

现在测试:

注意:使用与训练中相同的转换:

new = ["I like the ambiance of this hotel "]

pd.DataFrame(tfidf_transformer.transform(count_vect.transform(new)).todense(), 
             columns = count_vect.get_feature_names())

现在在此之上应用 model.predict。

【讨论】:

  • 不用tfidfTransformer
【解决方案3】:

您也可以使用 sklearn 管道。

from sklearn.pipeline import Pipeline
model_pipeline = Pipeline([('vect', CountVectorizer()),('tfidf', TfidfTransformer()), ('model', classifier())]) #call the Model which you want to use

model_pipeline.fit_transform(x,y) # here x is your text data, and y is going to be your target
model_pipeline.predict(['Food was good"'])  # predict your new sentence

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 2018-07-18
    相关资源
    最近更新 更多