【发布时间】: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