Skip to content

Instantly share code, notes, and snippets.

@breeko
breeko / Monthly Consumer Confidence
Last active December 20, 2015 00:09
Monthly Consumer Confidence Source: http://www.econstats.com/r/usind__m5.htm
date,confidence
2013-3-26,59.7
2013-2-26,69
2013-1-29,58.6
2012-12-27,65.1
2012-11-27,73.7
2012-10-1,72.2
2012-9-25,70.3
2012-8-28,60.6
2012-7-31,65.9
DATE,Ratio
11/1/2006,0.91
11/2/2006,0.9
11/3/2006,0.91
11/6/2006,0.76
11/7/2006,0.79
11/8/2006,0.78
11/9/2006,0.84
11/10/2006,1.06
11/13/2006,0.8
import numpy as np
import gzip
import json
def get_relative_frame(frame, position, new_position=(0,0)):
""" Rolls a frame such that position is moved to new_position
Input:
frame: 2-dimensional array
position: tuple (row, col)
import os
import pickle
import tensorflow as tf
from sklearn.model_selection import train_test_split
with open('djma_v3.pkl', 'rb') as input:
data = pickle.load(input)
X = data["X"]
y = data["y"]
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping
import pickle
import numpy as np
import json
def get_relative_frame(frame, position, new_position=(0,0)):
@breeko
breeko / mountain_car_td.py
Created April 29, 2017 20:16
TD-Lambda algorithm used to solve MountainCar-v0 openai environment
import numpy as np
import gym
from itertools import product
def init_centers(num_splits=4, num_obs=2):
""" Returns [num_splits**num_obs, num_obs] matrix of equidistant centers from (0,0) to (1,1) """
return np.array(list(product(np.linspace(0,1,num_splits),repeat=num_obs)))
def init_theta(num_splits=4,num_actions=2):
""" Returns random [num_splits*num_splits,action_space] matrix of value action pairs """
# Policy based reinforcement learning agent used to solve openai's CartPole challenge
# https://gym.openai.com/evaluations/eval_dMY1xQiST7GXe4Br5n31w
import numpy as np
import tensorflow as tf
import gym
ENVIRONMENT = "CartPole-v0"
SEED = 0
@breeko
breeko / cartpole_evolution.py
Created May 8, 2017 02:09
Evolutionary Algorithm for solving CartPole
import numpy as np
import gym
def evolve(W, sigma, prob_mutate=0.5):
rand = (np.random.randn(*W.shape) - 0.5) * sigma
mutate = np.random.choice([0,1],size=W.shape,p=(1-prob_mutate, prob_mutate))
return W + (mutate * rand)
def evaluate(W, num_trials=1, max_t=1000, render=False):
cum_rewards = []
@breeko
breeko / cartpole_td.py
Created June 2, 2017 10:43
TD Learning algorithm used to solve cartpole OpenAI environment
import gym
import numpy as np
from sklearn.preprocessing import StandardScaler
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import Adamax
# TD Learning
MEMORY = 1 # Number of prior states to consider when training our agent
@breeko
breeko / tic_tac_toe.py
Last active December 1, 2018 06:52
Tic-tac-toe board
class TicTacToe:
""" Tic-Tac Toe Board
Board is represented through a board object
1 ¦ 2 ¦ 3
--+---+--
4 ¦ 5 ¦ 6
--+---+--
7 ¦ 8 ¦ 9