Skip to content

Instantly share code, notes, and snippets.

@breeko
breeko / ExperienceReplayBiased.py
Last active February 24, 2018 23:38
Biased experience replay which biases samples towards those with extreme rewards
import numpy as np
class ExperienceReplay:
def __init__(self,buffer_size=50000,unusual_sample_factor=0.99):
""" Data structure used to hold game experiences """
# Buffer will contain [state,action,reward,next_state,done]
self.buffer = []
self.buffer_size = buffer_size
assert unusual_sample_factor <= 1, "unusual_sample_factor has to be <= 1"
@breeko
breeko / benchmark.py
Created February 25, 2018 21:42
Useful functions and classes to create benchmarks OpenAI environments
import numpy as np
def test_model(env, model, num_trials=1000):
rewards = []
for _ in range(num_trials):
done = False
env.reset()
state = env.state
cum_rewards = 0
@breeko
breeko / absorbent_barriers.py
Last active January 31, 2020 20:32
Testing the idea of survival, absorbent barriers based on different strategies and payoffs
import numpy as np
import matplotlib.pyplot as plt
MAX_NUM_STEPS = 1000000
NUM_TRIALS = 100
START_CAPITAL = 100
MIN_BET_SIZE = 0.01
def test(payout, strategy, start_capital=START_CAPITAL, num_trials=NUM_TRIALS, max_num_steps=MAX_NUM_STEPS, min_bet_size=MIN_BET_SIZE):
""" Returns the capital based on given strategy
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
pd.set_option("precision", 2)
pd.options.display.float_format = '{:20,.2f}'.format
df = pd.read_csv("Restaurant_Grades.csv")
# Replace spaces with underscores
df.columns = df.columns.str.replace("CUISINE DESCRIPTION","CUISINE")
gb_all = df.groupby("GRADE").GRADE.agg(["count"])
gb_all["perc"] = gb_all / gb_all.sum() * 100
gb_recent = df.drop_duplicates("KEY").groupby("GRADE").GRADE.agg(["count"])
gb_recent["perc"] = gb_recent / gb_recent.sum() * 100
gb_all = gb_all.join(gb_recent,lsuffix=" all", rsuffix=" most recent")#
gb_all[["perc all", "perc most recent"]].plot(kind="bar")
gb_all.style.format("{:,.2f}")
# Define colors to be used in visualizations
colors = {"A": "darkseagreen", "B": "dodgerblue", "C": "lightcoral", "P": "wheat", "Z": "lightgrey", "Not Yet Graded": "lightgrey"}
# Bar graph based on scores, segmented by grade
f, axes = plt.subplots(nrows=2,ncols=3,figsize=(12,7))
axes[0][0].hist(df[(df.SCORE > 0) & (df.SCORE < 50)].SCORE,bins=50, color="grey",alpha=0.5)
axes[0][0].set_title("ALL")
for ax, boro in zip(axes.ravel()[1:], df.BORO.unique()):
f, axes = plt.subplots(nrows=1,ncols=3,figsize=(15,5))
_ = axes[0].hist(df.GRADE_DATE.dt.dayofweek, bins=np.arange(8)-0.5, rwidth=0.95)
axes[0].set_title("Days of week (0 is Monday)")
_ = axes[1].hist(df.GRADE_DATE.dt.day, bins=31)
axes[1].set_title("Day of month")
_ = axes[2].hist(df[df.GRADE_DATE.dt.year < 2018].GRADE_DATE.dt.month, bins=np.arange(1,14)-0.5, rwidth=0.9)
axes[2].set_title("Month")
f.tight_layout()
f, ax = plt.subplots(figsize=(10,5))
df["YEAR"] = df.GRADE_DATE.dt.year
gb = df[["YEAR", "GRADE","SCORE"]].groupby(["YEAR","GRADE"]).agg("count")
gb["perc"] = gb / gb.sum(level=0)
for year in df.YEAR.unique():
bottom = 0
for grade in sorted(df.GRADE.unique()):
# Replace cuisine description of each restaurant with most common
most_common_cuisine = df.groupby(["DBA"])["CUISINE"].agg(lambda x: x.value_counts().index[0])
df = df.drop("CUISINE",axis=1)
df = df.join(most_common_cuisine, on="DBA")
# Calculate how many times each restaurant chain was graded
num_score_dba = df.groupby("DBA")[["SCORE"]].count()
num_score_dba.columns = ["NUM_SCORE_DBA"]
mean_score_dba = df.groupby("DBA")[["SCORE"]].mean()
max_num_ratings = max(df.groupby("KEY").size())
columns = [idx for idx in range(max_num_ratings)]
columns.insert(0, "KEY")
df_rest = pd.DataFrame(columns=columns)
for key in df.KEY.unique():
df_key = df[df.KEY == key]
new_row = {col: "NA" for col in columns}
new_row = {"KEY": key}
for idx, (k, v) in enumerate(df_key.iterrows()):