【发布时间】:2016-08-01 10:32:24
【问题描述】:
我是 Matlab/Octave 用户。 Numpy 文档说array 比matrix 更可取。有没有一种方便的方法来处理 rank-1 数组,而不需要不断地重塑它?
例子:
data = np.loadtxt("ex1data1.txt", usecols=(0,1), delimiter=',',dtype=None)
X = data[:, 0]
y = data[:, 1]
m = len(y)
print X.shape, y.shape
>>> (97L, ) (97L, )
我无法使用 concatenate、vstack、append 将新列添加到 X,除了速度较慢的 np.c_,无需重塑 X:
X = np.concatenate((np.ones((m, 1)), X), axis = 1)
>>> ValueError: all the input arrays must have same number of dimensions
X - y,如果不重塑 y 就无法完成 np.reshape(y, (-1, 1))
【问题讨论】:
-
这个问题是关于串联还是关于 rank 1 数组?
-
我一般会考虑排名 1 的数组。
-
我一直使用
np.arange(10).reshape(2,5)这样的表达方式。
标签: python arrays matlab numpy rank