ium_464979/IUM_05-predict.py

31 lines
1.1 KiB
Python
Raw Normal View History

2024-04-16 18:58:43 +02:00
import pandas as pd
import numpy as np
import tensorflow as tf
2024-05-06 19:46:35 +02:00
# Load the test data
2024-04-16 18:58:43 +02:00
test_data = pd.read_csv('./beer_reviews_test.csv')
X_test = test_data[['review_aroma', 'review_appearance', 'review_palate', 'review_taste']]
2024-05-06 19:39:44 +02:00
y_test = test_data['review_overall']
2024-04-16 18:58:43 +02:00
2024-05-06 19:46:35 +02:00
# Load the model
2024-04-16 18:58:43 +02:00
model = tf.keras.models.load_model('beer_review_sentiment_model.h5')
2024-05-06 19:46:35 +02:00
# Preprocess the test data
2024-04-16 18:58:43 +02:00
tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=10000)
2024-05-06 19:39:44 +02:00
tokenizer.fit_on_texts(X_test)
2024-04-16 18:58:43 +02:00
X_test_seq = tokenizer.texts_to_sequences(X_test)
X_test_pad = tf.keras.preprocessing.sequence.pad_sequences(X_test_seq, maxlen=100)
2024-05-06 19:46:35 +02:00
# Make predictions
2024-04-16 18:58:43 +02:00
predictions = model.predict(X_test_pad)
2024-05-06 19:46:35 +02:00
# Check the shape of the predictions
print(f'Predictions shape: {predictions.shape}')
# If predictions have more than one dimension, select only one dimension
if len(predictions.shape) > 1:
predictions = predictions[:, 0]
# Save predictions and actual data to a CSV file
results = pd.DataFrame({'Predictions': predictions, 'Actual': y_test})
2024-05-06 19:39:44 +02:00
results.to_csv('beer_review_sentiment_predictions.csv', index=False)