Skip to content

Latest commit

 

History

History
155 lines (121 loc) · 3.79 KB

NumPy 高维数组降维方法详细分析.md

File metadata and controls

155 lines (121 loc) · 3.79 KB
title date tags toc
NumPy 降维方法深度解析
2019-03-13 05:23:56 -0700
python
NumPy
true

numpy的flat、flatten、ravel、reshape

四个函数都是对多维数组进行降维(降至一维)

使用方法:

import numpy as np

a = np.arange(64).reshape([4,4,4])
print(a)

#对三维数组a进行降维打击
b = a.reshape(-1)
print('reshape方法:\n',b)
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)
a.resize(64)
print('resize方法:\n',a)

#
[[[ 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]]]
reshape方法:
 [ 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]
flat迭代器:
 [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]
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]
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]
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]
[Finished in 0.3s]

可见这几个降维操作区别还是蛮大的,其中最明显的是flat方法其返回的是一个iterator不能直接输出,需要使用for 来遍历整个iterator。

其中resize 和reshape类似,不同的是resize没有返回值。不能把结果直接赋值给另一个变量。

shape是ndarray中的一个字段,可以直接赋值改变shape形状a.shape = (3,4)

flatten 返回的是一个coper,其ndarray本身不变,修改返回值不影响原本的ndarray。

reshape,类似ravel 返回的是一个原数组的一个视图,实质还是引用,虽然ndarray本身的维度不变,但是对降维后的数组进行操作,原数组仍然发生改变。

ravel()相当于reshape(-1)相当于reshape(ndarray.size())

import numpy as np

a = np.arange(64).reshape([4,4,4])
print(a)

#对三维数组a进行降维打击
b = a.reshape(-1)
b[0] = 111
print('reshape方法:\n',b,'\n原数组\n',a,sep = '')
#
[[[ 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]]]
reshape方法:
[111   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]
原数组
[[[111   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]]]
[Finished in 0.3s]