NameError                                 Traceback (most recent call last)
<ipython-input-27-c374573557ec> in <cell line: 8>()
      6 
      7 from features_selectionsusingGA import feature_selection
----> 8 from visualplots import plot
      9 
     10 def main():

/content/visualplots.py in <module>
     91 
     92 # Call the plot function
---> 93 plot(best_features)

NameError: name 'best_features' is not defined


import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import classification_report, precision_score, f1_score, roc_curve, auc, confusion_matrix, roc_auc_score
from sklearn.preprocessing import label_binarize, OneHotEncoder

def plot(best_features):
    # Selected features and prediction made with the best individual
    X_train_selected = X_train_imputed[best_features]
    X_test_selected = X_test_imputed[best_features]

    clf = RandomForestClassifier(random_state=42)
    clf.fit(X_train_selected, y_train)
    y_pred = clf.predict(X_test_selected)

    # Print evaluation scores
    print("Classification Report:")
    print(classification_report(y_test, y_pred))
    print("Precision Score:", precision_score(y_test, y_pred))
    print("F1 Score:", f1_score(y_test, y_pred))
    print("Accuracy Score:", accuracy_score(y_test, y_pred))

    # Confusion Matrix Heatmap
    plt.figure(figsize=(10, 7))
    conf_matrix = confusion_matrix(y_test, y_pred)
    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
    plt.figure(figsize=(10, 7))
    sns.histplot(y_test, color="red", label="Actual", kde=True, stat="density", linewidth=0)
    sns.histplot(y_pred, color="blue", label="Predicted", kde=True, stat="density", linewidth=0)
    plt.title('Predicted vs Actual Distribution')
    plt.legend()
    plt.show()

    # ROC Curve (For binary classification)
    if len(np.unique(y)) == 2:
        y_test_bin = label_binarize(y_test, classes=[0, 1])
        y_score = clf.predict_proba(X_test_selected)[:, 1]

        fpr, tpr, _ = roc_curve(y_test_bin, y_score)
        roc_auc = auc(fpr, tpr)
        plt.figure(figsize=(10, 7))
        plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
        plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle=':')
        plt.xlim([0.0, 1.0])
        plt.ylim([0.0, 1.05])
        plt.xlabel('False Positive Rate')
        plt.ylabel('True Positive Rate')
        plt.title('Receiver operating characteristic Example')
        plt.legend(loc="lower right")
        plt.show()

    # One-hot encoding y_test for multi-class ROC AUC calculation
    onehot_encoder = OneHotEncoder(sparse=False)
    y_test_encoded = onehot_encoder.fit_transform(y_test.values.reshape(-1, 1))

    # Predict probabilities for each class
    y_pred_proba = clf.predict_proba(X_test_selected)

    # Compute ROC AUC for each class and micro-averaged
    roc_auc = roc_auc_score(y_test_encoded, y_pred_proba, multi_class='ovr', average="macro")
    print(f"ROC AUC Score (Macro-Average): {roc_auc}")

    # Plotting ROC curves per class
    fpr = dict()
    tpr = dict()
    roc_auc = dict()
    for i in range(y_test_encoded.shape[1]):
        fpr[i], tpr[i], _ = roc_curve(y_test_encoded[:, i], y_pred_proba[:, i])
        roc_auc[i] = auc(fpr[i], tpr[i])

    plt.figure(figsize=(10, 8))
    colors = ['blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'black']
    for i, color in zip(range(y_test_encoded.shape[1]), 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([0, 1], [0, 1], 'k--', lw=2)
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 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()

# Call the plot function
plot(best_features)

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from deap import base, creator, tools, algorithms

def feature_selection():
    # Load dataset
    df = pd.read_csv('Breast_cancer_Wisconsin_data.csv')

    # Drop 'id' column and any columns with all NaN values
    df = df.drop(['id'], axis=1).dropna(axis=1, how='all')

    # Preprocess Data
    df['diagnosis'] = df['diagnosis'].map({'M': 1, 'B': 0})  # Encode diagnosis column: M -> 1, B -> 0
    X = df.drop(['diagnosis'], axis=1)
    y = df['diagnosis']

    # Split dataset into training and testing sets BEFORE imputing
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

    # Create a SimpleImputer object to fill missing values
    imputer = SimpleImputer(strategy="mean")  # or median, most_frequent etc. based on your data

    # Apply the imputer to the train and test datasets AFTER splitting
    X_train_imputed = pd.DataFrame(imputer.fit_transform(X_train), columns=X_train.columns)
    X_test_imputed = pd.DataFrame(imputer.transform(X_test), columns=X_test.columns)

    # Evaluation function for the GA
    def evalFeatures(individual):
        features = [X.columns[i] for i in range(len(individual)) if individual[i] == 1]
        
        if len(features) == 0:
            return 0,  # Prevent usage of empty feature set

        # Select only the features from individual
        X_train_selected = X_train_imputed[features]
        X_test_selected = X_test_imputed[features]
        
        clf = RandomForestClassifier(random_state=42)
        clf.fit(X_train_selected, y_train)
        predictions = clf.predict(X_test_selected)
        return (accuracy_score(y_test, predictions),)

    # Genetic algorithm setup
    creator.create("FitnessMax", base.Fitness, weights=(1.0,))
    creator.create("Individual", list, fitness=creator.FitnessMax)

    toolbox = base.Toolbox()
    toolbox.register("attr_bool", np.random.randint, 0, 2)
    toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=len(X.columns))
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)
    toolbox.register("evaluate", evalFeatures)
    toolbox.register("mate", tools.cxTwoPoint)
    toolbox.register("mutate", tools.mutFlipBit, indpb=0.1)
    toolbox.register("select", tools.selTournament, tournsize=3)

    # Running the GA
    population = toolbox.population(n=50)
    results, logbook = algorithms.eaSimple(population, toolbox, cxpb=0.8, mutpb=0.2, ngen=40, verbose=True)

    # Identifying and printing the best set of features
    best_individual = tools.selBest(population, 1)[0]
    best_features = [X.columns[i] for i in range(len(best_individual)) if best_individual[i] == 1]
    print("Best Features Selected by GA:", best_features)

    return best_features

# Call the feature_selection function
best_features = feature_selection()