best_clf = genetic_algorithm(clf, params, X_train, y_train) NameError: name 'genetic_algorithm' is n

AbdulazizAbdulaziz Member Posts: 3 Newbie
edited August 2023 in Help
Hi Everyone 
I have this algorithm  :smile:

import pandas as pd
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load the KDDcup 1999 dataset
df = pd.read_csv("kddcup99.csv")

# Split the dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(df.drop("label", axis=1), df["label"], test_size=0.2)

# Create a support vector classifier
clf = SVC()

# Use the genetic algorithm to optimize the classifier hyperparameters
params = {
    "C": [1, 10, 100, 1000],
    "kernel": ["linear", "rbf"],
    "gamma": [0.01, 0.1, 1, 10],
}

best_clf = genetic_algorithm(clf, params, X_train, y_train)

# Train the classifier on the training set
best_clf.fit(X_train, y_train)

# Evaluate the classifier on the test set
y_pred = best_clf.predict(X_test)
print(accuracy_score(y_test, y_pred))

When executing, I get this error: 

best_clf = genetic_algorithm(clf, params, X_train, y_train) NameError: name 'genetic_algorithm' is not defined.

Can you help
Thank you.

Best Answer

  • MartinLiebigMartinLiebig Administrator, Moderator, Employee, RapidMiner Certified Analyst, RapidMiner Certified Expert, University Professor Posts: 3,483 RM Data Scientist
    Solution Accepted
    Hi,
    well - where should this come from? You are not importing it.

    BR,
    Martin
    - Sr. Director Data Solutions, Altair RapidMiner -
    Dortmund, Germany
    Abdulaziz

Answers

  • AbdulazizAbdulaziz Member Posts: 3 Newbie
    Do you know the import code?
  • MartinLiebigMartinLiebig Administrator, Moderator, Employee, RapidMiner Certified Analyst, RapidMiner Certified Expert, University Professor Posts: 3,483 RM Data Scientist
    Sorry, but I cannot know what kind of algorithm you want to use in your code.
    - Sr. Director Data Solutions, Altair RapidMiner -
    Dortmund, Germany
Sign In or Register to comment.