I am Matlab/Octave user. Numpy documentation says the array
is much more advisable to use rather than matrix
. Is there a convenient way to deal with rank-1 arrays, without reshaping it constantly?
Example:
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, )
I can't add new column to X using concatenate
, vstack
, append
, except np.c_
which is slower, without reshaping X:
X = np.concatenate((np.ones((m, 1)), X), axis = 1)
>>> ValueError: all the input arrays must have same number of dimensions
X - y, couldn't be done without reshaping y np.reshape(y, (-1, 1))
It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy dimensions are called axes. The number of axes is rank. For example, the coordinates of a point in 3D space [1, 2, 1] is an array of rank 1, because it has one axis.
To rank items in NumPy, we can use a special method called numpy. argsort() . In the numpy. argsort() method, indices are used to sort arrays in NumPy.
Axis 1 is the direction along the columns In a multi-dimensional NumPy array, axis 1 is the second axis. When we're talking about 2-d and multi-dimensional arrays, axis 1 is the axis that runs horizontally across the columns.
The rank of an array is the number of dimensions it has. A scalar is considered to have rank zero.
A simpler equivalent to np.reshape(y, (-1, 1))
is y[:, np.newaxis]
. Since np.newaxis
is an alias for None
, y[:, None]
also works. It's also worth mentioning np.expand_dims(y, axis=1)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With