『Numpy』np.ravel()和np.flatten()

What is the difference between flatten and ravel functions in numpy?

两者的功能是一致的,将多维数组降为一维,但是两者的区别是返回拷贝还是返回视图,np.flatten(0返回一份拷贝,对拷贝所做修改不会影响原始矩阵,而np.ravel()返回的是视图,修改时会影响原始矩阵

import numpy as np
a = np.array([[1 , 2] , [3 , 4]])
b = a.flatten()
print('b:' , b)
c = a.ravel()
print('c:' , c)
d = a.ravel('F')
print('d:' , d)

# 二者的区别
b[0] = 10
print('a:' , a)
c[0] = 10
print('a:' , a)
b: [1 2 3 4]
c: [1 2 3 4]
d: [1 3 2 4]
a: [[1 2]
    [3 4]]
a: [[10  2]
    [ 3  4]]

 

posted @ 2018-04-05 10:24  叠加态的猫  阅读(1589)  评论(0编辑  收藏  举报