Skip to content

Commit 51fd0ad

Browse files
author
Gordon Shotwell
authored
Adds additional exercises (posit-dev#54)
* Add Basic UI problems * Reactivity exercises * Add dynamic UI exercises * Reactive event and calc exercises
1 parent f9d3276 commit 51fd0ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+10579
-9243
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use `ui.sidebar()` to put the Account dropdown in a sidebar.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from shiny.express import render, ui, input
2+
from data_import import df
3+
from plots import plot_auc_curve, plot_precision_recall_curve
4+
from shinywidgets import render_plotly
5+
6+
with ui.sidebar():
7+
ui.input_select(
8+
"account",
9+
"Account",
10+
choices=[
11+
"Berge & Berge",
12+
"Fritsch & Fritsch",
13+
"Hintz & Hintz",
14+
"Mosciski and Sons",
15+
"Wolff Ltd",
16+
],
17+
)
18+
19+
20+
@render_plotly
21+
def precision_recall_plot():
22+
account_subset = df[df["account"] == input.account()]
23+
return plot_precision_recall_curve(
24+
account_subset, "is_electronics", "training_score"
25+
)
26+
27+
28+
@render_plotly
29+
def auc_plot():
30+
account_subset = df[df["account"] == input.account()]
31+
return plot_auc_curve(account_subset, "is_electronics", "training_score")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from shiny.express import render, ui, input
2+
from data_import import df
3+
from plots import plot_auc_curve, plot_precision_recall_curve
4+
from shinywidgets import render_plotly
5+
6+
ui.input_select(
7+
"account",
8+
"Account",
9+
choices=[
10+
"Berge & Berge",
11+
"Fritsch & Fritsch",
12+
"Hintz & Hintz",
13+
"Mosciski and Sons",
14+
"Wolff Ltd",
15+
],
16+
)
17+
18+
19+
@render_plotly
20+
def precision_recall_plot():
21+
account_subset = df[df["account"] == input.account()]
22+
return plot_precision_recall_curve(
23+
account_subset, "is_electronics", "training_score"
24+
)
25+
26+
27+
@render_plotly
28+
def auc_plot():
29+
account_subset = df[df["account"] == input.account()]
30+
return plot_auc_curve(account_subset, "is_electronics", "training_score")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from pathlib import Path
2+
import pandas as pd
3+
import numpy as np
4+
5+
file_path = Path(__file__).parent / "simulated-data.csv"
6+
7+
df = pd.read_csv(file_path, dtype={"sub_account": str})
8+
df["date"] = pd.to_datetime(df["date"], errors="coerce")
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import plotly.express as px
2+
from pandas import DataFrame
3+
import pandas as pd
4+
from sklearn.metrics import roc_curve, auc, precision_recall_curve
5+
import numpy as np
6+
7+
8+
import plotly.io as pio
9+
10+
# Set the default plotly theme to resemble ggplot's theme_light
11+
pio.templates.default = "plotly_white"
12+
13+
14+
def plot_score_distribution(df: DataFrame):
15+
fig = px.histogram(df, x="training_score", nbins=50, title="Model scores")
16+
fig.update_layout(xaxis_title="Score", yaxis_title="Density")
17+
return fig
18+
19+
20+
def plot_auc_curve(df: DataFrame, true_col: str, pred_col: str):
21+
fpr, tpr, _ = roc_curve(df[true_col], df[pred_col])
22+
roc_auc = auc(fpr, tpr)
23+
24+
roc_df = DataFrame({"False Positive Rate": fpr, "True Positive Rate": tpr})
25+
26+
fig = px.line(
27+
roc_df,
28+
x="False Positive Rate",
29+
y="True Positive Rate",
30+
title=f"Receiver Operating Characteristic (ROC) - AUC: {roc_auc.round(2)}",
31+
labels={
32+
"False Positive Rate": "False Positive Rate",
33+
"True Positive Rate": "True Positive Rate",
34+
},
35+
)
36+
fig.add_shape(type="line", line=dict(dash="dash"), x0=0, x1=1, y0=0, y1=1)
37+
return fig
38+
39+
40+
def plot_precision_recall_curve(df: DataFrame, true_col: str, pred_col: str):
41+
precision, recall, _ = precision_recall_curve(df[true_col], df[pred_col])
42+
43+
pr_df = DataFrame({"Recall": recall, "Precision": precision})
44+
45+
fig = px.line(
46+
pr_df,
47+
x="Recall",
48+
y="Precision",
49+
title="Precision-Recall Curve",
50+
labels={"Recall": "Recall", "Precision": "Precision"},
51+
)
52+
return fig
53+
54+
55+
def plot_api_response(df):
56+
account = df["account"].unique()
57+
58+
data = np.random.lognormal(0, 1 / len(account), 10000)
59+
df = pd.DataFrame({"Value": data})
60+
fig = px.histogram(df, x="Value", nbins=50, title="API response time")
61+
fig.update_layout(xaxis_title="Seconds", yaxis_title="Density")
62+
return fig

apps/problem-sets/2-basic-ui/2.1-sidebar/simulated-data.csv

Lines changed: 1001 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Wrap the two plots in cards.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from shiny.express import render, ui, input
2+
from data_import import df
3+
from plots import plot_auc_curve, plot_precision_recall_curve
4+
from shinywidgets import render_plotly
5+
6+
with ui.sidebar():
7+
ui.input_select(
8+
"account",
9+
"Account",
10+
choices=[
11+
"Berge & Berge",
12+
"Fritsch & Fritsch",
13+
"Hintz & Hintz",
14+
"Mosciski and Sons",
15+
"Wolff Ltd",
16+
],
17+
)
18+
19+
with ui.card():
20+
21+
@render_plotly
22+
def precision_recall_plot():
23+
account_subset = df[df["account"] == input.account()]
24+
return plot_precision_recall_curve(
25+
account_subset, "is_electronics", "training_score"
26+
)
27+
28+
29+
with ui.card():
30+
31+
@render_plotly
32+
def auc_plot():
33+
account_subset = df[df["account"] == input.account()]
34+
return plot_auc_curve(account_subset, "is_electronics", "training_score")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from shiny.express import render, ui, input
2+
from data_import import df
3+
from plots import plot_auc_curve, plot_precision_recall_curve
4+
from shinywidgets import render_plotly
5+
6+
with ui.sidebar():
7+
ui.input_select(
8+
"account",
9+
"Account",
10+
choices=[
11+
"Berge & Berge",
12+
"Fritsch & Fritsch",
13+
"Hintz & Hintz",
14+
"Mosciski and Sons",
15+
"Wolff Ltd",
16+
],
17+
)
18+
19+
20+
@render_plotly
21+
def precision_recall_plot():
22+
account_subset = df[df["account"] == input.account()]
23+
return plot_precision_recall_curve(
24+
account_subset, "is_electronics", "training_score"
25+
)
26+
27+
28+
@render_plotly
29+
def auc_plot():
30+
account_subset = df[df["account"] == input.account()]
31+
return plot_auc_curve(account_subset, "is_electronics", "training_score")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from pathlib import Path
2+
import pandas as pd
3+
import numpy as np
4+
5+
file_path = Path(__file__).parent / "simulated-data.csv"
6+
7+
df = pd.read_csv(file_path, dtype={"sub_account": str})
8+
df["date"] = pd.to_datetime(df["date"], errors="coerce")

0 commit comments

Comments
 (0)