noshot 8.0.0__py3-none-any.whl → 10.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,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
+ }
@@ -0,0 +1,269 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "f8f281db-b729-4df9-8735-639cd5cf64d8",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import numpy as np\n",
11
+ "import pandas as pd\n",
12
+ "import matplotlib.pyplot as plt\n",
13
+ "import tensorflow as tf\n",
14
+ "from tensorflow.keras.models import Sequential\n",
15
+ "from tensorflow.keras.layers import Dense, Dropout, BatchNormalization\n",
16
+ "from tensorflow.keras.optimizers import Adam\n",
17
+ "from tensorflow.keras import regularizers\n",
18
+ "from sklearn.preprocessing import LabelEncoder\n",
19
+ "from sklearn.model_selection import train_test_split\n",
20
+ "from sklearn.metrics import classification_report, confusion_matrix \n",
21
+ "from sklearn.metrics import ConfusionMatrixDisplay, accuracy_score, roc_curve, auc\n",
22
+ "\n",
23
+ "import warnings\n",
24
+ "warnings.filterwarnings('ignore')"
25
+ ]
26
+ },
27
+ {
28
+ "cell_type": "code",
29
+ "execution_count": null,
30
+ "id": "cb163478-4de9-4d16-ae12-d650a7c7bffd",
31
+ "metadata": {},
32
+ "outputs": [],
33
+ "source": [
34
+ "df = pd.read_csv('student-mat.csv', sep=';')\n",
35
+ "#df = pd.read_csv('student-por.csv', sep=';')\n",
36
+ "print(\"Shape:\",df.shape)\n",
37
+ "df = df.drop(columns=['G1','G2'])\n",
38
+ "df = df.rename(columns={'G3': 'target'})\n",
39
+ "df.head()"
40
+ ]
41
+ },
42
+ {
43
+ "cell_type": "code",
44
+ "execution_count": null,
45
+ "id": "91764ed3-4699-45e0-b795-0c79b77a78ca",
46
+ "metadata": {},
47
+ "outputs": [],
48
+ "source": [
49
+ "cat_cols = df.select_dtypes(include='object').columns.tolist()\n",
50
+ "df[cat_cols] = df[cat_cols].apply(LabelEncoder().fit_transform)\n",
51
+ "df.head()"
52
+ ]
53
+ },
54
+ {
55
+ "cell_type": "code",
56
+ "execution_count": null,
57
+ "id": "284440eb-4a57-496c-a7c3-847027372c4e",
58
+ "metadata": {},
59
+ "outputs": [],
60
+ "source": [
61
+ "X = df.drop(columns=['target'])\n",
62
+ "y = tf.keras.utils.to_categorical(df['target'], num_classes=21)"
63
+ ]
64
+ },
65
+ {
66
+ "cell_type": "code",
67
+ "execution_count": null,
68
+ "id": "e7a7a68d-1aeb-4099-ae62-3f7a06167fa6",
69
+ "metadata": {},
70
+ "outputs": [],
71
+ "source": [
72
+ "overfit = Sequential([\n",
73
+ " Dense(128, activation='relu', input_shape=(30,)),\n",
74
+ " Dense(64, activation='relu'),\n",
75
+ " Dense(32, activation='relu'),\n",
76
+ " Dense(21, activation='softmax')\n",
77
+ "])\n",
78
+ "\n",
79
+ "overfit.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])"
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "code",
84
+ "execution_count": null,
85
+ "id": "8f92d2c8-2117-46d0-82e6-51ccf67957f8",
86
+ "metadata": {},
87
+ "outputs": [],
88
+ "source": [
89
+ "x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=1)\n",
90
+ "history1 = overfit.fit(x_train, y_train, epochs=25,\n",
91
+ " batch_size=8, validation_split=0.2,\n",
92
+ " verbose=1)"
93
+ ]
94
+ },
95
+ {
96
+ "cell_type": "code",
97
+ "execution_count": null,
98
+ "id": "12d28840-d3d7-42f6-b33c-63c833123ddd",
99
+ "metadata": {},
100
+ "outputs": [],
101
+ "source": [
102
+ "regularized = Sequential([\n",
103
+ " Dense(128, activation='relu', input_shape=(30,), kernel_regularizer=regularizers.l2(0.002)),\n",
104
+ " Dropout(0.25),\n",
105
+ " Dense(64, activation='relu', kernel_regularizer=regularizers.l2(0.002)),\n",
106
+ " Dropout(0.25),\n",
107
+ " Dense(21, activation='softmax')\n",
108
+ "])\n",
109
+ "\n",
110
+ "regularized.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])"
111
+ ]
112
+ },
113
+ {
114
+ "cell_type": "code",
115
+ "execution_count": null,
116
+ "id": "f30536d9-df1f-41be-8821-75cab3a74d9b",
117
+ "metadata": {},
118
+ "outputs": [],
119
+ "source": [
120
+ "early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', \n",
121
+ " patience=10, \n",
122
+ " restore_best_weights=True)"
123
+ ]
124
+ },
125
+ {
126
+ "cell_type": "code",
127
+ "execution_count": null,
128
+ "id": "b8dd5fbb-71a5-4e00-925d-ff522b9ab314",
129
+ "metadata": {},
130
+ "outputs": [],
131
+ "source": [
132
+ "x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1)\n",
133
+ "history2 = regularized.fit(x_train, y_train, epochs=25, \n",
134
+ " batch_size=16, validation_split=0.3,\n",
135
+ " callbacks=[early_stopping], verbose=1)"
136
+ ]
137
+ },
138
+ {
139
+ "cell_type": "code",
140
+ "execution_count": null,
141
+ "id": "cb392228-7a26-4dc5-a2a3-5cd44875c4ea",
142
+ "metadata": {},
143
+ "outputs": [],
144
+ "source": [
145
+ "def plot_history(history1, history2):\n",
146
+ " plt.figure(figsize=(12, 5))\n",
147
+ " plt.subplot(1, 2, 1)\n",
148
+ " plt.plot(history1.history['accuracy'], label='Overfit Model Validation Accuracy')\n",
149
+ " plt.plot(history2.history['accuracy'], label='Regularized Model Validation Accuracy')\n",
150
+ " plt.title('Training Accuracy')\n",
151
+ " plt.xlabel('Epoch')\n",
152
+ " plt.ylabel('Accuracy')\n",
153
+ " plt.legend()\n",
154
+ "\n",
155
+ " plt.subplot(1, 2, 2)\n",
156
+ " plt.plot(history1.history['val_accuracy'], label='Overfit Model Validation Accuracy')\n",
157
+ " plt.plot(history2.history['val_accuracy'], label='Regularized Model Validation Accuracy')\n",
158
+ " plt.title('Validation Accuracy')\n",
159
+ " plt.xlabel('Epoch')\n",
160
+ " plt.ylabel('Accuracy')\n",
161
+ " plt.legend()\n",
162
+ "\n",
163
+ " plt.tight_layout()\n",
164
+ " plt.show()\n",
165
+ "\n",
166
+ "plot_history(history1, history2)"
167
+ ]
168
+ },
169
+ {
170
+ "cell_type": "code",
171
+ "execution_count": null,
172
+ "id": "8703400f-47a9-4c4e-b9b4-e302c69a209b",
173
+ "metadata": {},
174
+ "outputs": [],
175
+ "source": [
176
+ "loss1, accuracy1 = overfit.evaluate(x_train, y_train)\n",
177
+ "loss2, accuracy2 = overfit.evaluate(x_test, y_test)\n",
178
+ "print(f'Train accuracy Overfit Model: {accuracy1:.4f}')\n",
179
+ "print(f'Test accuracy Overfit Model: {accuracy2:.4f}')"
180
+ ]
181
+ },
182
+ {
183
+ "cell_type": "code",
184
+ "execution_count": null,
185
+ "id": "8548d072-206c-4ab6-8c98-c5fb63b644c7",
186
+ "metadata": {},
187
+ "outputs": [],
188
+ "source": [
189
+ "loss1, accuracy1 = regularized.evaluate(x_train, y_train)\n",
190
+ "loss2, accuracy2 = regularized.evaluate(x_test, y_test)\n",
191
+ "print(f'Train accuracy Regularized Model: {accuracy1:.4f}')\n",
192
+ "print(f'Test accuracy Regularized Model: {accuracy2:.4f}')"
193
+ ]
194
+ },
195
+ {
196
+ "cell_type": "code",
197
+ "execution_count": null,
198
+ "id": "ad978e23-15b8-402f-bb31-d00aa4771c93",
199
+ "metadata": {},
200
+ "outputs": [],
201
+ "source": [
202
+ "y_pred1 = np.argmax(overfit.predict(x_test), axis=1)\n",
203
+ "y_test1 = np.argmax(y_test, axis=1)\n",
204
+ "y_pred2 = np.argmax(regularized.predict(x_test), axis=1)\n",
205
+ "y_test2 = np.argmax(y_test, axis=1)"
206
+ ]
207
+ },
208
+ {
209
+ "cell_type": "code",
210
+ "execution_count": null,
211
+ "id": "1ca0a093-04ac-48f3-8454-010413d268cf",
212
+ "metadata": {},
213
+ "outputs": [],
214
+ "source": [
215
+ "print(\"Classification Report for Overfit:\")\n",
216
+ "print(classification_report(y_test1, y_pred1))\n",
217
+ "\n",
218
+ "print(\"\\nClassification Report for Regularized:\")\n",
219
+ "print(classification_report(y_test2, y_pred2))"
220
+ ]
221
+ },
222
+ {
223
+ "cell_type": "code",
224
+ "execution_count": null,
225
+ "id": "c31bd086-7fd9-4ea9-b0bf-9ac14bcca5bc",
226
+ "metadata": {},
227
+ "outputs": [],
228
+ "source": [
229
+ "labels1 = np.unique(np.concatenate((y_test1, y_pred1)))\n",
230
+ "labels2 = np.unique(np.concatenate((y_test2, y_pred2)))\n",
231
+ "\n",
232
+ "cm1 = confusion_matrix(y_test1, y_pred1, labels=labels1)\n",
233
+ "cm2 = confusion_matrix(y_test2, y_pred2, labels=labels2)\n",
234
+ "\n",
235
+ "fig, ax = plt.subplots(1, 2, figsize=(14, 6))\n",
236
+ "\n",
237
+ "ConfusionMatrixDisplay(cm1, display_labels=labels1).plot(ax=ax[0], colorbar=False)\n",
238
+ "ax[0].set_title(\"Overfit Confusion Matrix\", size=25)\n",
239
+ "\n",
240
+ "ConfusionMatrixDisplay(cm2, display_labels=labels2).plot(ax=ax[1], colorbar=False)\n",
241
+ "ax[1].set_title(\"Regularized Confusion Matrix\", size=25)\n",
242
+ "\n",
243
+ "plt.tight_layout()\n",
244
+ "plt.show()"
245
+ ]
246
+ }
247
+ ],
248
+ "metadata": {
249
+ "kernelspec": {
250
+ "display_name": "NEW-VENV-1",
251
+ "language": "python",
252
+ "name": "new-venv-1"
253
+ },
254
+ "language_info": {
255
+ "codemirror_mode": {
256
+ "name": "ipython",
257
+ "version": 3
258
+ },
259
+ "file_extension": ".py",
260
+ "mimetype": "text/x-python",
261
+ "name": "python",
262
+ "nbconvert_exporter": "python",
263
+ "pygments_lexer": "ipython3",
264
+ "version": "3.11.5"
265
+ }
266
+ },
267
+ "nbformat": 4,
268
+ "nbformat_minor": 5
269
+ }