How to go from numpy matrix to numpy array?

I am new to Python and Numpy, so maybe the name of my question is incorrect.

I am loading some data from 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 don't make a difference between (2194,) and (2194,1) , but it seems that sklearn.linear_model.LassoCV detects an error if you try to load y to y.shape=(2194,1) .

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

+6
source share
1 answer

Convert to an array first, then compress to remove extra dimensions:

 y = yAsqueeze() 

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 = yA 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,) 
+7
source

Source: https://habr.com/ru/post/958004/


All Articles