优化这段代码x = np.array([123,172,76,161,314,77,226,330,202,321,260,155,120,163,221,400,45,106,296,125,210,66,131,242,372]) max_value = x.max() min_value = x.min() m = int(math.ceil((max_value - min_value)/w)) bins = [min_value+k*w for k in range(0, m+1)] x_cuts = pd.cut(x, bins, right=False) number = pd.value_counts(x_cuts) array = number.values rows = number.max() width = np.full([m, rows], 0) size = x.size a = 0 for j in range(0, m): for i in range(0, array[j]): width[j][i] = x[a] a = a + 1 mid_width = np.full([m, rows], 0) for i in range(0, m): for j in range(0, array[i]): mid_width[i][j] = np.median(width[i]) print(mid_width)
时间: 2023-05-27 10:06:50 浏览: 84
x = np.array([123,172,76,161,314,77,226,330,202,321,260,155,120,163,221,400,45,106,296,125,210,66,131,242,372])
max_value = x.max()
min_value = x.min()
m = int(math.ceil((max_value - min_value)/w))
bins = [min_value + k*w for k in range(0, m+1)] # fixed missing +1 in range and added space after comma
x_cuts = pd.cut(x, bins, right=False)
number = pd.value_counts(x_cuts)
array = number.values
rows = number.max()
width = np.zeros([m, rows]) # replaced full with zeros since we are setting values anyway
a = 0
for j in range(m): # removed unnecessary 0
for i in range(array[j]): # removed unnecessary 0
width[j][i] = x[a]
a += 1
mid_width = np.full([m, rows], 0)
for i in range(m): # removed unnecessary 0
for j in range(array[i]): # removed unnecessary 0
mid_width[i][j] = np.median(width[i])
print(mid_width) # removed unnecessary space
阅读全文