我有两个数组:
import numpy as np
a = np.array([[1,2,3], [4,5,6]])
b = np.array([5, 6])
当我尝试使用以下代码将b
附加到a
的最后一列时,将a
附加到 np.hstack([a, b])
it thows an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<__array_function__ internals>", line 6, in hstack
File "C:\Users\utkal\anaconda3\envs\AIML\lib\site-packages\numpy\core\shape_base.py", line 345, in hstack
return _nx.concatenate(arrs, 1)
File "<__array_function__ internals>", line 6, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
1 2 3 5
4 5 6 6
我希望最终数组为:
var templates = DriveApp.getFilesByName('Template'); // get the files named Template (only one)
var template = templates.next(); // get the first files in the list
var newFile = template.makeCopy(name,newFolder); // Make a copy of the Template file and put it in NewFolder
var ss = SpreadsheetApp.open(newFile);
var sheet = ss.getSheets()[0];
sheet.getActiveRange('B1').setValue(name);
答案 0 :(得分:3)
您可以这样做:
np.hstack((a,b.reshape(2,1)))
array([[1, 2, 3, 5],
[4, 5, 6, 6]])
答案 1 :(得分:0)
np.hstack
是{= 1轴上的np.concatenate
的快捷方式。因此,这要求两个数组都具有相同的形状,并且所有尺寸都必须匹配,除了连接轴上。在您的情况下,既需要二维数组又需要匹配维度0
所以你需要
np.hstack([a, b[:,None]])
Out[602]:
array([[1, 2, 3, 5],
[4, 5, 6, 6]])
或使用np.concatenate
np.concatenate([a, b[:,None]], axis=1)
Out[603]:
array([[1, 2, 3, 5],
[4, 5, 6, 6]])