import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import label_binarize
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score, precision_score, f1_score, roc_curve, auc, roc_auc_score
from itertools import cycle
from scipy import interp
from deap import tools
from process import load_and_preprocess_data
from classifier import run_ga
Load and preprocess data
Assuming load_and_preprocess_data and evaluate_individual
are correctly implemented functions that perform
data loading/preprocessing and GA optimization, respectively.
X_train, X_test, y_train, y_test = load_and_preprocess_data()
Run GA optimization for features selection (Hyperparameters could be part of the individuals as well)
optimized_population = run_ga()
best_ind = tools.selBest(optimized_population, 1)[0]
best_features_indices = [i for i, bit in enumerate(best_ind) if bit == 1]
Assuming ‘best_features_indices’ has been determined as follows:
best_features_indices = [0, 1]
print("Best Individual (Features Selected) = ", best_features_indices)
print("Best Fitness = ", best_ind.fitness.values)
model = RandomForestClassifier(n_estimators=100, max_depth=4, random_state=42)
model.fit(X_train.iloc[:, best_features_indices], y_train)
Assuming the RandomForestClassifier is tuned or you’re using default params
model = RandomForestClassifier(n_estimators=100, max_depth=4, random_state=42)
Note: You need to adjust how features are used based on your data structure here
model.fit(X_train[:, best_features_indices], y_train)
Apply the same correction for making predictions
predictions = model.predict(X_test.iloc[:, best_features_indices])
Predict
predictions = model.predict(X_test[:, best_features_indices])
prediction_probs = model.predict_proba(X_test[:, best_features_indices])
Assuming y_test has integer labels for classes
n_classes = len(np.unique(y_test))
Binarize the output for multi-class ROC curve calculations
y_test_binarized = label_binarize(y_test, classes=np.arange(n_classes))
Evaluation - Classification Report
print("Classification Report:")
print(classification_report(y_test, predictions))
print("Precision Score:", precision_score(y_test, predictions, average='macro'))
print("F1 Score:", f1_score(y_test, predictions, average='macro'))
print("Accuracy Score:", accuracy_score(y_test, predictions))
Confusion Matrix Heatmap
plt.figure(figsize=(10, 7))
conf_matrix = confusion_matrix(y_test, predictions)
sns.heatmap(conf_matrix, annot=True, fmt="d")
plt.title('Confusion Matrix')
plt.ylabel('Actual Label')
plt.xlabel('Predicted Label')
plt.show()
Predicted vs Actual Plot Fix
Instead of density, use histogram for categorical data
plt.figure(figsize=(10, 7))
sns.histplot(y_test, color="red", label="Actual", stat="density", kde=False)
sns.histplot(predictions, color="blue", label="Predicted", stat="density", kde=False)
plt.title('Actual vs Predicted Distribution')
plt.legend()
plt.show()
Multi-Class ROC Curve and AUC
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(y_test_binarized[:, i], prediction_probs[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
Compute micro-average ROC curve and ROC area
fpr["micro"], tpr["micro"], _ = roc_curve(y_test_binarized.ravel(), prediction_probs.ravel())
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])
Aggregate all false positive rates
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))
Interpolate all ROC curves at these points
mean_tpr = np.zeros_like(all_fpr)
for i in range(n_classes):
mean_tpr += np.interp(all_fpr, fpr[i], tpr[i])
Average it and compute AUC
mean_tpr /= n_classes
fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])
Plot all ROC curves
plt.figure(figsize=(10, 8))
colors = cycle(['aqua', 'darkorange', 'cornflowerblue', 'green', 'red', 'purple'])
for i, color in zip(range(n_classes), colors):
plt.plot(fpr[i], tpr[i], color=color, lw=2,
label='ROC curve of class {0} (area = {1:0.2f})'.format(i, roc_auc[i]))
plt.plot(fpr["micro"], tpr["micro"],
label='micro-average ROC curve (area = {0:0.2f})'.format(roc_auc["micro"]),
color='deeppink', linestyle=':', linewidth=4)
plt.plot(fpr["macro"], tpr["macro"],
label='macro-average ROC curve (area = {0:0.2f})'.format(roc_auc["macro"]),
color='navy', linestyle=':', linewidth=4)
plt.plot([0, 1], [0, 1], 'k--', lw=2)
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Extension of Receiver Operating Characteristic to Multi-class')
plt.legend(loc="lower right")
plt.show()
NameError Traceback (most recent call last)
in <cell line: 21>()
19
20 # Run GA optimization for features selection (Hyperparameters could be part of the individuals as well)
---> 21 optimized_population = run_ga()
22 best_ind = tools.selBest(optimized_population, 1)[0]
23 # best_features_indices = [i for i, bit in enumerate(best_ind) if bit == 1]
1 frames
/content/classifier.py in evaluate_individual(individual)
40 return (0,)
41
---> 42 # model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=42)
43 model = RandomForestClassifier(n_estimators=100, max_depth=4, random_state=42)
44 model.fit(X_train.iloc[:, selected_features_indices], y_train)
NameError: name 'best_features_indices' is not defined
classifier.py:
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)