numpy中的降维方法:
代码示例:
import numpy as np a = np.array([[1, 2, 3], [4, 5, 6]]) c = [] for x in a.flat: c.append(x) print('flat迭代器降一维:\n', c) d = a.flatten() print('flatten方法降一维:\n', d) e = a.ravel() print('ravel方法降一维:\n', e) g = np.squeeze(a) print('squeeze方法降一维:\n', g) f = a.reshape(-1) print('reshape方法降一维:\n', f) a.resize((1, 6)) print('resize方法:\n', a)
结果:
flat迭代器降一维:
[1, 2, 3, 4, 5, 6]
flatten方法降一维:
[1 2 3 4 5 6]
ravel方法降一维:
[1 2 3 4 5 6]
squeeze方法降一维:
[[1 2 3]
[4 5 6]]
reshape方法降一维:
[1 2 3 4 5 6]
resize方法:
[[1 2 3 4 5 6]]
import numpy as np a = np.arange(64).reshape([4,4,4]) # [[[ 0 1 2 3] # [ 4 5 6 7] # [ 8 9 10 11] # [12 13 14 15]] # # [[16 17 18 19] # [20 21 22 23] # [24 25 26 27] # [28 29 30 31]] # # [[32 33 34 35] # [36 37 38 39] # [40 41 42 43] # [44 45 46 47]] # # [[48 49 50 51] # [52 53 54 55] # [56 57 58 59] # [60 61 62 63]]] print(a) # 对三维数组a进行降维打击 a_reshape = a.reshape(-1) # [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 # 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 # 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63] print('reshape方法:\n',a_reshape) c_flat = [] for x in a.flat: c_flat.append(x) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63] print('flat迭代器:\n',c_flat) d_flatten = a.flatten() # [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 # 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 # 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63] print('flatten方法:\n',d_flatten) e_ravel = a.ravel() # [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 # 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 # 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63] print('ravel方法:\n',e_ravel) f_resize = a.resize(64) # None resize 没有返回值,改变的是原数组 print('resize方法:\n',f_resize) # [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 # 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 # 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63] print(a)
到此这篇关于numpy降维方法的文章就介绍到这了,更多相关numpy 降维内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
编程 | 2023-02-24 21:36
编程 | 2023-02-21 12:51
编程 | 2023-02-21 12:47
编程 | 2023-02-21 00:15
编程 | 2023-02-21 00:08
编程 | 2023-02-20 21:46
编程 | 2023-02-20 21:42
编程 | 2023-02-20 21:36
编程 | 2023-02-20 21:32
编程 | 2023-02-20 18:12
网友评论