Numpy flat flatten 将多维数组压平为一维
        【摘要】 所属的课程名称及链接[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
 
 
-     * JupyterLab - Notebook - Conda-python3 
      
 
-   * Notebook - Multi-Engine 2.0 (python3) 
    
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. 沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
2. 欢迎各位同学一起来交流学习心得^_^
3. 沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
            【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
                cloudbbs@huaweicloud.com
                
            
        
        
        
        
        
        
        - 点赞
- 收藏
- 关注作者
 
             
           
评论(0)