amirTamer commited on
Commit
a40d10a
·
1 Parent(s): 16d92ee
GNB/Variance_threshhold_code_preprocessing.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+
4
+ # Load dataset
5
+ import keras
6
+ (X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data(path="mnist.npz")
7
+
8
+
9
+ # Flatten images: (60000, 28, 28) → (60000, 784)
10
+ X_train = X_train.reshape(X_train.shape[0], -1)
11
+ X_test = X_test.reshape(X_test.shape[0], -1)
12
+
13
+ # Normalize (important for variance stability)
14
+ X_train = X_train / 255.0
15
+ X_test = X_test / 255.0
16
+ def compute_pixel_variance(X):
17
+ mean = np.mean(X, axis=0)
18
+ variance = np.mean((X - mean) ** 2, axis=0)
19
+ return variance
20
+
21
+
22
+ def variance_threshold(X, threshold=1e-4):
23
+ variances = compute_pixel_variance(X)
24
+ mask = variances > threshold
25
+ X_reduced = X[:, mask]
26
+ return X_reduced, mask, variances
27
+ # Apply your method
28
+ X_reduced, mask, variances = variance_threshold(X_train, threshold=1e-4)
29
+
30
+ # Print results
31
+ original_features = X_train.shape[1]
32
+ remaining_features = X_reduced.shape[1]
33
+ removed_features = original_features - remaining_features
34
+
35
+ print(f"Original pixels: {original_features}")
36
+ print(f"Remaining pixels: {remaining_features}")
37
+ print(f"Removed pixels: {removed_features}")
38
+ print(f"Reduction: {removed_features/original_features * 100:.2f}%")
GNB/__pycache__/gaussian_naive_bayes.cpython-312.pyc ADDED
Binary file (1.99 kB). View file
 
GNB/__pycache__/gaussian_naive_bayes.cpython-313.pyc ADDED
Binary file (2.07 kB). View file
 
gaussian_naive_bayes.py → GNB/gaussian_naive_bayes.py RENAMED
File without changes
GNB/naive_bayes_binary_full_preprocessing.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import matplotlib.pyplot as plt
3
+ import seaborn as sns
4
+ import keras
5
+ from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
6
+ from DT.DecisionTree import DecisionTree
7
+
8
+ # ======================
9
+ # 1. LOAD DATA
10
+ # ======================
11
+ (X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()
12
+
13
+ # Flatten
14
+ X_train = X_train.reshape(X_train.shape[0], -1)
15
+ X_test = X_test.reshape(X_test.shape[0], -1)
16
+
17
+ # Normalize
18
+ X_train = X_train / 255.0
19
+ X_test = X_test / 255.0
20
+
21
+ # ======================
22
+ # 2. BINARY LABELS (6 vs NOT 6)
23
+ # ======================
24
+ y_train_bin = (y_train == 6).astype(int)
25
+ y_test_bin = (y_test == 6).astype(int)
26
+
27
+ # ======================
28
+ # 3. VARIANCE FILTER
29
+ # ======================
30
+ def compute_pixel_variance(X):
31
+ mean = np.mean(X, axis=0)
32
+ return np.mean((X - mean) ** 2, axis=0)
33
+
34
+ def variance_threshold(X, threshold=1e-4):
35
+ variances = compute_pixel_variance(X)
36
+ mask = variances > threshold
37
+ return X[:, mask], mask
38
+
39
+ X_train_clean, mask = variance_threshold(X_train)
40
+ X_test_clean = X_test[:, mask]
41
+
42
+ print("After variance filtering:", X_train_clean.shape)
43
+
44
+ # ======================
45
+ # 4. BALANCE DATA (UNDERSAMPLING)
46
+ # ======================
47
+ def balance_data(X, y):
48
+ idx_6 = np.where(y == 1)[0]
49
+ idx_not6 = np.where(y == 0)[0]
50
+
51
+ np.random.shuffle(idx_not6)
52
+ idx_not6 = idx_not6[:len(idx_6)]
53
+
54
+ idx_balanced = np.concatenate([idx_6, idx_not6])
55
+ np.random.shuffle(idx_balanced)
56
+
57
+ return X[idx_balanced], y[idx_balanced]
58
+
59
+ X_train_bal, y_train_bal = balance_data(X_train_clean, y_train_bin)
60
+
61
+ print("Balanced shape:", X_train_bal.shape)
62
+ print("Class counts:", np.bincount(y_train_bal))
63
+
64
+ # ======================
65
+ # 5. PCA (FULL)
66
+ # ======================
67
+ def pca_full(X):
68
+ mean = np.mean(X, axis=0)
69
+ X_centered = X - mean
70
+
71
+ cov_matrix = np.cov(X_centered, rowvar=False)
72
+
73
+ eigenvalues, eigenvectors = np.linalg.eigh(cov_matrix)
74
+
75
+ idx = np.argsort(eigenvalues)[::-1]
76
+ eigenvectors = eigenvectors[:, idx]
77
+ eigenvalues = eigenvalues[idx]
78
+
79
+ return eigenvalues, eigenvectors, mean
80
+
81
+ def choose_k(eigenvalues, threshold=0.95):
82
+ explained_variance_ratio = eigenvalues / np.sum(eigenvalues)
83
+ cumulative = np.cumsum(explained_variance_ratio)
84
+
85
+ k = np.argmax(cumulative >= threshold) + 1
86
+ return k, cumulative
87
+
88
+ # IMPORTANT: PCA ON BALANCED TRAINING DATA
89
+ eigenvalues, eigenvectors, mean = pca_full(X_train_bal)
90
+ k, cumulative = choose_k(eigenvalues, threshold=0.95)
91
+
92
+ print(f"\nOptimal number of components: {k}")
93
+
94
+ # ======================
95
+ # 6. PROJECT DATA
96
+ # ======================
97
+ components = eigenvectors[:, :k]
98
+
99
+ X_train_pca = (X_train_bal - mean) @ components
100
+ X_test_pca = (X_test_clean - mean) @ components
101
+
102
+ print("Shape after PCA (train):", X_train_pca.shape)
103
+ print("Shape after PCA (test):", X_test_pca.shape)
104
+
105
+ # ======================
106
+ # 7. DECISION TREE
107
+ # ======================
108
+ dt = DecisionTree(maxDepth=8, minSamplesSplit=10)
109
+ X_train_small = X_train_bal[:5000]
110
+ y_train_small = y_train_bal[:5000]
111
+ dt.fit(X_train_small, y_train_small)
112
+
113
+ # Predict
114
+ predictions = dt.predict(X_test_pca)
115
+
116
+ # ======================
117
+ # 8. EVALUATION
118
+ # ======================
119
+ accuracy = accuracy_score(y_test_bin, predictions)
120
+ print("\nAccuracy:", accuracy)
121
+
122
+ cm = confusion_matrix(y_test_bin, predictions)
123
+ print("\nConfusion Matrix:\n", cm)
124
+
125
+ plt.figure(figsize=(6, 5))
126
+ sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
127
+ plt.xlabel("Predicted")
128
+ plt.ylabel("Actual")
129
+ plt.title("Confusion Matrix (1 vs Not 1)")
130
+ plt.show()
131
+
132
+ print("\nClassification Report:\n")
133
+ print(classification_report(
134
+ y_test_bin, predictions,
135
+ target_names=["Not 1", "Is 1"]
136
+ ))
137
+
138
+ # ======================
139
+ # 9. PCA SPACE VISUALIZATION
140
+ # ======================
141
+ plt.figure(figsize=(8, 6))
142
+ plt.scatter(X_train_pca[:, 0], X_train_pca[:, 1],
143
+ c=y_train_bal, cmap='coolwarm', s=5)
144
+ plt.xlabel("PC1")
145
+ plt.ylabel("PC2")
146
+ plt.title("PCA Space (1 vs Not 1)")
147
+ plt.colorbar()
148
+ plt.show()
149
+
150
+ # ======================
151
+ # 10. EXPLAINED VARIANCE
152
+ # ======================
153
+ plt.plot(cumulative)
154
+ plt.axhline(y=0.95, color='r', linestyle='--')
155
+ plt.xlabel("Number of Components")
156
+ plt.ylabel("Cumulative Variance")
157
+ plt.title("Explained Variance Curve")
158
+ plt.show()
159
+
160
+ # Optional: train accuracy on balanced train set
161
+ train_preds = dt.predict(X_train_pca)
162
+ print("Train Accuracy:", accuracy_score(y_train_bal, train_preds))
163
+
164
+ # Count classes in test set
165
+ num_6 = np.sum(y_test_bin == 1)
166
+ num_not6 = np.sum(y_test_bin == 0)
167
+
168
+ print("\nTest set distribution:")
169
+ print("Is 6:", num_6)
170
+ print("Not 6:", num_not6)
171
+
172
+ ratio = num_not6 / num_6
173
+ print("\nRatio (Not 6 : 6) =", ratio)
174
+ print("Ratio (6 : Not 6) = 1 :", ratio)
GNB/pca+VT+NAIVE_MODEL_mutli.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import matplotlib.pyplot as plt
3
+ import seaborn as sns
4
+ import keras
5
+ from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
6
+
7
+ # ======================
8
+ # 1. LOAD DATA
9
+ # ======================
10
+ (X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()
11
+
12
+ # Flatten
13
+ X_train = X_train.reshape(X_train.shape[0], -1)
14
+ X_test = X_test.reshape(X_test.shape[0], -1)
15
+
16
+ # Normalize
17
+ X_train = X_train / 255.0
18
+ X_test = X_test / 255.0
19
+
20
+
21
+ # ======================
22
+ # 2. VARIANCE FILTER
23
+ # ======================
24
+ def compute_pixel_variance(X):
25
+ mean = np.mean(X, axis=0)
26
+ return np.mean((X - mean) ** 2, axis=0)
27
+
28
+ def variance_threshold(X, threshold=1e-4):
29
+ variances = compute_pixel_variance(X)
30
+ mask = variances > threshold
31
+ return X[:, mask], mask
32
+
33
+ X_train_clean, mask = variance_threshold(X_train)
34
+ X_test_clean = X_test[:, mask]
35
+
36
+ print("After variance filtering:", X_train_clean.shape)
37
+
38
+
39
+ # ======================
40
+ # 3. PCA (FULL)
41
+ # ======================
42
+ def pca_full(X):
43
+ mean = np.mean(X, axis=0)
44
+ X_centered = X - mean
45
+
46
+ cov_matrix = np.cov(X_centered, rowvar=False)
47
+
48
+ eigenvalues, eigenvectors = np.linalg.eigh(cov_matrix)
49
+
50
+ idx = np.argsort(eigenvalues)[::-1]
51
+ eigenvectors = eigenvectors[:, idx]
52
+ eigenvalues = eigenvalues[idx]
53
+
54
+ return eigenvalues, eigenvectors, mean
55
+
56
+
57
+ def choose_k(eigenvalues, threshold=0.95):
58
+ explained_variance_ratio = eigenvalues / np.sum(eigenvalues)
59
+ cumulative = np.cumsum(explained_variance_ratio)
60
+
61
+ k = np.argmax(cumulative >= threshold) + 1
62
+ return k, cumulative
63
+
64
+
65
+ # Run PCA
66
+ eigenvalues, eigenvectors, mean = pca_full(X_train_clean)
67
+ k, cumulative = choose_k(eigenvalues, threshold=0.95)
68
+
69
+ print(f"\nOptimal number of components: {k}")
70
+
71
+
72
+ # ======================
73
+ # 4. PROJECT DATA
74
+ # ======================
75
+ components = eigenvectors[:, :k]
76
+
77
+ X_train_pca = (X_train_clean - mean) @ components
78
+ X_test_pca = (X_test_clean - mean) @ components
79
+
80
+ print("Shape after PCA:", X_train_pca.shape)
81
+
82
+
83
+ # ======================
84
+ # 5. GAUSSIAN NAIVE BAYES
85
+ # ======================
86
+ def gaussian_naive_train(X, y):
87
+ classes = np.unique(y)
88
+ model = {}
89
+ priors = {}
90
+
91
+ for c in classes:
92
+ X_c = X[y == c]
93
+
94
+ priors[c] = len(X_c) / len(X)
95
+
96
+ mean = np.mean(X_c, axis=0)
97
+ var = np.var(X_c, axis=0) + 1e-9
98
+
99
+ model[c] = (mean, var)
100
+
101
+ return model, priors
102
+
103
+
104
+ def predict(X, model, priors):
105
+ predictions = []
106
+
107
+ for x in X:
108
+ best_class = None
109
+ best_score = -float("inf")
110
+
111
+ for c in model:
112
+ mean, var = model[c]
113
+
114
+ log_prob = -((x - mean) ** 2) / (2 * var) - 0.5 * np.log(2 * np.pi * var)
115
+ score = np.log(priors[c]) + np.sum(log_prob)
116
+
117
+ if score > best_score:
118
+ best_score = score
119
+ best_class = c
120
+
121
+ predictions.append(best_class)
122
+
123
+ return np.array(predictions)
124
+
125
+
126
+ # Train on PCA data
127
+ model, priors = gaussian_naive_train(X_train_pca, y_train)
128
+
129
+ # Predict
130
+ predictions = predict(X_test_pca, model, priors)
131
+
132
+
133
+ # ======================
134
+ # 6. EVALUATION
135
+ # ======================
136
+ accuracy = accuracy_score(y_test, predictions)
137
+ print("\nAccuracy:", accuracy)
138
+
139
+
140
+ # Confusion Matrix
141
+ cm = confusion_matrix(y_test, predictions)
142
+ print("\nConfusion Matrix:\n", cm)
143
+
144
+ plt.figure(figsize=(8,6))
145
+ sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
146
+ plt.xlabel("Predicted")
147
+ plt.ylabel("Actual")
148
+ plt.title("Confusion Matrix")
149
+ plt.show()
150
+
151
+
152
+ # Classification Report
153
+ print("\nClassification Report:\n")
154
+ print(classification_report(y_test, predictions))
155
+
156
+
157
+ # ======================
158
+ # 7. VISUALIZE PCA SPACE
159
+ # ======================
160
+ plt.figure(figsize=(8,6))
161
+ plt.scatter(X_train_pca[:, 0], X_train_pca[:, 1],
162
+ c=y_train, cmap='tab10', s=5)
163
+
164
+ plt.xlabel("PC1")
165
+ plt.ylabel("PC2")
166
+ plt.title("MNIST in PCA Space")
167
+ plt.colorbar()
168
+ plt.show()
169
+
170
+
171
+ # ======================
172
+ # 8. EXPLAINED VARIANCE
173
+ # ======================
174
+ plt.plot(cumulative)
175
+ plt.axhline(y=0.95, color='r', linestyle='--')
176
+ plt.xlabel("Number of Components")
177
+ plt.ylabel("Cumulative Variance")
178
+ plt.title("Explained Variance Curve")
179
+ plt.show()
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import numpy as np
3
  from skimage.feature import hog
4
- from gaussian_naive_bayes import GNB
5
 
6
 
7
 
 
1
  import gradio as gr
2
  import numpy as np
3
  from skimage.feature import hog
4
+ from GNB.gaussian_naive_bayes import GNB
5
 
6
 
7