首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python numpy ravel函数未展平数组

Python numpy ravel函数未展平数组
EN

Stack Overflow用户
提问于 2020-04-17 00:22:33
回答 1查看 268关注 0票数 0

我有一个名为x的数组,我试图对它进行ravel运算,但结果是相同的x,它没有展平任何东西。我也尝试过flatten()函数。有人能给我解释一下为什么会这样吗?

代码语言:javascript
复制
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)

我实际上试图重现这个问题中的代码:One-hot-encoding multiple columns in sklearn and naming columns,但我被ravel()阻塞了。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-17 02:33:24

代码语言:javascript
复制
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,)]

x是一个包含3个元素的一维数组。这些元素本身就是具有不同形状的数组。

展平它的一种方法是:

代码语言:javascript
复制
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,)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61255108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档