noshot 8.0.0__py3-none-any.whl → 9.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,409 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {
6
+ "id": "L1Yh9i9SlaTq"
7
+ },
8
+ "source": [
9
+ "***ML LAB CIA 2***"
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "markdown",
14
+ "metadata": {
15
+ "id": "oEflBCT-lgmq"
16
+ },
17
+ "source": [
18
+ "**Q1**"
19
+ ]
20
+ },
21
+ {
22
+ "cell_type": "code",
23
+ "execution_count": null,
24
+ "metadata": {
25
+ "colab": {
26
+ "base_uri": "https://localhost:8080/"
27
+ },
28
+ "id": "vjDqH0JGlZlk",
29
+ "outputId": "b476e41f-2c27-413b-a6f9-a118f9bc0b05"
30
+ },
31
+ "outputs": [],
32
+ "source": [
33
+ "import numpy as np\n",
34
+ "import pandas as pd\n",
35
+ "from sklearn.model_selection import train_test_split\n",
36
+ "from sklearn.preprocessing import LabelEncoder, StandardScaler\n",
37
+ "from tensorflow.keras.models import Sequential\n",
38
+ "from tensorflow.keras.layers import Dense\n",
39
+ "from tensorflow.keras.utils import to_categorical\n",
40
+ "\n",
41
+ "# Load the Iris dataset\n",
42
+ "from sklearn.datasets import load_iris\n",
43
+ "iris = load_iris()\n",
44
+ "X = iris.data # Features (sepal/petal dimensions)\n",
45
+ "y = iris.target # Labels (species: 0, 1, 2)\n",
46
+ "\n",
47
+ "# Preprocess data\n",
48
+ "scaler = StandardScaler()\n",
49
+ "X = scaler.fit_transform(X)\n",
50
+ "y = to_categorical(y) # One-hot encode labels for SoftMax\n",
51
+ "\n",
52
+ "# Split data into train/test sets\n",
53
+ "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
54
+ "\n",
55
+ "# Function to build and evaluate the model\n",
56
+ "def train_model(activation='softmax'):\n",
57
+ " model = Sequential([\n",
58
+ " Dense(16, activation='relu', input_shape=(4,)), # Hidden layer\n",
59
+ " Dense(3, activation=activation) # Output layer (SoftMax or Sigmoid)\n",
60
+ " ])\n",
61
+ "\n",
62
+ " # Compile with categorical crossentropy for SoftMax, binary for Sigmoid\n",
63
+ " loss = 'categorical_crossentropy' if activation == 'softmax' else 'binary_crossentropy'\n",
64
+ " model.compile(optimizer='adam', loss=loss, metrics=['accuracy'])\n",
65
+ "\n",
66
+ " # Train\n",
67
+ " history = model.fit(X_train, y_train, epochs=50, validation_split=0.2, verbose=0)\n",
68
+ "\n",
69
+ " # Evaluate\n",
70
+ " _, accuracy = model.evaluate(X_test, y_test, verbose=0)\n",
71
+ " print(f\"Activation: {activation}, Test Accuracy: {accuracy:.4f}\")\n",
72
+ "\n",
73
+ "# Compare SoftMax vs. Sigmoid\n",
74
+ "train_model(activation='softmax') # Use this for multi-class (correct)\n",
75
+ "train_model(activation='sigmoid') # Incorrect for multi-class (for comparison)"
76
+ ]
77
+ },
78
+ {
79
+ "cell_type": "markdown",
80
+ "metadata": {
81
+ "id": "SEFVF5sllivC"
82
+ },
83
+ "source": [
84
+ "**Q2**"
85
+ ]
86
+ },
87
+ {
88
+ "cell_type": "code",
89
+ "execution_count": null,
90
+ "metadata": {
91
+ "colab": {
92
+ "base_uri": "https://localhost:8080/",
93
+ "height": 522
94
+ },
95
+ "id": "pQrKim1ylkGy",
96
+ "outputId": "e2c55935-21c9-402a-aa00-03cbcffec7d3"
97
+ },
98
+ "outputs": [],
99
+ "source": [
100
+ "import numpy as np\n",
101
+ "from sklearn.model_selection import train_test_split\n",
102
+ "from sklearn.preprocessing import StandardScaler\n",
103
+ "from tensorflow.keras.models import Sequential\n",
104
+ "from tensorflow.keras.layers import Dense, Dropout, Input\n",
105
+ "from tensorflow.keras.regularizers import l2\n",
106
+ "import matplotlib.pyplot as plt\n",
107
+ "\n",
108
+ "# Generate synthetic data\n",
109
+ "np.random.seed(42)\n",
110
+ "X = np.random.rand(1000, 5) # 5 socio-economic features\n",
111
+ "y = X.dot(np.random.rand(5)) + np.random.rand(1000) * 0.1 # Grades (0-1 scale)\n",
112
+ "\n",
113
+ "# Split data\n",
114
+ "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
115
+ "\n",
116
+ "# Standardize\n",
117
+ "scaler = StandardScaler()\n",
118
+ "X_train = scaler.fit_transform(X_train)\n",
119
+ "X_test = scaler.transform(X_test)\n",
120
+ "\n",
121
+ "# Build model (with optional regularization)\n",
122
+ "def build_model(use_regularization=False):\n",
123
+ " model = Sequential()\n",
124
+ " model.add(Input(shape=(5,))) # Explicit input layer\n",
125
+ "\n",
126
+ " # Hidden layers with conditional L2/dropout\n",
127
+ " reg = l2(0.01) if use_regularization else None\n",
128
+ " model.add(Dense(128, activation='relu', kernel_regularizer=reg))\n",
129
+ " model.add(Dense(128, activation='relu', kernel_regularizer=reg))\n",
130
+ " if use_regularization:\n",
131
+ " model.add(Dropout(0.5)) # Only add dropout if regularization is enabled\n",
132
+ "\n",
133
+ " model.add(Dense(1)) # Output layer (linear for regression)\n",
134
+ " model.compile(optimizer='adam', loss='mse', metrics=['mae'])\n",
135
+ " return model\n",
136
+ "\n",
137
+ "# Train without regularization (overfit)\n",
138
+ "model_no_reg = build_model(use_regularization=False)\n",
139
+ "history_no_reg = model_no_reg.fit(X_train, y_train, epochs=100,\n",
140
+ " validation_split=0.2, verbose=0)\n",
141
+ "\n",
142
+ "# Train with dropout + L2 (regularized)\n",
143
+ "model_with_reg = build_model(use_regularization=True)\n",
144
+ "history_with_reg = model_with_reg.fit(X_train, y_train, epochs=100,\n",
145
+ " validation_split=0.2, verbose=0)\n",
146
+ "\n",
147
+ "# Plot results\n",
148
+ "plt.figure(figsize=(10, 5))\n",
149
+ "plt.plot(history_no_reg.history['val_loss'], label='No Regularization', linestyle='--')\n",
150
+ "plt.plot(history_with_reg.history['val_loss'], label='With Dropout + L2', linestyle='--')\n",
151
+ "plt.xlabel('Epochs')\n",
152
+ "plt.ylabel('Validation Loss (MSE)')\n",
153
+ "plt.legend()\n",
154
+ "plt.title('Overfitting Mitigation with Regularization')\n",
155
+ "plt.show()\n",
156
+ "\n",
157
+ "# Test performance\n",
158
+ "print(\"Test MAE (No Regularization):\", model_no_reg.evaluate(X_test, y_test, verbose=0)[1])\n",
159
+ "print(\"Test MAE (With Regularization):\", model_with_reg.evaluate(X_test, y_test, verbose=0)[1])"
160
+ ]
161
+ },
162
+ {
163
+ "cell_type": "markdown",
164
+ "metadata": {
165
+ "id": "nd5WFd9TloOS"
166
+ },
167
+ "source": [
168
+ "**Q3**"
169
+ ]
170
+ },
171
+ {
172
+ "cell_type": "code",
173
+ "execution_count": null,
174
+ "metadata": {
175
+ "colab": {
176
+ "base_uri": "https://localhost:8080/",
177
+ "height": 576
178
+ },
179
+ "id": "vx4k9Z8-lprC",
180
+ "outputId": "6de49e13-d025-453c-f84c-f316bea51680"
181
+ },
182
+ "outputs": [],
183
+ "source": [
184
+ "import numpy as np\n",
185
+ "import pandas as pd\n",
186
+ "from sklearn.model_selection import train_test_split\n",
187
+ "from sklearn.preprocessing import StandardScaler\n",
188
+ "from tensorflow.keras.models import Sequential\n",
189
+ "from tensorflow.keras.layers import Dense, Dropout\n",
190
+ "from tensorflow.keras.regularizers import l2\n",
191
+ "import matplotlib.pyplot as plt\n",
192
+ "\n",
193
+ "# Generate synthetic insurance claim data\n",
194
+ "np.random.seed(42)\n",
195
+ "n_samples = 1000\n",
196
+ "X = np.random.rand(n_samples, 10) # 10 features (e.g., age, BMI, medical history)\n",
197
+ "y = X.dot(np.random.rand(10)) * 10000 + np.random.randn(n_samples) * 500 # Claim amounts ($)\n",
198
+ "\n",
199
+ "# Split into train/test\n",
200
+ "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
201
+ "\n",
202
+ "# Standardize features\n",
203
+ "scaler = StandardScaler()\n",
204
+ "X_train = scaler.fit_transform(X_train)\n",
205
+ "X_test = scaler.transform(X_test)\n",
206
+ "\n",
207
+ "# Function to build and train the model\n",
208
+ "def train_model(use_regularization=False):\n",
209
+ " model = Sequential()\n",
210
+ " model.add(Dense(256, activation='relu', input_shape=(X_train.shape[1],)))\n",
211
+ " model.add(Dense(256, activation='relu'))\n",
212
+ " model.add(Dense(128, activation='relu'))\n",
213
+ "\n",
214
+ " if use_regularization:\n",
215
+ " model.add(Dropout(0.5))\n",
216
+ " model.add(Dense(64, activation='relu', kernel_regularizer=l2(0.01)))\n",
217
+ " else:\n",
218
+ " model.add(Dense(64, activation='relu'))\n",
219
+ "\n",
220
+ " model.add(Dense(1)) # Output layer for regression\n",
221
+ "\n",
222
+ " model.compile(optimizer='adam', loss='mse', metrics=['mae'])\n",
223
+ "\n",
224
+ " history = model.fit(X_train, y_train, epochs=100,\n",
225
+ " validation_split=0.2, verbose=0)\n",
226
+ " return model, history\n",
227
+ "\n",
228
+ "# Intentionally overfit (no regularization)\n",
229
+ "model_overfit, history_overfit = train_model(use_regularization=False)\n",
230
+ "\n",
231
+ "# Apply regularization (dropout + L2)\n",
232
+ "model_reg, history_reg = train_model(use_regularization=True)\n",
233
+ "\n",
234
+ "# Plot training vs validation loss\n",
235
+ "plt.figure(figsize=(10, 5))\n",
236
+ "plt.plot(history_overfit.history['loss'], label='Train (Overfit)')\n",
237
+ "plt.plot(history_overfit.history['val_loss'], label='Validation (Overfit)', linestyle='--')\n",
238
+ "plt.plot(history_reg.history['val_loss'], label='Validation (Regularized)', linestyle='--')\n",
239
+ "plt.xlabel('Epochs')\n",
240
+ "plt.ylabel('Loss (MSE)')\n",
241
+ "plt.legend()\n",
242
+ "plt.title('Overfitting vs. Regularization')\n",
243
+ "plt.show()\n",
244
+ "\n",
245
+ "# Evaluate on test data\n",
246
+ "print(\"Test MAE (Overfit Model): ${:,.2f}\".format(model_overfit.evaluate(X_test, y_test, verbose=0)[1]))\n",
247
+ "print(\"Test MAE (Regularized Model): ${:,.2f}\".format(model_reg.evaluate(X_test, y_test, verbose=0)[1]))"
248
+ ]
249
+ },
250
+ {
251
+ "cell_type": "markdown",
252
+ "metadata": {
253
+ "id": "yNu_4025lsQZ"
254
+ },
255
+ "source": [
256
+ "**Q4**"
257
+ ]
258
+ },
259
+ {
260
+ "cell_type": "code",
261
+ "execution_count": null,
262
+ "metadata": {
263
+ "colab": {
264
+ "base_uri": "https://localhost:8080/"
265
+ },
266
+ "id": "d0uyyJUlltZy",
267
+ "outputId": "fa5a6938-fd8e-4873-b45c-974b776b3eaf"
268
+ },
269
+ "outputs": [],
270
+ "source": [
271
+ "import numpy as np\n",
272
+ "from hmmlearn import hmm\n",
273
+ "import matplotlib.pyplot as plt\n",
274
+ "\n",
275
+ "# Define the hidden states and observations\n",
276
+ "states = [\"Cooking\", \"Sleeping\", \"Watching TV\"]\n",
277
+ "observations = [\"kitchen\", \"bedroom\", \"living room\"]\n",
278
+ "\n",
279
+ "# Create simulated sensor data sequences\n",
280
+ "# Each sequence is a day's worth of room observations\n",
281
+ "room_sequences = [\n",
282
+ " ['kitchen', 'bedroom', 'living room', 'kitchen', 'bedroom'],\n",
283
+ " ['kitchen', 'living room', 'living room', 'bedroom', 'bedroom'],\n",
284
+ " ['living room', 'kitchen', 'bedroom', 'kitchen', 'bedroom'],\n",
285
+ " ['bedroom', 'bedroom', 'living room', 'kitchen', 'living room']\n",
286
+ "]\n",
287
+ "\n",
288
+ "# Convert observations to numerical values\n",
289
+ "obs_map = {obs: i for i, obs in enumerate(observations)}\n",
290
+ "num_sequences = len(room_sequences)\n",
291
+ "sequence_lengths = [len(seq) for seq in room_sequences]\n",
292
+ "X = np.concatenate([[obs_map[obs] for obs in seq] for seq in room_sequences]).reshape(-1, 1)\n",
293
+ "\n",
294
+ "# Build and train the HMM\n",
295
+ "model = hmm.CategoricalHMM(n_components=len(states), random_state=42)\n",
296
+ "model.fit(X, lengths=sequence_lengths)\n",
297
+ "\n",
298
+ "# Print learned parameters\n",
299
+ "print(\"Start Probabilities:\", model.startprob_)\n",
300
+ "print(\"\\nTransition Matrix:\")\n",
301
+ "print(model.transmat_)\n",
302
+ "print(\"\\nEmission Probabilities:\")\n",
303
+ "print(model.emissionprob_)\n",
304
+ "\n",
305
+ "# Predict activities for a new sequence\n",
306
+ "new_sequence = ['kitchen', 'living room', 'bedroom', 'kitchen']\n",
307
+ "numeric_seq = np.array([obs_map[obs] for obs in new_sequence]).reshape(-1, 1)\n",
308
+ "predicted_states = model.predict(numeric_seq)\n",
309
+ "\n",
310
+ "print(\"\\nPredicted Activities:\")\n",
311
+ "for obs, state in zip(new_sequence, predicted_states):\n",
312
+ " print(f\"{obs} -> {states[state]}\")"
313
+ ]
314
+ },
315
+ {
316
+ "cell_type": "markdown",
317
+ "metadata": {
318
+ "id": "mbmjRZU6lvOB"
319
+ },
320
+ "source": [
321
+ "**Q5**"
322
+ ]
323
+ },
324
+ {
325
+ "cell_type": "code",
326
+ "execution_count": null,
327
+ "metadata": {
328
+ "colab": {
329
+ "base_uri": "https://localhost:8080/"
330
+ },
331
+ "id": "c4WLDsM_lyk6",
332
+ "outputId": "5c54c8ac-8e0b-4c81-c5a0-b77cdb01af4f"
333
+ },
334
+ "outputs": [],
335
+ "source": [
336
+ "import numpy as np\n",
337
+ "from hmmlearn import hmm\n",
338
+ "\n",
339
+ "# Define states and observations\n",
340
+ "states = [\"Genuine\", \"Intruder\"]\n",
341
+ "observations = [\"early\", \"mid\", \"late\"] # Login times\n",
342
+ "\n",
343
+ "# Simulated login sequences (each sequence is a separate user's login pattern)\n",
344
+ "sequences = [\n",
345
+ " ['early', 'early', 'mid', 'early', 'mid'], # Genuine user 1\n",
346
+ " ['late', 'late', 'early', 'late', 'late'], # Intruder 1\n",
347
+ " ['early', 'mid', 'early', 'mid', 'early'], # Genuine user 2\n",
348
+ " ['mid', 'late', 'late', 'mid', 'late'], # Intruder 2\n",
349
+ " ['early', 'early', 'early', 'mid', 'early'], # Genuine user 3\n",
350
+ " ['late', 'mid', 'late', 'late', 'mid'] # Intruder 3\n",
351
+ "]\n",
352
+ "\n",
353
+ "# Convert to numerical values and proper format\n",
354
+ "obs_map = {obs: i for i, obs in enumerate(observations)}\n",
355
+ "X = np.concatenate([[[obs_map[obs]] for obs in seq] for seq in sequences])\n",
356
+ "lengths = [len(seq) for seq in sequences] # All lengths are 5 in this case\n",
357
+ "\n",
358
+ "# Train HMM\n",
359
+ "model = hmm.CategoricalHMM(\n",
360
+ " n_components=len(states),\n",
361
+ " random_state=42 # Increased iterations for better convergence\n",
362
+ ")\n",
363
+ "model.fit(X, lengths=lengths)\n",
364
+ "\n",
365
+ "# Print learned parameters\n",
366
+ "print(\"Start Probabilities (Genuine vs Intruder):\\n\", model.startprob_)\n",
367
+ "print(\"\\nTransition Matrix:\\n\", model.transmat_)\n",
368
+ "print(\"\\nEmission Probabilities (Time of Day):\\n\", model.emissionprob_)\n",
369
+ "\n",
370
+ "# Predict on new sequences\n",
371
+ "test_sequences = [\n",
372
+ " ['early', 'mid', 'early', 'mid', 'early'], # Likely genuine\n",
373
+ " ['late', 'late', 'mid', 'late', 'late'], # Likely intruder\n",
374
+ "]\n",
375
+ "\n",
376
+ "for seq in test_sequences:\n",
377
+ " numeric_seq = np.array([[obs_map[obs]] for obs in seq])\n",
378
+ " logprob, state_sequence = model.decode(numeric_seq)\n",
379
+ " print(f\"\\nSequence: {seq}\")\n",
380
+ " print(\"Predicted States:\", [states[i] for i in state_sequence])\n",
381
+ " print(\"Log Probability:\", logprob)"
382
+ ]
383
+ }
384
+ ],
385
+ "metadata": {
386
+ "colab": {
387
+ "provenance": []
388
+ },
389
+ "kernelspec": {
390
+ "display_name": "Python 3 (ipykernel)",
391
+ "language": "python",
392
+ "name": "python3"
393
+ },
394
+ "language_info": {
395
+ "codemirror_mode": {
396
+ "name": "ipython",
397
+ "version": 3
398
+ },
399
+ "file_extension": ".py",
400
+ "mimetype": "text/x-python",
401
+ "name": "python",
402
+ "nbconvert_exporter": "python",
403
+ "pygments_lexer": "ipython3",
404
+ "version": "3.12.4"
405
+ }
406
+ },
407
+ "nbformat": 4,
408
+ "nbformat_minor": 4
409
+ }
@@ -0,0 +1,231 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "d9e9837e-70fc-47f0-9198-9c0bd862aa26",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import numpy as np\n",
11
+ "import pandas as pd\n",
12
+ "import seaborn as sns\n",
13
+ "import matplotlib.pyplot as plt\n",
14
+ "import tensorflow as tf\n",
15
+ "from tensorflow.keras.models import Sequential\n",
16
+ "from tensorflow.keras.layers import Dense, Input\n",
17
+ "from sklearn.model_selection import train_test_split\n",
18
+ "from sklearn.preprocessing import LabelEncoder, StandardScaler, label_binarize\n",
19
+ "from sklearn.metrics import classification_report, confusion_matrix \n",
20
+ "from sklearn.metrics import ConfusionMatrixDisplay, roc_curve, auc"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": null,
26
+ "id": "5d5852fb-929b-4e10-b71a-2806723e538c",
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "names = ['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width', 'target']\n",
31
+ "df = pd.read_csv('iris.data', names=names)\n",
32
+ "df.head()"
33
+ ]
34
+ },
35
+ {
36
+ "cell_type": "code",
37
+ "execution_count": null,
38
+ "id": "6944dd0f-23ad-4966-b9bd-93ea91f9ce02",
39
+ "metadata": {},
40
+ "outputs": [],
41
+ "source": [
42
+ "sns.countplot(df, x='target', hue='target', palette='viridis')\n",
43
+ "plt.show()"
44
+ ]
45
+ },
46
+ {
47
+ "cell_type": "code",
48
+ "execution_count": null,
49
+ "id": "790b368d-c13f-4f2d-971d-71cb0649e64e",
50
+ "metadata": {},
51
+ "outputs": [],
52
+ "source": [
53
+ "X = df.drop(columns='target')\n",
54
+ "y = df['target']\n",
55
+ "\n",
56
+ "X = StandardScaler().fit_transform(X)\n",
57
+ "y = LabelEncoder().fit_transform(y)"
58
+ ]
59
+ },
60
+ {
61
+ "cell_type": "code",
62
+ "execution_count": null,
63
+ "id": "c1bce7c1-f370-4f04-9663-64e3386baccb",
64
+ "metadata": {},
65
+ "outputs": [],
66
+ "source": [
67
+ "def build_model(output_activation):\n",
68
+ " model = Sequential([\n",
69
+ " Input(shape=(4,)),\n",
70
+ " Dense(64, activation='relu'),\n",
71
+ " Dense(32, activation='relu'),\n",
72
+ " Dense(3, activation=output_activation)\n",
73
+ " ])\n",
74
+ " model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n",
75
+ " return model"
76
+ ]
77
+ },
78
+ {
79
+ "cell_type": "code",
80
+ "execution_count": null,
81
+ "id": "998082ab-6ab5-4df2-99f6-53b5c304a818",
82
+ "metadata": {
83
+ "scrolled": true
84
+ },
85
+ "outputs": [],
86
+ "source": [
87
+ "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2)\n",
88
+ "model_softmax = build_model('softmax')\n",
89
+ "history_softmax = model_softmax.fit(X_train, y_train, \n",
90
+ " validation_data=(X_test, y_test), \n",
91
+ " epochs=25, verbose=1)"
92
+ ]
93
+ },
94
+ {
95
+ "cell_type": "code",
96
+ "execution_count": null,
97
+ "id": "f0dd395f-0831-4d78-9f06-0bb20d9bfdb7",
98
+ "metadata": {
99
+ "scrolled": true
100
+ },
101
+ "outputs": [],
102
+ "source": [
103
+ "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2)\n",
104
+ "model_sigmoid = build_model('sigmoid')\n",
105
+ "history_sigmoid = model_sigmoid.fit(X_train, y_train, \n",
106
+ " validation_data=(X_test, y_test), \n",
107
+ " epochs=25, verbose=1)"
108
+ ]
109
+ },
110
+ {
111
+ "cell_type": "code",
112
+ "execution_count": null,
113
+ "id": "8c1c01d7-d783-438b-834b-2820c6ed1afc",
114
+ "metadata": {},
115
+ "outputs": [],
116
+ "source": [
117
+ "plt.figure(figsize=(10,6))\n",
118
+ "\n",
119
+ "plt.plot(history_softmax.history['accuracy'], label='Softmax - Train Accuracy', color='blue', linewidth=2)\n",
120
+ "plt.plot(history_softmax.history['val_accuracy'], label='Softmax - Val Accuracy', color='blue', linestyle='--')\n",
121
+ "\n",
122
+ "plt.plot(history_sigmoid.history['accuracy'], label='Sigmoid - Train Accuracy', color='green', linewidth=2)\n",
123
+ "plt.plot(history_sigmoid.history['val_accuracy'], label='Sigmoid - Val Accuracy', color='green', linestyle='--')\n",
124
+ "\n",
125
+ "plt.title('Training and Validation Accuracy Comparison')\n",
126
+ "plt.xlabel('Epoch')\n",
127
+ "plt.ylabel('Accuracy')\n",
128
+ "plt.legend()\n",
129
+ "plt.grid(True)\n",
130
+ "plt.show()"
131
+ ]
132
+ },
133
+ {
134
+ "cell_type": "code",
135
+ "execution_count": null,
136
+ "id": "51329ded-8ea8-4404-94c7-65f968170e0c",
137
+ "metadata": {},
138
+ "outputs": [],
139
+ "source": [
140
+ "softmax_score = model_softmax.evaluate(X_test, y_test, verbose=0)\n",
141
+ "sigmoid_score = model_sigmoid.evaluate(X_test, y_test, verbose=0)\n",
142
+ "\n",
143
+ "print(f\"Test Accuracy with Softmax: {softmax_score[1]:.4f}\")\n",
144
+ "print(f\"Test Accuracy with Sigmoid: {sigmoid_score[1]:.4f}\")"
145
+ ]
146
+ },
147
+ {
148
+ "cell_type": "code",
149
+ "execution_count": null,
150
+ "id": "9eb9a3f8-4d1b-46fd-b7f0-c860c884ce5e",
151
+ "metadata": {},
152
+ "outputs": [],
153
+ "source": [
154
+ "plt.bar(['Softmax', 'Sigmoid'], [softmax_score[1], sigmoid_score[1]], color=['blue','green'])\n",
155
+ "plt.ylabel('Test Accuracy')\n",
156
+ "plt.title('Final Test Accuracy Comparison')\n",
157
+ "plt.ylim(0, 1)\n",
158
+ "plt.show()"
159
+ ]
160
+ },
161
+ {
162
+ "cell_type": "code",
163
+ "execution_count": null,
164
+ "id": "f6af8884-54fa-4fbe-905c-e9bb2ce85d8f",
165
+ "metadata": {},
166
+ "outputs": [],
167
+ "source": [
168
+ "y_pred_softmax = np.argmax(model_softmax.predict(X_test), axis=1)\n",
169
+ "y_pred_sigmoid = np.argmax(model_sigmoid.predict(X_test), axis=1)"
170
+ ]
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "execution_count": null,
175
+ "id": "0c6dbf0c-b52d-4560-844d-f78562585cca",
176
+ "metadata": {},
177
+ "outputs": [],
178
+ "source": [
179
+ "labels = df['target'].unique()\n",
180
+ "print(\"Classification Report for Softmax:\")\n",
181
+ "print(classification_report(y_test, y_pred_softmax, target_names=labels))\n",
182
+ "\n",
183
+ "print(\"\\nClassification Report for Sigmoid:\")\n",
184
+ "print(classification_report(y_test, y_pred_sigmoid, target_names=labels))"
185
+ ]
186
+ },
187
+ {
188
+ "cell_type": "code",
189
+ "execution_count": null,
190
+ "id": "246d7e32-f654-43f9-8535-caa20e613016",
191
+ "metadata": {},
192
+ "outputs": [],
193
+ "source": [
194
+ "cm_softmax = confusion_matrix(y_test, y_pred_softmax)\n",
195
+ "cm_sigmoid = confusion_matrix(y_test, y_pred_sigmoid)\n",
196
+ "\n",
197
+ "fig, ax = plt.subplots(1, 2, figsize=(14, 6))\n",
198
+ "\n",
199
+ "ConfusionMatrixDisplay(cm_softmax, display_labels=labels).plot(ax=ax[0])\n",
200
+ "ax[0].set_title(\"Softmax Confusion Matrix\", size=25)\n",
201
+ "\n",
202
+ "ConfusionMatrixDisplay(cm_sigmoid, display_labels=labels).plot(ax=ax[1])\n",
203
+ "ax[1].set_title(\"Sigmoid Confusion Matrix\", size=25)\n",
204
+ "\n",
205
+ "plt.tight_layout()\n",
206
+ "plt.show()"
207
+ ]
208
+ }
209
+ ],
210
+ "metadata": {
211
+ "kernelspec": {
212
+ "display_name": "NEW-VENV-1",
213
+ "language": "python",
214
+ "name": "new-venv-1"
215
+ },
216
+ "language_info": {
217
+ "codemirror_mode": {
218
+ "name": "ipython",
219
+ "version": 3
220
+ },
221
+ "file_extension": ".py",
222
+ "mimetype": "text/x-python",
223
+ "name": "python",
224
+ "nbconvert_exporter": "python",
225
+ "pygments_lexer": "ipython3",
226
+ "version": "3.11.5"
227
+ }
228
+ },
229
+ "nbformat": 4,
230
+ "nbformat_minor": 5
231
+ }