0% found this document useful (0 votes)
6 views4 pages

Import As Import As Import As From Import From Import From Import From Import From Import

The document outlines a process for building and evaluating a sentiment analysis model using the IMDB dataset with Keras. It includes data preprocessing, model architecture setup, training, and evaluation metrics such as accuracy and confusion matrix. The results indicate a test accuracy of 87% with a detailed classification report provided.

Uploaded by

yaseeniqbal365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Import As Import As Import As From Import From Import From Import From Import From Import

The document outlines a process for building and evaluating a sentiment analysis model using the IMDB dataset with Keras. It includes data preprocessing, model architecture setup, training, and evaluation metrics such as accuracy and confusion matrix. The results indicate a test accuracy of 87% with a detailed classification report provided.

Uploaded by

yaseeniqbal365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import numpy as np

import matplotlib.pyplot as plt

import seaborn as sns

from sklearn.metrics import confusion_matrix, classification_report

from tensorflow import keras

from tensorflow.keras.datasets import imdb

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense, Dropout, Embedding, GlobalMaxPooling1D

max_words = 10000

(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_words)

max_sequence_length = 500

x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=max_sequence_length)

x_test = keras.preprocessing.sequence.pad_sequences(x_test, maxlen=max_sequence_length)

model = Sequential()

model.add(Embedding(max_words, 100, input_length=max_sequence_length))

model.add(GlobalMaxPooling1D())

model.add(Dense(256, activation='relu'))

model.add(Dropout(0.5))

model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

batch_size = 64

epochs = 10
history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test,
y_test))

# Generate predictions

y_pred_probs = model.predict(x_test)

y_pred = (y_pred_probs > 0.5).astype(int)

# Plot predicted vs actual values

plt.figure(figsize=(10, 5))

plt.plot(y_test[:100], 'b.', label='Actual')

plt.plot(y_pred[:100], 'r.', label='Predicted')

plt.title('Actual vs Predicted Values')

plt.xlabel('Samples')

plt.ylabel('Sentiment')

plt.legend()

plt.show()

loss, accuracy = model.evaluate(x_test, y_test)

print("Test Loss:", loss)

print("Test Accuracy:", accuracy)


# Calculate metrics

cm = confusion_matrix(y_test, y_pred)

classification_rep = classification_report(y_test, y_pred)

# Plot confusion matrix

plt.figure(figsize=(8, 6))

sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')

plt.title('Confusion Matrix')

plt.xlabel('Predicted Label')

plt.ylabel('True Label')

plt.show()

# Print classification report

print("Classification Report:\n", classification_rep)


Classification Report:

precision recall f1-score support

0 0.86 0.87 0.87 12500

1 0.87 0.86 0.86 12500

accuracy 0.87 25000

macro avg 0.87 0.87 0.87 25000

weighted avg 0.87 0.87 0.87 25000

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy