我是Matlab / Octave用户。 Numpy文档说array
更适合使用而不是matrix
。有没有一种方便的方法来处理秩-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 = np.concatenate((np.ones((m, 1)), X), axis = 1)
>>> ValueError: all the input arrays must have same number of dimensions
更慢,而不重塑X:
np.reshape(y, (-1, 1))
X - y,如果不重塑y [self performSegueWithIdentifier: @"MySegue" sender: self];
答案 0 :(得分:3)
等同于np.reshape(y, (-1, 1))
的更简单的是y[:, np.newaxis]
。由于np.newaxis
是None
的别名,y[:, None]
也有效。值得一提的是np.expand_dims(y, axis=1)
。