Skip to content

Instantly share code, notes, and snippets.

@3catz
Last active November 5, 2020 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3catz/4cc217462907b3b9bebecc3435667195 to your computer and use it in GitHub Desktop.
Save 3catz/4cc217462907b3b9bebecc3435667195 to your computer and use it in GitHub Desktop.
GA algorithms and Logistic Regression
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