Usage Python reshape and a plurality of two-dimensional arrays into a three-dimensional array of examples

Today small for everyone to share a Python reshape usage and more two-dimensional arrays into a three-dimensional array instance, have a good reference value, we want to help. Come and see, to follow the small series together
reshape (shape): do not change the array element, returns an array of shape shape of the original array unchanged. It is processed for each row element

resize (shape): () function is consistent with .reshape, but modifying the original array

In [1]: a = np.arange(20)
#原数组不变
In [2]: a.reshape([4,5])
Out[2]:
array([[ 0, 1, 2, 3, 4],
  [ 5, 6, 7, 8, 9],
  [10, 11, 12, 13, 14],
  [15, 16, 17, 18, 19]])
  
In [3]: a
Out[3]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  17, 18, 19])
  
#修改原数组
In [4]: a.resize([4,5])
  
In [5]: a
Out[5]:
array([[ 0, 1, 2, 3, 4],
  [ 5, 6, 7, 8, 9],
  [10, 11, 12, 13, 14],
  [15, 16, 17, 18, 19]])

.swapaxes (ax1, ax2): the array of n dimensions swap two dimensions, without changing the original array

In [6]: a.swapaxes(1,0)
Out[6]:
array([[ 0, 5, 10, 15],
  [ 1, 6, 11, 16],
  [ 2, 7, 12, 17],
  [ 3, 8, 13, 18],
  [ 4, 9, 14, 19]])

.flatten (): reduce the dimension of the array, one-dimensional array is folded after the return, the same array In the original

 [7]: a.flatten()
Out[7]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  17, 18, 19])

A plurality of two-dimensional arrays into a three-dimensional array

method one:

For the two (or more) of the same dimensions of the matrix, directly np.array () to reconstruct a Array, which can play a role in a number of extensions disguised dimension. E.g:

import numpy as np
  
a = np.array([[1,2,3],[4,5,6]])
b = np.array([[2,2,3],[4,5,6]])
c = np.array([[3,2,3],[4,5,6]])
print('矩阵a:\n',a)
print('维数:',a.shape)
  
com = np.array([a,b,c])
print('合并矩阵:\n',com)
print('维数:',com.shape)
  
输出结果为:
  
矩阵a:
 [[1 2 3]
 [4 5 6]]
维数: (2, 3)
合并矩阵:
 [[[1 2 3]
 [4 5 6]]
  
 [[2 2 3]
 [4 5 6]]
  
 [[3 2 3]
 [4 5 6]]]
维数: (3, 2, 3)

Method Two:

However, if two array, use the moment will appear the following results:

import numpy as np
  
aa = np.array([[[1,2,3],[4,5,6]],[[2,2,3],[4,5,6]],[[3,2,3],[4,5,6]]])
a = np.array([[4,2,3],[4,5,6]])
  
com = np.array([aa,a])
print('合并矩阵:\n',com)
print('维数:',com.shape)
  
输出结果:
  
合并矩阵:
 [array([[[1, 2, 3],
  [4, 5, 6]],
  
  [[2, 2, 3],
  [4, 5, 6]],
  
  [[3, 2, 3],
  [4, 5, 6]]])
 array([[4, 2, 3],
  [4, 5, 6]])]
维数: (2,)

Can be seen: the output is not dimensionally correct, the above method is not applicable.

So, we need to use np.append and array.reshape () function after the reorganization of the array splicing, specifically to achieve the following:

import numpy as np
  
aa = np.array([[[1,2,3],[4,5,6]],[[2,2,3],[4,5,6]],[[3,2,3],[4,5,6]]])
a = np.array([[4,2,3],[4,5,6]])
data = np.append(aa,a)#先拼接成一个行向量
print(data)
  
dim = aa.shape#获取原矩阵的维数
print('原矩阵维数:',dim)
data1 = data.reshape(dim[0]+1,dim[1],dim[2])#再通过原矩阵的维数重新组合
  
print('合并矩阵:\n',data1)
print('维数:',data1.shape)

Now look at the use of two-dimensional data was promoted to reshape the distribution of three-dimensional data:

import numpy as np
b = np.arange(36).reshape((6,6))
b1 = b.reshape(2,3,6)

b elements:
Here Insert Picture Description
B1 elements: Here Insert Picture Description
see, 6 original matrix 6 is divided into two 3 matrix 6. Each row of data distribution does not change, only the first three rows designated as a dimension, then the last three rows designated as another dimension.

b1.reshape(6,6)

If you use this command, the data has been restored back, the same as b.

b1.reshape(3,12)

Here Insert Picture Description
If the reshape (3,12), the data corresponding to the first stretch is 1-dimensional, one-dimensional data then reorganized into 12 * 3

Method three:

Compared to the previous two methods, this method can be described as "Quxianjiuguo" of the model, specific ideas: first converted into a list, then converted back stitching.

This is because the append list of () function can not change the original list of functions in time to add dimension. Although not a speed test of this approach, but Intuitively time complexity pricey, recommended caution.

aa = np.array([[[1,2,3],[4,5,6]],[[2,2,3],[4,5,6]],[[3,2,3],[4,5,6]]])
a = np.array([[4,2,3],[4,5,6]])
  
#将array转换成list
aa = aa.tolist(aa)
a = a.list(a)
  
aa.append(a)#注意与方法二中np.append()用法的区别
com = np.array(aa)
print(com.shape)
  
输出结果:
  
合并矩阵:
  [[[1 2 3]
  [4 5 6]]
  
  [[2 2 3]
  [4 5 6]]
  
  [[3 2 3]
  [4 5 6]]
   
  [[4 2 3]
  [4, 5, 6]]]
维数: (4,2,3)

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
aa = np.array([[[1,2,3],[4,5,6]],[[2,2,3],[4,5,6]],[[3,2,3],[4,5,6]]])
a = np.array([[4,2,3],[4,5,6]])

The array is converted into # List
AA = aa.tolist (AA)
A = a.list (A)

aa.append (a) # Note that with the second approach np.append () usage distinction
COM = np.array (AA)
Print (com.shape)

Output:

The combined matrix:
[[[123]
[456]]

[[2 2 3]
[4 5 6]]

[[3 2 3]
[4 5 6]]

[[423]
[4, 5, 6]]]
Dimension: (4,2,3)
Note here:

Both types of conversion functions:

array转list:a = a.tolist()

list transfer array: a = np.array (a)
content on more than how many, and finally to recommend a good reputation in the number of public institutions [programmers], there are a lot of old-timers learning

Skills, learning experience, interview skills, workplace experience and other share, the more carefully prepared the zero-based introductory information, information on actual projects,

Every day, Python programmers explain the timing of technology, to share some of the ways to learn and need to pay attention to small details'!
Here Insert Picture Description

Published 64 original articles · won praise 13 · views 50000 +

Guess you like

Origin blog.csdn.net/chengxun02/article/details/105151314
Recommended