-
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
4 changed files
with
82 additions
and
20 deletions.
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
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,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 |
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
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