-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
73 lines (58 loc) · 2.22 KB
/
helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.display import display
def plot_path(df, deadline=False):
df = df.sort_values(by=['trial', 't'])
grid = sns.FacetGrid(df, col="trial", hue="trial", col_wrap=5, size=2.0, aspect=1.3)
grid.map(plt.axhline, y=0, ls=":", c=".5")
grid.map(plt.plot, "t", "reward", marker="o", ms=3, lw=.8)
if deadline:
grid.set(xticks=np.linspace(0, 50, 6, endpoint=True), xlim=(-1, 50), ylim=(-3, 13))
else:
grid.set(xticks=np.linspace(0, 150, 6, endpoint=True), xlim=(-1, 150), ylim=(-3, 13))
grid.fig.tight_layout(w_pad=1)
def plot_results(df, xs):
"""
:param df:
:type df:
:param xs:
:type xs:
:return:
:rtype:
"""
g = sns.PairGrid(df.sort_values("trial", ascending=False),
x_vars=xs, y_vars=["trial"],
size=10, aspect=.3)
# Draw a dot plot using the stripplot function
g.map(sns.stripplot, size=10, orient="h",
palette="Blues", edgecolor="gray")
# Use the same x axis limits on all columns and add better labels
g.set(xlim=(1, 13), xlabel="Rewards", ylabel="")
# Use semantically meaningful titles for the columns
titles = ["Random", "Basic agent", "Q-learning (basic)", "Q-learning (optimized)"]
for ax, title in zip(g.axes.flat, titles):
# Set a different title for each axes
ax.set(title=title)
# Make the grid horizontal instead of vertical
ax.xaxis.grid(False)
ax.yaxis.grid(True)
sns.despine(left=True, bottom=True)
def plot_opt(data, starting_point):
"""
:param data:
:type data:
:param starting_point:
:type starting_point:
:return:
:rtype:
"""
n = 100 - starting_point
data = data.sort_values(by=['trial', 't'])
for i in data.initial_value.unique():
plt.figure(figsize=(12, 8))
print("initial_value = {}".format(i))
df = data[data['initial_value'] == i].iloc[starting_point:, :]
display(df.groupby(['learning_rate', 'discount_rate'])['success'].agg(np.sum).unstack()/n)
sns.heatmap(df.groupby(['learning_rate', 'discount_rate'])['success'].agg(np.sum).unstack()/n, annot=True)
plt.show()