flatten的用法

from numpy import *
if __name__ == '__main__':
    '''
    flatten 用于数组
    '''
    a = array([[1,2,3,4,5],[3,4,5,6,7],[1,4,5,6,74],[23,4,5,6,7]])
    print(type(a))
    print(a.flatten())
    '''
    [ 1  2  3  4  5  3  4  5  6  7  1  4  5  6 74 23  4  5  6  7]
    '''
    print(type(a.flatten()))
    '''
    flatten 用于矩阵
    '''
    b = mat([[2,3,4],[3,4,5],[4,5,6]])
    print(b)
    print(type(b))
    print(b.flatten())
    print(type(b.flatten()))
    print(b.flatten().A)
    #flatten之后矩阵变成了数组
    print(b.flatten().A[0])
    print(type(b.flatten().A[0]))
    '''
    <class 'numpy.ndarray'>
    [ 1  2  3  4  5  3  4  5  6  7  1  4  5  6 74 23  4  5  6  7]
    <class 'numpy.ndarray'>
    [[2 3 4]
     [3 4 5]
     [4 5 6]]
    <class 'numpy.matrixlib.defmatrix.matrix'>
    [[2 3 4 3 4 5 4 5 6]]
    <class 'numpy.matrixlib.defmatrix.matrix'>
    [[2 3 4 3 4 5 4 5 6]]
    [2 3 4 3 4 5 4 5 6]
    <class 'numpy.ndarray'>
    '''

flatten不能用于列表
posted @ 2018-02-06 16:06  crr121  阅读(428)  评论(0编辑  收藏  举报