首先看ravel()
from numpy import *
a = b.arange(12).reshape(3, 4)
print(a)
b = a.ravel()
print(b)
b[1] = 9
print(b)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[ 0 1 2 3 4 5 6 7 8 9 10 11]
[ 0 9 2 3 4 5 6 7 8 9 10 11]
[[ 0 9 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
flatten
from numpy import *
a = b.arange(12).reshape(3, 4)
print(a)
b = a.flatten()
print(b)
b[1] = 9
print(b)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[ 0 1 2 3 4 5 6 7 8 9 10 11]
[ 0 9 2 3 4 5 6 7 8 9 10 11]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
flatten产生一个副本,ravel不产生,ravel会对原始数据产生影响,flatten()不会
import numpy as np
x1, x2 = np.mgrid[1:4:3j, 1:6:3j]
b = np.mgrid[1:4:3j, 1:6:3j]
print(x1)
'''
[[1. 1. 1. ]
[2.5 2.5 2.5]
[4. 4. 4. ]]
'''
print(x2)
'''
[[1. 3.5 6. ]
[1. 3.5 6. ]
[1. 3.5 6. ]]
'''
print(b.flat)#返回和flatten效果相同的迭代器
'''
<numpy.flatiter object at 0x0000020F5A7E8260>
'''
print(b.flatten())
'''
[1. 1. 1. 2.5 2.5 2.5 4. 4. 4. 1. 3.5 6. 1. 3.5 6. 1. 3.5 6. ]
'''
a1 = np.stack((x1.flat, x2.flat), axis=1)
print(a1)
'''
[[1. 1. ]
[1. 3.5]
[1. 6. ]
[2.5 1. ]
[2.5 3.5]
[2.5 6. ]
[4. 1. ]
[4. 3.5]
[4. 6. ]]
'''