-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Basic UI problems * Reactivity exercises * Add dynamic UI exercises * Reactive event and calc exercises
- Loading branch information
Gordon Shotwell
authored
Apr 3, 2024
1 parent
f9d3276
commit 51fd0ad
Showing
84 changed files
with
10,579 additions
and
9,243 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Use `ui.sidebar()` to put the Account dropdown in a sidebar. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from shiny.express import render, ui, input | ||
from data_import import df | ||
from plots import plot_auc_curve, plot_precision_recall_curve | ||
from shinywidgets import render_plotly | ||
|
||
with ui.sidebar(): | ||
ui.input_select( | ||
"account", | ||
"Account", | ||
choices=[ | ||
"Berge & Berge", | ||
"Fritsch & Fritsch", | ||
"Hintz & Hintz", | ||
"Mosciski and Sons", | ||
"Wolff Ltd", | ||
], | ||
) | ||
|
||
|
||
@render_plotly | ||
def precision_recall_plot(): | ||
account_subset = df[df["account"] == input.account()] | ||
return plot_precision_recall_curve( | ||
account_subset, "is_electronics", "training_score" | ||
) | ||
|
||
|
||
@render_plotly | ||
def auc_plot(): | ||
account_subset = df[df["account"] == input.account()] | ||
return plot_auc_curve(account_subset, "is_electronics", "training_score") |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from shiny.express import render, ui, input | ||
from data_import import df | ||
from plots import plot_auc_curve, plot_precision_recall_curve | ||
from shinywidgets import render_plotly | ||
|
||
ui.input_select( | ||
"account", | ||
"Account", | ||
choices=[ | ||
"Berge & Berge", | ||
"Fritsch & Fritsch", | ||
"Hintz & Hintz", | ||
"Mosciski and Sons", | ||
"Wolff Ltd", | ||
], | ||
) | ||
|
||
|
||
@render_plotly | ||
def precision_recall_plot(): | ||
account_subset = df[df["account"] == input.account()] | ||
return plot_precision_recall_curve( | ||
account_subset, "is_electronics", "training_score" | ||
) | ||
|
||
|
||
@render_plotly | ||
def auc_plot(): | ||
account_subset = df[df["account"] == input.account()] | ||
return plot_auc_curve(account_subset, "is_electronics", "training_score") |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from pathlib import Path | ||
import pandas as pd | ||
import numpy as np | ||
|
||
file_path = Path(__file__).parent / "simulated-data.csv" | ||
|
||
df = pd.read_csv(file_path, dtype={"sub_account": str}) | ||
df["date"] = pd.to_datetime(df["date"], errors="coerce") |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import plotly.express as px | ||
from pandas import DataFrame | ||
import pandas as pd | ||
from sklearn.metrics import roc_curve, auc, precision_recall_curve | ||
import numpy as np | ||
|
||
|
||
import plotly.io as pio | ||
|
||
# Set the default plotly theme to resemble ggplot's theme_light | ||
pio.templates.default = "plotly_white" | ||
|
||
|
||
def plot_score_distribution(df: DataFrame): | ||
fig = px.histogram(df, x="training_score", nbins=50, title="Model scores") | ||
fig.update_layout(xaxis_title="Score", yaxis_title="Density") | ||
return fig | ||
|
||
|
||
def plot_auc_curve(df: DataFrame, true_col: str, pred_col: str): | ||
fpr, tpr, _ = roc_curve(df[true_col], df[pred_col]) | ||
roc_auc = auc(fpr, tpr) | ||
|
||
roc_df = DataFrame({"False Positive Rate": fpr, "True Positive Rate": tpr}) | ||
|
||
fig = px.line( | ||
roc_df, | ||
x="False Positive Rate", | ||
y="True Positive Rate", | ||
title=f"Receiver Operating Characteristic (ROC) - AUC: {roc_auc.round(2)}", | ||
labels={ | ||
"False Positive Rate": "False Positive Rate", | ||
"True Positive Rate": "True Positive Rate", | ||
}, | ||
) | ||
fig.add_shape(type="line", line=dict(dash="dash"), x0=0, x1=1, y0=0, y1=1) | ||
return fig | ||
|
||
|
||
def plot_precision_recall_curve(df: DataFrame, true_col: str, pred_col: str): | ||
precision, recall, _ = precision_recall_curve(df[true_col], df[pred_col]) | ||
|
||
pr_df = DataFrame({"Recall": recall, "Precision": precision}) | ||
|
||
fig = px.line( | ||
pr_df, | ||
x="Recall", | ||
y="Precision", | ||
title="Precision-Recall Curve", | ||
labels={"Recall": "Recall", "Precision": "Precision"}, | ||
) | ||
return fig | ||
|
||
|
||
def plot_api_response(df): | ||
account = df["account"].unique() | ||
|
||
data = np.random.lognormal(0, 1 / len(account), 10000) | ||
df = pd.DataFrame({"Value": data}) | ||
fig = px.histogram(df, x="Value", nbins=50, title="API response time") | ||
fig.update_layout(xaxis_title="Seconds", yaxis_title="Density") | ||
return fig |
1,001 changes: 1,001 additions & 0 deletions
1,001
apps/problem-sets/2-basic-ui/2.1-sidebar/simulated-data.csv
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Wrap the two plots in cards. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from shiny.express import render, ui, input | ||
from data_import import df | ||
from plots import plot_auc_curve, plot_precision_recall_curve | ||
from shinywidgets import render_plotly | ||
|
||
with ui.sidebar(): | ||
ui.input_select( | ||
"account", | ||
"Account", | ||
choices=[ | ||
"Berge & Berge", | ||
"Fritsch & Fritsch", | ||
"Hintz & Hintz", | ||
"Mosciski and Sons", | ||
"Wolff Ltd", | ||
], | ||
) | ||
|
||
with ui.card(): | ||
|
||
@render_plotly | ||
def precision_recall_plot(): | ||
account_subset = df[df["account"] == input.account()] | ||
return plot_precision_recall_curve( | ||
account_subset, "is_electronics", "training_score" | ||
) | ||
|
||
|
||
with ui.card(): | ||
|
||
@render_plotly | ||
def auc_plot(): | ||
account_subset = df[df["account"] == input.account()] | ||
return plot_auc_curve(account_subset, "is_electronics", "training_score") |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from shiny.express import render, ui, input | ||
from data_import import df | ||
from plots import plot_auc_curve, plot_precision_recall_curve | ||
from shinywidgets import render_plotly | ||
|
||
with ui.sidebar(): | ||
ui.input_select( | ||
"account", | ||
"Account", | ||
choices=[ | ||
"Berge & Berge", | ||
"Fritsch & Fritsch", | ||
"Hintz & Hintz", | ||
"Mosciski and Sons", | ||
"Wolff Ltd", | ||
], | ||
) | ||
|
||
|
||
@render_plotly | ||
def precision_recall_plot(): | ||
account_subset = df[df["account"] == input.account()] | ||
return plot_precision_recall_curve( | ||
account_subset, "is_electronics", "training_score" | ||
) | ||
|
||
|
||
@render_plotly | ||
def auc_plot(): | ||
account_subset = df[df["account"] == input.account()] | ||
return plot_auc_curve(account_subset, "is_electronics", "training_score") |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from pathlib import Path | ||
import pandas as pd | ||
import numpy as np | ||
|
||
file_path = Path(__file__).parent / "simulated-data.csv" | ||
|
||
df = pd.read_csv(file_path, dtype={"sub_account": str}) | ||
df["date"] = pd.to_datetime(df["date"], errors="coerce") |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import plotly.express as px | ||
from pandas import DataFrame | ||
import pandas as pd | ||
from sklearn.metrics import roc_curve, auc, precision_recall_curve | ||
import numpy as np | ||
|
||
|
||
import plotly.io as pio | ||
|
||
# Set the default plotly theme to resemble ggplot's theme_light | ||
pio.templates.default = "plotly_white" | ||
|
||
|
||
def plot_score_distribution(df: DataFrame): | ||
fig = px.histogram(df, x="training_score", nbins=50, title="Model scores") | ||
fig.update_layout(xaxis_title="Score", yaxis_title="Density") | ||
return fig | ||
|
||
|
||
def plot_auc_curve(df: DataFrame, true_col: str, pred_col: str): | ||
fpr, tpr, _ = roc_curve(df[true_col], df[pred_col]) | ||
roc_auc = auc(fpr, tpr) | ||
|
||
roc_df = DataFrame({"False Positive Rate": fpr, "True Positive Rate": tpr}) | ||
|
||
fig = px.line( | ||
roc_df, | ||
x="False Positive Rate", | ||
y="True Positive Rate", | ||
title=f"Receiver Operating Characteristic (ROC) - AUC: {roc_auc.round(2)}", | ||
labels={ | ||
"False Positive Rate": "False Positive Rate", | ||
"True Positive Rate": "True Positive Rate", | ||
}, | ||
) | ||
fig.add_shape(type="line", line=dict(dash="dash"), x0=0, x1=1, y0=0, y1=1) | ||
return fig | ||
|
||
|
||
def plot_precision_recall_curve(df: DataFrame, true_col: str, pred_col: str): | ||
precision, recall, _ = precision_recall_curve(df[true_col], df[pred_col]) | ||
|
||
pr_df = DataFrame({"Recall": recall, "Precision": precision}) | ||
|
||
fig = px.line( | ||
pr_df, | ||
x="Recall", | ||
y="Precision", | ||
title="Precision-Recall Curve", | ||
labels={"Recall": "Recall", "Precision": "Precision"}, | ||
) | ||
return fig | ||
|
||
|
||
def plot_api_response(df): | ||
account = df["account"].unique() | ||
|
||
data = np.random.lognormal(0, 1 / len(account), 10000) | ||
df = pd.DataFrame({"Value": data}) | ||
fig = px.histogram(df, x="Value", nbins=50, title="API response time") | ||
fig.update_layout(xaxis_title="Seconds", yaxis_title="Density") | ||
return fig |
Oops, something went wrong.