Created
September 18, 2023 22:56
-
-
Save fsndzomga/2407224ba32f5d756dadf626fcb95f35 to your computer and use it in GitHub Desktop.
using the transformers library for sentiment analysis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from transformers import pipeline | |
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report | |
import pandas as pd | |
df = pd.read_csv('amazon_cells_labelled.txt', delimiter='\t', header=None, names=['Review', 'Sentiment']) | |
classifier = pipeline("sentiment-analysis") | |
predicted_sentiments = [] | |
for index, row in df.iterrows(): | |
result = classifier(row['Review']) | |
if result[0]['label'] == 'POSITIVE': | |
sentiment = 1 | |
else: | |
sentiment = 0 | |
predicted_sentiments.append(sentiment) | |
df['predicted_sentiment'] = predicted_sentiments | |
# Evaluate the performance using accuracy, confusion matrix, and classification report | |
accuracy = accuracy_score(df['Sentiment'], df['predicted_sentiment']) | |
conf_matrix = confusion_matrix(df['Sentiment'], df['predicted_sentiment']) | |
class_report = classification_report(df['Sentiment'], df['predicted_sentiment']) | |
accuracy, conf_matrix, class_report |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment