如何从numpy矩阵传递到numpy数组?

时间:2021-11-15 21:22:04

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

我是Python和Numpy的新手,所以也许我的问题的标题是错误的。

I load some data from a matlab file

我从matlab文件加载一些数据

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,).

y是一个向量,我想要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).

我没有(2194,)和(2194,1)之间的区别,但似乎sklearn.linear_model.LassoCV遇到错误,如果你试图加载y使y.shape =(2194,1)。

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

那么我怎么能改变我的y向量才能得到y.shape =(2194,)?

1 个解决方案

#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,)

#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,)