<jupyter_start><jupyter_text>Genetic Algorithm (GA) for Machine Learning (ML)URL https://datascienceplus.com/genetic-algorithm-in-machine-learning-using-python/ GA limitations- Not suitable for simple problems with available derivative information- Frequent calculation of fitness value is computationally expensive for some problems- Stochastic, i.e., no guarantee of the result solution being optimal- No guarantee of convergence to the optimal solution if not implemented properly<jupyter_code>import numpy as np
import pandas as pd
import random
import matplotlib.pyplot
%matplotlib inline

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score<jupyter_output><empty_output><jupyter_text>Data<jupyter_code>#import the breast cancer dataset 
from sklearn.datasets import load_breast_cancer
cancer=load_breast_cancer()
df = pd.DataFrame(cancer['data'], columns=cancer['feature_names'])
label=cancer["target"]

#splitting the model into training and testing set
X_train, X_test, y_train, y_test = train_test_split(df, 
                                                    label, 
                                                    test_size=0.30, 
                                                    random_state=101)<jupyter_output><empty_output><jupyter_text>ML modeling with Logistic Regression<jupyter_code>warning = False

if warning:
    # without warning, better convergence of Logistic Regression, but worse GA effect (below)
    logmodel = LogisticRegression(max_iter=5000)
else:
    # a lot of warning, worse convergence for Logistic regression, but this example shows better effect o GA
    import warnings
    warnings.filterwarnings('ignore')
    logmodel = LogisticRegression(max_iter=100)

# fit model
logmodel.fit(X_train,y_train)

# prediction
predictions = logmodel.predict(X_test)
print("Accuracy = "+ str(accuracy_score(y_test,predictions)))<jupyter_output><empty_output><jupyter_text>Genetic Algorithm (GA)<jupyter_code>#defining various steps required for the genetic algorithm
def initilization_of_population(size, n_feat):
    population = []
    for i in range(size):
        chromosome = np.ones(n_feat, dtype=np.bool)
        chromosome[:int(0.3*n_feat)]=False
        np.random.shuffle(chromosome)
        population.append(chromosome)
    return population


# use logistic regression as the fitness score function
def fitness_score(population):
    scores = []
    for chromosome in population:
        logmodel.fit(X_train.iloc[:,chromosome],y_train)
        predictions = logmodel.predict(X_test.iloc[:,chromosome])
        scores.append(accuracy_score(y_test,predictions))
    scores, population = np.array(scores), np.array(population) 
    inds = np.argsort(scores)
    return list(scores[inds][::-1]), list(population[inds,:][::-1])


def selection(pop_after_fit, n_parents):
    population_nextgen = []
    for i in range(n_parents):
        population_nextgen.append(pop_after_fit[i])
    return population_nextgen


def crossover(pop_after_sel):
    population_nextgen=pop_after_sel
    for i in range(len(pop_after_sel)):
        child=pop_after_sel[i]
        child[3:7]=pop_after_sel[(i+1)%len(pop_after_sel)][3:7]
        population_nextgen.append(child)
    return population_nextgen


def mutation(pop_after_cross,mutation_rate):
    population_nextgen = []
    for i in range(0,len(pop_after_cross)):
        chromosome = pop_after_cross[i]
        for j in range(len(chromosome)):
            if random.random() < mutation_rate:
                chromosome[j]= not chromosome[j]
        population_nextgen.append(chromosome)
    #print(population_nextgen)
    return population_nextgen


def generations(size,
                n_feat,
                n_parents,
                mutation_rate,
                n_gen,
                X_train,
                X_test, 
                y_train, 
                y_test):
    best_chromo= []
    best_score= []
    population_nextgen=initilization_of_population(size, n_feat)
    for i in range(n_gen):
        scores, pop_after_fit = fitness_score(population_nextgen)
        print(i, scores[:2])
        
        pop_after_sel = selection(pop_after_fit, n_parents)
        pop_after_cross = crossover(pop_after_sel)
        population_nextgen = mutation(pop_after_cross, mutation_rate)
        
        best_chromo.append(pop_after_fit[0])
        best_score.append(scores[0])  
    return best_chromo, best_score<jupyter_output><empty_output><jupyter_text>Optimization ML by GA**Note: long running**<jupyter_code>chromo, score=generations(size=200,
                          n_feat=30,
                          n_parents=100,
                          mutation_rate=0.10,
                          n_gen=38,
                          X_train=X_train,
                          X_test=X_test,
                          y_train=y_train,
                          y_test=y_test)
logmodel.fit(X_train.iloc[:,chromo[-1]], y_train)
predictions = logmodel.predict(X_test.iloc[:,chromo[-1]])
print("Accuracy score after GA is= " + str(accuracy_score(y_test,predictions)))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0869980879541109, 'fraction_numerical': 0.007839388145315488, 'mean_word_length': 3.1515873015873015, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '200206', 'license': 'Apache-2.0', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1532, 'n_words': 321, 'path': 'cvicenia/notebooks/tyzden-11/IAU_genetic_algorithm.ipynb', 'repo_name': 'xmajzel/2020-2021', 'size': '5230'}