Grok 4.1 Fast· xai
Frontier Reasoning Traces
Write Python code to perform sentiment analysis on tweets using an LSTM model.
- User
Write Python code to perform sentiment analysis on tweets using an LSTM model.
- Assistant
import pandas as pd import numpy as np import re import nltk from nltk.corpus import stopwords from nltk.stem import WordNetLemmatizer from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report, confusion_matrix, accuracy_score from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout, Bidirectional from tensorflow.keras.optimizers import Adam from tensorflow.keras.callbacks import EarlyStopping import matplotlib.pyplot as plt # Download required NLTK data (run once) nltk.download('stopwords') nltk.download('wordnet') nltk.download('omw-1.4') print("NLTK data downloaded. Ready to proceed.") # Step 1: Sample Tweet Dataset (for demo; replace with real CSV like Kaggle's Twitter Sentiment dataset) # Columns: 'text' (tweet), 'sentiment' (0: negative, 1: positive) sample_data = { 'text': [ "I love this product! It's amazing and super helpful.", "This is the worst app ever, full of bugs.", "Great service, highly recommend to everyone!", "Terrible quality, completely disappointed.", "Fantastic movie, best one this year! #loveit", "Hate this restaurant, food was awful.", "Outstanding performance by the team! Proud.", "Waste of money, never buying again.", "Absolutely brilliant, 5 stars all the way.", "So frustrating, app crashes every time.", "Incredible experience, will come back soon.", "Poor customer service, very rude staff.", "Loving every minute of this game! Epic.", "Disgusting food, got sick after eating.", "Perfect fit, comfortable and stylish.", "Horrible weather ruined our plans.", "Thrilled with the results, thank you!", "Boring lecture, fell asleep halfway.", "Best vacation ever, memories for life.", "Scam alert, do not trust this seller." ], 'sentiment': [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0] } df = pd.DataFrame(sample_data) print(f"Dataset shape: {df.shape}") print(df.head()) # Step 2: Text Preprocessing Function def preprocess_text(text): # Remove URLs, mentions, hashtags text = re.sub(r'http\S+|www\S+|https\S+', '', text) text = re.sub(r'@\w+|#\w+', '', text) # Remove punctuation and numbers, lowercase text = re.sub(r'[^a-zA-Z\s]', '', text) text = text.lower().strip() # Tokenize and remove stopwords, lemmatize tokens = text.split() stop_words = set(stopwords.words('english')) lemmatizer = WordNetLemmatizer() tokens = [lemmatizer.lemmatize(token) for token in tokens if token not in stop_words and len(token) > 2] return ' '.join(tokens) # Apply preprocessing df['clean_text'] = df['text'].apply(preprocess_text) print("\nSample preprocessed tweets:") print(df[['text', 'clean_text']].head()) # Step 3: Tokenization and Padding max_words = 1000 # Max vocabulary size max_len = 20 # Max sequence length (tweets are short) tokenizer = Tokenizer(num_words=max_words, oov_token='<OOV>') tokenizer.fit_on_texts(df['clean_text']) sequences = tokenizer.texts_to_sequences(df['clean_text']) X = pad_sequences(sequences, maxlen=max_len, padding='post', truncating='post') y = df['sentiment'].values # Train-test split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y) print(f"\nTrain shape: {X_train.shape}, Test shape: {X_test.shape}") # Step 4: Build LSTM Model vocab_size = max_words embedding_dim = 64 lstm_units = 64 model = Sequential([ Embedding(vocab_size, embedding_dim, input_length=max_len), Bidirectional(LSTM(lstm_units, dropout=0.3, recurrent_dropout=0.3, return_sequences=False)), Dropout(0.3), Dense(32, activation='relu'), Dropout(0.3), Dense(1, activation='sigmoid') ]) model.compile( optimizer=Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy'] ) model.summary() # Step 5: Train the Model early_stop = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True) history = model.fit( X_train, y_train, epochs=20, batch_size=16, validation_split=0.2, callbacks=[early_stop], verbose=1 ) # Plot training history plt.figure(figsize=(12, 4)) plt.subplot(1, 2, 1) plt.plot(history.history['accuracy'], label='Train Acc') plt.plot(history.history['val_accuracy'], label='Val Acc') plt.title('Model Accuracy') plt.legend() plt.subplot(1, 2, 2) plt.plot(history.history['loss'], label='Train Loss') plt.plot(history.history['val_loss'], label='Val Loss') plt.title('Model Loss') plt.legend() plt.show() # Step 6: Evaluate y_pred = (model.predict(X_test) > 0.5).astype(int).flatten() print("\nClassification Report:") print(classification_report(y_test, y_pred)) print("Confusion Matrix:") print(confusion_matrix(y_test, y_pred)) print(f"Test Accuracy: {accuracy_score(y_test, y_pred):.4f}") # Step 7: Prediction Function for New Tweets def predict_sentiment(new_tweet): clean = preprocess_text(new_tweet) seq = tokenizer.texts_to_sequences([clean]) padded = pad_sequences(seq, maxlen=max_len) pred = model.predict(padded, verbose=0)[0][0] sentiment = 'Positive' if pred > 0.5 else 'Negative' confidence = pred if pred > 0.5 else 1 - pred return sentiment, confidence # Examples test_tweets = [ "This is awesome! I love it so much.", "What a terrible day, everything went wrong.", "Neutral day, nothing special.", "OMG this tweet is fire! 🔥" ] print("\nPredictions on new tweets:") for tweet in test_tweets: sent, conf = predict_sentiment(tweet) print(f"Tweet: '{tweet}' → {sent} (confidence: {conf:.3f})") print("\nNote: This uses a small sample dataset for demo. For production, load a larger dataset (e.g., via pd.read_csv('sentiment140.csv')). Adjust hyperparameters as needed.")How to Run:
- Install dependencies:
pip install tensorflow pandas numpy scikit-learn nltk matplotlib - Run the script. It downloads NLTK data automatically.
- Customization:
- Replace
sample_datawithdf = pd.read_csv('your_tweets.csv')(ensure 'text' and 'sentiment' columns). - Tune
max_words,max_len,embedding_dim,lstm_units. - For multi-class (pos/neg/neu), change output to
Dense(3, activation='softmax')and usecategorical_crossentropy. - Use pre-trained embeddings like GloVe for better performance.
- Replace
This is a complete, runnable LSTM-based sentiment analyzer for tweets! 🚀
- Install dependencies:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.