noshot 4.0.0__py3-none-any.whl → 6.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 (42) hide show
  1. noshot/data/ML TS XAI/XAI/Q1.ipynb +377 -0
  2. noshot/data/ML TS XAI/XAI/Q2.ipynb +362 -0
  3. noshot/data/ML TS XAI/XAI/Q3.ipynb +637 -0
  4. noshot/data/ML TS XAI/XAI/Q4.ipynb +206 -0
  5. noshot/data/ML TS XAI/XAI/Q5.ipynb +1018 -0
  6. {noshot-4.0.0.dist-info → noshot-6.0.0.dist-info}/METADATA +1 -1
  7. noshot-6.0.0.dist-info/RECORD +14 -0
  8. noshot/data/ML TS XAI/ML/ML Lab CIA 2 (I Found Only This Check)/Copy_of_Pistachio_csv.ipynb +0 -269
  9. noshot/data/ML TS XAI/ML/ML Lab CIA 2 (I Found Only This Check)/weatherAUS.ipynb +0 -155
  10. noshot/data/ML TS XAI/ML/Main/1. EDA-PCA (Balance Scale Dataset).ipynb +0 -139
  11. noshot/data/ML TS XAI/ML/Main/1. EDA-PCA (Rice Dataset).ipynb +0 -181
  12. noshot/data/ML TS XAI/ML/Main/10. HMM Veterbi.ipynb +0 -228
  13. noshot/data/ML TS XAI/ML/Main/2. KNN (Balance Scale Dataset).ipynb +0 -117
  14. noshot/data/ML TS XAI/ML/Main/2. KNN (Iris Dataset).ipynb +0 -165
  15. noshot/data/ML TS XAI/ML/Main/2. KNN (Sobar-72 Dataset).ipynb +0 -251
  16. noshot/data/ML TS XAI/ML/Main/3. LDA (Balance Scale Dataset).ipynb +0 -78
  17. noshot/data/ML TS XAI/ML/Main/3. LDA (NPHA Doctor Visits Dataset).ipynb +0 -114
  18. noshot/data/ML TS XAI/ML/Main/4. Linear Regression (Machine Dataset).ipynb +0 -115
  19. noshot/data/ML TS XAI/ML/Main/4. Linear Regression (Real Estate Dataset).ipynb +0 -159
  20. noshot/data/ML TS XAI/ML/Main/5. Logistic Regression (Magic04 Dataset).ipynb +0 -200
  21. noshot/data/ML TS XAI/ML/Main/5. Logistic Regression (Wine Dataset).ipynb +0 -112
  22. noshot/data/ML TS XAI/ML/Main/6. Naive Bayes Classifier (Agaricus Lepiota Dataset).ipynb +0 -153
  23. noshot/data/ML TS XAI/ML/Main/6. Naive Bayes Classifier (Wine Dataset).ipynb +0 -89
  24. noshot/data/ML TS XAI/ML/Main/7. SVM (Rice Dataset).ipynb +0 -208
  25. noshot/data/ML TS XAI/ML/Main/8. FeedForward NN (Sobar72 Dataset).ipynb +0 -260
  26. noshot/data/ML TS XAI/ML/Main/9. CNN (Cifar10 Dataset).ipynb +0 -238
  27. noshot/data/ML TS XAI/ML/Main/data/agaricus-lepiota.data +0 -8124
  28. noshot/data/ML TS XAI/ML/Main/data/balance-scale.txt +0 -625
  29. noshot/data/ML TS XAI/ML/Main/data/doctor-visits.csv +0 -715
  30. noshot/data/ML TS XAI/ML/Main/data/iris.csv +0 -151
  31. noshot/data/ML TS XAI/ML/Main/data/machine-data.csv +0 -210
  32. noshot/data/ML TS XAI/ML/Main/data/magic04.data +0 -19020
  33. noshot/data/ML TS XAI/ML/Main/data/real-estate.xlsx +0 -0
  34. noshot/data/ML TS XAI/ML/Main/data/rice.arff +0 -3826
  35. noshot/data/ML TS XAI/ML/Main/data/sobar-72.csv +0 -73
  36. noshot/data/ML TS XAI/ML/Main/data/wine-dataset.csv +0 -179
  37. noshot/data/ML TS XAI/ML/Other Codes.ipynb +0 -158
  38. noshot/data/ML TS XAI/ML/Rolls Royce AllinOne.ipynb +0 -691
  39. noshot-4.0.0.dist-info/RECORD +0 -40
  40. {noshot-4.0.0.dist-info → noshot-6.0.0.dist-info}/WHEEL +0 -0
  41. {noshot-4.0.0.dist-info → noshot-6.0.0.dist-info}/licenses/LICENSE.txt +0 -0
  42. {noshot-4.0.0.dist-info → noshot-6.0.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,206 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "raw",
5
+ "metadata": {},
6
+ "source": [
7
+ "1.\t Train a Convolutional Neural Network (CNN) on the CIFAR-10 dataset. Then, apply Grad-CAM and Saliency Map techniques to visualize and interpret the model’s predictions.\n",
8
+ "\n",
9
+ "from tensorflow.keras.datasets import cifar10\n",
10
+ "# Load the dataset\n",
11
+ "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n",
12
+ "\n",
13
+ "3.\tPerform a SHAP -based explanation for an image classification model using the SHAP model. \n",
14
+ "Use the following dataset\n",
15
+ "from tensorflow.keras.datasets import mnist\n",
16
+ "# Loads the MNIST dataset\n",
17
+ "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n"
18
+ ]
19
+ },
20
+ {
21
+ "cell_type": "code",
22
+ "execution_count": null,
23
+ "metadata": {
24
+ "colab": {
25
+ "base_uri": "https://localhost:8080/",
26
+ "height": 1000
27
+ },
28
+ "id": "ZsDuI4okVUFU",
29
+ "outputId": "9422b070-b240-4f9d-e3d3-c8f211f15c4a"
30
+ },
31
+ "outputs": [],
32
+ "source": [
33
+ "# Step 1: Install TensorFlow (if not already installed)\n",
34
+ "\n",
35
+ "# Step 2: Import Libraries\n",
36
+ "import numpy as np\n",
37
+ "import matplotlib.pyplot as plt\n",
38
+ "import tensorflow as tf\n",
39
+ "from tensorflow.keras.datasets import cifar10\n",
40
+ "from tensorflow.keras.utils import to_categorical\n",
41
+ "from tensorflow.keras import Input, Model\n",
42
+ "from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n",
43
+ "import warnings\n",
44
+ "warnings.filterwarnings('ignore')\n",
45
+ "\n",
46
+ "# Step 3: Load and Preprocess CIFAR-10 Data\n",
47
+ "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n",
48
+ "x_train, x_test = x_train / 255.0, x_test / 255.0\n",
49
+ "y_train_cat = to_categorical(y_train, 10)\n",
50
+ "y_test_cat = to_categorical(y_test, 10)\n",
51
+ "\n",
52
+ "# Step 4: Define Model using Functional API\n",
53
+ "inputs = Input(shape=(32, 32, 3))\n",
54
+ "x = Conv2D(32, (3, 3), activation='relu', name='conv1')(inputs)\n",
55
+ "x = MaxPooling2D((2, 2))(x)\n",
56
+ "x = Conv2D(64, (3, 3), activation='relu', name='conv2')(x)\n",
57
+ "x = MaxPooling2D((2, 2))(x)\n",
58
+ "x = Flatten()(x)\n",
59
+ "x = Dense(64, activation='relu')(x)\n",
60
+ "x = Dropout(0.5)(x)\n",
61
+ "outputs = Dense(10, activation='softmax')(x)\n",
62
+ "\n",
63
+ "model = Model(inputs=inputs, outputs=outputs)\n",
64
+ "model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n",
65
+ "\n",
66
+ "# Step 5: Train the Model\n",
67
+ "model.fit(x_train, y_train_cat, epochs=3, validation_split=0.2)\n",
68
+ "\n",
69
+ "# Step 6: Grad-CAM Visualization\n",
70
+ "img = x_test[1]\n",
71
+ "img_tensor = tf.expand_dims(img, axis=0)\n",
72
+ "\n",
73
+ "# Predict once to build model\n",
74
+ "_ = model.predict(img_tensor)\n",
75
+ "\n",
76
+ "# Create Grad-CAM model\n",
77
+ "grad_model = tf.keras.models.Model(\n",
78
+ " [model.inputs],\n",
79
+ " [model.get_layer('conv2').output, model.output]\n",
80
+ ")\n",
81
+ "\n",
82
+ "# Compute Gradients\n",
83
+ "with tf.GradientTape() as tape:\n",
84
+ " conv_outputs, predictions = grad_model(img_tensor)\n",
85
+ " pred_class = tf.argmax(predictions[0])\n",
86
+ " loss = predictions[:, pred_class]\n",
87
+ "\n",
88
+ "grads = tape.gradient(loss, conv_outputs)\n",
89
+ "pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))\n",
90
+ "\n",
91
+ "# Generate Heatmap\n",
92
+ "heatmap = tf.reduce_sum(tf.multiply(pooled_grads, conv_outputs), axis=-1)[0]\n",
93
+ "heatmap = np.maximum(heatmap, 0) / np.max(heatmap)\n",
94
+ "\n",
95
+ "# Show Grad-CAM\n",
96
+ "plt.imshow(img)\n",
97
+ "plt.imshow(heatmap, cmap='jet', alpha=0.5)\n",
98
+ "plt.axis('off')\n",
99
+ "plt.title('Grad-CAM')\n",
100
+ "plt.show()\n",
101
+ "\n",
102
+ "# Step 7: Saliency Map\n",
103
+ "img_tensor = tf.convert_to_tensor(img_tensor)\n",
104
+ "\n",
105
+ "with tf.GradientTape() as tape:\n",
106
+ " tape.watch(img_tensor)\n",
107
+ " predictions = model(img_tensor)\n",
108
+ " loss = predictions[:, pred_class]\n",
109
+ "\n",
110
+ "grads = tape.gradient(loss, img_tensor)[0]\n",
111
+ "saliency = np.max(np.abs(grads), axis=-1)\n",
112
+ "\n",
113
+ "# Show Saliency Map\n",
114
+ "plt.imshow(saliency, cmap='hot')\n",
115
+ "plt.axis('off')\n",
116
+ "plt.title('Saliency Map')\n",
117
+ "plt.show()\n"
118
+ ]
119
+ },
120
+ {
121
+ "cell_type": "code",
122
+ "execution_count": null,
123
+ "metadata": {
124
+ "colab": {
125
+ "base_uri": "https://localhost:8080/",
126
+ "height": 1000
127
+ },
128
+ "id": "E6DueKo2e1PX",
129
+ "outputId": "edfc534c-90af-4e4e-9479-4147cc974474"
130
+ },
131
+ "outputs": [],
132
+ "source": [
133
+ "import numpy as np\n",
134
+ "import tensorflow as tf\n",
135
+ "import shap\n",
136
+ "import matplotlib.pyplot as plt\n",
137
+ "\n",
138
+ "# Load and preprocess MNIST data\n",
139
+ "from tensorflow.keras.datasets import mnist\n",
140
+ "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
141
+ "\n",
142
+ "# Normalize and reshape data\n",
143
+ "x_train = x_train / 255.0\n",
144
+ "x_test = x_test / 255.0\n",
145
+ "x_train = np.expand_dims(x_train, -1)\n",
146
+ "x_test = np.expand_dims(x_test, -1)\n",
147
+ "\n",
148
+ "# Define a simple CNN model\n",
149
+ "model = tf.keras.models.Sequential([\n",
150
+ " tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n",
151
+ " tf.keras.layers.MaxPooling2D((2, 2)),\n",
152
+ " tf.keras.layers.Flatten(),\n",
153
+ " tf.keras.layers.Dense(64, activation='relu'),\n",
154
+ " tf.keras.layers.Dense(10, activation='softmax')\n",
155
+ "])\n",
156
+ "\n",
157
+ "# Compile the model\n",
158
+ "model.compile(optimizer='adam',\n",
159
+ " loss='sparse_categorical_crossentropy',\n",
160
+ " metrics=['accuracy'])\n",
161
+ "\n",
162
+ "# Train the model\n",
163
+ "model.fit(x_train, y_train, epochs=3, batch_size=128, validation_split=0.1)\n",
164
+ "\n",
165
+ "# Prepare SHAP explainer\n",
166
+ "background = x_train[np.random.choice(x_train.shape[0], 100, replace=False)]\n",
167
+ "\n",
168
+ "# Wrap the model prediction function\n",
169
+ "explainer = shap.GradientExplainer(model, background)\n",
170
+ "\n",
171
+ "# Select a few test samples for explanation\n",
172
+ "test_images = x_test[:10]\n",
173
+ "\n",
174
+ "# Compute SHAP values\n",
175
+ "shap_values = explainer.shap_values(test_images)\n",
176
+ "\n",
177
+ "# Visualize SHAP values\n",
178
+ "shap.image_plot(shap_values, test_images)\n"
179
+ ]
180
+ }
181
+ ],
182
+ "metadata": {
183
+ "colab": {
184
+ "provenance": []
185
+ },
186
+ "kernelspec": {
187
+ "display_name": "Python 3 (ipykernel)",
188
+ "language": "python",
189
+ "name": "python3"
190
+ },
191
+ "language_info": {
192
+ "codemirror_mode": {
193
+ "name": "ipython",
194
+ "version": 3
195
+ },
196
+ "file_extension": ".py",
197
+ "mimetype": "text/x-python",
198
+ "name": "python",
199
+ "nbconvert_exporter": "python",
200
+ "pygments_lexer": "ipython3",
201
+ "version": "3.12.4"
202
+ }
203
+ },
204
+ "nbformat": 4,
205
+ "nbformat_minor": 4
206
+ }