首页 > 解决方案 > 在python中比较多维分布的测试

问题描述

我有以下数据集:

    import random
    import pandas as pd
    A = pd.DataFrame({'x':[random.uniform(0, 1) for i in range(0,100)], 'y':[random.uniform(0, 1) for i in range(0,100)]})
    B = pd.DataFrame({'x':[random.uniform(0, 1) for i in range(0,100)], 'y':[random.uniform(0, 1) for i in range(0,100)]})

从这两个数据集中,我可以生成以下图

    import matplotlib.pyplot as plt
    import scipy.stats as st
    
    def plot_2d_kde(df):
        # Extract x and y
        x = df['x']
        y = df['y']
        # Define the borders
        deltaX = (max(x) - min(x))/10
        deltaY = (max(y) - min(y))/10
        xmin = min(x) - deltaX
        xmax = max(x) + deltaX
        ymin = min(y) - deltaY
        ymax = max(y) + deltaY
    
        # Create meshgrid
        xx, yy = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
    
        # We will fit a gaussian kernel using the scipy’s gaussian_kde method
        positions = np.vstack([xx.ravel(), yy.ravel()])
        values = np.vstack([x, y])
        kernel = st.gaussian_kde(values)
        f = np.reshape(kernel(positions).T, xx.shape)
    
        fig = plt.figure(figsize=(13, 7))
        ax = plt.axes(projection='3d')
        surf = ax.plot_surface(xx, yy, f, rstride=1, cstride=1, cmap='coolwarm', edgecolor='none')
        ax.set_xlabel('x')
        ax.set_ylabel('y')
        ax.set_zlabel('PDF')
        ax.set_title('Surface plot of Gaussian 2D KDE')
        fig.colorbar(surf, shrink=0.5, aspect=5) # add color bar indicating the PDF
        ax.view_init(60, 35)

    plot_2d_kde(A)
    plot_2d_kde(B)

有没有办法测试多维 PDF 是否与 python 中的A不同B

标签: pythonpython-3.xscipystatistics

解决方案


推荐阅读