作者:韩信子@ShowMeAI
数据分析实战系列:https://www.showmeai.tech/tutorials/40
本文地址:https://www.showmeai.tech/article-detail/320
申明:版权所有,转载请分割平台与作者并注明出处
珍藏ShowMeAI查看更多精彩内容

大家都看过十分酷的实时数据看板,能用最直观的形式给到咱们业务数据的信息,如下图所示。

而在 Python 中,咱们也有十分易用的工具,能够产出丑陋的数据分析可视化后果,并反对交互式操作和勾选部分数据深入分析,ShowMeAI在本篇内容中,将给大家解说到 Altair 这样一个功能强大的 Python 交互式数据分析工具,它能产出如下图所示的交互剖析后果:

数据分析实现模板

为了让大家在本人的数据上体验 Altair 的剖析后果,咱们上面编写的一个函数模板,用于为数据集中的所有特色生成交互式图表。

具体一点说,咱们心愿它为数值型字段(特色)返回『直方图+散点图』,为类别型特色返回『柱状图+箱线图』,Altair 返回的这些图表后果都是能够交互式操作的。

# 导入工具库import altair as altimport pandas as pd# 忽律数据规模限度alt.data_transformers.enable('default', max_rows=None)# 构建chart函数,它读取数据和字段名称,返回一个交互式图表后果def chart(dataset, column_name, target_var):    w = 500    single = alt.selection_single()        # 灰度图与柱状图        # 如果是类别型字段,咱们不必分桶    if (column_name in dataset.select_dtypes(include='object').columns.to_list()):        a = alt.Chart(dataset).mark_bar().encode(        alt.X(column_name + ':N', bin=False),        alt.Y('count()'),        color = alt.condition(single, alt.value('#4c78a8'), alt.value('lightgray')),        tooltip=['count()', alt.Tooltip(column_name, bin=False)]        ).add_selection(single).properties(width=w)            # 如果是数值型字段,咱们先分桶    else:        a = alt.Chart(dataset).mark_bar().encode(        alt.X(column_name + ':Q', bin=True),        alt.Y('count()'),        color = alt.condition(single, alt.value('#4c78a8'), alt.value('lightgray')),        tooltip=['count()', alt.Tooltip(column_name, bin=True)]        ).add_selection(single).properties(width=w)        # 对于类别型字段,咱们构建它和指标字段的一个箱线图表;对于数值型字段,咱们构建它们和指标字段的散点分布图    try:                if (column_name in dataset.select_dtypes(include='object').columns.to_list()):            b = alt.Chart(dataset).mark_boxplot().encode( #, title="Boxplot of " + column_name            alt.X(column_name + ':N'),            alt.Y(target_var),            color = alt.condition(single, alt.value('#4c78a8'), alt.value('lightgray')),            tooltip=[target_var]            ).add_selection(single).properties(width=w)        else:            b = alt.Chart(dataset).mark_point().encode(            alt.X(column_name + ':Q'),            alt.Y(target_var),            color = alt.condition(single, alt.value('#4c78a8'), alt.value('lightgray')),            tooltip=[target_var]            ).add_selection(single).properties(width=w)    except:        pass        return(a | b)

大家能够把它利用在本人的数据上,失去的后果图如下所示(而且它们是能够用鼠标交互操作的)。在上面的内容里,咱们会通知大家如何把后果存储为 html 报告,大家每次关上 html 后果文件,即可进行交互式操作,而无需每次都从新剖析。

数据分析&交互文档报告

上面的代码能够将所有可视化后果编译到一个 html 文档中,关上这个 html 文件,大家就能够失去一个可交互的可视化数据分析平台。

# 把所有的altair图表增加到一个列表里myl = []for col in dataset.columns:    try:        myl.append(chart(dataset, col, target_var))    except:        pass    else:        pass# 编译所有的图表到1个html文件中a = myl[0]for i in range(1,len(myl)):    a = a & myl[i]a.properties(    title = 'Feature Histograms & Boxplots').configure_axis(labelFontSize=15, titleFontSize=25)a.save('figures/Feature_Visuals.html')# 查看是否所有的字段都能够被可视化print('Features accounted for:', len(myl), 'out of', len(dataset.T))

参考资料

  • Altair:https://altair-viz.github.io/