使用标准Python数组,我可以执行以下操作:
arr = []
arr.append([1,2,3])
arr.append([4,5,6])
# arr is now [[1,2,3],[4,5,6]]
Run Code Online (Sandbox Code Playgroud)
但是,我不能在numpy做同样的事情.例如:
arr = np.array([])
arr = np.append(arr, np.array([1,2,3]))
arr = np.append(arr, np.array([4,5,6]))
# arr is now [1,2,3,4,5,6]
Run Code Online (Sandbox Code Playgroud)
我也研究过vstack,但是当我vstack在一个空阵列上使用时,我得到:
ValueError: all the input array dimensions except for the concatenation axis must match exactly
Run Code Online (Sandbox Code Playgroud)
那么如何在numpy中向空数组添加一个新行呢?
我正在尝试运行第二页上显示的代码:
在代码的底部,您必须添加以下行:
simFlips(100,100)
show()
Run Code Online (Sandbox Code Playgroud)
这是我在ubuntu上运行时遇到的错误:
Traceback (most recent call last):
File "coin.py", line 36, in <module>
simFlips(100,100)
File "coin.py", line 16, in simFlips
diffs.append(abs(heads - tails))
AttributeError: 'numpy.ndarray' object has no attribute 'append'
Run Code Online (Sandbox Code Playgroud)
请告诉我我做错了什么给了我最后一个错误.提前致谢!
我想追加如下所示的 numpy 数组
Run Code Online (Sandbox Code Playgroud)A: [[1,2,3],[2,3,1],[1,4,2]] B: [[1,3,3],[3,3,1],[1,4,5]] A+B = [[[1,2,3],[2,3,1],[1,4,2]], [[1,3,3],[3,3,1],[1,4,5]]]
我怎样才能做到这一点?
====================
从注释中复制的代码,并为清晰起见进行了格式化:
X = np.empty([54, 7])
for seq in train_set:
print(seq)
temp = dp.set_xdata(seq) #make 2d numpy array
print(temp.shape)
X = np.row_stack((X[None], temp[None]))
X = np.delete(X, 0, 0)
print("X: ",X)
ValueError: all the input arrays must have same number of dimensions.
Run Code Online (Sandbox Code Playgroud) 我一直在Python中使用包含一维值列表的数组.到目前为止,我一直在使用array.append(value)函数一次向数组中添加一个值.
现在,我想将另一个数组中的所有值添加到主数组中.换句话说,我不想一次添加一个值.辅助阵列收集十个值,当收集这些值时,它们都被转移到主阵列.问题是,我不能简单地使用代码'array.append(other_array)',因为我收到以下错误:
unsupported operand type(s) for +: 'int' and 'list'
Run Code Online (Sandbox Code Playgroud)
我哪里错了?