Last active
November 5, 2020 22:25
-
-
Save 3catz/4cc217462907b3b9bebecc3435667195 to your computer and use it in GitHub Desktop.
GA algorithms and Logistic Regression
This file contains hidden or 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
from sklearn.metrics import * | |
mcc = make_scorer(matthews_corrcoef) | |
estimator = LogisticRegression(solver = "liblinear", C = 6, tol = 1, fit_intercept = True) | |
from sklearn.model_selection import * | |
report = pd.DataFrame() | |
nofeats = [] | |
chosen_feats = [] | |
cvscore = [] | |
rkf = RepeatedStratifiedKFold(n_repeats = 2, n_splits = 10) | |
for i in range(2,11): | |
selector = GeneticSelectionCV(estimator, | |
cv = rkf, | |
verbose = 0, | |
scoring = mcc, | |
max_features = i, | |
n_population = 200, | |
crossover_proba = 0.5, | |
mutation_proba = 0.2, | |
n_generations = 10, | |
crossover_independent_proba=0.5, | |
mutation_independent_proba=0.05, | |
#tournament_size = 3, | |
n_gen_no_change=10, | |
caching=True, | |
n_jobs=-1) | |
selector = selector.fit(D[allfeats], y) | |
genfeats = D[allfeats].columns[selector.support_] | |
genfeats = list(genfeats) | |
print("Chosen Feats: ", genfeats) | |
cv_score = selector.generation_scores_[-1] | |
nofeats.append(len(genfeats)) | |
chosen_feats.append(genfeats) | |
cvscore.append(cv_score) | |
report["No of Feats"] = nofeats | |
report["Chosen Feats"] = chosen_feats | |
report["Scores"] = cvscore | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment