Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • grosch1/jupyter-dashboarding
  • petrova1/jupyter-dashboarding
2 results
Select Git revision
Show changes
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Usage
Start voila with the gridstack template. If `show_handles` is set to True, you can drag the individual dashboard items around.
```
voila --template=gridstack --VoilaConfiguration.resources='{"gridstack": {"show_handles": True}}' ./dashboards/gridstack-scotch.ipynb
```
%% Cell type:markdown id: tags:
# Got Scotch? # Got Scotch?
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
In this notebook, we're going to create a dashboard that recommends scotches based on their taste profiles. In this notebook, we're going to create a dashboard that recommends scotches based on their taste profiles.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
%matplotlib widget %matplotlib widget
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import pandas as pd import pandas as pd
import seaborn as sns import seaborn as sns
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import os import os
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import ipywidgets as widgets import ipywidgets as widgets
from traitlets import Unicode, List, Instance, link, HasTraits from traitlets import Unicode, List, Instance, link, HasTraits
from IPython.display import display, clear_output, HTML, Javascript from IPython.display import display, clear_output, HTML, Javascript
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
display(widgets.Button()) display(widgets.Button())
``` ```
%% Output %% Output
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Load Data <span style="float: right; font-size: 0.5em"><a href="#Got-Scotch?">Top</a></span> ## Load Data <span style="float: right; font-size: 0.5em"><a href="#Got-Scotch?">Top</a></span>
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
features = [[2, 2, 2, 0, 0, 2, 1, 2, 2, 2, 2, 2], features = [[2, 2, 2, 0, 0, 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1, 0, 3],
[2, 3, 0, 0, 1, 0, 2, 1, 1, 2, 2, 1]] [2, 3, 0, 0, 1, 0, 2, 1, 1, 2, 2, 1]]
feature_names = ['Body', 'Sweetness', 'Smoky', feature_names = ['Body', 'Sweetness', 'Smoky',
'Medicinal', 'Tobacco', 'Honey', 'Medicinal', 'Tobacco', 'Honey',
'Spicy', 'Winey', 'Nutty', 'Spicy', 'Winey', 'Nutty',
'Malty', 'Fruity', 'cluster'] 'Malty', 'Fruity', 'cluster']
brand_names = ['Aberfeldy', brand_names = ['Aberfeldy',
'Aberlour', 'Aberlour',
'AnCnoc', 'AnCnoc',
'Ardbeg', 'Ardbeg',
'Ardmore', 'Ardmore',
'ArranIsleOf', 'ArranIsleOf',
'Auchentoshan', 'Auchentoshan',
'Auchroisk', 'Auchroisk',
'Aultmore', 'Aultmore',
'Balblair', 'Balblair',
'Balmenach', 'Balmenach',
'Belvenie', 'Belvenie',
'BenNevis', 'BenNevis',
'Benriach', 'Benriach',
'Benrinnes', 'Benrinnes',
'Benromach', 'Benromach',
'Bladnoch', 'Bladnoch',
'BlairAthol', 'BlairAthol',
'Bowmore', 'Bowmore',
'Bruichladdich', 'Bruichladdich',
'Bunnahabhain', 'Bunnahabhain',
'Caol Ila', 'Caol Ila',
'Cardhu', 'Cardhu',
'Clynelish', 'Clynelish',
'Craigallechie', 'Craigallechie',
'Craigganmore', 'Craigganmore',
'Dailuaine', 'Dailuaine',
'Dalmore', 'Dalmore',
'Dalwhinnie', 'Dalwhinnie',
'Deanston', 'Deanston',
'Dufftown', 'Dufftown',
'Edradour', 'Edradour',
'GlenDeveronMacduff', 'GlenDeveronMacduff',
'GlenElgin', 'GlenElgin',
'GlenGarioch', 'GlenGarioch',
'GlenGrant', 'GlenGrant',
'GlenKeith', 'GlenKeith',
'GlenMoray', 'GlenMoray',
'GlenOrd', 'GlenOrd',
'GlenScotia', 'GlenScotia',
'GlenSpey', 'GlenSpey',
'Glenallachie', 'Glenallachie',
'Glendronach', 'Glendronach',
'Glendullan', 'Glendullan',
'Glenfarclas', 'Glenfarclas',
'Glenfiddich', 'Glenfiddich',
'Glengoyne', 'Glengoyne',
'Glenkinchie', 'Glenkinchie',
'Glenlivet', 'Glenlivet',
'Glenlossie', 'Glenlossie',
'Glenmorangie', 'Glenmorangie',
'Glenrothes', 'Glenrothes',
'Glenturret', 'Glenturret',
'Highland Park', 'Highland Park',
'Inchgower', 'Inchgower',
'Isle of Jura', 'Isle of Jura',
'Knochando', 'Knochando',
'Lagavulin', 'Lagavulin',
'Laphroig', 'Laphroig',
'Linkwood', 'Linkwood',
'Loch Lomond', 'Loch Lomond',
'Longmorn', 'Longmorn',
'Macallan', 'Macallan',
'Mannochmore', 'Mannochmore',
'Miltonduff', 'Miltonduff',
'Mortlach', 'Mortlach',
'Oban', 'Oban',
'OldFettercairn', 'OldFettercairn',
'OldPulteney', 'OldPulteney',
'RoyalBrackla', 'RoyalBrackla',
'RoyalLochnagar', 'RoyalLochnagar',
'Scapa', 'Scapa',
'Speyburn', 'Speyburn',
'Speyside', 'Speyside',
'Springbank', 'Springbank',
'Strathisla', 'Strathisla',
'Strathmill', 'Strathmill',
'Talisker', 'Talisker',
'Tamdhu', 'Tamdhu',
'Tamnavulin', 'Tamnavulin',
'Teaninich', 'Teaninich',
'Tobermory', 'Tobermory',
'Tomatin', 'Tomatin',
'Tomintoul', 'Tomintoul',
'Tormore', 'Tormore',
'Tullibardine'] 'Tullibardine']
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
features_df = pd.DataFrame(features, columns=feature_names, index=brand_names) features_df = pd.DataFrame(features, columns=feature_names, index=brand_names)
features_df = features_df.drop('cluster', axis=1) features_df = features_df.drop('cluster', axis=1)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
norm = (features_df ** 2).sum(axis=1).apply('sqrt') norm = (features_df ** 2).sum(axis=1).apply('sqrt')
normed_df = features_df.divide(norm, axis=0) normed_df = features_df.divide(norm, axis=0)
sim_df = normed_df.dot(normed_df.T) sim_df = normed_df.dot(normed_df.T)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def radar(df, ax=None): def radar(df, ax=None):
# calculate evenly-spaced axis angles # calculate evenly-spaced axis angles
num_vars = len(df.columns) num_vars = len(df.columns)
theta = 2*np.pi * np.linspace(0, 1-1./num_vars, num_vars) theta = 2*np.pi * np.linspace(0, 1-1./num_vars, num_vars)
# rotate theta such that the first axis is at the top # rotate theta such that the first axis is at the top
theta += np.pi/2 theta += np.pi/2
if not ax: if not ax:
fig = plt.figure(figsize=(4, 4)) fig = plt.figure(figsize=(4, 4))
ax = fig.add_subplot(1,1,1, projection='polar') ax = fig.add_subplot(1,1,1, projection='polar')
else: else:
ax.clear() ax.clear()
for d, color in zip(df.itertuples(), sns.color_palette()): for d, color in zip(df.itertuples(), sns.color_palette()):
ax.plot(theta, d[1:], color=color, alpha=0.7) ax.plot(theta, d[1:], color=color, alpha=0.7)
ax.fill(theta, d[1:], facecolor=color, alpha=0.5) ax.fill(theta, d[1:], facecolor=color, alpha=0.5)
ax.set_xticklabels(df.columns) ax.set_xticklabels(df.columns)
legend = ax.legend(df.index, loc=(0.9, .95)) legend = ax.legend(df.index, loc=(0.9, .95))
return ax return ax
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
class RadarWidget(HasTraits): class RadarWidget(HasTraits):
factors_keys = List(['Aberfeldy']) factors_keys = List(['Aberfeldy'])
def __init__(self, df, **kwargs): def __init__(self, df, **kwargs):
self.df = df self.df = df
super(RadarWidget, self).__init__(**kwargs) super(RadarWidget, self).__init__(**kwargs)
self.ax = None self.ax = None
self.factors_keys_changed() self.factors_keys_changed()
def factors_keys_changed(self): def factors_keys_changed(self):
new_value = self.factors_keys new_value = self.factors_keys
if self.ax: if self.ax:
self.ax.clear() self.ax.clear()
self.ax = radar(self.df.loc[new_value], self.ax) self.ax = radar(self.df.loc[new_value], self.ax)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We now define a *get_similar( )* function to return the data of the top n similar scotches to a given scotch. We now define a *get_similar( )* function to return the data of the top n similar scotches to a given scotch.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def get_similar(name, n, top=True): def get_similar(name, n, top=True):
a = sim_df[name].sort_values(ascending=False) a = sim_df[name].sort_values(ascending=False)
a.name = 'Similarity' a.name = 'Similarity'
df = pd.DataFrame(a) #.join(features_df).iloc[start:end] df = pd.DataFrame(a) #.join(features_df).iloc[start:end]
return df.head(n) if top else df.tail(n) return df.head(n) if top else df.tail(n)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We also need a function *on_pick_scotch* that will display a table of the top 5 similar scotches that Radar View watches, based on a given selected Scotch. We also need a function *on_pick_scotch* that will display a table of the top 5 similar scotches that Radar View watches, based on a given selected Scotch.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def on_pick_scotch(Scotch): def on_pick_scotch(Scotch):
name = Scotch name = Scotch
# Get top 6 similar whiskeys, and remove this one # Get top 6 similar whiskeys, and remove this one
top_df = get_similar(name, 6).iloc[1:] top_df = get_similar(name, 6).iloc[1:]
# Get bottom 5 similar whiskeys # Get bottom 5 similar whiskeys
df = top_df df = top_df
# Make table index a set of links that the radar widget will watch # Make table index a set of links that the radar widget will watch
df.index = ['''<a class="scotch" href="#" data-factors_keys='["{}","{}"]'>{}</a>'''.format(name, i, i) for i in df.index] df.index = ['''<a class="scotch" href="#" data-factors_keys='["{}","{}"]'>{}</a>'''.format(name, i, i) for i in df.index]
tmpl = f'''<p>If you like {name} you might want to try these five brands. Click one to see how its taste profile compares.</p>''' tmpl = f'''<p>If you like {name} you might want to try these five brands. Click one to see how its taste profile compares.</p>'''
prompt_w.value = tmpl prompt_w.value = tmpl
table.value = df.to_html(escape=False) table.value = df.to_html(escape=False)
radar_w.factors_keys = [name] radar_w.factors_keys = [name]
plot = radar_w.factors_keys_changed() plot = radar_w.factors_keys_changed()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
prompt_w = widgets.HTML(value='Aberfeldy') prompt_w = widgets.HTML(value='Aberfeldy')
display(prompt_w) display(prompt_w)
``` ```
%% Output %% Output
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
table = widgets.HTML( table = widgets.HTML(
value="Hello <b>World</b>" value="Hello <b>World</b>"
) )
display(table) display(table)
``` ```
%% Output %% Output
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
radar_w = RadarWidget(df=features_df) radar_w = RadarWidget(df=features_df)
``` ```
%% Output %% Output
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
picker_w = widgets.interact(on_pick_scotch, Scotch=list(sim_df.index)) picker_w = widgets.interact(on_pick_scotch, Scotch=list(sim_df.index))
``` ```
%% Output %% Output
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
radar_w.factors_keys radar_w.factors_keys
``` ```
%% Output %% Output
['Aberfeldy'] ['Aberfeldy']
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Powered by data from https://www.mathstat.strath.ac.uk/outreach/nessie/nessie_whisky.html and inspired by analysis from http://blog.revolutionanalytics.com/2013/12/k-means-clustering-86-single-malt-scotch-whiskies.html. This dashboard originated as a Jupyter Notebook. Powered by data from https://www.mathstat.strath.ac.uk/outreach/nessie/nessie_whisky.html and inspired by analysis from http://blog.revolutionanalytics.com/2013/12/k-means-clustering-86-single-malt-scotch-whiskies.html. This dashboard originated as a Jupyter Notebook.
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
......
This diff is collapsed.