Skip to content

Instantly share code, notes, and snippets.

View gkhayes's full-sized avatar

Dr Genevieve Hayes gkhayes

View GitHub Profile
@gkhayes
gkhayes / Mountain_Car.py
Created February 22, 2019 08:09
Use Q-learning to solve the OpenAI Gym Mountain Car problem
import numpy as np
import gym
import matplotlib.pyplot as plt
# Import and initialize Mountain Car Environment
env = gym.make('MountainCar-v0')
env.reset()
# Define Q-learning function
def QLearning(env, learning, discount, epsilon, min_eps, episodes):
@gkhayes
gkhayes / OpenAI_Gym_Test.py
Created February 16, 2019 02:14
Run OpenAI Gym to make sure it has been installed correctly.
import gym
env = gym.make('CartPole-v0')
env.reset()
for _ in range(1000):
env.render()
env.step(env.action_space.sample())
env.close()
@gkhayes
gkhayes / Iris_Logistic_Fit_Predict2.py
Last active March 3, 2019 04:36
Fit a logistic regression to the Iris dataset and make predictions - Attempt 2
# Initialize logistic regression object and fit object
lr_model2 = mlrose.LogisticRegression(algorithm = 'random_hill_climb', max_iters = 1000,
bias = True, 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)
# Predict labels for train set and assess accuracy
y_train_pred = lr_model2.predict(X_train_scaled)
@gkhayes
gkhayes / Iris_Logistic_Fit_Predict.py
Last active March 3, 2019 04:37
Fit a logistic regression to the Iris dataset and make predictions
# Initialize logistic regression object and fit object
lr_model1 = mlrose.LogisticRegression(algorithm = 'random_hill_climb', max_iters = 1000,
bias = True, 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)
# Predict labels for train set and assess accuracy
y_train_pred = lr_model1.predict(X_train_scaled)
@gkhayes
gkhayes / Iris_Logistic_NN_Init.py
Last active March 3, 2019 04:34
Initialize logistic regression using Neural Network class
lr_nn_model1 = mlrose.NeuralNetwork(hidden_nodes = [], activation = 'sigmoid',
algorithm = 'random_hill_climb', max_iters = 1000,
bias = True, is_classifier = True, learning_rate = 0.0001,
early_stopping = True, clip_max = 5, max_attempts = 100,
random_state = 3)
@gkhayes
gkhayes / Iris_NN_Fit_Predict2.py
Last active March 3, 2019 04:34
Attempt 2 at fitting a neural network from the Iris dataset
# Initialize neural network object and fit object
nn_model2 = mlrose.NeuralNetwork(hidden_nodes = [2], activation = 'relu',
algorithm = 'gradient_descent', max_iters = 1000,
bias = True, is_classifier = True, 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)
# Predict labels for train set and assess accuracy
@gkhayes
gkhayes / Iris_NN_Predict.py
Created January 23, 2019 06:41
Make predictions from neural network fitted to Iris dataset
from sklearn.metrics import accuracy_score
# 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)
print('Training accuracy: ', y_train_accuracy)
# Predict labels for test set and assess accuracy
@gkhayes
gkhayes / Iris_NN_Fit.py
Last active March 3, 2019 04:33
Fit neural network to the Iris dataset
# Initialize neural network object and fit object
nn_model1 = mlrose.NeuralNetwork(hidden_nodes = [2], activation = 'relu',
algorithm = 'random_hill_climb', max_iters = 1000,
bias = True, is_classifier = True, 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)
@gkhayes
gkhayes / Iris_Preprocessing.py
Created January 21, 2019 06:53
Pre-process the Iris dataset
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder
# 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()
@gkhayes
gkhayes / Iris_Exploration.py
Last active January 21, 2019 06:49
Load and explore Iris dataset
from sklearn.datasets import load_iris
# Load the Iris dataset
data = load_iris()
# Get feature values
print('The feature values for Obs 0 are: ', data.data[0])
# Get feature names
print('The feature names are: ', data.feature_names)