I often have two numpy
1d arrays, x
and y
, and would like to perform some quick sklearn fitting + prediction using them.
import numpy as np
from sklearn import linear_model
# This is an example for the 1d aspect - it's obtained from something else.
x = np.array([1, 3, 2, ...])
y = np.array([12, 32, 4, ...])
Now I'd like to do something like
linear_model.LinearRegression().fit(x, y)...
The problem is that it expects an X
which is a 2d column array. For this reason, I usually feed it
x.reshape((len(x), 1))
which I find cumbersome and hard to read.
Is there some shorter way to transform a 1d array to a 2d column array (or, alternatively, get sklearn
to accept 1d arrays)?
One dimensional array contains elements only in one dimension. In other words, the shape of the NumPy array should contain only one value in the tuple.
Size of a numpy array can be changed by using resize() function of the NumPy library.
You can slice your array, creating a newaxis:
x[:, None]
This:
>>> x = np.arange(5)
>>> x[:, None]
array([[0],
[1],
[2],
[3],
[4]])
Is equivalent to:
>>> x.reshape(len(x), 1)
array([[0],
[1],
[2],
[3],
[4]])
If you find it more readable, you can use a transposed matrix:
np.matrix(x).T
If you want an array:
np.matrix(x).T.A
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