amirTamer commited on
Commit
3d97c90
·
1 Parent(s): 01c0965
DT/DecisionTree.py ADDED
@@ -0,0 +1,299 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import math
3
+
4
+
5
+ class DecisionTree:
6
+
7
+ class Node:
8
+ def __init__(self, feature=None, threshold=None, left=None, right=None, decision=None):
9
+ self.feature = feature
10
+ self.threshold = threshold
11
+ self.left = left
12
+ self.right = right
13
+ self.decision = decision
14
+
15
+
16
+ def __init__(self,
17
+ maxDepth=None,
18
+ minSamplesSplit=2,
19
+ criterion="gini",
20
+ maxFeatures=None,
21
+ minSampleLeafs=1,
22
+ classWeights=None,
23
+ randomState=None):
24
+
25
+ self.maxDepth = maxDepth
26
+ self.minSamplesSplit = minSamplesSplit
27
+ self.minSampleLeafs = minSampleLeafs
28
+ self.criterion = criterion
29
+ self.maxFeatures = maxFeatures
30
+ self.allowedCriteria = ["entropy", "gini"]
31
+ self.classWeights = classWeights
32
+
33
+ # NEW: deterministic randomness
34
+ self.randomState = randomState
35
+ self.rng = np.random.default_rng(randomState)
36
+
37
+ if criterion not in self.allowedCriteria:
38
+ raise ValueError("Criterion has to be either entropy or gini")
39
+
40
+ self.root = None
41
+
42
+
43
+ def majorityLabel(self, labels):
44
+ counts = np.bincount(labels)
45
+ return np.argmax(counts)
46
+
47
+
48
+ def getThreshold(self, featureValues, labels):
49
+
50
+ entropy = []
51
+ labelCount = len(labels)
52
+
53
+ sortedIndices = np.argsort(featureValues)
54
+ sortedFeatureValues = featureValues[sortedIndices]
55
+ sortedLabels = labels[sortedIndices]
56
+
57
+ leftLabelCounts = np.zeros(np.max(labels) + 1, dtype=int)
58
+ rightLabelCounts = np.bincount(sortedLabels,
59
+ minlength=np.max(labels) + 1)
60
+
61
+ thresholds = []
62
+
63
+ for i in range(len(sortedLabels) - 1):
64
+
65
+ leftLabelCounts[sortedLabels[i]] += 1
66
+ rightLabelCounts[sortedLabels[i]] -= 1
67
+
68
+ if np.sum(leftLabelCounts) < self.minSampleLeafs:
69
+ continue
70
+
71
+ if np.sum(rightLabelCounts) < self.minSampleLeafs:
72
+ continue
73
+
74
+ if sortedFeatureValues[i] == sortedFeatureValues[i + 1]:
75
+ continue
76
+
77
+ w_entropy = self.computeImpurity(
78
+ leftLabelCounts,
79
+ rightLabelCounts,
80
+ labelCount
81
+ )
82
+
83
+ threshold = (
84
+ sortedFeatureValues[i] +
85
+ sortedFeatureValues[i + 1]
86
+ ) / 2
87
+
88
+ thresholds.append(threshold)
89
+ entropy.append(w_entropy)
90
+
91
+ if len(entropy) == 0:
92
+ return None, None
93
+
94
+ minEntropyIndex = np.argmin(entropy)
95
+
96
+ return thresholds[minEntropyIndex], entropy[minEntropyIndex]
97
+
98
+
99
+ def computeImpurity(self,
100
+ leftLabelCounts,
101
+ rightLabelCounts,
102
+ labelCount):
103
+
104
+ leftEntropy = 0
105
+ rightEntropy = 0
106
+
107
+ if self.classWeights is not None:
108
+
109
+ weightsArr = np.array([
110
+ self.classWeights.get(i, 1)
111
+ for i in range(leftLabelCounts.size)
112
+ ])
113
+
114
+ else:
115
+
116
+ weightsArr = np.ones(leftLabelCounts.size)
117
+
118
+ weightedCountsL = weightsArr * leftLabelCounts
119
+ weightedCountsR = weightsArr * rightLabelCounts
120
+
121
+ wLeftLabelCount = np.sum(weightedCountsL)
122
+ wRightLabelCount = np.sum(weightedCountsR)
123
+
124
+ totalWeight = wLeftLabelCount + wRightLabelCount
125
+
126
+
127
+ for count in weightedCountsL:
128
+
129
+ if count == 0:
130
+ continue
131
+
132
+ prob = count / wLeftLabelCount
133
+
134
+ if self.criterion == "entropy":
135
+ leftEntropy += -prob * np.log2(prob)
136
+ else:
137
+ leftEntropy += prob ** 2
138
+
139
+
140
+ for count in weightedCountsR:
141
+
142
+ if count == 0:
143
+ continue
144
+
145
+ prob = count / wRightLabelCount
146
+
147
+ if self.criterion == "entropy":
148
+ rightEntropy += -prob * np.log2(prob)
149
+ else:
150
+ rightEntropy += prob ** 2
151
+
152
+
153
+ if self.criterion == "entropy":
154
+
155
+ w_entropy = (
156
+ (wLeftLabelCount / totalWeight) * leftEntropy +
157
+ (wRightLabelCount / totalWeight) * rightEntropy
158
+ )
159
+
160
+ else:
161
+
162
+ w_entropy = (
163
+ (wLeftLabelCount / totalWeight) * (1 - leftEntropy) +
164
+ (wRightLabelCount / totalWeight) * (1 - rightEntropy)
165
+ )
166
+
167
+ return w_entropy
168
+
169
+
170
+ def getFeature(self, trainingData, labels):
171
+
172
+ entropy = []
173
+ thresholds = []
174
+ featureIndices = []
175
+
176
+ noOfFeatures = trainingData.shape[1]
177
+
178
+
179
+ if self.maxFeatures is not None:
180
+
181
+ current_max = self.maxFeatures
182
+
183
+ if current_max == "sqrt":
184
+ current_max = int(math.sqrt(noOfFeatures))
185
+
186
+ elif current_max == "log2":
187
+ current_max = int(np.log2(noOfFeatures))
188
+
189
+
190
+ featureArr = self.rng.choice(
191
+ noOfFeatures,
192
+ size=current_max,
193
+ replace=False
194
+ )
195
+
196
+ else:
197
+
198
+ featureArr = np.arange(noOfFeatures)
199
+
200
+
201
+ for j in featureArr:
202
+
203
+ featureValues = trainingData[:, j]
204
+
205
+ optimalThreshold, minEntropy = self.getThreshold(
206
+ featureValues,
207
+ labels
208
+ )
209
+
210
+ if optimalThreshold is None:
211
+ continue
212
+
213
+ entropy.append(minEntropy)
214
+ thresholds.append(optimalThreshold)
215
+ featureIndices.append(j)
216
+
217
+
218
+ if len(entropy) == 0:
219
+ return None, None
220
+
221
+
222
+ minEntropyIndex = np.argmin(entropy)
223
+
224
+ return (
225
+ featureIndices[minEntropyIndex],
226
+ thresholds[minEntropyIndex]
227
+ )
228
+
229
+
230
+ def buildTree(self, data, labels, depth=0):
231
+
232
+ if len(np.unique(labels)) == 1:
233
+ return self.Node(decision=np.unique(labels)[0])
234
+
235
+ if self.maxDepth is not None and depth >= self.maxDepth:
236
+ return self.Node(decision=self.majorityLabel(labels))
237
+
238
+ if len(labels) < self.minSamplesSplit:
239
+ return self.Node(decision=self.majorityLabel(labels))
240
+
241
+
242
+ feature, threshold = self.getFeature(data, labels)
243
+
244
+ if feature is None:
245
+ return self.Node(decision=self.majorityLabel(labels))
246
+
247
+
248
+ leftMask = data[:, feature] <= threshold
249
+ rightMask = ~leftMask
250
+
251
+
252
+ leftChild = self.buildTree(
253
+ data[leftMask],
254
+ labels[leftMask],
255
+ depth + 1
256
+ )
257
+
258
+ rightChild = self.buildTree(
259
+ data[rightMask],
260
+ labels[rightMask],
261
+ depth + 1
262
+ )
263
+
264
+
265
+ return self.Node(
266
+ feature,
267
+ threshold,
268
+ leftChild,
269
+ rightChild
270
+ )
271
+
272
+
273
+ def fit(self, trainingData, labels):
274
+
275
+ self.root = self.buildTree(trainingData, labels)
276
+
277
+
278
+ def traverseTree(self, dataPoint, node):
279
+
280
+ if node.decision is not None:
281
+ return node.decision
282
+
283
+ if dataPoint[node.feature] <= node.threshold:
284
+ return self.traverseTree(dataPoint, node.left)
285
+
286
+ return self.traverseTree(dataPoint, node.right)
287
+
288
+
289
+ def predict(self, datapoints):
290
+
291
+ if self.root is None:
292
+ raise ValueError(
293
+ "Decision tree has not been trained. Call fit() first."
294
+ )
295
+
296
+ return np.array([
297
+ self.traverseTree(point, self.root)
298
+ for point in datapoints
299
+ ])
DT/__pycache__/DecisionTree.cpython-312.pyc ADDED
Binary file (8.98 kB). View file
 
DT/__pycache__/DecisionTree.cpython-313.pyc ADDED
Binary file (9.23 kB). View file
 
DT/ayKalambeyet2al.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
3
+ from sklearn.datasets import fetch_openml
4
+ from MNISTclassification.DT.DecisionTree import DecisionTree
5
+
6
+
7
+ def load_mnist_multiclass():
8
+ mnist = fetch_openml('mnist_784', version=1)
9
+
10
+ X = mnist.data.to_numpy(dtype=np.float32) / 255.0
11
+ y = mnist.target.to_numpy().astype(int) # labels 0 to 9
12
+
13
+ # You can keep 10k for training if your tree is slow
14
+ X_train = X[:10000]
15
+ X_test = X[10000:12000] # keep test smaller too, otherwise prediction may take long
16
+ y_train = y[:10000]
17
+ y_test = y[10000:12000]
18
+
19
+ return X_train, y_train, X_test, y_test
20
+
21
+
22
+ def train_model(X_train, y_train):
23
+ dt = DecisionTree(maxDepth=8, minSamplesSplit=50)
24
+ dt.fit(X_train, y_train)
25
+ return dt
26
+
27
+
28
+ def evaluate_model(model, X_test, y_test):
29
+ y_pred = model.predict(X_test)
30
+
31
+ print("Accuracy:", accuracy_score(y_test, y_pred))
32
+ print("\nConfusion Matrix:")
33
+ print(confusion_matrix(y_test, y_pred))
34
+
35
+ print("\nClassification Report:")
36
+ print(
37
+ classification_report(
38
+ y_test,
39
+ y_pred,
40
+ labels=np.arange(10),
41
+ target_names=[str(i) for i in range(10)]
42
+ )
43
+ )
44
+
45
+
46
+ def main():
47
+ X_train, y_train, X_test, y_test = load_mnist_multiclass()
48
+
49
+ print("Training data shape:", X_train.shape)
50
+ print("Test data shape:", X_test.shape)
51
+
52
+ unique, counts = np.unique(y_train, return_counts=True)
53
+ print("\nTraining class distribution:")
54
+ for cls, cnt in zip(unique, counts):
55
+ print(f"Digit {cls}: {cnt}")
56
+
57
+ model = train_model(X_train, y_train)
58
+ evaluate_model(model, X_test, y_test)
59
+
60
+
61
+ if __name__ == "__main__":
62
+ main()
GNB/naive_bayes_binary_full_preprocessing.py CHANGED
@@ -3,7 +3,7 @@ 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
 
3
  import seaborn as sns
4
  import keras
5
  from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
6
+ from MNISTclassification.DT.DecisionTree import DecisionTree
7
 
8
  # ======================
9
  # 1. LOAD DATA
app.py CHANGED
@@ -4,6 +4,7 @@ from skimage.feature import hog
4
  import cv2
5
  from GNB.gaussian_naive_bayes import GNB
6
  from LogisticRegresssion.LogisticRegression import LogReg
 
7
 
8
 
9
 
@@ -37,6 +38,10 @@ pca = package['pca']
37
  best_weight = package['best_weight']
38
  hog_params = package['hog_params']
39
 
 
 
 
 
40
  def preprocess_image(image , model):
41
 
42
  image = np.array(image)
@@ -104,10 +109,13 @@ def classify_with_logreg(image):
104
  prediction = logreg_trained_model.predict(processed_image)
105
 
106
  return "Target Detected: This is a 6!" if prediction[0] == 1 else "Not a 6."
 
107
  def classify_with_DT(image):
108
  if image is None:
109
  return "Please upload an image."
110
  processedImage = preprocess_image(image , "DT")
 
 
111
 
112
 
113
 
 
4
  import cv2
5
  from GNB.gaussian_naive_bayes import GNB
6
  from LogisticRegresssion.LogisticRegression import LogReg
7
+ from DT.DecisionTree import DecisionTree
8
 
9
 
10
 
 
38
  best_weight = package['best_weight']
39
  hog_params = package['hog_params']
40
 
41
+ with open("dt.pkl" , "rb") as f:
42
+ package = pickle.load(f)
43
+
44
+ dt = package['model']
45
  def preprocess_image(image , model):
46
 
47
  image = np.array(image)
 
109
  prediction = logreg_trained_model.predict(processed_image)
110
 
111
  return "Target Detected: This is a 6!" if prediction[0] == 1 else "Not a 6."
112
+
113
  def classify_with_DT(image):
114
  if image is None:
115
  return "Please upload an image."
116
  processedImage = preprocess_image(image , "DT")
117
+ result = dt.predict(processedImage)
118
+ return "Target Detected: This is a 6!" if result[0] == 1 else "Not a 6."
119
 
120
 
121