Overview#
mlrose_ky is a Python package for applying some of the most common randomized optimization and search algorithms to a range of different optimization problems, over both discrete- and continuous-valued parameter spaces. This notebook contains the examples used in the mlrose_ky tutorial.
Import Libraries#
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder
from sklearn.metrics import accuracy_score
import mlrose_ky as mlrose
Example 1: 8-Queens Using Pre-Defined Fitness Function#
# Define optimization problem object
problem = mlrose.DiscreteOpt(length=8, fitness_fn=fitness, maximize=False, max_val=8)
# Solve using simulated annealing - attempt 1
init_state = np.array([0, 1, 2, 3, 4, 5, 6, 7])
best_state, best_fitness, _ = mlrose.simulated_annealing(problem, schedule=schedule, max_iters=1000, init_state=init_state, random_state=1)
The best state found is: [6 3 7 2 1 5 2 5]
The fitness at the best state is: 3.0
# Solve using simulated annealing - attempt 2
best_state, best_fitness, _ = mlrose.simulated_annealing(
problem, schedule=schedule, max_attempts=100, max_iters=1000, init_state=init_state, random_state=1
)
array([4, 2, 0, 6, 1, 7, 5, 3])
0.0
Example 2: 8-Queens Using Custom Fitness Function#
# Define alternative N-Queens fitness function for maximization problem
def queens_max(state):
# Initialize counter
fitness = 0
# For all pairs of queens
for i in range(len(state) - 1):
for j in range(i + 1, len(state)):
# Check for horizontal, diagonal-up and diagonal-down attacks
if (state[j] != state[i]) and (state[j] != state[i] + (j - i)) and (state[j] != state[i] - (j - i)):
# If no attacks, then increment counter
fitness += 1
return fitness
# Check function is working correctly
state = np.array([1, 4, 1, 3, 5, 5, 2, 7])
# The fitness of this state should be 22
queens_max(state)
22
# Define optimization problem object
problem_cust = mlrose.DiscreteOpt(length=8, fitness_fn=fitness_cust, max_val=8)
# Solve using simulated annealing - attempt 1
best_state, best_fitness, _ = mlrose.simulated_annealing(problem_cust, schedule=schedule, max_iters=1000, init_state=init_state,
random_state=1)
array([6, 4, 7, 3, 6, 2, 5, 1])
26.0
# Solve using simulated annealing - attempt 2
best_state, best_fitness, _ = mlrose.simulated_annealing(
problem_cust, schedule=schedule, max_attempts=100, max_iters=1000, init_state=init_state, random_state=1
)
array([4, 1, 3, 5, 7, 2, 0, 6])
28.0
Example 3: Travelling Salesperson Using Coordinate-Defined Fitness Function#
# Create list of city coordinates
coords_list = [(1, 1), (4, 2), (5, 2), (6, 4), (4, 4), (3, 6), (1, 5), (2, 3)]
# Initialize fitness function object using coords_list
fitness_coords = mlrose.TravellingSales(coords=coords_list)
# Define optimization problem object
problem_fit = mlrose.TSPOpt(length=8, fitness_fn=fitness_coords)
# Solve using genetic algorithm - attempt 1
best_state, best_fitness, _ = mlrose.genetic_alg(problem_fit, random_state=2)
array([0, 7, 6, 5, 4, 3, 2, 1])
17.342617547667327
# Solve using genetic algorithm - attempt 2
best_state, best_fitness, _ = mlrose.genetic_alg(problem_fit, mutation_prob=0.2, max_attempts=100, random_state=2)
array([0, 7, 6, 5, 4, 3, 2, 1])
17.342617547667327
Example 4: Travelling Salesperson Using Distance-Defined Fitness Function#
# Create list of distances between pairs of cities
dist_list = [
(0, 1, 3.1623),
(0, 2, 4.1231),
(0, 3, 5.8310),
(0, 4, 4.2426),
(0, 5, 5.3852),
(0, 6, 4.0000),
(0, 7, 2.2361),
(1, 2, 1.0000),
(1, 3, 2.8284),
(1, 4, 2.0000),
(1, 5, 4.1231),
(1, 6, 4.2426),
(1, 7, 2.2361),
(2, 3, 2.2361),
(2, 4, 2.2361),
(2, 5, 4.4721),
(2, 6, 5.0000),
(2, 7, 3.1623),
(3, 4, 2.0000),
(3, 5, 3.6056),
(3, 6, 5.0990),
(3, 7, 4.1231),
(4, 5, 2.2361),
(4, 6, 3.1623),
(4, 7, 2.2361),
(5, 6, 2.2361),
(5, 7, 3.1623),
(6, 7, 2.2361),
]
# Initialize fitness function object using dist_list
fitness_dists = mlrose.TravellingSales(distances=dist_list)
# Define optimization problem object
problem_fit2 = mlrose.TSPOpt(length=8, fitness_fn=fitness_dists)
# Solve using genetic algorithm
best_state, best_fitness, _ = mlrose.genetic_alg(problem_fit2, mutation_prob=0.2, max_attempts=100, random_state=2)
array([7, 0, 1, 2, 3, 4, 5, 6])
17.3428
Example 5: Travelling Salesperson Defining Fitness Function as Part of Optimization Problem Definition Step#
# Create list of city coordinates
coords_list = [(1, 1), (4, 2), (5, 2), (6, 4), (4, 4), (3, 6), (1, 5), (2, 3)]
# Define optimization problem object
problem_no_fit = mlrose.TSPOpt(length=8, coords=coords_list)
# Solve using genetic algorithm
best_state, best_fitness, _ = mlrose.genetic_alg(problem_no_fit, mutation_prob=0.2, max_attempts=100, random_state=2)
array([0, 7, 6, 5, 4, 3, 2, 1])
17.342617547667327
Example 6: Fitting a Neural Network to the Iris Dataset#
[5.1 3.5 1.4 0.2]
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
0
setosa
[4.3 2. 1. 0.1]
[7.9 4.4 6.9 2.5]
[0 1 2]
# Split data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=3)
# Normalize feature data
scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# One hot encode target values
one_hot = OneHotEncoder()
y_train_hot = np.asarray(one_hot.fit_transform(y_train.reshape(-1, 1)).todense())
y_test_hot = np.asarray(one_hot.transform(y_test.reshape(-1, 1)).todense())
# Initialize neural network object and fit object - attempt 1
nn_model1 = mlrose.NeuralNetwork(hidden_nodes=[2], max_iters=1000, learning_rate=0.0001, early_stopping=True, clip_max=5, max_attempts=100,
random_state=3)
nn_model1.fit(X_train_scaled, y_train_hot)
NeuralNetwork(clip_max=5, early_stopping=True, hidden_nodes=[2], learning_rate=0.0001, max_attempts=100, max_iters=1000, random_state=3)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
NeuralNetwork(clip_max=5, early_stopping=True, hidden_nodes=[2], learning_rate=0.0001, max_attempts=100, max_iters=1000, random_state=3)
# Predict labels for train set and assess accuracy
y_train_pred = nn_model1.predict(X_train_scaled)
y_train_accuracy = accuracy_score(y_train_hot, y_train_pred)
y_train_accuracy
0.45
# Predict labels for test set and assess accuracy
y_test_pred = nn_model1.predict(X_test_scaled)
y_test_accuracy = accuracy_score(y_test_hot, y_test_pred)
y_test_accuracy
0.5333333333333333
# Initialize neural network object and fit object - attempt 2
nn_model2 = mlrose.NeuralNetwork(hidden_nodes=[2], algorithm="gradient_descent", max_iters=1000, learning_rate=0.0001, early_stopping=True,
clip_max=5, max_attempts=100, random_state=3)
nn_model2.fit(X_train_scaled, y_train_hot)
NeuralNetwork(algorithm='gradient_descent', clip_max=5, early_stopping=True, hidden_nodes=[2], learning_rate=0.0001, max_attempts=100, max_iters=1000, random_state=3)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
NeuralNetwork(algorithm='gradient_descent', clip_max=5, early_stopping=True, hidden_nodes=[2], learning_rate=0.0001, max_attempts=100, max_iters=1000, random_state=3)
# Predict labels for train set and assess accuracy
y_train_pred = nn_model2.predict(X_train_scaled)
y_train_accuracy = accuracy_score(y_train_hot, y_train_pred)
y_train_accuracy
0.625
# Predict labels for test set and assess accuracy
y_test_pred = nn_model2.predict(X_test_scaled)
y_test_accuracy = accuracy_score(y_test_hot, y_test_pred)
y_test_accuracy
0.5666666666666667
Example 7: Fitting a Logistic Regression to the Iris Data#
# Initialize logistic regression object and fit object - attempt 1
lr_model1 = mlrose.LogisticRegression(max_iters=1000, learning_rate=0.0001, early_stopping=True, clip_max=5, max_attempts=100,
random_state=3)
lr_model1.fit(X_train_scaled, y_train_hot)
LogisticRegression(clip_max=5, early_stopping=True, learning_rate=0.0001, max_attempts=100, max_iters=1000, random_state=3)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LogisticRegression(clip_max=5, early_stopping=True, learning_rate=0.0001, max_attempts=100, max_iters=1000, random_state=3)
# Predict labels for train set and assess accuracy
y_train_pred = lr_model1.predict(X_train_scaled)
y_train_accuracy = accuracy_score(y_train_hot, y_train_pred)
y_train_accuracy
0.19166666666666668
# Predict labels for test set and assess accuracy
y_test_pred = lr_model1.predict(X_test_scaled)
y_test_accuracy = accuracy_score(y_test_hot, y_test_pred)
y_test_accuracy
0.06666666666666667
# Initialize logistic regression object and fit object - attempt 2
lr_model2 = mlrose.LogisticRegression(max_iters=1000, learning_rate=0.01, early_stopping=True, clip_max=5, max_attempts=100, random_state=3)
lr_model2.fit(X_train_scaled, y_train_hot)
LogisticRegression(clip_max=5, early_stopping=True, learning_rate=0.01, max_attempts=100, max_iters=1000, random_state=3)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LogisticRegression(clip_max=5, early_stopping=True, learning_rate=0.01, max_attempts=100, max_iters=1000, random_state=3)
# Predict labels for train set and assess accuracy
y_train_pred = lr_model2.predict(X_train_scaled)
y_train_accuracy = accuracy_score(y_train_hot, y_train_pred)
y_train_accuracy
0.6833333333333333
# Predict labels for test set and assess accuracy
y_test_pred = lr_model2.predict(X_test_scaled)
y_test_accuracy = accuracy_score(y_test_hot, y_test_pred)
y_test_accuracy
0.7
Example 8: Fitting a Logistic Regression to the Iris Data using the NeuralNetwork() class#
# Initialize neural network object and fit object - attempt 1
lr_nn_model1 = mlrose.NeuralNetwork(hidden_nodes=[], activation="sigmoid", max_iters=1000, learning_rate=0.0001, early_stopping=True,
clip_max=5, max_attempts=100, random_state=3)
lr_nn_model1.fit(X_train_scaled, y_train_hot)
NeuralNetwork(activation='sigmoid', clip_max=5, early_stopping=True, hidden_nodes=[], learning_rate=0.0001, max_attempts=100, max_iters=1000, random_state=3)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
NeuralNetwork(activation='sigmoid', clip_max=5, early_stopping=True, hidden_nodes=[], learning_rate=0.0001, max_attempts=100, max_iters=1000, random_state=3)
# Predict labels for train set and assess accuracy
y_train_pred = lr_nn_model1.predict(X_train_scaled)
y_train_accuracy = accuracy_score(y_train_hot, y_train_pred)
y_train_accuracy
0.19166666666666668
# Predict labels for test set and assess accuracy
y_test_pred = lr_nn_model1.predict(X_test_scaled)
y_test_accuracy = accuracy_score(y_test_hot, y_test_pred)
y_test_accuracy
0.06666666666666667
# Initialize neural network object and fit object - attempt 2
lr_nn_model2 = mlrose.NeuralNetwork(hidden_nodes=[], activation="sigmoid", max_iters=1000, learning_rate=0.01, early_stopping=True,
clip_max=5, max_attempts=100, random_state=3)
lr_nn_model2.fit(X_train_scaled, y_train_hot)
NeuralNetwork(activation='sigmoid', clip_max=5, early_stopping=True, hidden_nodes=[], learning_rate=0.01, max_attempts=100, max_iters=1000, random_state=3)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
NeuralNetwork(activation='sigmoid', clip_max=5, early_stopping=True, hidden_nodes=[], learning_rate=0.01, max_attempts=100, max_iters=1000, random_state=3)
# Predict labels for train set and assess accuracy
y_train_pred = lr_nn_model2.predict(X_train_scaled)
y_train_accuracy = accuracy_score(y_train_hot, y_train_pred)
y_train_accuracy
0.6833333333333333
# Predict labels for test set and assess accuracy
y_test_pred = lr_nn_model2.predict(X_test_scaled)
y_test_accuracy = accuracy_score(y_test_hot, y_test_pred)
y_test_accuracy
0.7