---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-18-666b713c2466> in <cell line: 109>()
    108 
    109 if __name__ == "__main__":
--> 110     optimized_population = run_ga()
    111     best_ind = tools.selBest(optimized_population, 1)[0]
    112     print("Best Individual = ", best_ind)

1 frames
<ipython-input-18-666b713c2466> in evaluate_individual(individual)
     44     model.fit(X_train.iloc[:, selected_features_indices], y_train)
     45 
---> 46     X_train_selected = X_train_imputed.iloc[:, selected_features_indices]
     47     X_test_selected = X_test_imputed.iloc[:, selected_features_indices]
     48 

AttributeError: 'numpy.ndarray' object has no attribute 'iloc'


import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from deap import base, creator, tools, algorithms
import random
from sklearn.impute import SimpleImputer

from process import load_and_preprocess_data

# Loading and preprocessing data.
X_train, X_test, y_train, y_test = load_and_preprocess_data()

# Impute missing values.
imputer = SimpleImputer(strategy='mean')
X_train_imputed = imputer.fit_transform(X_train)
X_test_imputed = imputer.transform(X_test)

# You need to convert the imputed data back to a DataFrame here if required
# E.g., if load_and_preprocess_data() returns DataFrames and you need them later
# Columns should be obtained from X_train before fitting the imputer

# X_train_imputed = pd.DataFrame(X_train_imputed, columns=X_train.columns) 
# X_test_imputed = pd.DataFrame(X_test_imputed, columns=X_train.columns)

# Convert imputed NumPy arrays back to DataFrame to maintain the usage of .iloc
# X_train = pd.DataFrame(X_train_imputed_np, columns=X_train.columns)
# X_test = pd.DataFrame(X_test_imputed_np, columns=X_test.columns)

# Convert back to DataFrame if necessary
# X_train = pd.DataFrame(X_train_imputed, columns=X_train_raw.columns)
# X_test = pd.DataFrame(X_test_imputed, columns=X_test_raw.columns)

X_train = pd.DataFrame(X_train_imputed, columns=X_train.columns)
X_test = pd.DataFrame(X_test_imputed, columns=X_test.columns)

# Define the evaluation function.
def evaluate_individual(individual):
    selected_features_indices = [i for i, bit in enumerate(individual) if bit == 1]
    if not selected_features_indices:
        return (0,) 

    # model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=42)
    model = RandomForestClassifier(n_estimators=100, max_depth=4, random_state=42)
    model.fit(X_train.iloc[:, selected_features_indices], y_train)

    X_train_selected = X_train_imputed.iloc[:, selected_features_indices]
    X_test_selected = X_test_imputed.iloc[:, selected_features_indices]

    # Apply the same correction for making predictions
    predictions = model.predict(X_test.iloc[:, selected_features_indices])
    prediction_probs = model.predict_proba(X_test.iloc[:, selected_features_indices])

    # model.fit(X_train_selected, y_train)

    # predictions = model.predict(X_test_selected)
    score = accuracy_score(y_test, predictions)

    return (score,)

# Setup DEAP.
if not hasattr(creator, "FitnessMax"):
    creator.create("FitnessMax", base.Fitness, weights=(1.0,))
if not hasattr(creator, "Individual"):
    creator.create("Individual", list, fitness=creator.FitnessMax)

toolbox = base.Toolbox()
toolbox.register("attr_bool", random.randint, 0, 1)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=X_train_imputed.shape[1])
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

toolbox.register("evaluate", evaluate_individual)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)

# Run Genetic Algorithm.
def run_ga():
    pop = toolbox.population(n=50)
    CXPB, MUTPB, NGEN = 0.5, 0.2, 40

    for g in range(NGEN):
        fitnesses = list(map(toolbox.evaluate, pop))
        for ind, fit in zip(pop, fitnesses):
            ind.fitness.values = fit

        offspring = toolbox.select(pop, len(pop))
        offspring = list(map(toolbox.clone, offspring))

        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            if random.random() < CXPB:
                toolbox.mate(child1, child2)
                del child1.fitness.values
                del child2.fitness.values

        for mutant in offspring:
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values

        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        pop[:] = offspring

    return pop

if __name__ == "__main__":
    optimized_population = run_ga()
    best_ind = tools.selBest(optimized_population, 1)[0]
    print("Best Individual = ", best_ind)
    print("Best Fitness = ", best_ind.fitness.values)