7

I am new to Python and Numpy so maybe the title of my question is wrong.

I load some data from a matlab file

data=scipy.io.loadmat("data.mat")
x=data['x']
y=data['y']
>>> x.shape
(2194, 12276)
>>> y.shape
(2194, 1)

y is a vector and I would like to have y.shape = (2194,).

I do not the difference between (2194,) and (2194,1) but seems that sklearn.linear_model.LassoCV encounter an error if you try to load y such that y.shape=(2194,1).

So how can I change my y vector in order to have y.shape=(2194,)??

1 Answer 1

8

First convert to an array, then squeeze to remove extra dimensions:

y = y.A.squeeze()

In steps:

In [217]: y = np.matrix([1,2,3]).T

In [218]: y
Out[218]: 
matrix([[1],
        [2],
        [3]])

In [219]: y.shape
Out[219]: (3, 1)

In [220]: y = y.A

In [221]: y
Out[221]: 
array([[1],
       [2],
       [3]])

In [222]: y.shape
Out[222]: (3, 1)

In [223]: y.squeeze()
Out[223]: array([1, 2, 3])

In [224]: y = y.squeeze()

In [225]: y.shape
Out[225]: (3,)
Sign up to request clarification or add additional context in comments.

4 Comments

I like the y.A syntax (which I believe is an alias for y.__array__()) to get the underlying array better than np.asarray.
ok it works thank! But can you add a little explanation about the difference between elements with size (2194,) and (2194,1) ?
The data is the same, but the shape is different; so is the number of dimensions (ndim). Get into a Python shell, import numpy, and play around with a small array. Look at is shape and ndim, transpose it, squeeze it, convert it to matrix and back. Add dimensions. In matlab arrays have a minimum of 2 dimensions. numpy matrix is modeled on old matlab arrays which couldn't have more dimensions.
@Donbeo, As @hpaulj says, they'r the same data, just different views. As you become more familiar with numpy, the concept of a view vs a copy will become important. Certain actions jut give a view, for example, reshape and slicing (a[:10] is a view of the first ten items), and np.asarray doesn't copy the data if possible. Others make a copy, for example, np.array and 'fancy indexing' a[a>5] both return copies.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.