Spaces:
Sleeping
Sleeping
File size: 7,533 Bytes
835448b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Movie Sentiment Analysis Model using Perceptron"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.datasets import imdb\n",
"import numpy as np\n",
"from tqdm import tqdm\n",
"from Perceptron import Perceptron\n",
"from tensorflow.keras.preprocessing import sequence\n",
"from sklearn.metrics import confusion_matrix, classification_report\n",
"import pickle"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|ββββββββββ| 5/5 [00:00<00:00, 20.08it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Training Completed\n",
"Accuracy: 50.00%\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"# Load the IMDB dataset\n",
"top_words = 5000\n",
"(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words)\n",
"\n",
"# Truncate and pad input sequences\n",
"max_review_length = 500\n",
"X_train = sequence.pad_sequences(X_train, maxlen=max_review_length)\n",
"X_test = sequence.pad_sequences(X_test, maxlen=max_review_length)\n",
"\n",
"# Convert data to binary (0/1) for perceptron\n",
"X_train_bin = np.where(X_train > 0, 1, 0)\n",
"X_test_bin = np.where(X_test > 0, 1, 0)\n",
"\n",
"# Initialize and train the Perceptron\n",
"perceptron = Perceptron(learning_rate=0.1, epochs=5)\n",
"perceptron.fit(X_train_bin, y_train)\n",
"\n",
"# Evaluate on test data\n",
"predictions = perceptron.predict(X_test_bin)\n",
"accuracy = np.mean(predictions == y_test)\n",
"print(\"Accuracy: {:.2f}%\".format(accuracy * 100))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Confusion Matrix:\n",
"[[ 0 12500]\n",
" [ 0 12500]]\n",
"\n",
"Classification Report:\n",
" precision recall f1-score support\n",
"\n",
" 0 0.00 0.00 0.00 12500\n",
" 1 0.50 1.00 0.67 12500\n",
"\n",
" accuracy 0.50 25000\n",
" macro avg 0.25 0.50 0.33 25000\n",
"weighted avg 0.25 0.50 0.33 25000\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"d:\\STUDY\\Sem3\\deeplearning\\DLENV\\lib\\site-packages\\sklearn\\metrics\\_classification.py:1471: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
" _warn_prf(average, modifier, msg_start, len(result))\n",
"d:\\STUDY\\Sem3\\deeplearning\\DLENV\\lib\\site-packages\\sklearn\\metrics\\_classification.py:1471: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
" _warn_prf(average, modifier, msg_start, len(result))\n",
"d:\\STUDY\\Sem3\\deeplearning\\DLENV\\lib\\site-packages\\sklearn\\metrics\\_classification.py:1471: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
" _warn_prf(average, modifier, msg_start, len(result))\n"
]
}
],
"source": [
"# Calculate confusion matrix\n",
"cm = confusion_matrix(y_test, predictions)\n",
"\n",
"# Generate classification report\n",
"report = classification_report(y_test, predictions)\n",
"\n",
"# Display confusion matrix and classification report\n",
"print(\"Confusion Matrix:\")\n",
"print(cm)\n",
"print(\"\\nClassification Report:\")\n",
"print(report)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Save the instance of the Perceptron class\n",
"with open('perceptron_movie_model.pkl', 'wb') as file:\n",
" pickle.dump(perceptron, file)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def predict_sentiment_perceptron(review, perceptron_model, max_review_length):\n",
" word_index = imdb.get_word_index()\n",
" review = review.lower().split()\n",
" review = [word_index[word] if (word in word_index and word_index[word] < top_words) else 0 for word in review]\n",
" review_bin = np.where(np.array(review) > 0, 1, 0)\n",
" # Padding or truncating the review to match the perceptron's input size\n",
" review_bin_padded = np.pad(review_bin, (0, max_review_length - len(review_bin)), 'constant')\n",
" prediction = perceptron_model.predict([review_bin_padded])\n",
" if prediction[0] == 1:\n",
" return \"Positive\"\n",
" else:\n",
" return \"Negative\"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Predicted Sentiment: Positive\n"
]
}
],
"source": [
"# Example usage after training the perceptron\n",
"example_review = \"This movie was fantastic! I loved every bit of it.\"\n",
"sentiment = predict_sentiment_perceptron(example_review, perceptron, max_review_length)\n",
"print(\"Predicted Sentiment:\", sentiment)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Predicted Sentiment: Positive\n"
]
}
],
"source": [
"# Example usage after training the perceptron\n",
"example_review = \"This movie was bad!.\"\n",
"sentiment = predict_sentiment_perceptron(example_review, perceptron, max_review_length)\n",
"print(\"Predicted Sentiment:\", sentiment)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Predicted Sentiment: Positive\n"
]
}
],
"source": [
"example_review = \"This movie was terrible. The acting was awful, and the plot was confusing.\"\n",
"sentiment = predict_sentiment_perceptron(example_review, perceptron, max_review_length)\n",
"print(\"Predicted Sentiment:\", sentiment)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "DLENV",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|