SumantBobade commited on
Commit
029d082
·
verified ·
1 Parent(s): bc3884c
Files changed (2) hide show
  1. app.py +719 -0
  2. requirements.txt +12 -0
app.py ADDED
@@ -0,0 +1,719 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """app.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1umH6P4k0xEUEZsizNZfLzFttGrqivmwq
8
+ """
9
+
10
+ import pandas as pd
11
+ import numpy as np
12
+ import matplotlib.pyplot as plt
13
+ import seaborn as sns
14
+ from sklearn.preprocessing import StandardScaler
15
+ from sklearn.decomposition import PCA
16
+ from xgboost import XGBClassifier
17
+ from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, classification_report
18
+ from sklearn.model_selection import train_test_split
19
+ import warnings
20
+ warnings.filterwarnings("ignore")
21
+
22
+ df = pd.read_csv("/content/diabetes_prediction_dataset.csv")
23
+ df.head(10)
24
+
25
+ df.describe()
26
+
27
+ df.info()
28
+
29
+ df.isnull().sum()
30
+
31
+ print(df.duplicated().sum())
32
+
33
+ df = df.drop_duplicates()
34
+ print("________Removed Duplicate________")
35
+ print(df.duplicated().sum())
36
+
37
+ #Function to add counts on bars
38
+
39
+ def add_counts(ax):
40
+ for p in ax.patches:
41
+ ax.annotate(f'{int(p.get_height())}', (p.get_x()+p.get_width()/2., p.get_height()),
42
+ ha ='center', va='center', fontsize=10, color='black', xytext=(0,5), textcoords='offset points')
43
+
44
+ #set up the matplotlib figure
45
+ fig, axes = plt.subplots(3, 2, figsize=(15, 15))
46
+
47
+ #Plot gender grouped by dibetes
48
+ ax = sns.countplot(ax=axes[0,0], x='gender', hue='diabetes', data=df)
49
+ ax.set_title('Gender Grouped by Diabetes')
50
+ add_counts(ax)
51
+
52
+ #Plot hypertension groupef by diabetes
53
+ ax = sns.countplot(ax=axes[0,1], x='hypertension', hue='diabetes', data=df)
54
+ ax.set_title('Hypertension Grouped by Diabetes')
55
+ add_counts(ax)
56
+
57
+ #Plot heart disease grouped by diabetes
58
+ ax = sns.countplot(ax=axes[1,0], x='heart_disease', hue='diabetes', data=df)
59
+ ax.set_title('Heart Disease Grouped by Diabetes')
60
+ add_counts(ax)
61
+
62
+ #Plot smoking history groupde by diabetes
63
+ ax = sns.countplot(ax=axes[1,1], x='smoking_history', hue='diabetes', data=df)
64
+ ax.set_title('Smoking History Grouped by Diabetes')
65
+ add_counts(ax)
66
+
67
+ # Plot diabetes
68
+ ax = sns.countplot(ax=axes[2, 0], x='diabetes', data=df)
69
+ axes[2, 0].set_title('Diabetes Count')
70
+ add_counts(ax)
71
+
72
+ # Create pie plot for diabetes
73
+ diabetes_counts = df['diabetes'].value_counts()
74
+ axes[2, 1].pie(diabetes_counts, labels=diabetes_counts.index, autopct='%1.1f%%', startangle=90)
75
+ axes[2, 1].set_title('Diabetes Distribution')
76
+ axes[2, 1].axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
77
+ axes[2, 1].legend(title='Diabetes:', loc='upper right')
78
+ # Adjust the layout
79
+ plt.tight_layout()
80
+
81
+ # Show the plots
82
+ plt.show()
83
+
84
+ #Calculate minimum, maximum, and average age
85
+ min_age = df['age'].min()
86
+ max_age = df['age'].max()
87
+ avg_age = df['age'].mean()
88
+
89
+ #Count of individuals with and without diabetes
90
+ diabetes_counts = df['diabetes'].value_counts()
91
+
92
+ #Group by dibetes status and calculate min and max ages
93
+ grouped_ages = df.groupby('diabetes')['age'].agg(['min', 'max'])
94
+
95
+ #Print the results
96
+ print("Minimum Age:", min_age)
97
+ print("Maximum Age:", max_age)
98
+ print("Average Age:", avg_age)
99
+ print(diabetes_counts)
100
+ print("Age Statistics by Diabetes Status:")
101
+ print(grouped_ages)
102
+
103
+ # Plotting
104
+ fig, ax = plt.subplots(1, 2, figsize=(14, 6))
105
+
106
+ # Plot for overall min, max, and average age
107
+ bars = ax[0].bar(['Min Age', 'Max Age', 'Avg Age'], [min_age, max_age, avg_age], color=['blue', 'red', 'green'])
108
+ ax[0].set_title('Overall Age Statistics')
109
+ ax[0].set_ylabel('Age')
110
+
111
+ # Annotate bars with their values
112
+ for bar in bars:
113
+ yval = bar.get_height()
114
+ ax[0].text(bar.get_x() + bar.get_width()/2, yval, round(yval, 2), va='bottom') # Add text to the top of the bars
115
+
116
+ # Plot for min and max ages grouped by diabetes status
117
+ grouped_bars = grouped_ages.plot(kind='bar', ax=ax[1])
118
+ ax[1].set_title('Age Statistics by Diabetes Status')
119
+ ax[1].set_ylabel('Age')
120
+
121
+ # Annotate bars with their values
122
+ for p in grouped_bars.patches:
123
+ grouped_bars.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005))
124
+
125
+ plt.tight_layout()
126
+ plt.show()
127
+
128
+ cross_table = pd.crosstab(df['diabetes'], df['smoking_history'])
129
+
130
+ # Create subplots
131
+ fig, ax = plt.subplots(1, 2, figsize=(20, 8))
132
+
133
+ # Plotting the cross table as a heatmap
134
+ sns.heatmap(cross_table, cmap='YlOrRd', annot=True, fmt='d', linewidths=0.5, linecolor='black', ax=ax[0])
135
+ ax[0].set_title('Diabetes and Smoking History (Heatmap)')
136
+ ax[0].set_xlabel('Smoking History')
137
+ ax[0].set_ylabel('Diabetes')
138
+
139
+ # Plotting the cross table with separate bars for smoking history
140
+ cross_table.plot(kind='bar', stacked=False, ax=ax[1], color=plt.cm.Paired.colors)
141
+ ax[1].set_title('Diabetes and Smoking History (Bar Plot)')
142
+ ax[1].set_xlabel('Diabetes')
143
+ ax[1].set_ylabel('Count')
144
+ ax[1].legend(title='Smoking History', bbox_to_anchor=(1.05, 1), loc='upper left')
145
+
146
+ # Annotate bars with their values
147
+ for container in ax[1].containers:
148
+ ax[1].bar_label(container)
149
+
150
+ plt.tight_layout()
151
+ plt.show()
152
+
153
+ #incode the data
154
+
155
+ from sklearn.preprocessing import LabelEncoder
156
+ le = LabelEncoder()
157
+ df['gender'] = le.fit_transform(df['gender'])
158
+ df['smoking_history'] = le.fit_transform(df['smoking_history'])
159
+ df.head()
160
+
161
+ ##Assume df is your datafram
162
+
163
+ #Selecting features and target variable
164
+ features = ['gender', 'age', 'hypertension', 'heart_disease', 'smoking_history', 'bmi', 'HbA1c_level', 'blood_glucose_level']
165
+ X= df[features]
166
+ Y= df['diabetes']
167
+
168
+ # Standardizing the features
169
+ scaler = StandardScaler()
170
+ X_scaled = scaler.fit_transform(X)
171
+
172
+ # Applying PCA
173
+ pca = PCA()
174
+ X_pca = pca.fit_transform(X_scaled)
175
+
176
+ # Plotting the cumulative explained variance
177
+ plt.figure(figsize=(10, 6))
178
+ plt.plot(range(1, len(pca.explained_variance_ratio_) + 1),
179
+ pca.explained_variance_ratio_.cumsum(), marker='o', linestyle='--')
180
+ plt.title('Explained Variance by Number of Principal Components')
181
+ plt.xlabel('Number of Principal Components')
182
+ plt.ylabel('Cumulative Explained Variance')
183
+ plt.grid()
184
+
185
+ # Find the index of the maximum cumulative explained variance
186
+ max_index = pca.explained_variance_ratio_.cumsum().argmax()
187
+
188
+ # Annotate the point with the highest cumulative explained variance
189
+ plt.annotate(f'Max: PC {max_index + 1}',
190
+ xy=(max_index + 1, pca.explained_variance_ratio_.cumsum()[max_index]),
191
+ xytext=(max_index + 2, pca.explained_variance_ratio_.cumsum()[max_index] - 0.05),
192
+ arrowprops=dict(facecolor='black', arrowstyle='->', color='black'))
193
+
194
+ plt.show()
195
+
196
+ # Printing explained variance ratios
197
+ for i, ratio in enumerate(pca.explained_variance_ratio_.cumsum()):
198
+ print(f'Principal Component {i+1}: {ratio:.4f} cumulative explained variance')
199
+
200
+ # Choose the number of components that explain most of the variance
201
+ n_components = max_index + 1
202
+
203
+ # Applying PCA with the optimal number of components
204
+ pca = PCA(n_components=n_components)
205
+ X_pca = pca.fit_transform(X_scaled)
206
+
207
+ #Splitting the date into traing and testing sets
208
+ X_train, X_test, y_train, y_test = train_test_split(X_pca, Y, test_size=0.2, random_state=42)
209
+
210
+ #Initializing and training the XGBoost model
211
+ xgb_model = XGBClassifier()
212
+ xgb_model.fit(X_train, y_train)
213
+
214
+ #Making predictions on the test set
215
+ y_pred = xgb_model.predict(X_test)
216
+
217
+ # Evaluating the model
218
+ accuracy = accuracy_score(y_test, y_pred)
219
+ print(f'XGBoost Accuracy: {accuracy:.4f}')
220
+ print(f'XGBoost Classification Report:\n{classification_report(y_test, y_pred)}')
221
+
222
+ # Compute the confusion matrix
223
+ conf_matrix = confusion_matrix(y_test, y_pred)
224
+
225
+ # Plotting the confusion matrix
226
+ plt.figure(figsize=(8, 6))
227
+ sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=['No Diabetes', 'Diabetes'], yticklabels=['No Diabetes', 'Diabetes'])
228
+ plt.title('Confusion Matrix')
229
+ plt.xlabel('Predicted')
230
+ plt.ylabel('Actual')
231
+ plt.show()
232
+
233
+ import pickle
234
+
235
+ # Save the model
236
+ with open('Diabetes_model.pkl', 'wb') as f:
237
+ pickle.dump(xgb_model, f)
238
+
239
+ # Prepare custom data
240
+ custom_data = [
241
+ [1, 45, 0, 0, 1, 25.6, 6.5, 110],
242
+ [0, 35, 1, 0, 0, 28.2, 7.2, 130],
243
+ [1, 55, 1, 1, 1, 31.4, 8.0, 150],
244
+ [0, 42, 0, 1, 0, 26.9, 7.0, 120],
245
+ [1, 50, 1, 0, 1, 29.7, 7.8, 140]
246
+ ]
247
+
248
+ # Convert to pandas DataFrame
249
+ custom_df = pd.DataFrame(custom_data, columns=features)
250
+
251
+ # Standardize the custom data
252
+ custom_X = scaler.transform(custom_df[features])
253
+
254
+ # Apply PCA transformation
255
+ custom_X_pca = pca.transform(custom_X)
256
+
257
+ # Make predictions using the trained XGBoost model
258
+ custom_predictions = xgb_model.predict(custom_X_pca)
259
+
260
+ # Print the predictions
261
+ for i, pred in enumerate(custom_predictions):
262
+ if pred == 0:
263
+ print(f"Person {i+1} is not predicted to have diabetes.")
264
+ else:
265
+ print(f"Person {i+1} is predicted to have diabetes.")
266
+
267
+ import xgboost as xgb
268
+ from sklearn.model_selection import train_test_split
269
+ from sklearn.metrics import accuracy_score
270
+
271
+ # Step 1: Split the data
272
+ X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
273
+
274
+ # Step 2: Instantiate the classifier
275
+ xgb_clf = xgb.XGBClassifier(use_label_encoder=False, eval_metric='logloss')
276
+
277
+ # Step 3: Train the model
278
+ xgb_clf.fit(X_train, y_train)
279
+
280
+ # Step 4: Make predictions
281
+ y_pred = xgb_clf.predict(X_test)
282
+
283
+ # Step 5: Evaluate the model
284
+ accuracy = accuracy_score(y_test, y_pred)
285
+ print(f"Model accuracy: {accuracy:.2f}")
286
+
287
+ # Compute the confusion matrix
288
+ conf_matrix = confusion_matrix(y_test, y_pred)
289
+
290
+ # Plotting the confusion matrix
291
+ plt.figure(figsize=(8, 6))
292
+ sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=['No Hypertension', 'Hypertension'], yticklabels=['No Hypertension', 'Hypertension'])
293
+ plt.title('Confusion Matrix')
294
+ plt.xlabel('Predicted')
295
+ plt.ylabel('Actual')
296
+ plt.show()
297
+
298
+ import pickle
299
+
300
+ # Save the model
301
+ with open('hypertension_model.pkl', 'wb') as f:
302
+ pickle.dump(xgb_model, f)
303
+
304
+ features = ['gender', 'age', 'diabetes', 'heart_disease', 'smoking_history', 'bmi', 'HbA1c_level', 'blood_glucose_level']
305
+ customs_data = [
306
+ [1, 45, 0, 0, 1, 25.6, 6.5, 110],
307
+ [0, 35, 1, 0, 0, 28.2, 7.2, 130],
308
+ [1, 55, 1, 1, 1, 31.4, 8.0, 150],
309
+ [0, 42, 1, 1, 0, 26.9, 7.0, 120],
310
+ [1, 50, 1, 0, 1, 29.7, 7.8, 140]
311
+ ]
312
+
313
+ custom_df = pd.DataFrame(customs_data, columns=features)
314
+ custom_predictions = xgb_model.predict(custom_df)
315
+
316
+ for i, pred in enumerate(custom_predictions):
317
+ if pred == 0:
318
+ print(f"Person {i+1} is not predicted to have hypertension.")
319
+ else:
320
+ print(f"Person {i+1} is predicted to have hypertension.")
321
+
322
+ import numpy as np
323
+ import pandas as pd
324
+ import matplotlib.pyplot as plt
325
+ import seaborn as sns
326
+ from IPython.display import display
327
+ import cv2
328
+ import io
329
+ from PIL import Image
330
+ from sklearn.model_selection import train_test_split
331
+ from sklearn.preprocessing import MinMaxScaler
332
+ import tensorflow as tf
333
+
334
+ print(tf.__version__)
335
+
336
+ import kagglehub
337
+
338
+ # Download latest version
339
+ path = kagglehub.dataset_download("borhanitrash/alzheimer-mri-disease-classification-dataset")
340
+
341
+ print("Path to dataset files:", path)
342
+
343
+ train ='/content/train-00000-of-00001-c08a401c53fe5312.parquet'
344
+ test = '/content/test-00000-of-00001-44110b9df98c5585.parquet'
345
+ categorias = {
346
+ 0: 'Mild_Demented',
347
+ 1: 'Moderate_Demented',
348
+ 2: 'Non_Demented',
349
+ 3: 'Very_Mild_Demented'
350
+ }
351
+ data_train = pd.read_parquet(train)
352
+ data_test = pd.read_parquet(test)
353
+ data_train.head()
354
+
355
+ img_dict = data_train['image'][0]
356
+ byte_string = img_dict['bytes']
357
+ nparr = np.frombuffer(byte_string, np.uint8)
358
+ img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
359
+
360
+ image = Image.open(io.BytesIO(byte_string))
361
+ display(image)
362
+
363
+ def extraccion_y_transformacion(images_set):
364
+ et_list_images=[]
365
+ images_bytes = images_set['image']
366
+ for img_dict in images_bytes:
367
+ byte_string = img_dict['bytes']
368
+ nparr = np.frombuffer(byte_string, np.uint8)
369
+ img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
370
+ et_list_images.append(img)
371
+ return et_list_images
372
+
373
+ def visualizar_imagenes(image_set, categorias, limit=5):
374
+ fig, axes = plt.subplots(1, limit, figsize=(10, 5))
375
+ image_bytes = image_set['image']
376
+
377
+ for i, (ax, row) in enumerate(zip(axes, image_set.iterrows())):
378
+ img_dict = row[1]['image']
379
+ label = row[1]['label']
380
+ name = categorias[label]
381
+
382
+ byte_string = img_dict['bytes']
383
+ image = Image.open(io.BytesIO(byte_string))
384
+
385
+ ax.imshow(image, cmap='gray')
386
+ ax.set_title(name)
387
+ ax.axis('off')
388
+
389
+ if i + 1 == limit:
390
+ break
391
+
392
+ plt.tight_layout()
393
+ plt.show()
394
+
395
+ train_transformado = extraccion_y_transformacion(data_train)
396
+ test_transformado = extraccion_y_transformacion(data_test)
397
+ print(train_transformado[:1])
398
+
399
+ visualizar_imagenes(data_train, categorias, limit=5)
400
+
401
+ y_test = []
402
+ for label in data_test['label']:
403
+ y_test.append(label)
404
+
405
+ y_train = []
406
+ for label in data_train['label']:
407
+ y_train.append(label)
408
+
409
+ y_train = np.array(y_train)
410
+ y_test = np.array(y_test)
411
+
412
+ unique, counts = np.unique(y_train, return_counts=True)
413
+ plt.bar(unique, counts)
414
+ plt.xlabel('Clases')
415
+ plt.ylabel('Cantidad')
416
+ plt.title('Distribucion de clases')
417
+ plt.xticks(unique)
418
+ plt.show()
419
+
420
+ y_train = tf.one_hot(y_train.astype(np.int32), depth=4)
421
+ y_test = tf.one_hot(y_test.astype(np.int32), depth=4)
422
+ y_train
423
+
424
+ train_transformado = np.array(train_transformado)/255
425
+ test_transformado = np.array(test_transformado)/255
426
+
427
+ train_transformado = [np.expand_dims(img, axis=-1) for img in train_transformado] # agregar el canal de escala de grises
428
+ test_transformado = [np.expand_dims(img, axis=-1) for img in test_transformado]
429
+ #test_transformado = [np.expand_dims(img, axis=-1) for img in test_transformado]
430
+ train_transformado = np.array(train_transformado)
431
+ test_transformado = np.array(test_transformado)
432
+ train_transformado[0].shape
433
+
434
+ train_transformado.shape
435
+
436
+ class MinMaxScaler3D(MinMaxScaler):
437
+ def fit_transform(self, X, y=None):
438
+ x = np.reshape(X, newshape=(X.shape[0]*X.shape[1], X.shape[2]))
439
+ return np.reshape(super().fit_transform(x, y=y), newshape=X.shape)
440
+
441
+ scaler = MinMaxScaler3D()
442
+ train_scaled = [scaler.fit_transform(X=img) for img in train_transformado]
443
+ train_scaled = np.array(train_scaled)
444
+ test_scaled = [scaler.fit_transform(X=img) for img in test_transformado]
445
+ test_scaled = np.array(test_scaled)
446
+
447
+ train_scaled.shape
448
+
449
+ from tensorflow import keras
450
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
451
+ from tensorflow.keras import layers
452
+ from tensorflow.keras import Sequential, initializers
453
+ from sklearn.preprocessing import StandardScaler
454
+ from tensorflow.keras.optimizers import Adam
455
+
456
+ #optimizer = Adam()
457
+ optimizer = Adam(
458
+ learning_rate=0.001, # Tasa de aprendizaje
459
+ beta_1=0.9, # Decay rate del primer momento
460
+ beta_2=0.999, # Decay rate del segundo momento
461
+ epsilon=1e-07 # Término de suavizado
462
+ )
463
+
464
+ model = Sequential([
465
+ layers.Input(shape=(128,128,1)),
466
+
467
+ layers.Conv2D(64, kernel_size=(2,2), activation='relu',kernel_initializer = initializers.HeNormal(seed=42), padding='same'),
468
+ #layers.BatchNormalization(),
469
+ layers.MaxPooling2D(pool_size=(2,2)),
470
+ #layers.Dropout(0.25),
471
+
472
+ layers.Conv2D(64,kernel_size=(2,2), activation='relu', kernel_initializer = initializers.HeNormal(seed=42), padding='same'),
473
+ #layers.BatchNormalization(),
474
+ layers.MaxPooling2D(pool_size=(2,2)),
475
+ #layers.Dropout(0.25),
476
+
477
+ layers.Conv2D(128, kernel_size=(3,3), activation='relu', kernel_initializer = initializers.HeNormal(seed=42), padding='same'),
478
+ #layers.BatchNormalization(),
479
+ layers.MaxPooling2D(pool_size=(2,2)),
480
+ #layers.Dropout(0.25),
481
+
482
+ layers.Flatten(),
483
+ layers.Dropout(0.25),
484
+ layers.Dense(256, activation='relu'),
485
+ layers.Dense(len(categorias), activation='softmax')
486
+ ])
487
+
488
+ model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=["accuracy"])
489
+
490
+ model = Sequential([
491
+ Conv2D(32, (3,3), activation='relu', input_shape=(128, 128, 3)),
492
+ ...
493
+ ])
494
+
495
+
496
+ class myCallback(tf.keras.callbacks.Callback):
497
+ def on_epoch_end(self, epoch, logs={}):
498
+ if (logs.get('accuracy') > 0.995):
499
+ print("\nReached 99.5% accuracy so cancelling training!")
500
+ self.model.stop_training = True
501
+
502
+ callbacks = myCallback()
503
+ history = model.fit(
504
+ train_scaled,
505
+ y_train,
506
+ batch_size=10,
507
+ epochs=20,
508
+ validation_split=0.1,
509
+ callbacks=[callbacks]
510
+ )
511
+
512
+ plt.plot(history.history['loss'], label='Train loss')
513
+ plt.plot(history.history['val_loss'], label='Validation loss')
514
+ plt.plot(history.history['accuracy'], label='Train accuracy')
515
+ plt.plot(history.history['val_accuracy'], label='Validation accuracy')
516
+ plt.legend()
517
+ plt.title('Loss and accuracy (also validation) per Epoch')
518
+ plt.show()
519
+
520
+ history.model.layers
521
+
522
+ w, b = history.model.layers[0].get_weights()
523
+
524
+ b.shape
525
+
526
+ test_loss, test_acc = model.evaluate(test_scaled, y_test, verbose=2)
527
+ print(f'Test accuracy: {test_acc}'
528
+
529
+ predictions = model.predict(test_scaled)
530
+
531
+ predictions[0]
532
+
533
+ np.argmax(predictions[0])
534
+
535
+ data_test['label'][0]
536
+
537
+ from sklearn.metrics import classification_report
538
+ predicted_classes = np.argmax(predictions, axis=1)
539
+ true_classes = np.argmax(y_test, axis=1)
540
+ report = classification_report(true_classes, predicted_classes)
541
+ print(report)
542
+
543
+ def plot_image(i, predictions_array, true_label, img):
544
+ predictions_array, true_label, img = predictions_array, true_label[i], img[i]
545
+ plt.grid(False)
546
+ plt.xticks([])
547
+ plt.yticks([])
548
+
549
+ plt.imshow(img, cmap=plt.cm.binary)
550
+
551
+ predicted_label = np.argmax(predictions_array)
552
+ if predicted_label == true_label:
553
+ color = 'blue'
554
+ else:
555
+ color = 'red'
556
+
557
+ plt.xlabel("{} {:2.0f}% ({})".format(categorias[predicted_label],
558
+ 100*np.max(predictions_array),
559
+ categorias[true_label]),
560
+ color=color)
561
+
562
+ def plot_value_array(i, predictions_array, true_label):
563
+ predictions_array, true_label = predictions_array, true_label[i]
564
+ plt.grid(False)
565
+ plt.xticks(range(4))
566
+ plt.yticks([])
567
+ thisplot = plt.bar(range(4), predictions_array, color="#777777")
568
+ plt.ylim([0, 1])
569
+ predicted_label = np.argmax(predictions_array)
570
+
571
+ thisplot[predicted_label].set_color('red')
572
+ thisplot[true_label].set_color('blue')
573
+
574
+ i = 0
575
+ plt.figure(figsize=(6,3))
576
+ plt.subplot(1,2,1)
577
+ plot_image(i, predictions[i], np.argmax(y_test, axis=1), test_scaled)
578
+ plt.subplot(1,2,2)
579
+ plot_value_array(i, predictions[i], np.argmax(y_test, axis=1))
580
+ plt.show()
581
+
582
+ i = 8
583
+ plt.figure(figsize=(6,3))
584
+ plt.subplot(1,2,1)
585
+ plot_image(i, predictions[i], np.argmax(y_test, axis=1), test_scaled)
586
+ plt.subplot(1,2,2)
587
+ plot_value_array(i, predictions[i], np.argmax(y_test, axis=1))
588
+ plt.show()
589
+
590
+ num_rows = 4
591
+ num_cols = 3
592
+ num_images = num_rows*num_cols
593
+ plt.figure(figsize=(2*2*num_cols, 2*num_rows))
594
+ for i in range(num_images):
595
+ plt.subplot(num_rows, 2*num_cols, 2*i+1)
596
+ plot_image(i, predictions[i], np.argmax(y_test, axis=1), test_scaled)
597
+ plt.subplot(num_rows, 2*num_cols, 2*i+2)
598
+ plot_value_array(i, predictions[i], np.argmax(y_test, axis=1))
599
+ plt.tight_layout()
600
+ plt.show()
601
+
602
+
603
+
604
+
605
+
606
+
607
+
608
+
609
+
610
+
611
+
612
+ !pip install streamlit ngrok
613
+
614
+ with open("app.py", "w") as file:
615
+ file.write("""
616
+ # Streamlit Multi-Page App for Hypertension and Diabetes Prediction
617
+
618
+ import streamlit as st
619
+ from streamlit_option_menu import option_menu
620
+ import pandas as pd
621
+ import numpy as np
622
+
623
+ # Placeholder models (replace with actual models trained in the notebook)
624
+ class PlaceholderModel:
625
+ def predict(self, X):
626
+ return np.random.choice([0, 1], size=(len(X),))
627
+
628
+ diabetes_model = PlaceholderModel()
629
+ hypertension_model = PlaceholderModel()
630
+
631
+ # Streamlit App Pages
632
+ st.set_page_config(page_title="Health Prediction App", layout="wide")
633
+
634
+ # Sidebar Navigation
635
+ with st.sidebar:
636
+ selected = option_menu(
637
+ "Navigation", ["Home", "Hypertension", "Diabetes"],
638
+ icons=["house", "activity", "heart"],
639
+ menu_icon="menu-app", default_index=0
640
+ )
641
+
642
+ if selected == "Home":
643
+ st.title("Health Prediction App")
644
+ st.write("Select the prediction model from the sidebar to get started.")
645
+
646
+ elif selected == "Hypertension":
647
+ st.title("Hypertension Prediction")
648
+
649
+ # Input form for Hypertension
650
+ age = st.number_input("Age", min_value=0, max_value=120, value=30)
651
+ systolic_bp = st.number_input("Systolic Blood Pressure", min_value=50, max_value=250, value=120)
652
+ diastolic_bp = st.number_input("Diastolic Blood Pressure", min_value=30, max_value=150, value=80)
653
+ cholesterol = st.number_input("Cholesterol Level", min_value=50, max_value=400, value=200)
654
+ smoking = st.selectbox("Smoking Status", ("Non-Smoker", "Former Smoker", "Current Smoker"))
655
+ activity = st.selectbox("Physical Activity Level", ("Low", "Moderate", "High"))
656
+
657
+ smoking_encoded = {"Non-Smoker": 0, "Former Smoker": 1, "Current Smoker": 2}[smoking]
658
+ activity_encoded = {"Low": 0, "Moderate": 1, "High": 2}[activity]
659
+
660
+ data = pd.DataFrame({
661
+ 'Age': [age],
662
+ 'SystolicBP': [systolic_bp],
663
+ 'DiastolicBP': [diastolic_bp],
664
+ 'Cholesterol': [cholesterol],
665
+ 'SmokingStatus': [smoking_encoded],
666
+ 'PhysicalActivity': [activity_encoded]
667
+ })
668
+
669
+ st.write("Input Data:", data)
670
+
671
+ if st.button("Predict Hypertension"):
672
+ prediction = hypertension_model.predict(data)
673
+ st.subheader("Prediction Result")
674
+ st.write("Hypertension Detected" if prediction[0] == 1 else "No Hypertension Detected")
675
+
676
+ elif selected == "Diabetes":
677
+ st.title("Diabetes Prediction")
678
+
679
+ # Input form for Diabetes
680
+ pregnancies = st.number_input("Pregnancies", min_value=0, max_value=20, value=1)
681
+ glucose = st.number_input("Glucose Level", min_value=0, max_value=300, value=100)
682
+ blood_pressure = st.number_input("Blood Pressure", min_value=0, max_value=200, value=80)
683
+ skin_thickness = st.number_input("Skin Thickness", min_value=0, max_value=100, value=20)
684
+ insulin = st.number_input("Insulin Level", min_value=0, max_value=900, value=30)
685
+ bmi = st.number_input("BMI", min_value=0.0, max_value=70.0, value=25.0)
686
+ dpf = st.number_input("Diabetes Pedigree Function", min_value=0.0, max_value=3.0, value=0.5)
687
+ age = st.number_input("Age", min_value=0, max_value=120, value=30)
688
+
689
+ data = pd.DataFrame({
690
+ 'Pregnancies': [pregnancies],
691
+ 'Glucose': [glucose],
692
+ 'BloodPressure': [blood_pressure],
693
+ 'SkinThickness': [skin_thickness],
694
+ 'Insulin': [insulin],
695
+ 'BMI': [bmi],
696
+ 'DiabetesPedigreeFunction': [dpf],
697
+ 'Age': [age]
698
+ })
699
+
700
+ st.write("Input Data:", data)
701
+
702
+ if st.button("Predict Diabetes"):
703
+ prediction = diabetes_model.predict(data)
704
+ st.subheader("Prediction Result")
705
+ st.write("Diabetes Detected" if prediction[0] == 1 else "No Diabetes Detected")
706
+
707
+ """)
708
+
709
+ !pip install pyngrok
710
+
711
+ !ngrok config add-authtoken 2r6YO98poBRtVcoGAcpePugmNTz_6Gb7QdBULNA8UfpeSgnLD
712
+
713
+ !pip install streamlit-option-menu
714
+
715
+
716
+ from pyngrok import ngrok
717
+ !streamlit run app.py &>/dev/null&
718
+ public_url = ngrok.connect(8501)
719
+ print(f"Streamlit app is live at {public_url}")
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pandas
2
+ numpy
3
+ matplotlib
4
+ seaborn
5
+ scikit-learn
6
+ xgboost
7
+ tensorflow
8
+ streamlit
9
+ streamlit-option-menu
10
+ pyngrok
11
+ opencv-python
12
+ Pillow