我使用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