# Define the outer and inner loop parameters
outer_cv = KFold(n_splits=5, shuffle=True)
inner_cv = KFold(n_splits=5, shuffle=True)
population_size = 100
generations = 40
crossover_proba = 0.5
mutation_proba = 0.2
scoring = 'accuracy'
# Run the nested genetic algorithm
selector = GeneticSelectionCV(estimator=RandomForestClassifier(n_estimators=100, random_state=0),
cv=outer_cv,
verbose=1,
scoring=scoring,
n_population=population_size,
crossover_proba=crossover_proba,
mutation_proba=mutation_proba,
n_generations=generations,
fitness=fitness)
selector.fit(X_scaled, y)
# Get the selected features and fit the model on the whole dataset
X_selected = X_scaled[:, selector.support_]
clf = RandomForestClassifier(n_estimators=100, random_state=0)
clf.fit(X_selected, y)
This code uses the pandas library to load and preprocess the dataset, and the StandardScaler class from sklearn.preprocessing to scale the features. The genetic algorithm is run using the GeneticSelectionCV class from the genetic_selection package, and the fitness function is modified to use the dataset and the IHM-27k platform. The selected features are used to train a Random Forest classifier on the entire dataset.
import numpy as np
from sklearn.svm import SVC
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import KFold
from genetic_selection import GeneticSelectionCV
# Define the inner loop parameters
inner_population_size = 50
inner_generations = 20
inner_crossover_proba = 0.5
inner_mutation_proba = 0.2
# Define the outer loop parameters
outer_population_size = 100
outer_generations = 40
outer_crossover_proba = 0.5
outer_mutation_proba = 0.2
# Define the fitness function for the inner GA
def inner_fitness(X, y, individual):
if np.count_nonzero(individual) == 0:
return 0.0
X_filtered = X[:, individual==1]
clf = MLPClassifier(hidden_layer_sizes=(10,), max_iter=1000, random_state=0)
score = 0.0
for train_index, test_index in inner_cv.split(X_filtered, y):
X_train, X_test = X_filtered[train_index], X_filtered[test_index]
y_train, y_test = y[train_index], y[test_index]
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
score += accuracy_score(y_test, y_pred)
return score / inner_cv.get_n_splits()
# Define the fitness function for the outer GA
def outer_fitness(X, y, individual):
if np.count_nonzero(individual) == 0:
return 0.0
X_filtered = X[:, individual==1]
clf = SVC(kernel='linear')
score = 0.0
for train_index, test_index in outer_cv.split(X_filtered, y):
X_train, X_test = X_filtered[train_index], X_filtered[test_index]
y_train, y_test = y[train_index], y[test_index]
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
score += accuracy_score(y_test, y_pred)
return score / outer_cv.get_n_splits()
# Load the dataset
X = ...
y = ...
# Define the inner and outer cross-validation
inner_cv = KFold(n_splits=5, shuffle=True)
outer_cv = KFold(n_splits=5, shuffle=True)
# Initialize the outer GA
selector_outer = GeneticSelectionCV(estimator=SVC(kernel='linear'),
cv=outer_cv,
verbose=1,
scoring='accuracy',
n_population=outer_population_size,
crossover_proba=outer_crossover_proba,
mutation_proba=outer_mutation_proba,
n_generations=outer_generations,
fitness=outer_fitness)
# Run the outer GA
selector_outer.fit(X, y)
# Get the selected features from the outer GA
X_filtered_outer = X[:, selector_outer.support_]
# Initialize the inner GA
selector_inner = GeneticSelectionCV(estimator=MLPClassifier(hidden_layer_sizes=(10,), max_iter=1000, random_state=0),
cv=inner_cv,
verbose=1,
scoring='accuracy',
n_population=inner_population_size,
crossover_proba=inner_crossover_proba,
mutation_proba=inner_mutation_proba,
n_generations=inner_generations,
fitness=inner_fitness)
# Run the inner GA with the filtered features from the outer GA
selector_inner.fit(X_filtered_outer, y)
# Get the selected features from the inner GA
X_filtered_inner = X_filtered_outer
import numpy as np
from sklearn.svm import SVC
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import KFold
from genetic_selection import GeneticSelectionCV
# Define the inner loop parameters
inner_population_size = 50
inner_generations = 20
inner_crossover_proba = 0.5
inner_mutation_proba = 0.2
# Define the outer loop parameters
outer_population_size = 100
outer_generations = 40
outer_crossover_proba = 0.5
outer_mutation_proba = 0.2
# Define the fitness function for the inner GA
def inner_fitness(X, y, individual, outer_selector):
if np.count_nonzero(individual) == 0:
return 0.0
X_filtered = X[:, individual==1]
clf = MLPClassifier(hidden_layer_sizes=(10,), max_iter=1000, random_state=0)
score = 0.0
for train_index, test_index in inner_cv.split(X_filtered, y):
X_train, X_test = X_filtered[train_index], X_filtered[test_index]
y_train, y_test = y[train_index], y[test_index]
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
score += accuracy_score(y_test, y_pred)
# Get the selected features from the inner GA
selector_inner = GeneticSelectionCV(estimator=clf,
cv=inner_cv,
verbose=0,
scoring='accuracy',
n_population=inner_population_size,
crossover_proba=inner_crossover_proba,
mutation_proba=inner_mutation_proba,
n_generations=inner_generations,
fitness=inner_fitness)
selector_inner.fit(X_filtered, y)
selected_features_inner = np.array(selector_inner.support_)
# Update the outer GA with the selected features from the inner GA
X_filtered_inner = X_filtered[:, selected_features_inner]
outer_selector.estimator_ = SVC(kernel='linear')
outer_selector.fit(X_filtered_inner, y)
selected_features_outer = np.array(outer_selector.support_)
# Update the individual of the inner GA with the selected features from the outer GA
selected_features_outer_inner = np.zeros_like(individual)
selected_features_outer_inner[individual==1] = selected_features_inner
selected_features_outer_inner[selected_features_outer==0] = 0
return score / inner_cv.get_n_splits(), selected_features_outer_inner
# Define the fitness function for the outer GA
def outer_fitness(X, y, individual):
if np.count_nonzero(individual) == 0:
return 0.0
X_filtered = X[:, individual==1]
clf = SVC(kernel='linear')
score = 0.0
for train_index, test_index in outer_cv.split(X_filtered, y):
X_train, X_test = X_filtered[train_index], X_filtered[test_index]
y_train, y_test = y[train_index], y[test_index]
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
score += accuracy_score(y_test, y_pred)
return score / outer_cv.get_n_splits()
# Load the dataset
X = ...
y = ...
# Define the inner and outer cross-validation
inner_cv = KFold(n_splits=5, shuffle=True)
outer_cv = KFold(n_splits=5, shuffle=True)
# Initialize the outer GA
selector_outer = GeneticSelectionCV(estimator=S