如何仅展平 numpy 数组的某些维度 [英] How to flatten only some dimensions of a numpy array

查看:83
本文介绍了如何仅展平 numpy 数组的某些维度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种快速的方法可以子展平"或仅展平 numpy 数组中的一些第一维?

例如,给定一个维度为 (50,100,25) 的 numpy 数组,结果维度将是 (5000,25)

解决方案

看一看 numpy.reshape .

<预><代码>>>>arr = numpy.zeros((50,100,25))>>>形状# (5​​0, 100, 25)>>>new_arr = arr.reshape(5000,25)>>>new_arr.shape# (5​​000, 25)# 一个形状维度可以是-1.# 在这种情况下,该值是从# 数组的长度和剩余的维度.>>>another_arr = arr.reshape(-1, arr.shape[-1])>>>another_arr.shape# (5​​000, 25)

Is there a quick way to "sub-flatten" or flatten only some of the first dimensions in a numpy array?

For example, given a numpy array of dimensions (50,100,25), the resultant dimensions would be (5000,25)

解决方案

Take a look at numpy.reshape .

>>> arr = numpy.zeros((50,100,25))
>>> arr.shape
# (50, 100, 25)

>>> new_arr = arr.reshape(5000,25)
>>> new_arr.shape   
# (5000, 25)

# One shape dimension can be -1. 
# In this case, the value is inferred from 
# the length of the array and remaining dimensions.
>>> another_arr = arr.reshape(-1, arr.shape[-1])
>>> another_arr.shape
# (5000, 25)

这篇关于如何仅展平 numpy 数组的某些维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆