brief = B[:8, :8]
average = 0
for i in range(8):
average += sum(brief[i])
average -= brief[0][0]
average /= 64
print(average, 'is average\n\n')
for i in range(0, 8):
for j in range(0, 8):
if brief[i][j] >= average:
brief[i][j] = 0
print(brief[i][j])
if brief[i][j] < average:
brief[i][j] = 1
print(brief[i][j])
for b in range(8):
for bb in range(8):
print(brief[b][bb])
简要是B矩阵的8x8部分,其值如下:
[[-79.59774442081138, 158.7202095869316, 112.56820969421881, -59.04458113044859, 110.79507835753822, 65.46782110183209, -1.3342162705874172, 25.437988304756363],
[322.5377343539482, 8659.5625, 456.1372383004441, 1039.2670348171582, 502.43880254201326, 201.0009891626345, 209.16093093311468, -208.50047755598916],
[-112.56820969421881, 224.46427302053877, -159.19548884162273, -83.50164741931893, 156.6879024574203, 92.58548050122643, -1.8868667450035765, 35.974748060074624],
[37.72684830839333, 624.7408144869114, 53.3538205433223, -449.0466120624775, 32.59027278985499, -43.22267150470408, -85.65936443956468, 219.2142421380014],
[-28.001674523528344, 249.9757286483847, -39.60034788033096, 31.11673361617372, 130.48068801733655, -160.1201473099224, -112.78964698763158, -158.7758135283589],
[54.7357856623738, -359.607876182155, 77.40809043087583, 46.68603856944288, -89.88043774029418, 282.9769031346061, 27.867634849967388, -202.2926232693297],
[15.271034089670682, 70.44041778968491, 21.596503521074148, 7.9278092841192915, -37.515872842738105, 27.38507374399468, -10.177891707673886, -118.39173788971809],
[20.304659220046318, -234.8242040396121, 28.71512444835341, 146.63941111295003, -59.20821857294881, -73.98127851564756, 121.3747483435147, 17.835871018082692]]
然后我找到平均值,如果简要的每个元素大于(或等于)平均值,则将其更改为0,如果小于平均值,则将其更改为1。在比较 brief 元素以求平均值的循环中,我还打印了新值。因此输出为1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 etc...
。如您所见,有0和1。但!当我在该循环之后打印简报时,我得到的所有元素都为 1 。输出有什么问题?
P.S。 B被创建为 np.zeros ,然后填充数字
答案 0 :(得分:2)
用以下几行替换双if
语句:
for i in range(0, 8):
for j in range(0, 8):
if brief[i][j] >= average:
brief[i][j] = 0
else:
brief[i][j] = 1
问题在于,在代码中您首先分配了brief[i][j] = 0
,然后再次签入第二条if
语句if brief[i][j] < average:
,这意味着if 0 < average
,这对于您的average
值,因此一切变成1