-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python3 | ||
|
||
|
||
from pyrimidine import * | ||
from digit_converter import IntervalConverter | ||
|
||
c = IntervalConverter(lb=-60, ub=60) | ||
class _BinaryChromosome(BinaryChromosome): | ||
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 | ||
''' | ||
|
||
@classmethod | ||
def create_model(cls, *args, **kwargs): | ||
return LinearRegression(*args, **kwargs) | ||
|
||
def _fit(self, X, Y): | ||
self.pop.ezolve(n_iter=self.max_iter) | ||
model_ = self.pop.solution | ||
self.coef_ = model_.coef_ | ||
self.intercept_ = model_.intercept_ | ||
|
||
@classmethod | ||
def config(cls, X, Y, n_individuals=10, *args, **kwargs): | ||
|
||
input_dim = X.shape[1] | ||
# output_dim = Y.shape[1] | ||
|
||
class MyIndividual(MixedIndividual): | ||
params={'sigma':0.02} | ||
element_class = FloatChromosome, _BinaryChromosome | ||
|
||
def decode(self): | ||
model = cls.create_model(*args, **kwargs) | ||
model.coef_ = np.asarray(self[0]) | ||
model.intercept_ = self[1].decode() | ||
return model | ||
|
||
def _fitness(self): | ||
model = self.decode() | ||
return model.score(X, Y) | ||
|
||
class MyPopulation(HOFPopulation): | ||
element_class = MyIndividual | ||
|
||
pop = MyPopulation.random(n_individuals=n_individuals, size=(input_dim, 8)) | ||
|
||
return pop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[pytest] | ||
python_files = tests/test_*.py | ||
python_files = tests/test_*.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters