Skip to content

Commit

Permalink
learn
Browse files Browse the repository at this point in the history
  • Loading branch information
Freakwill committed Dec 15, 2023
1 parent 0c9df50 commit 7423642
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 20 deletions.
30 changes: 25 additions & 5 deletions pyrimidine/learn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,50 @@

from sklearn.base import BaseEstimator as BE

from ..chromosome import BinaryChromosome

import warnings, os
warnings.filterwarnings("ignore")
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'


class BaseEstimator(BE):

"""Base class for machine learning by GA
Attributes:
estimated_params (tuple): estimtated/learnable parameters by GA
pop (Population): the population for GA
"""

pop = None
estimated_params = ()

@classmethod
def config(cls, X, Y, *args, **kwargs):
def config(cls, X, Y=None, *args, **kwargs):
# configure a population for GA
raise NotImplementedError

def fit(self, X, Y, pop=None, warm_start=False):
def fit(self, X, Y=None, pop=None, warm_start=False):
if warm_start:
self.pop = pop or self.pop or self.config(X, Y)
else:
self.pop = pop or self.config(X, Y)
self._fit(X, Y)
self._fit()
return self

def _fit(self, X, Y):
def _fit(self):
self.pop.ezolve(n_iter=self.max_iter)
model_ = self.pop.solution
for k in self.estimated_params:
setattr(self, k, getattr(model_, k))
setattr(self, k, getattr(model_, k))


# def ga_opt(model_class):
# class cls(BaseEstimator, model_class):
# @classmethod
# def create_model(cls, *args, **kwargs):
# return model_class(*args, **kwargs)

# return cls

43 changes: 43 additions & 0 deletions pyrimidine/learn/cluster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python3

"""GA for linear regression
"""

import numpy as np
from sklearn.cluster import KMeans
from ..learn import BaseEstimator

from .population import StandardPopulation


class GALinearRegression(BaseEstimator, KMeans):
'''Linear Regression by GA
'''

estimated_params = ('cluster_centers_',)

@classmethod
def create_model(cls, *args, **kwargs):
return KMeans(*args, **kwargs)

@classmethod
def config(cls, X, Y=None, n_clusters=2, n_individuals=10, *args, **kwargs):

n_features = X.shape[1]

class MyIndividual(FloatChromosome // (n_clusters, n_features)):

def decode(self):
model = cls.create_model(*args, **kwargs)
model.cluster_centers_ = np.asarray(self)
return model

def _fitness(self):
model = self.decode()
return model.score(X)

MyPopulation = StandardPopulation[MyIndividual]

pop = MyPopulation.random(n_individuals=n_individuals)

return pop
15 changes: 8 additions & 7 deletions pyrimidine/learn/linear_regression.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#!/usr/bin/env python3

"""GA for linear regression
"""

from pyrimidine import StandardPopulation
import numpy as np
import numpy.linalg as LA
from sklearn.linear_model import LinearRegression
from ..learn import BaseEstimator

from .population import StandardPopulation
from digit_converter import IntervalConverter

c = IntervalConverter(lb=-60, ub=60)
Expand All @@ -10,12 +17,6 @@ def decode(self):
return c(self)


import numpy as np
import numpy.linalg as LA
from sklearn.linear_model import LinearRegression
from ..learn import BaseEstimator


class GALinearRegression(BaseEstimator, LinearRegression):
'''Linear Regression by GA
'''
Expand Down
14 changes: 6 additions & 8 deletions pyrimidine/learn/neural_network.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3

"""GA for optimization of neural networks
"""

import numpy as np
import numpy.linalg as LA
from scipy.special import softmax
from scipy.stats import entropy

from sklearn.neural_network import MLPRegressor

Expand All @@ -12,12 +12,10 @@
from ..learn import BaseEstimator


class GAANN(BaseEstimator, MLPRegressor):
"""GA for ANN
class GAMLPRegression(BaseEstimator, MLPRegressor):
"""GA for MLP Regression
"""

pop = None

hidden_dim = 4
max_iter = 100
n_layers = 3
Expand All @@ -33,7 +31,7 @@ def create_model(cls, *args, **kwargs):
return model

def __init__(self, *args, **kwargs):
# create GAANN object
# create MLPRegressor
super().__init__(hidden_layer_sizes=(self.hidden_dim,), max_iter=1, *args, **kwargs)
self.out_activation_ = 'identity'
self.n_layers_ = 3
Expand Down

0 comments on commit 7423642

Please sign in to comment.