Python numpy ravel函数未压平数组



我有一个名为x的数组数组,我试图对它进行ravel,但结果是一样的x。它没有使任何东西变平。我也尝试过函数flatn((。有人能解释一下为什么会发生这种事吗?

x = np.array([np.array(['0 <= ... < 200 DM', '< 0 DM', 'no checking account'], dtype=object),
np.array(['critical account/ other credits existing (not at this bank)',
'existing credits paid back duly till now'], dtype=object),
np.array(['(vacation - does not exist?)', 'domestic appliances'],
dtype=object)], dtype=object)
np.ravel(x)

实际上,我正试图重现这个问题中的代码:在sklearn中对多个列进行一次热编码并命名列但是我被旅行挡住了。

感谢

In [455]: x = np.array([np.array(['0 <= ... < 200 DM', '< 0 DM', 'no checking account'], dtype=object),
...:  
...:        np.array(['critical account/ other credits existing (not at this bank)', 
...:        'existing credits paid back duly till now'], dtype=object), 
...:        np.array(['(vacation - does not exist?)', 'domestic appliances'], 
...:       dtype=object)], dtype=object)                                                          
In [456]: x                                                                                            
Out[456]: 
array([array(['0 <= ... < 200 DM', '< 0 DM', 'no checking account'], dtype=object),
array(['critical account/ other credits existing (not at this bank)',
'existing credits paid back duly till now'], dtype=object),
array(['(vacation - does not exist?)', 'domestic appliances'],
dtype=object)], dtype=object)
In [457]: x.shape                                                                                      
Out[457]: (3,)
In [458]: [i.shape for i in x]                                                                         
Out[458]: [(3,), (2,), (2,)]

CCD_ 1是具有3个元素的1d阵列。这些元素本身就是阵列,具有不同的形状。

一种使其变平的方法是:

In [459]: np.hstack(x)                                                                                 
Out[459]: 
array(['0 <= ... < 200 DM', '< 0 DM', 'no checking account',
'critical account/ other credits existing (not at this bank)',
'existing credits paid back duly till now',
'(vacation - does not exist?)', 'domestic appliances'],
dtype=object)
In [460]: _.shape                                                                                      
Out[460]: (7,)

最新更新