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,274 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "55832f35-459a-4a0e-a379-35484f92d1c5",
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\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": "f3344d11-95e5-481b-93e4-318bf834d2c0",
31
+ "metadata": {},
32
+ "outputs": [],
33
+ "source": [
34
+ "df = pd.read_csv(\"insurance.csv\")\n",
35
+ "df.head()"
36
+ ]
37
+ },
38
+ {
39
+ "cell_type": "code",
40
+ "execution_count": null,
41
+ "id": "62045487-f95d-402a-b18e-85009ff58903",
42
+ "metadata": {},
43
+ "outputs": [],
44
+ "source": [
45
+ "labels = ['Low', 'Medium', 'High', 'Very High']\n",
46
+ "df['charges'] = pd.qcut(df['charges'], q=4, labels=labels)\n",
47
+ "df.head()"
48
+ ]
49
+ },
50
+ {
51
+ "cell_type": "code",
52
+ "execution_count": null,
53
+ "id": "eb3bd408-f8a3-41fd-a778-e7608071e5e4",
54
+ "metadata": {},
55
+ "outputs": [],
56
+ "source": [
57
+ "cols = ['sex', 'smoker', 'region', 'charges']\n",
58
+ "df[cols] = df[cols].apply(LabelEncoder().fit_transform)\n",
59
+ "df.head()"
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "code",
64
+ "execution_count": null,
65
+ "id": "c07602a7-b504-4d9b-a3c6-68bceee8280b",
66
+ "metadata": {},
67
+ "outputs": [],
68
+ "source": [
69
+ "X = df.drop(columns=['charges'])\n",
70
+ "y = tf.keras.utils.to_categorical(df['charges'], num_classes=4)"
71
+ ]
72
+ },
73
+ {
74
+ "cell_type": "code",
75
+ "execution_count": null,
76
+ "id": "89d7dd41-4d03-4f50-8639-9065bd17b7f6",
77
+ "metadata": {},
78
+ "outputs": [],
79
+ "source": [
80
+ "overfit = Sequential([\n",
81
+ " Dense(64, activation='relu', input_shape=(6,)),\n",
82
+ " Dense(64, activation='relu'),\n",
83
+ " Dense(4, activation='softmax')\n",
84
+ "])\n",
85
+ "\n",
86
+ "overfit.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])"
87
+ ]
88
+ },
89
+ {
90
+ "cell_type": "code",
91
+ "execution_count": null,
92
+ "id": "8d55f010-fe99-40a4-a7f7-8fbe26e15e71",
93
+ "metadata": {},
94
+ "outputs": [],
95
+ "source": [
96
+ "x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)\n",
97
+ "history1 = overfit.fit(x_train, y_train, epochs=25, \n",
98
+ " batch_size=16, validation_split=0.2,\n",
99
+ " verbose=1)"
100
+ ]
101
+ },
102
+ {
103
+ "cell_type": "code",
104
+ "execution_count": null,
105
+ "id": "1151c540-a049-4cde-b6ec-51e214cf52ba",
106
+ "metadata": {},
107
+ "outputs": [],
108
+ "source": [
109
+ "regularized = Sequential([\n",
110
+ " Dense(128, activation='relu', input_shape=(6,), kernel_regularizer=regularizers.l2(0.001)),\n",
111
+ " Dropout(0.1),\n",
112
+ " Dense(64, activation='relu', kernel_regularizer=regularizers.l2(0.001)),\n",
113
+ " Dropout(0.1),\n",
114
+ " Dense(4, activation='softmax')\n",
115
+ "])\n",
116
+ "\n",
117
+ "regularized.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])"
118
+ ]
119
+ },
120
+ {
121
+ "cell_type": "code",
122
+ "execution_count": null,
123
+ "id": "88058b99-e8cd-47b6-a4bb-bc626996deca",
124
+ "metadata": {},
125
+ "outputs": [],
126
+ "source": [
127
+ "early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', \n",
128
+ " patience=10, \n",
129
+ " restore_best_weights=True)"
130
+ ]
131
+ },
132
+ {
133
+ "cell_type": "code",
134
+ "execution_count": null,
135
+ "id": "22964de8-9838-4f7c-9141-bffdf4def8b1",
136
+ "metadata": {},
137
+ "outputs": [],
138
+ "source": [
139
+ "x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1)\n",
140
+ "history2 = regularized.fit(x_train, y_train, epochs=25, \n",
141
+ " batch_size=16, validation_split=0.3,\n",
142
+ " callbacks=[early_stopping], verbose=1)"
143
+ ]
144
+ },
145
+ {
146
+ "cell_type": "code",
147
+ "execution_count": null,
148
+ "id": "829e7156-0ce9-4e48-becc-48d8c607fc4d",
149
+ "metadata": {},
150
+ "outputs": [],
151
+ "source": [
152
+ "def plot_history(history1, history2):\n",
153
+ " plt.figure(figsize=(12, 5))\n",
154
+ " plt.subplot(1, 2, 1)\n",
155
+ " plt.plot(history1.history['accuracy'], label='Overfit Model Validation Accuracy')\n",
156
+ " plt.plot(history2.history['accuracy'], label='Regularized Model Validation Accuracy')\n",
157
+ " plt.title('Training Accuracy')\n",
158
+ " plt.xlabel('Epoch')\n",
159
+ " plt.ylabel('Accuracy')\n",
160
+ " plt.legend()\n",
161
+ "\n",
162
+ " plt.subplot(1, 2, 2)\n",
163
+ " plt.plot(history1.history['val_accuracy'], label='Overfit Model Validation Accuracy')\n",
164
+ " plt.plot(history2.history['val_accuracy'], label='Regularized Model Validation Accuracy')\n",
165
+ " plt.title('Validation Accuracy')\n",
166
+ " plt.xlabel('Epoch')\n",
167
+ " plt.ylabel('Accuracy')\n",
168
+ " plt.legend()\n",
169
+ "\n",
170
+ " plt.tight_layout()\n",
171
+ " plt.show()\n",
172
+ "\n",
173
+ "plot_history(history1, history2)"
174
+ ]
175
+ },
176
+ {
177
+ "cell_type": "code",
178
+ "execution_count": null,
179
+ "id": "6ff519cf-eca6-48fe-923c-aaa18721608e",
180
+ "metadata": {},
181
+ "outputs": [],
182
+ "source": [
183
+ "loss1, accuracy1 = overfit.evaluate(x_train, y_train)\n",
184
+ "loss2, accuracy2 = overfit.evaluate(x_test, y_test)\n",
185
+ "print(f'Train accuracy Overfit Model: {accuracy1:.4f}')\n",
186
+ "print(f'Test accuracy Overfit Model: {accuracy2:.4f}')"
187
+ ]
188
+ },
189
+ {
190
+ "cell_type": "code",
191
+ "execution_count": null,
192
+ "id": "0aa14e69-14d8-4566-a532-59d17b89dd04",
193
+ "metadata": {},
194
+ "outputs": [],
195
+ "source": [
196
+ "loss1, accuracy1 = regularized.evaluate(x_train, y_train)\n",
197
+ "loss2, accuracy2 = regularized.evaluate(x_test, y_test)\n",
198
+ "print(f'Train accuracy Regularized Model: {accuracy1:.4f}')\n",
199
+ "print(f'Test accuracy Regularized Model: {accuracy2:.4f}')"
200
+ ]
201
+ },
202
+ {
203
+ "cell_type": "code",
204
+ "execution_count": null,
205
+ "id": "d1aadc08-5ef6-4285-b889-d4dad9ddb3fd",
206
+ "metadata": {},
207
+ "outputs": [],
208
+ "source": [
209
+ "y_pred1 = np.argmax(overfit.predict(x_test), axis=1)\n",
210
+ "y_test1 = np.argmax(y_test, axis=1)\n",
211
+ "y_pred2 = np.argmax(regularized.predict(x_test), axis=1)\n",
212
+ "y_test2 = np.argmax(y_test, axis=1)"
213
+ ]
214
+ },
215
+ {
216
+ "cell_type": "code",
217
+ "execution_count": null,
218
+ "id": "c36a9495-2a6b-47da-b50a-060d0166412c",
219
+ "metadata": {},
220
+ "outputs": [],
221
+ "source": [
222
+ "print(\"Classification Report for Overfit:\")\n",
223
+ "print(classification_report(y_test1, y_pred1))\n",
224
+ "\n",
225
+ "print(\"\\nClassification Report for Regularized:\")\n",
226
+ "print(classification_report(y_test2, y_pred2))"
227
+ ]
228
+ },
229
+ {
230
+ "cell_type": "code",
231
+ "execution_count": null,
232
+ "id": "8dec9763-db18-4476-9f2a-acfc3bd5b63b",
233
+ "metadata": {},
234
+ "outputs": [],
235
+ "source": [
236
+ "labels = df['charges'].unique()\n",
237
+ "cm1 = confusion_matrix(y_test1, y_pred1)\n",
238
+ "cm2 = confusion_matrix(y_test2, y_pred2)\n",
239
+ "\n",
240
+ "fig, ax = plt.subplots(1, 2, figsize=(14, 6))\n",
241
+ "\n",
242
+ "ConfusionMatrixDisplay(cm1, display_labels=labels).plot(ax=ax[0])\n",
243
+ "ax[0].set_title(\"Overfit Confusion Matrix\", size=25)\n",
244
+ "\n",
245
+ "ConfusionMatrixDisplay(cm2, display_labels=labels).plot(ax=ax[1])\n",
246
+ "ax[1].set_title(\"Regularized Confusion Matrix\", size=25)\n",
247
+ "\n",
248
+ "plt.tight_layout()\n",
249
+ "plt.show()"
250
+ ]
251
+ }
252
+ ],
253
+ "metadata": {
254
+ "kernelspec": {
255
+ "display_name": "NEW-VENV-1",
256
+ "language": "python",
257
+ "name": "new-venv-1"
258
+ },
259
+ "language_info": {
260
+ "codemirror_mode": {
261
+ "name": "ipython",
262
+ "version": 3
263
+ },
264
+ "file_extension": ".py",
265
+ "mimetype": "text/x-python",
266
+ "name": "python",
267
+ "nbconvert_exporter": "python",
268
+ "pygments_lexer": "ipython3",
269
+ "version": "3.11.5"
270
+ }
271
+ },
272
+ "nbformat": 4,
273
+ "nbformat_minor": 5
274
+ }
@@ -0,0 +1,263 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "bd31d202-9ef3-419d-85aa-478e28199448",
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.layers import Input, Dense, Dropout\n",
15
+ "from tensorflow.keras.models import Sequential\n",
16
+ "from tensorflow.keras.regularizers import l2\n",
17
+ "from tensorflow.keras.callbacks import EarlyStopping\n",
18
+ "from sklearn.preprocessing import StandardScaler, LabelEncoder\n",
19
+ "from sklearn.model_selection import train_test_split\n",
20
+ "from sklearn.metrics import mean_squared_error, r2_score"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": null,
26
+ "id": "10fef1a2-2c53-42b4-b8ec-f1dae5b14a1c",
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "df = pd.read_csv('insurance.csv')\n",
31
+ "df.head()"
32
+ ]
33
+ },
34
+ {
35
+ "cell_type": "code",
36
+ "execution_count": null,
37
+ "id": "6079f0b7-b6b3-493e-8a73-ff3f944db314",
38
+ "metadata": {},
39
+ "outputs": [],
40
+ "source": [
41
+ "cat_cols = ['sex', 'smoker', 'region']\n",
42
+ "df[cat_cols] = df[cat_cols].apply(LabelEncoder().fit_transform)\n",
43
+ "print(df.head())"
44
+ ]
45
+ },
46
+ {
47
+ "cell_type": "code",
48
+ "execution_count": null,
49
+ "id": "4a4d03e5-a817-4b66-9ab6-d32b18fe441a",
50
+ "metadata": {},
51
+ "outputs": [],
52
+ "source": [
53
+ "X = df.drop(columns='charges')\n",
54
+ "y = df['charges']\n",
55
+ "X = StandardScaler().fit_transform(X)\n",
56
+ "X_train_full, X_test, y_train_full, y_test = train_test_split(X, y, test_size=0.2, \n",
57
+ " random_state=42)\n",
58
+ "X_train, X_val, y_train, y_val = train_test_split(X_train_full, y_train_full, test_size=0.2, \n",
59
+ " random_state=42)"
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "code",
64
+ "execution_count": null,
65
+ "id": "f4c58e24-e103-4d92-993c-6db9f9b57657",
66
+ "metadata": {},
67
+ "outputs": [],
68
+ "source": [
69
+ "overfit_model = Sequential([\n",
70
+ " Input(shape=(X_train.shape[1],)),\n",
71
+ " Dense(64, activation='relu'),\n",
72
+ " Dense(64, activation='relu'),\n",
73
+ " Dense(1, activation='linear'),\n",
74
+ "])"
75
+ ]
76
+ },
77
+ {
78
+ "cell_type": "code",
79
+ "execution_count": null,
80
+ "id": "533969a3-9b46-4fd7-a21a-968e96821c08",
81
+ "metadata": {},
82
+ "outputs": [],
83
+ "source": [
84
+ "reg_model = Sequential([\n",
85
+ " Input(shape=(X_train.shape[1],)),\n",
86
+ " Dense(64, activation='relu'),\n",
87
+ " Dropout(0.1),\n",
88
+ " Dense(64, activation='relu', kernel_regularizer=l2(0.001)),\n",
89
+ " Dropout(0.1),\n",
90
+ " Dense(1, activation='linear'),\n",
91
+ "])"
92
+ ]
93
+ },
94
+ {
95
+ "cell_type": "code",
96
+ "execution_count": null,
97
+ "id": "c9471a5d-3c49-4531-bae6-8138c9812261",
98
+ "metadata": {},
99
+ "outputs": [],
100
+ "source": [
101
+ "print(\"\\nOverfit Model Summary:\")\n",
102
+ "overfit_model.summary()"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": null,
108
+ "id": "3efc2e9b-2b1e-416c-888e-9ea092de9d3f",
109
+ "metadata": {},
110
+ "outputs": [],
111
+ "source": [
112
+ "print(\"\\nRegularized Model Summary:\")\n",
113
+ "reg_model.summary()"
114
+ ]
115
+ },
116
+ {
117
+ "cell_type": "code",
118
+ "execution_count": null,
119
+ "id": "c76d9120-15c2-447b-9e9c-83c16c118096",
120
+ "metadata": {},
121
+ "outputs": [],
122
+ "source": [
123
+ "overfit_model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mae', 'mse'])\n",
124
+ "reg_model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mae', 'mse'])"
125
+ ]
126
+ },
127
+ {
128
+ "cell_type": "code",
129
+ "execution_count": null,
130
+ "id": "c05813ae-dd34-4caf-b008-9e683216c891",
131
+ "metadata": {},
132
+ "outputs": [],
133
+ "source": [
134
+ "history_overfit = overfit_model.fit(\n",
135
+ " X_train, y_train,\n",
136
+ " validation_data=(X_val, y_val),\n",
137
+ " epochs=20,\n",
138
+ " batch_size=16,\n",
139
+ " verbose=1\n",
140
+ ")"
141
+ ]
142
+ },
143
+ {
144
+ "cell_type": "code",
145
+ "execution_count": null,
146
+ "id": "124d821f-3272-4a0b-9a24-0fedc7f7cfee",
147
+ "metadata": {},
148
+ "outputs": [],
149
+ "source": [
150
+ "history_reg = reg_model.fit(\n",
151
+ " X_train, y_train,\n",
152
+ " validation_data=(X_val, y_val),\n",
153
+ " epochs=20,\n",
154
+ " batch_size=16,\n",
155
+ " verbose=1\n",
156
+ ")"
157
+ ]
158
+ },
159
+ {
160
+ "cell_type": "code",
161
+ "execution_count": null,
162
+ "id": "856e0e03-f681-4061-83b9-83da2823adda",
163
+ "metadata": {},
164
+ "outputs": [],
165
+ "source": [
166
+ "plt.figure(figsize=(14, 6))\n",
167
+ "plt.subplot(1, 2, 1)\n",
168
+ "plt.plot(history_overfit.history['val_loss'], label='Overfit Val Loss', color='blue', linestyle='--')\n",
169
+ "plt.plot(history_reg.history['val_loss'], label='Regularized Val Loss', color='brown', linestyle='--')\n",
170
+ "plt.title('Model Loss Comparison (MSE)')\n",
171
+ "plt.xlabel('Epoch')\n",
172
+ "plt.ylabel('Mean Squared Error Loss')\n",
173
+ "plt.legend()\n",
174
+ "plt.grid(True)"
175
+ ]
176
+ },
177
+ {
178
+ "cell_type": "code",
179
+ "execution_count": null,
180
+ "id": "f5315d68-df04-4883-b021-5b3d1be6c180",
181
+ "metadata": {},
182
+ "outputs": [],
183
+ "source": [
184
+ "plt.plot(history_overfit.history['val_mae'], label='Overfit Val MAE', color='blue')\n",
185
+ "plt.plot(history_reg.history['val_mae'], label='Regularized Val MAE', color='brown')\n",
186
+ "plt.title('Model Mean Absolute Error Comparison')\n",
187
+ "plt.xlabel('Epoch')\n",
188
+ "plt.ylabel('Mean Absolute Error')\n",
189
+ "plt.legend()\n",
190
+ "plt.grid(True)\n",
191
+ "plt.show()"
192
+ ]
193
+ },
194
+ {
195
+ "cell_type": "code",
196
+ "execution_count": null,
197
+ "id": "bef16d6a-c694-495e-91b8-4b6021146b21",
198
+ "metadata": {},
199
+ "outputs": [],
200
+ "source": [
201
+ "loss_overfit, mae_overfit, mse_overfit_eval = overfit_model.evaluate(X_test, y_test, verbose=0)\n",
202
+ "y_pred_overfit = overfit_model.predict(X_test, verbose=0).flatten()\n",
203
+ "r2_overfit = r2_score(y_test, y_pred_overfit)\n",
204
+ "\n",
205
+ "loss_reg, mae_reg, mse_reg_eval = reg_model.evaluate(X_test, y_test, verbose=0)\n",
206
+ "y_pred_reg = reg_model.predict(X_test, verbose=0).flatten()\n",
207
+ "r2_reg = r2_score(y_test, y_pred_reg)"
208
+ ]
209
+ },
210
+ {
211
+ "cell_type": "code",
212
+ "execution_count": null,
213
+ "id": "cce008a5-7588-4574-a2a9-5b34f12038d5",
214
+ "metadata": {},
215
+ "outputs": [],
216
+ "source": [
217
+ "print(\"\\nTest Set Performance:\")\n",
218
+ "print(f\"Overfit Model - MSE: {mse_overfit_eval:.2f}, MAE: {mae_overfit:.2f}, R2: {r2_overfit:.4f}\")\n",
219
+ "print(f\"Regularized Model - MSE: {mse_reg_eval:.2f}, MAE: {mae_reg:.2f}, R2: {r2_reg:.4f}\")"
220
+ ]
221
+ },
222
+ {
223
+ "cell_type": "code",
224
+ "execution_count": null,
225
+ "id": "40c9647b-55d7-4d9a-a0ae-061f71cbec72",
226
+ "metadata": {},
227
+ "outputs": [],
228
+ "source": [
229
+ "plt.figure(figsize=(10, 7))\n",
230
+ "plt.scatter(y_test, y_pred_overfit, color='skyblue', edgecolors='k', linewidth=0.5, label=f'Overfit Model (R2={r2_overfit:.3f})')\n",
231
+ "plt.scatter(y_test, y_pred_reg, color='sandybrown', edgecolors='k', linewidth=0.5, label=f'Regularized Model (R2={r2_reg:.3f})')\n",
232
+ "plt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], color='black', linestyle='--', linewidth=2, label='Perfect Prediction')\n",
233
+ "plt.title('Actual vs Predicted Charges (Test Set)')\n",
234
+ "plt.xlabel('Actual Charges ($)')\n",
235
+ "plt.ylabel('Predicted Charges ($)')\n",
236
+ "plt.legend()\n",
237
+ "plt.grid(True)\n",
238
+ "plt.show()"
239
+ ]
240
+ }
241
+ ],
242
+ "metadata": {
243
+ "kernelspec": {
244
+ "display_name": "NEW-VENV-1",
245
+ "language": "python",
246
+ "name": "new-venv-1"
247
+ },
248
+ "language_info": {
249
+ "codemirror_mode": {
250
+ "name": "ipython",
251
+ "version": 3
252
+ },
253
+ "file_extension": ".py",
254
+ "mimetype": "text/x-python",
255
+ "name": "python",
256
+ "nbconvert_exporter": "python",
257
+ "pygments_lexer": "ipython3",
258
+ "version": "3.11.5"
259
+ }
260
+ },
261
+ "nbformat": 4,
262
+ "nbformat_minor": 5
263
+ }