我正在尝试对评论进行情感分析;数据集包含两个主要的列:第一个是具有用户评论的“评论”,第二个列是正面还是负面。我从源头获得了处理数据的模板,培训和测试还可以。但是,我想输入一个文本并希望模型预测它是正数还是负数。我尝试了很多形式的输入:仅字符串,字符串列表,从numpy到数组等。有什么想法如何输入要预测的数据? 这是我的代码:
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('Restaurant_Reviews.tsv',delimiter='\t',quoting=3)
import re
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
corpus=[]
for i in range(0,1000):
review=re.sub('[^a-zA-Z]',' ',dataset['Review'][i])
review.lower()
review=review.split()
ps=PorterStemmer()
review=[ps.stem(word) for word in review if not word in set(stopwords.words('english'))]
review=' '.join(review)
corpus.append(review)
#the bag of word
from sklearn.feature_extraction.text import CountVectorizer
cv=CountVectorizer(max_features=1500)
X=cv.fit_transform(corpus).toarray()
y=dataset.iloc[:,1].values
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2,random_state = 0)
# Fitting Naive Bayes to the Training set
from sklearn.naive_bayes import GaussianNB
classifier = GaussianNB()
classifier.fit(X_train,y_train)
# Predicting the Test set results
xeval=["I like it okay"]
prediction=classifier.predict(xeval)```
the error in this case is:
Expected 2D array,got 1D array instead:
array=['I like it okay'].
Reshape your data either using array.reshape(-1,1) if your data has a single feature or array.reshape(1,-1) if it contains a single sample.