【发布时间】:2017-12-27 18:58:27
【问题描述】:
我正在使用 python-3.x,我想创建一个名为 result_array 的 numpy 数组,用于保存每个循环的二进制结果(minimum_array)
# 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.array([result_array, minimum_arrray])应该可以工作。np.vstack将一个元组作为输入np.vstack((result_array, minimum_arrray)) -
@Eskapp 我收到此错误
NameError: name 'result_array' is not defined我不知道为什么?
标签: arrays python-3.x numpy binary