![](/img/trans.png)
[英]How to predict future data or data of an unknown range after training an LSTM model with a time series dataset?
[英]How to predict the label after training the dataset in NLP
我正在嘗試對評論進行情緒分析; 該數據集包含兩個主要列:第一個是“評論”,其中包含用戶的評論,第二個列是正面還是負面; 我從源頭得到了一個模板來預處理數據,訓練和測試還可以。 但是,我想輸入一個文本並希望 model 預測它是正面還是負面。 我嘗試了這么多輸入的 forms:僅字符串,字符串列表,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.
正如 G.Anderson 已經提到的那樣,您的分類器是用數字數據訓練的,就像您使用的那樣:
X=cv.fit_transform(corpus).toarray()
CountVectorizer 就是為此而生的。
要使用它,您還必須使用經過訓練的 CountVectorizer,您必須實現:
# Predicting the Test set results
xeval=["I like it okay"]
xeval_numeric = cv.transform(xeval).toarray()
prediction=classifier.predict(xeval_numeric)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.