Untitled
unknown
plain_text
2 years ago
3.7 kB
11
Indexable
from sklearn.datasets import load_breast_cancer
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import numpy as np
import pandas as pd
import random
bc_data = load_breast_cancer() # Dataset laden
X = bc_data.data
y = bc_data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Split the dataset into training and testing sets
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
mlp = MLPClassifier(random_state=42, max_iter=300)
mlp.fit(X_train_scaled, y_train) # Train model op geschaalde trainingsdata
def create_population(pop_size): # Individuen genereren (generational model)
population = []
for _ in range(pop_size):
hidden_sizes = [50, 100, 150]
activations = ["relu", "logistic", "tanh"]
learn_rates = [0.001, 0.01, 0.0001]
max_iters = [500, 600, 700, 800, 900, 1000]
individual = {
"hidden_layer_sizes": (random.choice(hidden_sizes),),
"activation": random.choice(activations),
"learning_rate_init": random.choice(learn_rates),
"max_iter": random.choice(max_iters)
}
population.append(individual)
return population
# Genereer population
pop_count = 10
initial_pop = create_population(pop_count)
def fitness_score(individual, X_train_scaled, y_train, X_test_scaled, y_test):
# Initialiseer MLPC model met hyperparameters van het individu
mlp = MLPClassifier(hidden_layer_sizes=individual["hidden_layer_sizes"],
activation=individual["activation"],
learning_rate_init=individual["learning_rate_init"],
max_iter=individual["max_iter"],
random_state=42)
# Train model op nieuwe data
mlp.fit(X_train_scaled, y_train)
# Evalueer accuracu van model op de testdata
accuracy = mlp.score(X_test_scaled, y_test)
return accuracy
def evaluate_population(population, X_train_scaled, y_train, X_test_scaled, y_test):
fitness_scores = [fitness_score(individual, X_train_scaled, y_train, X_test_scaled, y_test) for individual in population]
sorted_population = sorted(zip(fitness_scores, population), reverse=True, key=lambda x: x[0])
return [individual for _, individual in sorted_population], sorted_population[0]
def parent_selection(sorted_population, elite_size=2):
parents = []
for i in range(elite_size):
parents.append(sorted_population[i][1])
return parents
def crossover(parent1, parent2, mutation_rate=0.1):
child = {}
# Loop door alle keys in de parent hyperparameters
for key in parent1:
# Kies willekeurig van welke ouder het kind de waarde erft
if random.random() > 0.5:
child[key] = parent1[key]
else:
child[key] = parent2[key]
if random.random() < mutation_rate:
mutation_key = random.choice(list(child.keys()))
if mutation_key == "hidden_layer_sizes":
child[mutation_key] = (random.choice([50, 100, 150]),)
elif mutation_key == "activation":
child[mutation_key] = random.choice(["relu", "logistic", "tanh"])
elif mutation_key == "learning_rate_init":
child[mutation_key] = random.choice([0.001, 0.01, 0.0001])
elif mutation_key == "max_iter":
child[mutation_key] = random.choice([500, 600, 700])
return child
Editor is loading...
Leave a Comment