3
3
import multiprocessing as mp
4
4
import time
5
5
6
+ from .MahalanobisDistanceClassifier import MahalanobisDistanceClassifier
7
+
6
8
from random import Random
7
9
8
10
#
9
11
# By using this file, you are agreeing to this product's EULA
10
12
#
11
13
# This product can be obtained in https://github.com/jespb/Python-M3GP
12
14
#
13
- # Copyright ©2019-2022 J. E. Batista
15
+ # Copyright ©2019-2025 J. E. Batista
14
16
#
15
17
16
18
class ClassifierNotTrainedError (Exception ):
@@ -38,7 +40,7 @@ class M3GP:
38
40
dim_min = None
39
41
dim_max = None
40
42
41
- model_name = None
43
+ model_class = None
42
44
fitnessType = None
43
45
44
46
verbose = None
@@ -73,7 +75,7 @@ def checkIfTrained(self):
73
75
74
76
def __init__ (self , operators = [("+" ,2 ),("-" ,2 ),("*" ,2 ),("/" ,2 )], max_initial_depth = 6 , population_size = 500 ,
75
77
max_generation = 100 , tournament_size = 5 , elitism_size = 1 , max_depth = 17 ,
76
- dim_min = 1 , dim_max = 9999 , threads = 1 , random_state = 42 , verbose = True , model_name = "MahalanobisDistanceClassifier" , fitnessType = "Accuracy" ):
78
+ dim_min = 1 , dim_max = 9999 , threads = 1 , random_state = 42 , verbose = True , model_class = None , fitnessType = "Accuracy" ):
77
79
78
80
if sum ( [0 if op in [("+" ,2 ),("-" ,2 ),("*" ,2 ),("/" ,2 )] else 0 for op in operators ] ) > 0 :
79
81
print ( "[Warning] Some of the following operators may not be supported:" , operators )
@@ -93,7 +95,9 @@ def __init__(self, operators=[("+",2),("-",2),("*",2),("/",2)], max_initial_dept
93
95
self .dim_min = max (1 , dim_min )
94
96
self .dim_max = max (1 , dim_max )
95
97
96
- self .model_name = model_name
98
+ self .model_class = model_class
99
+ if self .model_class is None :
100
+ self .model_class = MahalanobisDistanceClassifier ()
97
101
self .fitnessType = fitnessType
98
102
99
103
self .verbose = verbose
@@ -191,7 +195,7 @@ def fit(self,Tr_x, Tr_y, Te_x = None, Te_y = None):
191
195
print (" > Max Depth: " + str (self .max_depth ))
192
196
print (" > Minimum Dimensions: " + str (self .dim_min ))
193
197
print (" > Maximum Dimensions: " + str (self .dim_max ))
194
- print (" > Wrapped Model: " + self .model_name )
198
+ print (" > Wrapped Model: " + self .model_class . __class__ . __name__ )
195
199
print (" > Fitness Type: " + self .fitnessType )
196
200
print (" > Threads: " + str (self .threads ))
197
201
print ()
@@ -206,12 +210,13 @@ def fit(self,Tr_x, Tr_y, Te_x = None, Te_y = None):
206
210
self .population = []
207
211
208
212
while len (self .population ) < self .population_size :
209
- ind = Individual (self .operators , self .terminals , self .max_depth , self .model_name , self .fitnessType )
213
+ ind = Individual (self .operators , self .terminals , self .max_depth , self .model_class , self .fitnessType )
210
214
ind .create (self .rng , n_dims = self .dim_min )
211
215
self .population .append (ind )
212
216
213
217
self .bestIndividual = self .population [0 ]
214
218
self .bestIndividual .fit (self .Tr_x , self .Tr_y )
219
+ self .bestIndividual .getFitness ()
215
220
216
221
if not self .Te_x is None :
217
222
self .trainingAccuracyOverTime = []
0 commit comments