Spaces:
Sleeping
Sleeping
File size: 9,117 Bytes
0e876c8 045e883 0e876c8 045e883 0e876c8 045e883 0e876c8 045e883 0e876c8 045e883 0e876c8 045e883 0e876c8 045e883 0e876c8 045e883 0e876c8 045e883 0e876c8 045e883 0e876c8 045e883 0e876c8 045e883 0e876c8 045e883 0e876c8 045e883 0e876c8 |
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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dataset from hugging face"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" id place label \\\n",
"0 2401 Borderlands Positive \n",
"1 2401 Borderlands Positive \n",
"2 2401 Borderlands Positive \n",
"3 2401 Borderlands Positive \n",
"4 2401 Borderlands Positive \n",
"\n",
" text \n",
"0 im getting on borderlands and i will murder yo... \n",
"1 I am coming to the borders and I will kill you... \n",
"2 im getting on borderlands and i will kill you ... \n",
"3 im coming on borderlands and i will murder you... \n",
"4 im getting on borderlands 2 and i will murder ... \n"
]
}
],
"source": [
"import pandas as pd \n",
"\n",
"column_names = ['id',\"place\",\"label\", \"text\"]\n",
"#Train Dataset\n",
"train_df = pd.read_csv(\"twitter_training.csv\", names=column_names, header=None)\n",
"\n",
"#Test Dataset\n",
"test_df = pd.read_csv(\"twitter_validation.csv\", names=column_names, header=None)\n",
"\n",
"\n",
"print(train_df.head())\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[nltk_data] Downloading package stopwords to C:\\Users\\Regino Balogo\n",
"[nltk_data] Jr\\AppData\\Roaming\\nltk_data...\n",
"[nltk_data] Package stopwords is already up-to-date!\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sample cleaned text:\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>text</th>\n",
" <th>clean_text</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>im getting on borderlands and i will murder yo...</td>\n",
" <td>im getting borderlands murder</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>I am coming to the borders and I will kill you...</td>\n",
" <td>coming borders kill</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>im getting on borderlands and i will kill you ...</td>\n",
" <td>im getting borderlands kill</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>im coming on borderlands and i will murder you...</td>\n",
" <td>im coming borderlands murder</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>im getting on borderlands 2 and i will murder ...</td>\n",
" <td>im getting borderlands 2 murder</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" text \\\n",
"0 im getting on borderlands and i will murder yo... \n",
"1 I am coming to the borders and I will kill you... \n",
"2 im getting on borderlands and i will kill you ... \n",
"3 im coming on borderlands and i will murder you... \n",
"4 im getting on borderlands 2 and i will murder ... \n",
"\n",
" clean_text \n",
"0 im getting borderlands murder \n",
"1 coming borders kill \n",
"2 im getting borderlands kill \n",
"3 im coming borderlands murder \n",
"4 im getting borderlands 2 murder "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import re\n",
"import nltk\n",
"from nltk.corpus import stopwords\n",
"\n",
"# Stopwords\n",
"nltk.download(\"stopwords\")\n",
"stop_words = set(stopwords.words(\"english\"))\n",
"\n",
"# Clean Text\n",
"def preprocess_text(text):\n",
" if isinstance(text, float): # Handle missing values\n",
" return \"\"\n",
" \n",
" text = text.lower() # Convert to lowercase\n",
" text = re.sub(r\"\\W\", \" \", text) # Remove special characters\n",
" text = re.sub(r\"\\s+\", \" \", text).strip() # Remove extra spaces\n",
" text = \" \".join([word for word in text.split() if word not in stop_words]) # Remove stopwords\n",
" return text\n",
"\n",
"# Apply preprocessing to the text column\n",
"train_df[\"clean_text\"] = train_df[\"text\"].apply(preprocess_text)\n",
"test_df[\"clean_text\"] = test_df[\"text\"].apply(preprocess_text)\n",
"\n",
"print(\"Sample cleaned text:\")\n",
"display(train_df[[\"text\", \"clean_text\"]].head())\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TF-IDF vectorization complete! β
\n",
"Training data shape: (74682, 5000)\n",
"Testing data shape: (1000, 5000)\n"
]
}
],
"source": [
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"\n",
"# TF-IDF Vectorizer\n",
"vectorizer = TfidfVectorizer(max_features=5000)\n",
"\n",
"# Fit and transform training data, then transform test data\n",
"X_train = vectorizer.fit_transform(train_df[\"clean_text\"])\n",
"X_test = vectorizer.transform(test_df[\"clean_text\"])\n",
"\n",
"# Extract labels\n",
"y_train = train_df[\"label\"]\n",
"y_test = test_df[\"label\"]\n",
"\n",
"print(\"TF-IDF vectorization complete! β
\")\n",
"print(f\"Training data shape: {X_train.shape}\")\n",
"print(f\"Testing data shape: {X_test.shape}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model Accuracy: 0.8120\n",
"\n",
"Classification Report:\n",
" precision recall f1-score support\n",
"\n",
" Irrelevant 0.82 0.73 0.77 172\n",
" Negative 0.78 0.89 0.83 266\n",
" Neutral 0.85 0.76 0.80 285\n",
" Positive 0.81 0.84 0.82 277\n",
"\n",
" accuracy 0.81 1000\n",
" macro avg 0.81 0.81 0.81 1000\n",
"weighted avg 0.81 0.81 0.81 1000\n",
"\n"
]
}
],
"source": [
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.metrics import accuracy_score, classification_report\n",
"\n",
"# Train the model\n",
"model = LogisticRegression(max_iter=1000)\n",
"model.fit(X_train, y_train)\n",
"\n",
"# Make predictions\n",
"y_pred = model.predict(X_test)\n",
"\n",
"# Evaluate the model\n",
"accuracy = accuracy_score(y_test, y_pred)\n",
"print(f\"Model Accuracy: {accuracy:.4f}\")\n",
"\n",
"# Display classification report\n",
"print(\"\\nClassification Report:\")\n",
"print(classification_report(y_test, y_pred))\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model and vectorizer saved successfully! β
\n"
]
}
],
"source": [
"import joblib\n",
"\n",
"# Save the trained model\n",
"joblib.dump(model, \"sentiment_model.pkl\")\n",
"\n",
"# Save the TF-IDF vectorizer\n",
"joblib.dump(vectorizer, \"tfidf_vectorizer.pkl\")\n",
"\n",
"print(\"Model and vectorizer saved successfully! β
\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|