我使用python-3.x中,我想创建一个numpy的阵列称为result_array从每个回路保持二进制结果(minimum_arrray)添加结果到numpy的阵列
# The code:
import random
import numpy as np
np.set_printoptions(threshold=np.nan)
i = 0
ii= 0
e = 2
y = 2
x = 2
bin_arrray_size = 5
# result_array = np.ndarray(shape=(), dtype=int)
# result_array = np.ndarray(shape=(), dtype=int)
for ii in range (e):
bin_arrray = np.random.randint (2, size=(bin_arrray_size, y*x))
print ("bin_arrray:" '\n', bin_arrray)
flot= np.zeros ((bin_arrray_size, 1))
for i in range (bin_arrray_size):
X = bin_arrray[i]
decimal=int(''.join(map(str,X[:].tolist())), 2)
flot[i] = (decimal * 2.324)
# print ("flot:" '\n', flot)
for flot in np.nditer(flot, flags=['external_loop','buffered'], order='F'):
print (" the flot :" '\n', flot)
minimum_arrray = bin_arrray[flot.argpartition(0)[:1]]
print ("Minimum_arrray:" '\n', minimum_arrray)
# result_array = np.vstack (result_array, minimum_arrray)
print ("bin_arrray type:" '\n',type(bin_arrray))
print("bin_arrray shape:" '\n', bin_arrray.shape)
print ("flot type:" '\n', type(flot))
print("flot shape:" '\n', flot.shape)
print ("minimum_arrray type:" '\n', type(minimum_arrray))
print("minimum_arrray shape:" '\n', minimum_arrray.shape)
########## the Result #######
bin_arrray:
[[1 0 0 0]
[0 0 0 1]
[0 1 0 0]
[0 1 0 1]
[1 0 1 1]]
the flot :
[ 18.592 2.324 9.296 11.62 25.564]
Minimum_arrray:
[[0 0 0 1]] ################ First result
bin_arrray:
[[0 1 0 0]
[0 1 0 1]
[0 0 0 1]
[1 0 0 1]
[1 0 0 0]]
the flot :
[ 9.296 11.62 2.324 20.916 18.592]
Minimum_arrray:
[[0 0 0 1]] ################ Second Result
bin_arrray type:
<class 'numpy.ndarray'>
bin_arrray shape:
(5, 4)
flot type:
<class 'numpy.ndarray'>
flot shape:
(5,)
minimum_arrray type:
<class 'numpy.ndarray'>
minimum_arrray shape:
(1, 4)
我已经试过这以前生产循环:
result_array = np.random.random((2,4))
这
result_array = np.ndarray(shape=(), dtype=int)
循环
result_array = np.vstack (result_array, minimum_arrray)
但他们没有工作
我想看到一个名为(result_array)新的数组什么后
,并期待这样的:
result_array:
[[0 0 0 1]
[0 0 0 1]]
'result_array = np。数组([result_array,minimum_arrray])'应该工作。 'np.vstack'需要一个元组作为输入'np.vstack((result_array,minimum_arrray))' – Eskapp
@Eskapp我得到这个错误'NameError:name'result_array'没有定义'我不知道为什么? – azeez