可视化自助法

用 Python 提升数据可视化

Nick Strayer

Instructor

简单置信带,带有用箭头标注的上下边界

用 Python 提升数据可视化

一条钟形曲线

用 Python 提升数据可视化

由大量黑白点组成的总体,抽取了一个较小样本

用 Python 提升数据可视化

对一个小样本的黑白点多次重抽样,生成同等规模的新数据集

用 Python 提升数据可视化

多组黑白点对应到一组估计值,表示黑白比例的估计

用 Python 提升数据可视化
denver_may = pollution.query("city == 'Denver' & month == 8")

# Perform bootstrapped mean on a vector
def bootstrap(data, n_boots): 
    return [np.mean(np.random.choice(data,len(data))) 
            for _ in range(n_boots) ]

# Generate 1,000 bootstrap samples
boot_means = bootstrap(denver_may.NO2, 1000)


# Get lower and upper 95% interval bounds lower, upper = np.percentile(boot_means, [2.5, 97.5]) # Shaded background of interval plt.axvspan(lower, upper, color='grey', alpha=0.2)
# Plot histogram of samples sns.histplot(boot_means, bins = 100)
用 Python 提升数据可视化

细分直方图,x 轴上一段浅灰色区域高亮

用 Python 提升数据可视化
# Make dataframe of bootstraped data
denver_may_boot = pd.concat([
    denver_may.sample(n=len(denver_may), replace=True).assign(sample=i) 
    for i in range(100)])

# Plot regressions for each sample
sns.lmplot('CO', 'O3', data=denver_may_boot, scatter=False,

# Tell seaborn to draw a regression # line for each resample's data hue='sample',
# Make lines orange and transparent line_kws = {'color': 'coral', 'alpha': 0.2},
# No confidence intervals ci=None, legend = False)
用 Python 提升数据可视化

一系列相似的向下倾斜的直线

用 Python 提升数据可视化
aug_pol = pollution.query("month == 8")

# Holder DataFrame for bootstrap samples
city_boots = pd.DataFrame()

for city in ['Denver', 'Long Beach', 'Houston', 'Indianapolis']:
    # Filter to city's NO2
    city_NO2 = aug_pol[aug_pol.city == city].NO2
    # Perform 100 bootstrap samples of city's NO2 & put in DataFrame
    cur_boot = pd.DataFrame({ 'NO2_avg': bootstrap(city_NO2, 100), 
                              'city': city })
    # Append to other city's bootstraps
    city_boots = pd.concat([city_boots,cur_boot])

# Use beeswarm plot to visualize bootstrap samples
sns.swarmplot(y="city", x="NO2_avg", data=city_boots, 

# Set all the colors to be the same color='coral')
用 Python 提升数据可视化

四个蜂群图,其中某组在 x 轴上明显更靠右

用 Python 提升数据可视化

开始(重)抽样

用 Python 提升数据可视化

Preparing Video For Download...