Numpy排名1阵列

时间:2016-04-11 10:09:53

标签: python arrays matlab numpy rank

我是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, )

我无法使用concatenatevstackappend向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];

,就无法完成

1 个答案:

答案 0 :(得分:3)

等同于np.reshape(y, (-1, 1))的更简单的是y[:, np.newaxis]。由于np.newaxisNone的别名,y[:, None]也有效。值得一提的是np.expand_dims(y, axis=1)