【问题标题】:code produces a 2d histogram but the results dont match with hist2d代码生成二维直方图,但结果与 hist2d 不匹配
【发布时间】:2018-05-18 01:38:55
【问题描述】:

我正在尝试编写一个直方图构建器来为我的作业构建一个二维直方图。这是[我的代码][1]:

def Build2DHistogramClassifier(X1,X2,T,B,x1min,x1max,x2min,x2max):
 HF=np.zeros((B,B),dtype='int');#initialising a empty array of integer type
 HM=np.zeros((B,B),dtype='int');
 bin_row_indices=(np.round(((B-1)*(X1-x1min)/(x1max-x1min)))).astype('int32');"""this logic decides which bin the value goes into"""
 bin_column_indices=(np.round(((B-1)*(X2-x2min)/(x2max-x2min)))).astype('int32');"""np.round-->applies the formula to all the values in the array"""
 for i,(r,c) in enumerate(zip(bin_row_indices, bin_column_indices)):
     """enumerate-->if we put array or list into it gives output with index/count i """
     if T[i]=='Female':
         HF[r,c]+=1;
     else:
         HM[r,c]+=1;
return [HF, HM]

但问题是我得到的结果(每个 bin 中的计数)与我在 numpy 中使用 hist2d 函数得到的结果不匹配(我传递了相同的 bin 大小)

如果我的代码格式不正确,我很抱歉。请单击指向我使用相同代码创建的要点的超链接。

我的代码有什么错误?

如何改正?

谢谢

【问题讨论】:

    标签: python-3.x numpy


    【解决方案1】:

    在分配给 bin 时通过四舍五入,您将 bin 视为 bin 中心。 numpy 约定是将它们用作 bin 边缘。

    从您的代码中删除对round() 的两个调用,并将B-1 更改为B。您现在应该使用您的函数和 np.histogram2d 获得相同的结果。

    【讨论】:

      猜你喜欢
      • 2010-09-30
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      • 2015-08-07
      相关资源
      最近更新 更多