amirTamer commited on
Commit
0fa5aba
·
1 Parent(s): 17a9019

Added RF folder

Browse files
RF/__pycache__/grappaBroskiSkibiddiLewandoski.cpython-312.pyc ADDED
Binary file (3.14 kB). View file
 
RF/__pycache__/grappaBroskiSkibiddiLewandoski.cpython-313.pyc ADDED
Binary file (3.2 kB). View file
 
RF/__pycache__/test.cpython-312.pyc ADDED
Binary file (2.47 kB). View file
 
RF/__pycache__/test.cpython-313.pyc ADDED
Binary file (2.59 kB). View file
 
RF/grappaBroskiSkibiddiLewandoski.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from DT.DecisionTree import DecisionTree
3
+
4
+ class HobaTitoMambo:
5
+
6
+ def __init__(self, n_trees=10, max_depth=10, max_features="sqrt", min_samples_split=2, min_sample_leafs=1, criterion="gini",random_state=None):
7
+
8
+ self.n_trees = n_trees
9
+ self.max_depth = max_depth
10
+ self.max_features = max_features
11
+ self.min_samples_split = min_samples_split
12
+ self.min_sample_leafs = min_sample_leafs
13
+ self.criterion = criterion
14
+ self.random_state = random_state
15
+ self.trees = []
16
+ self.rng = np.random.default_rng(self.random_state)
17
+
18
+ def __bootstrap_samples(self, x, y):
19
+ n_samples = x.shape[0]
20
+ idxs = self.rng.choice(n_samples, size=n_samples, replace=True)
21
+ return x[idxs], y[idxs]
22
+
23
+ def fit(self, x, y):
24
+ self.trees = []
25
+
26
+ for i in range(self.n_trees):
27
+ tree_seed = self.random_state + i if self.random_state is not None else None
28
+
29
+ dt = DecisionTree(maxDepth=self.max_depth,minSamplesSplit=self.min_samples_split,criterion=self.criterion,maxFeatures=self.max_features,minSampleLeafs=self.min_sample_leafs,randomState=tree_seed)
30
+
31
+ x_s, y_s = self.__bootstrap_samples(x, y)
32
+ dt.fit(x_s, y_s)
33
+ self.trees.append(dt)
34
+
35
+ def predict(self, x_test):
36
+ preds = np.array([tree.predict(x_test) for tree in self.trees])
37
+ preds = preds.T
38
+
39
+ res = np.zeros(len(x_test), dtype=int)
40
+
41
+ for i, sample_votes in enumerate(preds):
42
+ vals, counts = np.unique(sample_votes, return_counts=True)
43
+ res[i] = vals[np.argmax(counts)]
44
+
45
+ return res
RF/test.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pickle
3
+ from tensorflow import keras
4
+ # from sklearn.metrics import accuracy_score, f1_score, classification_report, confusion_matrix
5
+ from RF.grappaBroskiSkibiddiLewandoski import HobaTitoMambo
6
+ from Metrics import classification_report2, confusion_matrix2, accuracy_score2
7
+
8
+ #Download Data
9
+ (X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()
10
+
11
+ X_train = X_train / 255.0
12
+ X_test = X_test / 255.0
13
+
14
+
15
+ from skimage.feature import hog
16
+
17
+ def extract_hog_features(X):
18
+ features = []
19
+
20
+ for img in X:
21
+ img_2d = img.reshape(28, 28)
22
+
23
+ hog_features = hog(
24
+ img_2d,
25
+ orientations=9,
26
+ pixels_per_cell=(4, 4),
27
+ cells_per_block=(2, 2),
28
+ block_norm='L2-Hys',
29
+ feature_vector=True
30
+ )
31
+
32
+ features.append(hog_features)
33
+
34
+ return np.array(features)
35
+
36
+
37
+ X_train_HOG = extract_hog_features(X_train)
38
+ X_test_HOG = extract_hog_features(X_test)
39
+
40
+ print("Initializing HobaTitoMambo Random Forest...")
41
+ rf = HobaTitoMambo(
42
+ n_trees=10,
43
+ max_depth=15,
44
+ min_samples_split=20,
45
+ min_sample_leafs=5,
46
+ max_features="sqrt",
47
+ criterion="gini",
48
+ random_state=42
49
+ )
50
+
51
+ print("Training model (this may take a few minutes)...")
52
+ rf.fit(X_train_HOG, y_train)
53
+
54
+ print("Predicting on test set...")
55
+ predictions = rf.predict(X_test_HOG)
56
+
57
+ conf_matrix = confusion_matrix2(y_test, predictions)
58
+
59
+ print("\n--- Evaluation Results ---")
60
+ print("\nConfusion Matrix:")
61
+ print(conf_matrix)
62
+
63
+ print("\nDetailed Classification Report:")
64
+ print(classification_report2(y_test, predictions))
65
+
66
+ model_filename = 'rf_hog_model.pkl'
67
+ print(f"\nSaving model to {model_filename}...")
68
+ with open(model_filename, 'wb') as file:
69
+ pickle.dump(rf, file)
70
+ print("Model saved successfully!")