Skip to content

Commit

Permalink
Merge pull request #92 from rodrigo-arenas/0.8.1
Browse files Browse the repository at this point in the history
Version 0.81 Release
  • Loading branch information
rodrigo-arenas committed Mar 9, 2022
2 parents f21ff28 + 4fc9ce7 commit 667e396
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
11 changes: 11 additions & 0 deletions docs/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ Release Notes

Some notes on new features in various releases

What's new in 0.8.1
-------------------

^^^^^^^^^
Features:
^^^^^^^^^

* If the `max_features` parameter from :class:`~sklearn_genetic.GAFeatureSelectionCV` is set,
the initial population is now sampled giving more probability to solutions with less than `max_features` features.


What's new in 0.8.0
-------------------

Expand Down
2 changes: 1 addition & 1 deletion sklearn_genetic/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.8.0"
__version__ = "0.8.1"
14 changes: 12 additions & 2 deletions sklearn_genetic/genetic_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
create_gasearch_cv_results_,
create_feature_selection_cv_results_,
)
from .utils.random import weighted_choice


class GASearchCV(BaseSearchCV):
Expand Down Expand Up @@ -940,6 +941,7 @@ def __init__(
self.n_features = None
self.X_ = None
self.y_ = None
self.features_proportion = None
self.callbacks = None
self.best_features_ = None
self.best_estimator_ = None
Expand Down Expand Up @@ -999,7 +1001,11 @@ def _register(self):

# Register the array to choose the features
# Each binary value represents if the feature is selected or not
self.toolbox.register("features", random.randint, 0, 1)

if self.features_proportion:
self.toolbox.register("features", weighted_choice, self.features_proportion)
else:
self.toolbox.register("features", random.randint, 0, 1)

self.toolbox.register(
"individual",
Expand Down Expand Up @@ -1107,10 +1113,11 @@ def evaluate(self, individual):
self.logbook.record(parameters=current_generation_features)

# Penalize individuals with more features than the max_features parameter

if self.max_features and (
n_selected_features > self.max_features or n_selected_features == 0
):
score = -self.criteria_sign * 10000
score = -self.criteria_sign * 100000

return [score, n_selected_features]

Expand All @@ -1136,6 +1143,9 @@ def fit(self, X, y, callbacks=None):
self.X_, self.y_ = check_X_y(X, y)
self.n_features = X.shape[1]

if self.max_features:
self.features_proportion = self.max_features/self.n_features

# Make sure the callbacks are valid
self.callbacks = check_callback(callbacks)

Expand Down
20 changes: 20 additions & 0 deletions sklearn_genetic/utils/random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import random


def weighted_choice(weight):
"""
Parameters
----------
weight: float
Weight of choosing a chromosome
Returns
-------
Bool random (not uniform) choice
"""

# This help to don't generate individuals of the same size on average
p = random.uniform(0, weight)
choice = random.choices([0, 1], [1-p, p])[0]

return choice

0 comments on commit 667e396

Please sign in to comment.