Numpy flat flatten 将多维数组压平为一维

举报
千江有水千江月 发表于 2020/12/26 20:38:42 2020/12/26
【摘要】 所属的课程名称及链接[AI基础课程--常用框架工具]环境信息* ModelArts  * Notebook - Multi-Engine 2.0 (python3)    * JupyterLab - Notebook - Conda-python3      * numpy 1.19.1Numpy flat flatten 将多维数组压平为一维temp = np.array([[1,2,3...

所属的课程名称及链接


环境信息

  • * ModelArts
    •   * Notebook - Multi-Engine 2.0 (python3)
      •     * JupyterLab - Notebook - Conda-python3
        •       * numpy 1.19.1


Numpy flat flatten 将多维数组压平为一维

temp = np.array([[1,2,3,4],[5,6,7,8]])

print("temp.flat",temp.flat)  # temp.flat 返回的是一维迭代器
print("type(temp.flat)",type(temp.flat))
print("list((temp.flat))",list((temp.flat)))

print("temp",temp)

print("---")

temp_new = temp.flatten()  # 返回一维的副本
print("temp.flatten()",temp_new)
print("temp",temp)
print("temp_new",temp_new)
temp.flat <numpy.flatiter object at 0x55d8a1a049e0>
type(temp.flat) <class 'numpy.flatiter'>
list((temp.flat)) [1, 2, 3, 4, 5, 6, 7, 8]
temp [[1 2 3 4]
 [5 6 7 8]]
---
temp.flatten() [1 2 3 4 5 6 7 8]
temp [[1 2 3 4]
 [5 6 7 8]]
temp_new [1 2 3 4 5 6 7 8]


help

 |  flat : numpy.flatiter object
 |      Flattened version of the array as an iterator.  The iterator
 |      allows assignments, e.g., ``x.flat = 3`` (See `ndarray.flat` for
 |      assignment examples; TODO).
help(np.array([]).flatten)
Help on built-in function flatten:

flatten(...) method of numpy.ndarray instance
    a.flatten(order='C')
    
    Return a copy of the array collapsed into one dimension.
    
    Parameters
    ----------
    order : {'C', 'F', 'A', 'K'}, optional
        'C' means to flatten in row-major (C-style) order.
        'F' means to flatten in column-major (Fortran-
        style) order. 'A' means to flatten in column-major
        order if `a` is Fortran *contiguous* in memory,
        row-major order otherwise. 'K' means to flatten
        `a` in the order the elements occur in memory.
        The default is 'C'.
    
    Returns
    -------
    y : ndarray
        A copy of the input array, flattened to one dimension.
    
    See Also
    --------
    ravel : Return a flattened array.
    flat : A 1-D flat iterator over the array.
    
    Examples
    --------
    >>> a = np.array([[1,2], [3,4]])
    >>> a.flatten()
    array([1, 2, 3, 4])
    >>> a.flatten('F')
    array([1, 3, 2, 4])


备注

1. 感谢老师的教学与课件  
2. 欢迎各位同学一起来交流学习心得^_^  
3. 沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。  


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200