noshot 10.0.0__py3-none-any.whl → 12.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.
Files changed (74) hide show
  1. noshot/data/DLE FSD BDA/DLE/1. DNN (Image Classification).ipynb +389 -0
  2. noshot/data/DLE FSD BDA/DLE/2. DNN vs CNN.ipynb +516 -0
  3. noshot/data/DLE FSD BDA/DLE/3. CNN (Object Detecrion).ipynb +259 -0
  4. noshot/data/DLE FSD BDA/DLE/4. FCN (Image Segmentaion).ipynb +274 -0
  5. noshot/main.py +3 -3
  6. {noshot-10.0.0.dist-info → noshot-12.0.0.dist-info}/METADATA +1 -1
  7. noshot-12.0.0.dist-info/RECORD +13 -0
  8. {noshot-10.0.0.dist-info → noshot-12.0.0.dist-info}/WHEEL +1 -1
  9. noshot/data/ML TS XAI/ML/CNN(Image_for_Folders_5).ipynb +0 -201
  10. noshot/data/ML TS XAI/ML/CNN(Image_form_Folder_2).ipynb +0 -201
  11. noshot/data/ML TS XAI/ML/Json Codes/ML LAB CIA 2.ipynb +0 -409
  12. noshot/data/ML TS XAI/ML/ML 1/1. EDA-PCA (Balance Scale Dataset).ipynb +0 -147
  13. noshot/data/ML TS XAI/ML/ML 1/1. EDA-PCA (Rice Dataset).ipynb +0 -181
  14. noshot/data/ML TS XAI/ML/ML 1/10. HMM Veterbi.ipynb +0 -152
  15. noshot/data/ML TS XAI/ML/ML 1/2. KNN (Balance Scale Dataset).ipynb +0 -117
  16. noshot/data/ML TS XAI/ML/ML 1/2. KNN (Iris Dataset).ipynb +0 -156
  17. noshot/data/ML TS XAI/ML/ML 1/2. KNN (Sobar-72 Dataset).ipynb +0 -215
  18. noshot/data/ML TS XAI/ML/ML 1/3. LDA (Balance Scale Dataset).ipynb +0 -78
  19. noshot/data/ML TS XAI/ML/ML 1/3. LDA (NPHA Doctor Visits Dataset).ipynb +0 -114
  20. noshot/data/ML TS XAI/ML/ML 1/4. Linear Regression (Machine Dataset).ipynb +0 -115
  21. noshot/data/ML TS XAI/ML/ML 1/4. Linear Regression (Real Estate Dataset).ipynb +0 -146
  22. noshot/data/ML TS XAI/ML/ML 1/5. Logistic Regression (Magic04 Dataset).ipynb +0 -130
  23. noshot/data/ML TS XAI/ML/ML 1/5. Logistic Regression (Wine Dataset).ipynb +0 -112
  24. noshot/data/ML TS XAI/ML/ML 1/6. Naive Bayes Classifier (Agaricus Lepiota Dataset).ipynb +0 -118
  25. noshot/data/ML TS XAI/ML/ML 1/6. Naive Bayes Classifier (Wine Dataset).ipynb +0 -89
  26. noshot/data/ML TS XAI/ML/ML 1/7. SVM (Rice Dataset).ipynb +0 -120
  27. noshot/data/ML TS XAI/ML/ML 1/8. FeedForward NN (Sobar72 Dataset).ipynb +0 -262
  28. noshot/data/ML TS XAI/ML/ML 1/9. CNN (Cifar10 Dataset).ipynb +0 -156
  29. noshot/data/ML TS XAI/ML/ML 2/1. PCA.ipynb +0 -162
  30. noshot/data/ML TS XAI/ML/ML 2/10. CNN.ipynb +0 -100
  31. noshot/data/ML TS XAI/ML/ML 2/11. HMM.ipynb +0 -336
  32. noshot/data/ML TS XAI/ML/ML 2/2. KNN.ipynb +0 -149
  33. noshot/data/ML TS XAI/ML/ML 2/3. LDA.ipynb +0 -132
  34. noshot/data/ML TS XAI/ML/ML 2/4. Linear Regression.ipynb +0 -86
  35. noshot/data/ML TS XAI/ML/ML 2/5. Logistic Regression.ipynb +0 -115
  36. noshot/data/ML TS XAI/ML/ML 2/6. Naive Bayes (Titanic).ipynb +0 -196
  37. noshot/data/ML TS XAI/ML/ML 2/6. Naive Bayes (Wine).ipynb +0 -98
  38. noshot/data/ML TS XAI/ML/ML 2/7. SVM Linear.ipynb +0 -109
  39. noshot/data/ML TS XAI/ML/ML 2/8. SVM Non-Linear.ipynb +0 -195
  40. noshot/data/ML TS XAI/ML/ML 2/9. FNN With Regularization.ipynb +0 -189
  41. noshot/data/ML TS XAI/ML/ML 2/9. FNN Without Regularization.ipynb +0 -197
  42. noshot/data/ML TS XAI/ML/ML 2/All in One Lab CIA 1 Q.ipynb +0 -1087
  43. noshot/data/ML TS XAI/ML/ML 3 (Latest)/1. PCA EDA.ipynb +0 -274
  44. noshot/data/ML TS XAI/ML/ML 3 (Latest)/10. CNN.ipynb +0 -170
  45. noshot/data/ML TS XAI/ML/ML 3 (Latest)/11. HMM 2.ipynb +0 -1087
  46. noshot/data/ML TS XAI/ML/ML 3 (Latest)/11. HMM 3.ipynb +0 -178
  47. noshot/data/ML TS XAI/ML/ML 3 (Latest)/11. HMM 4.ipynb +0 -185
  48. noshot/data/ML TS XAI/ML/ML 3 (Latest)/11. HMM.ipynb +0 -106
  49. noshot/data/ML TS XAI/ML/ML 3 (Latest)/2. KNN.ipynb +0 -177
  50. noshot/data/ML TS XAI/ML/ML 3 (Latest)/3. LDA.ipynb +0 -195
  51. noshot/data/ML TS XAI/ML/ML 3 (Latest)/4. Linear Regression.ipynb +0 -267
  52. noshot/data/ML TS XAI/ML/ML 3 (Latest)/5. Logistic Regression.ipynb +0 -104
  53. noshot/data/ML TS XAI/ML/ML 3 (Latest)/6. Bayesian Classifier.ipynb +0 -109
  54. noshot/data/ML TS XAI/ML/ML 3 (Latest)/7. SVM.ipynb +0 -220
  55. noshot/data/ML TS XAI/ML/ML 3 (Latest)/8. MLP.ipynb +0 -99
  56. noshot/data/ML TS XAI/ML/ML 3 (Latest)/9. Ridge - Lasso.ipynb +0 -211
  57. noshot/data/ML TS XAI/ML/ML 3 (Latest)/9. Ridge Lasso 2.ipynb +0 -99
  58. noshot/data/ML TS XAI/ML/ML 3 (Latest)/Image Load Example.ipynb +0 -118
  59. noshot/data/ML TS XAI/ML/ML 3 (Latest)/Updated_Untitled.ipynb +0 -603
  60. noshot/data/ML TS XAI/ML/ML Lab AllinOne.ipynb +0 -961
  61. noshot/data/ML TS XAI/ML/ML Lab H Sec/1. Iris Dataset (Softmax vs Sigmoid).ipynb +0 -231
  62. noshot/data/ML TS XAI/ML/ML Lab H Sec/2. Student Dataset (Overfit vs Regularized).ipynb +0 -269
  63. noshot/data/ML TS XAI/ML/ML Lab H Sec/3. Insurance Target Categorical (Overfit vs Regularized).ipynb +0 -274
  64. noshot/data/ML TS XAI/ML/ML Lab H Sec/3. Insurance Target Numerical (Overfit vs Regularized).ipynb +0 -263
  65. noshot/data/ML TS XAI/ML/ML Lab H Sec/4. Smart House System HMM.ipynb +0 -198
  66. noshot/data/ML TS XAI/ML/ML Lab H Sec/5. Fraud Detection System HMM.ipynb +0 -201
  67. noshot/data/ML TS XAI/ML/ML Lab H Sec/insurance.csv +0 -1339
  68. noshot/data/ML TS XAI/ML/ML Lab H Sec/iris1.data +0 -151
  69. noshot/data/ML TS XAI/ML/ML Lab H Sec/student-mat.csv +0 -396
  70. noshot/data/ML TS XAI/ML/ML Lab H Sec/student-por.csv +0 -650
  71. noshot/data/ML TS XAI/ML/Rolls Royce AllinOne.ipynb +0 -691
  72. noshot-10.0.0.dist-info/RECORD +0 -72
  73. {noshot-10.0.0.dist-info → noshot-12.0.0.dist-info}/licenses/LICENSE.txt +0 -0
  74. {noshot-10.0.0.dist-info → noshot-12.0.0.dist-info}/top_level.txt +0 -0
@@ -1,691 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": null,
6
- "id": "c9eed15a-01b3-4a60-830c-ffe6cf24913f",
7
- "metadata": {},
8
- "outputs": [],
9
- "source": [
10
- "#CNN\n",
11
- "import tensorflow as tf\n",
12
- "from tensorflow.keras.models import Sequential\n",
13
- "from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\n",
14
- "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
15
- "from tensorflow.keras.utils import to_categorical\n",
16
- "from sklearn.metrics import classification_report, confusion_matrix\n",
17
- "import matplotlib.pyplot as plt\n",
18
- "import seaborn as sns\n",
19
- "import numpy as np\n",
20
- "\n",
21
- "# Set directory paths\n",
22
- "train_dir = 'path_to_train_directory'\n",
23
- "test_dir = 'path_to_test_directory'\n",
24
- "\n",
25
- "# Image Data Generator for data preprocessing (rescaling and augmentation)\n",
26
- "train_datagen = ImageDataGenerator(\n",
27
- " rescale=1./255,\n",
28
- " horizontal_flip=True,\n",
29
- " zoom_range=0.2,\n",
30
- " shear_range=0.2,\n",
31
- " rotation_range=20\n",
32
- ")\n",
33
- "\n",
34
- "test_datagen = ImageDataGenerator(rescale=1./255)\n",
35
- "\n",
36
- "# Flow images from directories\n",
37
- "train_generator = train_datagen.flow_from_directory(\n",
38
- " train_dir,\n",
39
- " target_size=(32, 32),\n",
40
- " batch_size=32,\n",
41
- " class_mode='categorical'\n",
42
- ")\n",
43
- "\n",
44
- "test_generator = test_datagen.flow_from_directory(\n",
45
- " test_dir,\n",
46
- " target_size=(32, 32),\n",
47
- " batch_size=32,\n",
48
- " class_mode='categorical'\n",
49
- ")\n",
50
- "\n",
51
- "# Build CNN\n",
52
- "model = Sequential([\n",
53
- " Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)),\n",
54
- " MaxPooling2D(2,2),\n",
55
- " Conv2D(64, (3,3), activation='relu'),\n",
56
- " MaxPooling2D(2,2),\n",
57
- " Conv2D(64, (3,3), activation='relu'),\n",
58
- " MaxPooling2D(2,2),\n",
59
- " Flatten(),\n",
60
- " Dense(128, activation='relu'),\n",
61
- " Dense(10, activation='softmax') # 10 classes in CIFAR-10 dataset\n",
62
- "])\n",
63
- "\n",
64
- "# Compile model\n",
65
- "model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n",
66
- "\n",
67
- "# Train model\n",
68
- "history = model.fit(\n",
69
- " train_generator,\n",
70
- " epochs=10,\n",
71
- " validation_data=test_generator\n",
72
- ")\n",
73
- "\n",
74
- "# Plot accuracy & loss\n",
75
- "plt.figure(figsize=(12, 4))\n",
76
- "plt.subplot(1, 2, 1)\n",
77
- "plt.plot(history.history['accuracy'], label='Train')\n",
78
- "plt.plot(history.history['val_accuracy'], label='Val')\n",
79
- "plt.title(\"Model Accuracy\")\n",
80
- "plt.xlabel(\"Epochs\")\n",
81
- "plt.ylabel(\"Accuracy\")\n",
82
- "plt.legend()\n",
83
- "\n",
84
- "plt.subplot(1, 2, 2)\n",
85
- "plt.plot(history.history['loss'], label='Train')\n",
86
- "plt.plot(history.history['val_loss'], label='Val')\n",
87
- "plt.title(\"Model Loss\")\n",
88
- "plt.xlabel(\"Epochs\")\n",
89
- "plt.ylabel(\"Loss\")\n",
90
- "plt.legend()\n",
91
- "plt.show()\n",
92
- "\n",
93
- "# Predictions and classification report\n",
94
- "y_pred = model.predict(test_generator)\n",
95
- "y_pred_classes = np.argmax(y_pred, axis=1)\n",
96
- "\n",
97
- "# True labels from test_generator\n",
98
- "y_true = test_generator.classes\n",
99
- "\n",
100
- "# Classification report\n",
101
- "print(\"Classification Report:\")\n",
102
- "print(classification_report(y_true, y_pred_classes))\n",
103
- "\n",
104
- "# Confusion matrix\n",
105
- "cm = confusion_matrix(y_true, y_pred_classes)\n",
106
- "plt.figure(figsize=(10, 8))\n",
107
- "sns.heatmap(cm, annot=True, fmt=\"d\", cmap='Blues')\n",
108
- "plt.title(\"Confusion Matrix\")\n",
109
- "plt.xlabel(\"Predicted Label\")\n",
110
- "plt.ylabel(\"True Label\")\n",
111
- "plt.show()\n"
112
- ]
113
- },
114
- {
115
- "cell_type": "code",
116
- "execution_count": null,
117
- "id": "fcbf452d-1962-4ae8-bb35-595f0f664d8a",
118
- "metadata": {},
119
- "outputs": [],
120
- "source": [
121
- "#CNN preloaded data\n",
122
- "\n",
123
- "import tensorflow as tf\n",
124
- "from tensorflow.keras.models import Sequential\n",
125
- "from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\n",
126
- "from tensorflow.keras.utils import to_categorical\n",
127
- "from sklearn.metrics import classification_report, confusion_matrix\n",
128
- "import matplotlib.pyplot as plt\n",
129
- "import seaborn as sns\n",
130
- "import numpy as np\n",
131
- "\n",
132
- "# Load dataset\n",
133
- "(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()\n",
134
- "\n",
135
- "# Normalize and one-hot encode\n",
136
- "x_train, x_test = x_train / 255.0, x_test / 255.0\n",
137
- "y_train_cat = to_categorical(y_train, 10)\n",
138
- "y_test_cat = to_categorical(y_test, 10)\n",
139
- "\n",
140
- "# Build CNN\n",
141
- "model = Sequential([\n",
142
- " Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)),\n",
143
- " MaxPooling2D(2,2),\n",
144
- " Conv2D(64, (3,3), activation='relu'),\n",
145
- " MaxPooling2D(2,2),\n",
146
- " Conv2D(64, (3,3), activation='relu'),\n",
147
- " MaxPooling2D(2,2),\n",
148
- " Flatten(),\n",
149
- " Dense(128, activation='relu'),\n",
150
- " Dense(10, activation='softmax')\n",
151
- "])\n",
152
- "\n",
153
- "# Compile\n",
154
- "model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n",
155
- "\n",
156
- "# Train\n",
157
- "history = model.fit(x_train, y_train_cat, epochs=10, validation_data=(x_test, y_test_cat))\n",
158
- "\n",
159
- "# Plot accuracy & loss\n",
160
- "plt.figure(figsize=(12, 4))\n",
161
- "plt.subplot(1, 2, 1)\n",
162
- "plt.plot(history.history['accuracy'], label='Train')\n",
163
- "plt.plot(history.history['val_accuracy'], label='Val')\n",
164
- "plt.title(\"Model Accuracy\")\n",
165
- "plt.xlabel(\"Epochs\")\n",
166
- "plt.ylabel(\"Accuracy\")\n",
167
- "plt.legend()\n",
168
- "\n",
169
- "plt.subplot(1, 2, 2)\n",
170
- "plt.plot(history.history['loss'], label='Train')\n",
171
- "plt.plot(history.history['val_loss'], label='Val')\n",
172
- "plt.title(\"Model Loss\")\n",
173
- "plt.xlabel(\"Epochs\")\n",
174
- "plt.ylabel(\"Loss\")\n",
175
- "plt.legend()\n",
176
- "plt.show()\n",
177
- "\n",
178
- "# Predictions and classification report\n",
179
- "y_pred = model.predict(x_test)\n",
180
- "y_pred_classes = np.argmax(y_pred, axis=1)\n",
181
- "y_true = y_test.flatten()\n",
182
- "\n",
183
- "# Classification report\n",
184
- "print(\"Classification Report:\")\n",
185
- "print(classification_report(y_true, y_pred_classes))\n",
186
- "\n",
187
- "# Confusion matrix\n",
188
- "cm = confusion_matrix(y_true, y_pred_classes)\n",
189
- "plt.figure(figsize=(10, 8))\n",
190
- "sns.heatmap(cm, annot=True, fmt=\"d\", cmap='Blues')\n",
191
- "plt.title(\"Confusion Matrix\")\n",
192
- "plt.xlabel(\"Predicted Label\")\n",
193
- "plt.ylabel(\"True Label\")\n",
194
- "plt.show()\n"
195
- ]
196
- },
197
- {
198
- "cell_type": "code",
199
- "execution_count": null,
200
- "id": "5a729281-229e-4a51-9f7c-71f1c6f65c42",
201
- "metadata": {},
202
- "outputs": [],
203
- "source": [
204
- "#SVM\n",
205
- "\n",
206
- "import pandas as pd\n",
207
- "from sklearn.model_selection import train_test_split\n",
208
- "from sklearn.preprocessing import StandardScaler\n",
209
- "from sklearn.svm import SVC\n",
210
- "from sklearn.metrics import classification_report, confusion_matrix\n",
211
- "from sklearn.decomposition import PCA\n",
212
- "import matplotlib.pyplot as plt\n",
213
- "import seaborn as sns\n",
214
- "\n",
215
- "# Load dataset\n",
216
- "df = pd.read_csv('ML TS XAI/ML/data/wine-dataset.csv') # Change to your CSV filename\n",
217
- "\n",
218
- "# Split features and target\n",
219
- "X = df.drop('target', axis=1) # Change 'label' to your actual target column\n",
220
- "y = df['target']\n",
221
- "\n",
222
- "# Train-test split\n",
223
- "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
224
- "\n",
225
- "# Standardize features\n",
226
- "scaler = StandardScaler()\n",
227
- "X_train = scaler.fit_transform(X_train)\n",
228
- "X_test = scaler.transform(X_test)\n",
229
- "\n",
230
- "# Train SVM\n",
231
- "svm_model = SVC(kernel='rbf', C=1.0, gamma='scale')\n",
232
- "svm_model.fit(X_train, y_train)\n",
233
- "\n",
234
- "# Predict\n",
235
- "y_pred = svm_model.predict(X_test)\n",
236
- "\n",
237
- "# Classification report\n",
238
- "print(\"Classification Report:\")\n",
239
- "print(classification_report(y_test, y_pred))\n",
240
- "\n",
241
- "# Confusion Matrix\n",
242
- "cm = confusion_matrix(y_test, y_pred)\n",
243
- "plt.figure(figsize=(8, 6))\n",
244
- "sns.heatmap(cm, annot=True, fmt=\"d\", cmap='Purples')\n",
245
- "plt.title(\"Confusion Matrix\")\n",
246
- "plt.xlabel(\"Predicted\")\n",
247
- "plt.ylabel(\"Actual\")\n",
248
- "plt.show()\n",
249
- "\n",
250
- "# Optional: PCA 2D visualization (for visual separation)\n",
251
- "pca = PCA(n_components=2)\n",
252
- "X_pca = pca.fit_transform(X_test)\n",
253
- "\n",
254
- "plt.figure(figsize=(8, 6))\n",
255
- "sns.scatterplot(x=X_pca[:, 0], y=X_pca[:, 1], hue=y_pred, palette='Set2', s=60)\n",
256
- "plt.title(\"SVM Predictions (Visualized with PCA)\")\n",
257
- "plt.xlabel(\"PCA 1\")\n",
258
- "plt.ylabel(\"PCA 2\")\n",
259
- "plt.legend(title='Predicted Class')\n",
260
- "plt.grid(True)\n",
261
- "plt.show()\n"
262
- ]
263
- },
264
- {
265
- "cell_type": "code",
266
- "execution_count": null,
267
- "id": "47118414-c5b3-4f3a-9f45-e607861207d7",
268
- "metadata": {},
269
- "outputs": [],
270
- "source": [
271
- "#logistic Regression\n",
272
- "\n",
273
- "import pandas as pd\n",
274
- "from sklearn.model_selection import train_test_split\n",
275
- "from sklearn.preprocessing import StandardScaler\n",
276
- "from sklearn.linear_model import LogisticRegression\n",
277
- "from sklearn.metrics import classification_report, confusion_matrix, roc_curve, auc\n",
278
- "from sklearn.decomposition import PCA\n",
279
- "import matplotlib.pyplot as plt\n",
280
- "import seaborn as sns\n",
281
- "\n",
282
- "# Load dataset\n",
283
- "# Load dataset\n",
284
- "df = pd.read_csv('ML TS XAI/ML/data/wine-dataset.csv') # Change to your CSV filename\n",
285
- "\n",
286
- "# Split features and target\n",
287
- "X = df.drop('target', axis=1) # Change 'label' to your actual target column\n",
288
- "y = df['target']\n",
289
- "\n",
290
- "# Train-test split\n",
291
- "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
292
- "\n",
293
- "# Feature scaling\n",
294
- "scaler = StandardScaler()\n",
295
- "X_train = scaler.fit_transform(X_train)\n",
296
- "X_test = scaler.transform(X_test)\n",
297
- "\n",
298
- "# Train logistic regression\n",
299
- "logreg = LogisticRegression(max_iter=1000)\n",
300
- "logreg.fit(X_train, y_train)\n",
301
- "\n",
302
- "# Predict labels\n",
303
- "y_pred = logreg.predict(X_test)\n",
304
- "\n",
305
- "# Classification report\n",
306
- "print(\"Classification Report:\")\n",
307
- "print(classification_report(y_test, y_pred))\n",
308
- "\n",
309
- "# Confusion Matrix\n",
310
- "cm = confusion_matrix(y_test, y_pred)\n",
311
- "plt.figure(figsize=(8, 6))\n",
312
- "sns.heatmap(cm, annot=True, fmt=\"d\", cmap='Greens')\n",
313
- "plt.title(\"Confusion Matrix\")\n",
314
- "plt.xlabel(\"Predicted\")\n",
315
- "plt.ylabel(\"Actual\")\n",
316
- "plt.show()\n",
317
- "\n",
318
- "# PCA 2D Scatter plot\n",
319
- "pca = PCA(n_components=2)\n",
320
- "X_pca = pca.fit_transform(X_test)\n",
321
- "\n",
322
- "plt.figure(figsize=(8, 6))\n",
323
- "sns.scatterplot(x=X_pca[:, 0], y=X_pca[:, 1], hue=y_pred, palette='coolwarm', s=60)\n",
324
- "plt.title(\"Logistic Regression Predictions (PCA 2D)\")\n",
325
- "plt.xlabel(\"PCA Component 1\")\n",
326
- "plt.ylabel(\"PCA Component 2\")\n",
327
- "plt.legend(title='Predicted Class')\n",
328
- "plt.grid(True)\n",
329
- "plt.show()\n",
330
- "\n",
331
- "# ROC Curve (Only for binary classification)\n",
332
- "if len(y.unique()) == 2:\n",
333
- " y_prob = logreg.predict_proba(X_test)[:, 1]\n",
334
- " fpr, tpr, _ = roc_curve(y_test, y_prob)\n",
335
- " roc_auc = auc(fpr, tpr)\n",
336
- "\n",
337
- " plt.figure(figsize=(8, 6))\n",
338
- " plt.plot(fpr, tpr, label=f\"ROC Curve (AUC = {roc_auc:.2f})\", color='darkorange')\n",
339
- " plt.plot([0, 1], [0, 1], linestyle='--', color='gray')\n",
340
- " plt.title(\"ROC Curve\")\n",
341
- " plt.xlabel(\"False Positive Rate\")\n",
342
- " plt.ylabel(\"True Positive Rate\")\n",
343
- " plt.legend()\n",
344
- " plt.grid(True)\n",
345
- " plt.show()\n",
346
- "else:\n",
347
- " print(\"ROC Curve skipped — works only for binary classification.\")\n"
348
- ]
349
- },
350
- {
351
- "cell_type": "code",
352
- "execution_count": null,
353
- "id": "1a438b33-1336-4f80-8bd8-85bf18d04fe9",
354
- "metadata": {},
355
- "outputs": [],
356
- "source": [
357
- "#Bayseian Classifier\n",
358
- "import pandas as pd\n",
359
- "from sklearn.model_selection import train_test_split\n",
360
- "from sklearn.preprocessing import StandardScaler\n",
361
- "from sklearn.naive_bayes import GaussianNB\n",
362
- "from sklearn.metrics import classification_report, confusion_matrix, roc_curve, auc\n",
363
- "from sklearn.decomposition import PCA\n",
364
- "import matplotlib.pyplot as plt\n",
365
- "import seaborn as sns\n",
366
- "\n",
367
- "# Load dataset\n",
368
- "# Load dataset\n",
369
- "df = pd.read_csv('ML TS XAI/ML/data/wine-dataset.csv') # Change to your CSV filename\n",
370
- "\n",
371
- "# Split features and target\n",
372
- "X = df.drop('target', axis=1) # Change 'label' to your actual target column\n",
373
- "y = df['target']\n",
374
- "\n",
375
- "# Train-test split\n",
376
- "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
377
- "\n",
378
- "# Feature scaling\n",
379
- "scaler = StandardScaler()\n",
380
- "X_train = scaler.fit_transform(X_train)\n",
381
- "X_test = scaler.transform(X_test)\n",
382
- "\n",
383
- "# Train Naive Bayes Classifier\n",
384
- "nb = GaussianNB()\n",
385
- "nb.fit(X_train, y_train)\n",
386
- "\n",
387
- "# Predict labels\n",
388
- "y_pred = nb.predict(X_test)\n",
389
- "\n",
390
- "# Classification report\n",
391
- "print(\"Classification Report:\")\n",
392
- "print(classification_report(y_test, y_pred))\n",
393
- "\n",
394
- "# Confusion Matrix\n",
395
- "cm = confusion_matrix(y_test, y_pred)\n",
396
- "plt.figure(figsize=(8, 6))\n",
397
- "sns.heatmap(cm, annot=True, fmt=\"d\", cmap='Blues')\n",
398
- "plt.title(\"Confusion Matrix\")\n",
399
- "plt.xlabel(\"Predicted\")\n",
400
- "plt.ylabel(\"Actual\")\n",
401
- "plt.show()\n",
402
- "\n",
403
- "# PCA 2D Scatter plot\n",
404
- "pca = PCA(n_components=2)\n",
405
- "X_pca = pca.fit_transform(X_test)\n",
406
- "\n",
407
- "plt.figure(figsize=(8, 6))\n",
408
- "sns.scatterplot(x=X_pca[:, 0], y=X_pca[:, 1], hue=y_pred, palette='coolwarm', s=60)\n",
409
- "plt.title(\"Naive Bayes Predictions (PCA 2D)\")\n",
410
- "plt.xlabel(\"PCA Component 1\")\n",
411
- "plt.ylabel(\"PCA Component 2\")\n",
412
- "plt.legend(title='Predicted Class')\n",
413
- "plt.grid(True)\n",
414
- "plt.show()\n",
415
- "\n",
416
- "# ROC Curve (Only for binary classification)\n",
417
- "if len(y.unique()) == 2:\n",
418
- " y_prob = nb.predict_proba(X_test)[:, 1]\n",
419
- " fpr, tpr, _ = roc_curve(y_test, y_prob)\n",
420
- " roc_auc = auc(fpr, tpr)\n",
421
- "\n",
422
- " plt.figure(figsize=(8, 6))\n",
423
- " plt.plot(fpr, tpr, label=f\"ROC Curve (AUC = {roc_auc:.2f})\", color='darkorange')\n",
424
- " plt.plot([0, 1], [0, 1], linestyle='--', color='gray')\n",
425
- " plt.title(\"ROC Curve\")\n",
426
- " plt.xlabel(\"False Positive Rate\")\n",
427
- " plt.ylabel(\"True Positive Rate\")\n",
428
- " plt.legend()\n",
429
- " plt.grid(True)\n",
430
- " plt.show()\n",
431
- "else:\n",
432
- " print(\"ROC Curve skipped — works only for binary classification.\")\n"
433
- ]
434
- },
435
- {
436
- "cell_type": "code",
437
- "execution_count": null,
438
- "id": "76cb7d36-be60-42a4-8089-8dd3f33d32e6",
439
- "metadata": {},
440
- "outputs": [],
441
- "source": [
442
- "#FeedFOrwardNeuralNetwork\n",
443
- "\n",
444
- "import pandas as pd\n",
445
- "from sklearn.model_selection import train_test_split\n",
446
- "from sklearn.preprocessing import StandardScaler\n",
447
- "from sklearn.metrics import classification_report, confusion_matrix, roc_curve, auc\n",
448
- "from sklearn.decomposition import PCA\n",
449
- "import matplotlib.pyplot as plt\n",
450
- "import seaborn as sns\n",
451
- "from tensorflow.keras.models import Sequential\n",
452
- "from tensorflow.keras.layers import Dense\n",
453
- "\n",
454
- "# Load dataset\n",
455
- "# Load dataset\n",
456
- "df = pd.read_csv('ML TS XAI/ML/data/wine-dataset.csv') # Change to your CSV filename\n",
457
- "\n",
458
- "# Split features and target\n",
459
- "X = df.drop('target', axis=1) # Change 'label' to your actual target column\n",
460
- "y = df['target']\n",
461
- "# Train-test split\n",
462
- "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
463
- "\n",
464
- "# Feature scaling\n",
465
- "scaler = StandardScaler()\n",
466
- "X_train = scaler.fit_transform(X_train)\n",
467
- "X_test = scaler.transform(X_test)\n",
468
- "\n",
469
- "# Build Feedforward Neural Network\n",
470
- "model = Sequential()\n",
471
- "model.add(Dense(128, input_dim=X_train.shape[1], activation='relu')) # Hidden Layer\n",
472
- "model.add(Dense(64, activation='relu')) # Hidden Layer\n",
473
- "model.add(Dense(1, activation='sigmoid')) # Output Layer (for binary classification)\n",
474
- "\n",
475
- "# Compile the model\n",
476
- "model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])\n",
477
- "\n",
478
- "# Train the model\n",
479
- "history = model.fit(X_train, y_train, epochs=50, batch_size=32, validation_data=(X_test, y_test))\n",
480
- "\n",
481
- "# Predict labels\n",
482
- "y_pred = (model.predict(X_test) > 0.5).astype(\"int32\")\n",
483
- "\n",
484
- "# Classification report\n",
485
- "print(\"Classification Report:\")\n",
486
- "print(classification_report(y_test, y_pred))\n",
487
- "\n",
488
- "# Confusion Matrix\n",
489
- "cm = confusion_matrix(y_test, y_pred)\n",
490
- "plt.figure(figsize=(8, 6))\n",
491
- "sns.heatmap(cm, annot=True, fmt=\"d\", cmap='Blues')\n",
492
- "plt.title(\"Confusion Matrix\")\n",
493
- "plt.xlabel(\"Predicted\")\n",
494
- "plt.ylabel(\"Actual\")\n",
495
- "plt.show()\n",
496
- "\n",
497
- "# PCA 2D Scatter plot\n",
498
- "pca = PCA(n_components=2)\n",
499
- "X_pca = pca.fit_transform(X_test)\n",
500
- "\n",
501
- "plt.figure(figsize=(8, 6))\n",
502
- "sns.scatterplot(x=X_pca[:, 0], y=X_pca[:, 1], hue=y_pred.flatten(), palette='coolwarm', s=60)\n",
503
- "plt.title(\"Feedforward Neural Network Predictions (PCA 2D)\")\n",
504
- "plt.xlabel(\"PCA Component 1\")\n",
505
- "plt.ylabel(\"PCA Component 2\")\n",
506
- "plt.legend(title='Predicted Class')\n",
507
- "plt.grid(True)\n",
508
- "plt.show()\n",
509
- "\n",
510
- "# ROC Curve (Only for binary classification)\n",
511
- "if len(y.unique()) == 2:\n",
512
- " y_prob = model.predict(X_test)\n",
513
- " fpr, tpr, _ = roc_curve(y_test, y_prob)\n",
514
- " roc_auc = auc(fpr, tpr)\n",
515
- "\n",
516
- " plt.figure(figsize=(8, 6))\n",
517
- " plt.plot(fpr, tpr, label=f\"ROC Curve (AUC = {roc_auc:.2f})\", color='darkorange')\n",
518
- " plt.plot([0, 1], [0, 1], linestyle='--', color='gray')\n",
519
- " plt.title(\"ROC Curve\")\n",
520
- " plt.xlabel(\"False Positive Rate\")\n",
521
- " plt.ylabel(\"True Positive Rate\")\n",
522
- " plt.legend()\n",
523
- " plt.grid(True)\n",
524
- " plt.show()\n",
525
- "else:\n",
526
- " print(\"ROC Curve skipped — works only for binary classification.\")\n",
527
- "# Plot training & validation accuracy and loss\n",
528
- "plt.figure(figsize=(12, 6))\n",
529
- "\n",
530
- "# Accuracy plot\n",
531
- "plt.subplot(1, 2, 1)\n",
532
- "plt.plot(history.history['accuracy'], label='train accuracy')\n",
533
- "plt.plot(history.history['val_accuracy'], label='val accuracy')\n",
534
- "plt.title('Training and Validation Accuracy')\n",
535
- "plt.xlabel('Epochs')\n",
536
- "plt.ylabel('Accuracy')\n",
537
- "plt.legend()\n",
538
- "\n",
539
- "# Loss plot\n",
540
- "plt.subplot(1, 2, 2)\n",
541
- "plt.plot(history.history['loss'], label='train loss')\n",
542
- "plt.plot(history.history['val_loss'], label='val loss')\n",
543
- "plt.title('Training and Validation Loss')\n",
544
- "plt.xlabel('Epochs')\n",
545
- "plt.ylabel('Loss')\n",
546
- "plt.legend()\n",
547
- "\n",
548
- "plt.tight_layout()\n",
549
- "plt.show()\n"
550
- ]
551
- },
552
- {
553
- "cell_type": "code",
554
- "execution_count": null,
555
- "id": "cc441bb8-59f8-4c0d-8cfc-526d308bb229",
556
- "metadata": {},
557
- "outputs": [],
558
- "source": [
559
- "import numpy as np\n",
560
- "\n",
561
- "# Define the HMM parameters (example with 2 states and 3 observations)\n",
562
- "states = ['S1', 'S2']\n",
563
- "observations = ['O1', 'O2', 'O3']\n",
564
- "\n",
565
- "# Transition probabilities\n",
566
- "A = np.array([\n",
567
- " [0.7, 0.3], # P(S1->S1, S1->S2)\n",
568
- " [0.4, 0.6] # P(S2->S1, S2->S2)\n",
569
- "])\n",
570
- "\n",
571
- "# Emission probabilities\n",
572
- "B = np.array([\n",
573
- " [0.5, 0.4, 0.1], # P(O1|S1), P(O2|S1), P(O3|S1)\n",
574
- " [0.1, 0.3, 0.6] # P(O1|S2), P(O2|S2), P(O3|S2)\n",
575
- "])\n",
576
- "\n",
577
- "# Initial probabilities\n",
578
- "pi = np.array([0.6, 0.4]) # P(S1), P(S2)\n",
579
- "\n",
580
- "# Observations sequence (e.g., O1, O2, O1)\n",
581
- "obs_sequence = [0, 1, 0] # Indices of O1, O2, O1 in the 'observations' list\n",
582
- "\n",
583
- "# FORWARD ALGORITHM (Forward Chaining)\n",
584
- "def forward(A, B, pi, obs_sequence):\n",
585
- " T = len(obs_sequence)\n",
586
- " N = len(A)\n",
587
- " \n",
588
- " # Initialize forward probability matrix (alpha)\n",
589
- " alpha = np.zeros((T, N))\n",
590
- " \n",
591
- " # Initialization step\n",
592
- " alpha[0, :] = pi * B[:, obs_sequence[0]]\n",
593
- " \n",
594
- " # Recursion step\n",
595
- " for t in range(1, T):\n",
596
- " for j in range(N):\n",
597
- " alpha[t, j] = B[j, obs_sequence[t]] * np.sum(alpha[t-1, :] * A[:, j])\n",
598
- " \n",
599
- " # Termination step\n",
600
- " prob = np.sum(alpha[T-1, :])\n",
601
- " return prob, alpha\n",
602
- "\n",
603
- "# BACKWARD ALGORITHM (Backward Chaining)\n",
604
- "def backward(A, B, obs_sequence):\n",
605
- " T = len(obs_sequence)\n",
606
- " N = len(A)\n",
607
- " \n",
608
- " # Initialize backward probability matrix (beta)\n",
609
- " beta = np.zeros((T, N))\n",
610
- " \n",
611
- " # Initialization step\n",
612
- " beta[T-1, :] = 1\n",
613
- " \n",
614
- " # Recursion step (backward direction)\n",
615
- " for t in range(T-2, -1, -1):\n",
616
- " for i in range(N):\n",
617
- " beta[t, i] = np.sum(A[i, :] * B[:, obs_sequence[t+1]] * beta[t+1, :])\n",
618
- " \n",
619
- " return beta\n",
620
- "\n",
621
- "# VITERBI ALGORITHM (for finding the most likely state sequence)\n",
622
- "def viterbi(A, B, pi, obs_sequence):\n",
623
- " T = len(obs_sequence)\n",
624
- " N = len(A)\n",
625
- " \n",
626
- " # Initialize viterbi probability matrix (delta) and path matrix (psi)\n",
627
- " delta = np.zeros((T, N))\n",
628
- " psi = np.zeros((T, N), dtype=int)\n",
629
- " \n",
630
- " # Initialization step\n",
631
- " delta[0, :] = pi * B[:, obs_sequence[0]]\n",
632
- " \n",
633
- " # Recursion step\n",
634
- " for t in range(1, T):\n",
635
- " for j in range(N):\n",
636
- " delta[t, j] = np.max(delta[t-1, :] * A[:, j]) * B[j, obs_sequence[t]]\n",
637
- " psi[t, j] = np.argmax(delta[t-1, :] * A[:, j])\n",
638
- " \n",
639
- " # Termination step (finding the best last state)\n",
640
- " best_path_prob = np.max(delta[T-1, :])\n",
641
- " best_last_state = np.argmax(delta[T-1, :])\n",
642
- " \n",
643
- " # Backtrack to find the best path (most likely sequence of states)\n",
644
- " best_path = np.zeros(T, dtype=int)\n",
645
- " best_path[T-1] = best_last_state\n",
646
- " \n",
647
- " for t in range(T-2, -1, -1):\n",
648
- " best_path[t] = psi[t+1, best_path[t+1]]\n",
649
- " \n",
650
- " # Map state indices to state names\n",
651
- " best_state_sequence = [states[i] for i in best_path]\n",
652
- " \n",
653
- " return best_path_prob, best_state_sequence\n",
654
- "\n",
655
- "# Run the forward algorithm\n",
656
- "forward_prob, alpha = forward(A, B, pi, obs_sequence)\n",
657
- "print(f\"Forward Probability: {forward_prob}\")\n",
658
- "\n",
659
- "# Run the backward algorithm\n",
660
- "beta = backward(A, B, obs_sequence)\n",
661
- "print(f\"Backward Matrix (Beta):\\n{beta}\")\n",
662
- "\n",
663
- "# Run the Viterbi algorithm\n",
664
- "viterbi_prob, viterbi_states = viterbi(A, B, pi, obs_sequence)\n",
665
- "print(f\"Viterbi Probability: {viterbi_prob}\")\n",
666
- "print(f\"Most Likely State Sequence (Viterbi): {viterbi_states}\")\n"
667
- ]
668
- }
669
- ],
670
- "metadata": {
671
- "kernelspec": {
672
- "display_name": "Python 3 (ipykernel)",
673
- "language": "python",
674
- "name": "python3"
675
- },
676
- "language_info": {
677
- "codemirror_mode": {
678
- "name": "ipython",
679
- "version": 3
680
- },
681
- "file_extension": ".py",
682
- "mimetype": "text/x-python",
683
- "name": "python",
684
- "nbconvert_exporter": "python",
685
- "pygments_lexer": "ipython3",
686
- "version": "3.12.4"
687
- }
688
- },
689
- "nbformat": 4,
690
- "nbformat_minor": 5
691
- }