Everything runs right until I get to the final lines of code that simply don't run. No error or anything, it just doesn't execute and I don't know why. I want to test the accuracy of a model to predict the rating a person gave to a hotel based on a review, so it includes integers and strings, but to what I know, all my data is set as a str
Code:
(I have some variables with data text, but are too long so I will just skip that)
train_reviews = [r1, r2]
train_reviews_label = [rating1, rating2]
test_reviews = [r3]
test_reviews_label = [rating3]
vectorizer = CountVectorizer() # Countvectorizer
train_vect = vectorizer.fit_transform(train_reviews) # fit_transform fits the train tweets and returns the sparse matrix of the tweets
model = LogisticRegression() # create a LogisticRegression Model
model.fit(train_vect, train_reviews_label) # Fit the data values to the model
I even made a prediction test and it ran just fine:
#Prediction test
test_vect = vectorizer.transform(reviews)
result = model.predict(test_vect)
print('Actual Category: {}\nPredicted Category: {}'.format(rating3, result[0]))
X_train, X_test, y_train, y_test = train_test_split(tokens, reviews, test_size=0.2, random_state=2)
def train_model(reviews_to_train,train_labels):
"""
param: reviews_to_train - list of tweets to train on
return: the vectorizer, the logistic regression model, the train_vector
"""
train_reviews = [" ".join(t) for t in reviews_to_train]
train_reviews_label = [l for l in train_labels]
# vectorizer = Initialize CountVectorizer
# fit the train_tweets in the CountVectorizer using the vectorizer's fit method
# train_vect = get the sparse matrix of the train_tweets from the vectorizer using the transform function
# model = initialize a Logistic Regression model
# fit train_vect and train_tweets_label using the fit function of LogisticRegression
vectorizer = CountVectorizer() # Countvectorizer
train_vect = vectorizer.fit_transform(train_reviews) # fit_transform fits the train tweets and returns the sparse matrix of the tweets
model = LogisticRegression() # create a LogisticRegression Model
model.fit(train_vect, train_reviews_label) # Fit the data values to the model
return model,vectorizer
def predict(reviews_to_test, vectorizer, model):
"""
param: reviews_to_test - list of tweets to test the model on
param: vectorizer - the CountVectorizer
param: model - the LogisticRegression model
return result (the prediction), the test_vect
"""
test_reviews = [" ".join(t) for t in reviews_to_test]
print (test_reviews)
### Your code starts here ###
# test_vect = transform the test_tweets to sparce matrix using the vectorizer's transform function
test_vect = vectorizer.transform(test_reviews)
# result = predict the result using the model's predict function on test_vect
result = model.predict(test_vect)
return result
And this are the lines that don't run for apparently reason:
model,train_countvect = train_model(X_train,y_train)
#Predict labels for test set
y_pred = predict(X_test,train_countvect,model)
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))