Python 向numpy数组添加行:';numpy.ndarray和#x27;对象没有属性';追加';或';vstack&x27;
我有一个2D numpy数组,我想在其末尾附加行 我尝试了Python 向numpy数组添加行:';numpy.ndarray和#x27;对象没有属性';追加';或';vstack&x27;,python,arrays,numpy,append,Python,Arrays,Numpy,Append,我有一个2D numpy数组,我想在其末尾附加行 我尝试了append和vstack两种方法,但无论哪种方法都会出现如下错误: 'numpy.ndarray' object has no attribute 'vstack' 或者它说没有属性append 这是我的代码: g = np.zeros([m, no_features]) # then somewhere in the middle of a for-loop I build a new array called temp_g # a
append
和vstack
两种方法,但无论哪种方法都会出现如下错误:
'numpy.ndarray' object has no attribute 'vstack'
或者它说没有属性append
这是我的代码:
g = np.zeros([m, no_features])
# then somewhere in the middle of a for-loop I build a new array called temp_g
# and try to append it to g. temp_g is a 2D array
g.vstack((temp_g,g))
我做了g.append(temp_g)
但是没有区别,我得到了相同的错误,说没有这样的属性
我第一次声明数组g
的方式是否有问题 >>将numpy作为np导入
>>> import numpy as np
>>> a = np.array([1, 2, 3])
>>> b = np.array([5, 6, 7])
>>> np.vstack(a, b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: vstack() takes exactly 1 argument (2 given)
>>>
>>> np.vstack((a, b))
array([[1, 2, 3],
[5, 6, 7]])
>>>a=np.array([1,2,3])
>>>b=np.数组([5,6,7])
>>>np.vstack(a,b)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:vstack()正好接受1个参数(给定2个)
>>>
>>>np.vstack((a,b))
数组([[1,2,3],
[5, 6, 7]])
vstack是一个numpy函数,它只接受元组形式的单个参数。您需要调用它并将输出分配给变量。所以与其说是g.vstack((temp_g,g))
使用
g=np.vstack((temp_g,g))
vstack
是一个numpy函数,而不是ndarray
实例方法np.vstack((g,temp-g))