main.py
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 process import load_and_preprocess_data
from classifier import evaluate_individual

# 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 and model parameters
best_features, best_params = evaluate_individual(X_train, X_test, y_train, y_test)

# Initialize and train the model with best parameters found by GA
model = RandomForestClassifier(**best_params)
model.fit(X_train[best_features], y_train)

# Predict
predictions = model.predict(X_test[best_features])
prediction_probs = model.predict_proba(X_test[best_features])

# 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()




process.py:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder

# Load the datasets
def load_and_preprocess_data():
    train_df = pd.read_csv('train.csv')
    test_df = pd.read_csv('test.csv')
    gender_submission = pd.read_csv('gender_submission.csv')  # Example target variable for test set

    # Basic preprocessing to demonstrate - you will need to expand this significantly
    # Encode categorical variables as an example
    le = LabelEncoder()
    train_df['Sex'] = le.fit_transform(train_df['Sex'])
    test_df['Sex'] = le.transform(test_df['Sex'])
    
    # Select features - this is a starter example.
    features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch']
    
    # Handle missing values, engineer features, etc.
    
    # Split training data into X and y
    X_train = train_df[features]
    y_train = train_df['Survived']
    
    X_test = test_df[features]
    
    # You'd normally not have y_test in real-world scenarios as it's your job to predict it, but here for the sake of demonstration:
    y_test = gender_submission['Survived']
    
    return X_train, X_test, y_train, y_test


classifier.py:
from sklearn.ensemble import RandomForestClassifier
from deap import base, creator, tools, algorithms

def evaluate_individual(individual):
    # Decode individual to model parameters and selected features
    # For demonstration, let’s assume ‘individual’ is a dict with keys ‘features’ and ‘params’
    selected_features = individual[‘features’]
    model_params = individual[‘params’]
    
    model = RandomForestClassifier(**model_params)
    model.fit(X_train[selected_features], y_train)
    
    # Evaluate model
    predictions = model.predict(X_test[selected_features])
    score = accuracy_score(y_test, predictions)
    
    # Subset your X_train and X_test based on selected_features
    # Initialize your model with model_params
    # Fit, predict, and evaluate the model
    # Return evaluation metric (like accuracy or F1-score)
    return score


error:-
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-facb844e0eca> in <cell line: 20>()
     18 
     19 # Run GA optimization for features and model parameters
---> 20 best_features, best_params = evaluate_individual(X_train, X_test, y_train, y_test)
     21 
     22 # Initialize and train the model with best parameters found by GA

TypeError: evaluate_individual() takes 1 positional argument but 4 were given


fix the python program