【问题标题】:Create a Weighted Histogram in Excel在 Excel 中创建加权直方图
【发布时间】:2015-09-15 18:28:21
【问题描述】:

我正在使用 Excel 2013,并且我有以下格式的数据集:

Pt_Count    Min_Z        Max_Z
45028       2.02174      2.08377
91236       1.98366      2.0932
2439        1.56177      1.61484
6844       -1.07632     -1.04243
4575       -1.09536     -1.04628

我想制作一个加权直方图,其中 Min_Z 或 Max_Z 出现的次数乘以 Pt_Count。

换句话说,前两行的 Max_Z 值介于 2 和 2.5 之间,但我不希望直方图反映频率 2 我希望它反映频率 136264 (=45028+91236)。

这在 Excel 中是否可行?目前,如果我不尝试使用数据分析工具“加权”我的值,它工作正常。

【问题讨论】:

    标签: excel-2013


    【解决方案1】:

    这并不完全是在 MS Excel 中创建加权直方图的解决方案,而是实现相同结果的替代方法。使用 python 的 numpy 库来实现这一点更容易(编码效率不高,但希望有人可以改进它?):

    import numpy as np
    from matplotlib import pyplot as plt
    
    #Setting up empty lists
    minZ = []
    minZ_Weight = []
    
    maxZ = []
    maxZ_Weight = []
    
    #Choosing bin values 
    Bins = np.array([-1.3,-1.2,-1.1,-1.0,-0.9,-0.8,-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0])
    
    if __name__ == '__main__':
        data = open("Height_data.csv")
    
        data.readline()
        #read a text file line by line
        for line in data:
            item = line.split(",")#Items in a csv file are split according to ','
            #convert text file strings to floats and add to relevant list
            minZ += [float(item[6])]
            minZ_Weight += [float(item[2])]
    
            maxZ += [float(item[8])]
            maxZ_Weight += [float(item[2])]
        #Creating numpy array (keeps the same order as the values were put in)
        minH_Array = np.array(minH)
        minH_W_Array = np.array(minH_W)
    
        maxH_Array = np.array(maxH)
        maxH_W_Array = np.array(maxH_W)
    
        hist, bin_edges = np.histogram(a=minH_Array, bins = Bins, weights = minH_W_Array)
        hist2,bin_edges2 = np.histogram(a=maxH_Array, bins=Bins, weights=maxH_W_Array)
    
        #Plotting Min and Max Z values separately
         fig = plt.figure(figsize = plt.figaspect(0.45))
         ax = fig.add_subplot(1,2,1)
         plt.title("Minimum Heights")
         ax.bar(bin_edges[:-1],hist,width=0.1)
         ax.set_xlim(min(bin_edges),max(bin_edges))
         ax.set_ylim([0,100000])
    
         ax = fig.add_subplot(1,2,2)
         plt.title("Maximum Heights")
         ax.bar(bin_edges2[:-1],hist2,width=0.1)
         ax.set_xlim(min(bin_edges2),max(bin_edges2))
         ax.set_ylim([0,100000])
    
        #Appends new values to the end of the numpy array
        combined_H = np.append(arr=minH_Array,values=maxH_Array)
        combined_W = np.append(arr=minH_W_Array, values=maxH_W_Array)
    
        histC, bin_edgesC = np.histogram(a=combined_H, bins = Bins, weights = combined_W)
    
        #OR you can plot them on the same axis
        #Plotting Min and Max on same axis
        plt.title("Combined Histogram of Heights")
        plt.bar(bin_edgesC[:-1],histC,width=0.1)
        plt.xlim(min(bin_edges),max(bin_edges))
    
        plt.show()
    

    对于那些感兴趣的人来说,Matlab 是我能找到的唯一其他创建加权直方图的方法:Matlab Weighted Histogram(但我完全没有使用它的经验,因此选择了 Python)。

    【讨论】:

    • 干得好+1。 (完全披露,我无法验证。)我很好奇,当你发表原始帖子时,你有没有想过这个答案?我猜不是您的演示文稿。
    • 老实说一点也不。我的程序是使用 PCL(处理点云)用 C++ 编写的,由于我根本没有任何 C++ 经验,所以我觉得让 Excel 做我上面提到的事情会更容易。在偶然发现 python 替代方案后,我认为该解决方案与 Excel 一样容易/快速实现。
    【解决方案2】:

    回答您最初的问题“在 excel 中甚至可能吗?”是的,有可能。您需要在 Excel 中安装 Analysis Toolpak 插件。

    编程语言让它变得更容易。 R 可以在大约 3 行代码中完成,或者可能更少。

    【讨论】:

    • 我确实在 Excel 中启用了分析工具库,它允许我制作普通的直方图,但它不允许我制作加权直方图,或者至少我无法弄清楚.
    • 为此,您需要有一个附加列来定义范围,即 -1.5:2.0
    • @Bhush_Techidiot 我有同样的问题,我添加了额外的列。没有帮助。请您指出考虑权重的函数名称吗?到目前为止,即使激活了分析工具包,也只有不支持权重的 FREQUENCY 可用。
    猜你喜欢
    • 2013-11-19
    • 2018-04-16
    • 2020-04-13
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 2022-01-21
    • 2021-02-19
    • 2017-01-29
    相关资源
    最近更新 更多