This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import gym | |
| env = gym.make('CartPole-v0') | |
| env.reset() | |
| for _ in range(1000): | |
| env.render() | |
| env.step(env.action_space.sample()) | |
| env.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
NewerOlder