def image2vector(image):
    """
    Argument:
    image -- a numpy array of shape (length, height, depth)
    
    Returns:
    v -- a vector of shape (length*height*depth, 1)
    """
    

    将三维变为一维输出
    v = np.array(image).reshape(np.array(image).shape[1]*np.array(image).shape[0]*np.array(image).shape[2],1)

    return v
a = [[[1,2,3],
     [3,4,5]],
     [[5,6,7],
      [8,9,10]]
    ]
s = image2vector(a)
print (s)
#注意这里a未改为多维数组,故在image2vector()函数中将image改为三维数组类型
# Python虽然也提供了array模块,但其只支持一维数组,不支持多维数组
#当a被声明为数组类型时,则函数中代码为 v = image.reshape(image.shape[0]*image.shape[1]*image.shape[2],1)

注意a未转格式为数组,会出现错误。函数np.array()将image改为多维数组类型.。Python也提供array模块,但只支持一维数组,不支持多维数组。numpy模块填补这一缺点

或者如下修改


def image2vector(image):
    """
    Argument:
    image -- a numpy array of shape (length, height, depth)
    
    Returns:
    v -- a vector of shape (length*height*depth, 1)
    """
    

    v = image.reshape(image.shape[0]*image.shape[1]*image.shape[2],1)

    
    return v
a = np.array([[[1,2,3],
     [3,4,5]],
     [[5,6,7],
      [8,9,10]]])
s = image2vector(a)
print(s)