Skip to content

Commit

Permalink
Fix: RMSE fitness is displayed as negative for consistency in the fit…
Browse files Browse the repository at this point in the history
…ness function; If the SimpleThresholdClassifier has <0.50 accuracy in training, it will invert the prediction labels
  • Loading branch information
João Batista committed Nov 30, 2021
1 parent 14445a7 commit 631f0bb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
11 changes: 9 additions & 2 deletions stgp/Individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ def fit(self, Tr_x, Tr_y):

self.model.fit(hyper_X,Tr_y)

# Binary classification with an extra slot for missing values
if len(list(set(Tr_y))) <= 3:
# If the accuracy is < 0.5, invert the labels
if self.getAccuracy(Tr_x, Tr_y, pred="Tr") < 0.5:
self.trainingClassPredictions = None
self.model.invertPredictions()


def getSize(self):
'''
Expand Down Expand Up @@ -139,7 +146,7 @@ def getFitness(self):
if self.fitnessType == "RMSE":
self.getTrainingValuePredictions()
waf = mean_squared_error(self.trainingValuePredictions, self.convertLabelsToInt(self.training_Y))**0.5
self.fitness = waf
self.fitness = -waf

return self.fitness

Expand Down Expand Up @@ -194,7 +201,7 @@ def getRMSE(self, X, Y,pred=None):
elif pred == "Te":
pred = self.getTestValuePredictions(X)
else:
pred = self.predict(X)
pred = self.predict(X, classOutput = False)

return mean_squared_error(pred, Y)**0.5

Expand Down
4 changes: 2 additions & 2 deletions stgp/Population.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ def nextGeneration(self):
model = pool.map(fitIndividuals, [(ind, self.Tr_x, self.Tr_y) for ind in self.population] )
for i in range(len(self.population)):
self.population[i].model = model[i][0].model
self.population[i].fitness = model[i][1]
self.population[i].labelToInt = model[i][0].labelToInt
self.population[i].intToLabel = model[i][0].intToLabel
self.population[i].trainingPredictions = model[i][1]
self.population[i].training_X = self.Tr_x
self.population[i].training_Y = self.Tr_y
else:
Expand Down Expand Up @@ -201,7 +201,7 @@ def fitIndividuals(a):
ind,x,y = a
ind.fit(x,y)

return ( ind, ind.predict(x) )
return ( ind, ind.getFitness() )

def getTrainingPredictions(ind):
return ind.getTrainingPredictions()
11 changes: 9 additions & 2 deletions stgp/SimpleThresholdClassifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class SimpleThresholdClassifier:

threshold = None
isPositive1 = True

def __init__(self, threshold = 0):
self.threshold = threshold
Expand All @@ -24,5 +25,11 @@ def predict(self, X):
Receives X, a 1-D array of real values
Return a list of predictions based on the value
"""
predictions = [ 1 if value > self.threshold else 0 for value in X]
return predictions
if self.isPositive1:
predictions = [ 1 if value > self.threshold else 0 for value in X]
else:
predictions = [ 1 if value < self.threshold else 0 for value in X]
return predictions

def invertPredictions(self):
self.isPositive1 = not self.isPositive1

0 comments on commit 631f0bb

Please sign in to comment.