太實(shí)用了!4種方法教你輕松制作交互式儀表板!
↓推薦關(guān)注↓
讓客戶深刻記住你的數(shù)據(jù)洞察和發(fā)現(xiàn)的最好方式,是創(chuàng)建交互式儀表板。為什么要互動(dòng)呢?一方面是比較有趣,另一方面是客戶對動(dòng)作的記憶比靜態(tài)的洞察力更深刻。
在本文中,我給大家分享 4 款 Python 工具包,使用它們?yōu)閿?shù)據(jù)科學(xué)項(xiàng)目創(chuàng)建交互式儀表板非常的棒。喜歡本文記得收藏、關(guān)注、點(diǎn)贊。
1、Widgets
Ipywidgets(縮寫為 Widgets) 是一個(gè)代碼簡單直觀的交互式包,它為 Jupyter Notebooks 中的 GUI 提供 HTML 架構(gòu)。
該包允許我們直接在 Jupyter Notebook 單元中創(chuàng)建交互式儀表板。

只需幾行代碼,你就可以將 Jupyter Notebook 改為儀表板。讓我用幾行代碼展示如何做到這一點(diǎn)。
首先,我們需要安裝所需的包
pip?install?ipywidgets
然后,我們需要在 Jupyter Notebook 中啟用 Ipywidgets。要啟用它,請?jiān)诿钐崾痉袀鬟f以下代碼。
jupyter?nbextension?enable?--py?widgetsnbextension
我們可以在 Jupyter Notebook 中創(chuàng)建交互式儀表板,并配備所有必要的軟件包。我將使用泰坦尼克號樣本數(shù)據(jù)進(jìn)行舉例。
import?seaborn?as?sns
titanic?=?sns.load_dataset('titanic')
titanic.head()

我想創(chuàng)建一個(gè)交互式儀表板,獲取按類別變量分組的泰坦尼克號票價(jià)平均值。在這種情況下,使用如下代碼:
#Creating?the?interactive?dashboard
from?ipywidgets?import?interact
@interact
def?create_fare_plot(col?=?titanic.drop(['fare',?'age'],?axis?=1).columns):
????sns.barplot(data?=?titanic,?x?=?col,?y?='fare')
????plt.title(f'Mean?Bar?Plot?of?the?Fare?grouped?by?the?{col}')

通過添加@interact代碼,我們啟動(dòng)了交互過程。
2、Voila
Voila-dashboards 是一個(gè)簡單的 Python 包,它將一個(gè)簡單的 Jupyter Notebook 變成一個(gè)漂亮的 Web 儀表板。
只需一行安裝代碼,我們就可以快速渲染 Jupyter Notebook。
讓我們安裝 Voila-dashboards
pip?install?voila
完成 Voila 包的安裝后,刷新 Jupyter Notebook 并查看 notebook 選項(xiàng)卡。在那里你會發(fā)現(xiàn)一個(gè)新的 Voila 按鈕。
現(xiàn)在按下按鈕,即可自動(dòng)生成 Voila 儀表板。
3、Dash by Plotly
Dash by Plotly 是一個(gè)開源 Python 包,它是基于 Plotly 可視化的低代碼框架包。
要試用 Dash,先安裝軟件包。
pip?install?dash
安裝完成后,我將使用以下代碼創(chuàng)建一個(gè)簡單的 Titanic 儀表板。
import?dash
from?dash?import?dcc,?html
import?plotly.express?as?px
import?pandas?as?pd
import?seaborn?as?sns
app?=?dash.Dash()
df?=?sns.load_dataset('titanic')
fig?=?px.scatter(
df,
x="fare",
y="age",
size="pclass",
color="alive",
hover_name="embark_town",
log_x=True,
size_max=60
)
app.layout?=?html.Div(children?=?[
html.H1(children='Titanic?Dashboard'),
dcc.Graph(id="fare_vs_age",?figure=fig)])
if?__name__?==?"__main__":
????app.run_server(debug=True)
運(yùn)行上述代碼后,將在默認(rèn)(http://127.0.0.1:8050/)中啟動(dòng)儀表板
我們可以添加一個(gè)回調(diào)交互來讓用戶輸入具有特定的輸出。
import?dash
from?dash?import?dcc,?html,?Input,?Output
import?plotly.express?as?px
import?pandas?as?pd
import?seaborn?as?sns
app?=?dash.Dash()
df?=?sns.load_dataset('titanic')
fig?=?px.scatter(
df,
x="fare",
y="age",
size="pclass",
color="alive",
hover_name="embark_town",
log_x=True,
size_max=60
)
app.layout?=?html.Div(children?=?[
html.H1(children='Titanic?Dashboard'),
dcc.Graph(id="fare_vs_age",?figure=fig),
#Add?interactive?callback?here
html.H4("Change?the?value?in?the?text?box?to?see?callbacks?in?action"),
html.Div([
"Input:?",
dcc.Input(id='my-input',?value='initial?value',?type='text')
]),
html.Br(),
html.Div(id='my-output'),
])
@app.callback(
Output(component_id='my-output',?component_property='children'),
Input(component_id='my-input',?component_property='value')
)
def?update_output_div(input_value):
????return?f'Output:?{input_value}'
if?__name__?==?"__main__":
???app.run_server(debug=True)

Dash by Plotly 在創(chuàng)建儀表板時(shí)非常方便,它提供了許多有用的 API。
4、Streamlit
Streamlit 是一個(gè)開源 Python 包,旨在為數(shù)據(jù)科學(xué)家和機(jī)器學(xué)習(xí)項(xiàng)目創(chuàng)建一個(gè) Web 應(yīng)用程序。
Streamlit 提供的 API 易于任何初學(xué)者使用,非常適合希望以交互方式構(gòu)建其數(shù)據(jù)組合的任何人。
讓我們先安裝 Streamlit 包。
pip?install?streamlit
安裝過程完成后,我們可以創(chuàng)建交互式儀表板。
讓我給你下面的代碼示例。
import?streamlit?as?st
import?pandas?as?pd
import?plotly.express?as?px
import?seaborn?as?sns
df?=?sns.load_dataset('titanic')
st.title('Titanic?Dashboard')
st.subheader('Dataset')
st.dataframe(df)
st.subheader('Data?Numerical?Statistic')
st.dataframe(df.describe())
st.subheader('Data?Visualization?with?respect?to?Survived')
left_column,?right_column?=?st.columns(2)
with?left_column:
???'Numerical?Plot'
????num_feat?=?st.selectbox(
???'Select?Numerical?Feature',?df.select_dtypes('number').columns)
????fig?=?px.histogram(df,?x?=?num_feat,?color?=?'survived')
????st.plotly_chart(fig,?use_container_width=True)
with?right_column:
???'Categorical?column'
????cat_feat?=?st.selectbox(
????'Select?Categorical?Feature',?df.select_dtypes(exclude?=???'number').columns)
????fig?=?px.histogram(df,?x?=cat_feat,?color?=?'survived'?)
st.plotly_chart(fig,?use_container_width=True)
使用 VScode 將文件保存為 titanic_st.py,然后在終端中運(yùn)行該代碼。
streamlit?run?titanic_st.py

Streamlit 在上述地址上運(yùn)行,我們可以訪問我們的儀表板。

使用上面的簡單代碼,我們創(chuàng)建了一個(gè)交互式儀表板,API 并不難理解,我們只使用最少數(shù)量的代碼。
結(jié)論
當(dāng)我們需要展示數(shù)據(jù)科學(xué)項(xiàng)目時(shí)建議用交互式儀表板,它將改善大大改善用戶體驗(yàn)。
長按或掃描下方二維碼,后臺回復(fù):加群,即可申請入群。一定要備注:來源+研究方向+學(xué)校/公司,否則不拉入群中,見諒!
(長按三秒,進(jìn)入后臺)
推薦閱讀

