用您的数据洞察力和发现打动客户的最佳方式是创建交互式仪表板。为什么互动?一方面更有趣,另一方面,来访者对行动的记忆比静态的洞察更深刻。在本文中,我将与您分享4个Python工具包,它们非常适合为数据科学项目创建交互式仪表板。如果喜欢本文,记得收藏、关注、点赞哦。1.WidgetsIpywidgets(简称Widgets)是一个交互包,代码简单直观,为JupyterNotebooks中的GUI提供HTML架构。这个包允许我们直接在JupyterNotebook单元格中创建交互式仪表板。只需几行代码,您就可以将JupyterNotebook变成仪表板。让我用几行代码展示如何做到这一点。首先,我们需要安装所需的包。pipinstallipywidgets然后,我们需要在JupyterNotebook中启用Ipywidgets。要启用它,请在命令提示符中传递以下代码。jupyternbextensionenable--pywidgetsnbextension我们可以使用所有必要的包在JupyterNotebook中创建交互式仪表板。我将使用泰坦尼克号示例数据作为示例。importseabornasnsstitanic=sns.load_dataset('titanic')titanic.head()我想创建一个交互式仪表板,获取按分类变量分组的泰坦尼克号平均票价。在这种情况下,使用如下代码:#Creatingtheinteractivedashboardfromipywidgetsimportinteract@interactdefcreate_fare_plot(col=titanic.drop(['fare','age'],axis=1).columns):sns.barplot(data=titanic,x=col,y='fare')plt.title(f'MeanBarPlotoftheFaregroupedbythe{col}')通过添加@interact代码,我们开始交互过程。2.VoilaVoila-dashboards是一个简单的Python包,可以将简单的JupyterNotebook变成漂亮的Web仪表板。只需一行设置代码,我们就可以快速渲染JupyterNotebooks。让我们安装Voila-dashboards。pipinstallvoila完成安装Voila包后,刷新JupyterNotebook并查看笔记本选项卡。在那里你会发现一个新的Voila按钮。Voila仪表板现在只需按一下按钮即可自动生成。3.DashbyPlotlyDashbyPlotly是一个开源的Python包,是一个基于Plotly可视化的低代码框架包。要试用Dash,请先安装软件包。安装pipinstalldash后,我将使用以下代码创建一个简单的Titanic仪表板。从dash导入dcc,html导入plotly.expressaspximportpandasaspdimportseabornassnsapp=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='TitanicDashboard'),dcc.Graph(id="fare_vs_age",figure=fig)])if__name__=="__main__":app.run_server(debug=True)运行以上代码后,会在默认(http://127.0.0.1:8050/)启动仪表板。我们可以添加一个回调交互,让用户输入有特定的输出。importdashfromdashimportdcc,html,Input,Outputimportplotly.expressaspximportpandasaspdimportseabornassnsapp=dash.Dash()df=sns.load_dataset('titanic')fig=px.scatter(df,x="票价",y="age",size="pclass",color="alive",hover_name="embark_town",log_x=True,size_max=60)app.layout=html.Div(children=[html.H1(children='TitanicDashboard'),dcc.Graph(id="fare_vs_age",figure=fig),#Addinteractivecallbackherehtml.H4("Changethevalueinthetextboxtoseecallbacksinaction"),html.Div(["Input:",dcc.Input(id='my-input',value='initialvalue',type='text')]),html.Br(),html.Div(id='my-output'),])@app.callback(Output(component_id='my-output',component_property='children'),输入(component_id='my-input',component_property='value'))defupdate_output_div(input_value):returnf'Output:{input_value}'if__name__=="__main__":app.run_server(debug=True)DashbyPlotly在创建仪表板时非常方便,它提供了很多有用的API。4.StreamlitStreamlit是一个开源Python包,旨在为数据科学家和机器学习项目创建Web应用程序。Streamlit提供的API对于任何初学者来说都很容易使用,并且非常适合希望以交互方式构建其数据组合的任何人。让我们先安装Streamlit包。pipinstallstreamlit安装过程完成后,我们就可以创建交互式仪表板了。让我给你下面的代码示例。导入streamlit作为stimportpandas作为pdimportplotly.express作为pximportseaborn作为snsdf=sns.load_dataset('titanic')st.title('TitanicDashboard')st.subheader('Dataset')st.dataframe(df)st.subheader('DataNumericalStatistic')st.dataframe(df.describe())st.subheader('DataVisualizationrespecttoSurvived')left_column,right_column=st.columns(2)withleft_column:'NumericalPlot'num_feat=st.selectbox('选择数值特征',df.select_dtypes('number').columns)fig=px.histogram(df,x=num_feat,color='survived')st.plotly_chart(fig,use_container_width=True)withright_column:'分类列'cat_feat=st.selectbox('选择分类特征',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,然后在终端中运行代码。streamlitruntitanic_st.pyStreamlit在上述地址上运行,我们可以访问我们的仪表板。使用上面的简单代码,我们创建了一个交互式仪表板,API不难理解,而且我们只使用了最少的代码。结论当我们需要展示数据科学项目时,建议使用交互式仪表板,这将大大改善用户体验。
