Python's reshape usage

 

Refer to: https: //blog.csdn.net/qq_29831163/article/details/90112000#reshape (1% 2C-1)% E8% BD% AC% E5% 8C% 96% E6% 88% 901% E8% A1% 8C% EF% BC% 9A

 

Three common usage related reshape function in numpy 

  reshape (1, -1) is converted into a line: 

  reshape (2, -1) is converted into two lines: 

  reshape (-1,1) is converted to a: 

  reshape (-1,2) is converted into two

 

Three common usage related reshape function in numpy

  • numpy.arange (n) .reshape (a, b) n natural numbers sequentially generates and displays a row in an array of columns b

np.arange ( 16) .reshape ( 2, . 8) # 16 generates a natural number, are displayed in the form of rows and 8 columns 2

import numpy as np


arr = np.arange(16).reshape(2,8)
print(arr)

result:  

[[ 0 1 2 3 4 5 6 7]
[ 8 9 10 11 12 13 14 15]]

 

  • mat (or array) .reshape (c, -1) must be in a matrix format or array format, in order to use .reshape (c, -1) function, this represents a matrix or array recombinant, expressed in the form of a column line c d

arr.reshape ( 4, -1) # arr into the format of 4 rows, the number of columns automatically calculated (c = 4, d = 16 /4 = 4)

 

Arr into the format # 4 rows, the number of columns automatically calculated (C = 4, D = 16/4 = 4)
arr2 is arr.reshape = ((4, -1))
Print (arr2 is)

result:

[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]

 

#reshape (2, -1) is converted into two lines:
arr5 arr.reshape = ((2, -1))
Print (arr5)

result:

[[ 0 1 2 3 4 5 6 7]
[ 8 9 10 11 12 13 14 15]]

 

#reshape (-1,2) is converted into two:
arr6 arr.reshape = ((- 1,2))
Print (arr6)

result:

[[ 0 1]
[ 2 3]
[ 4 5]
[ 6 7]
[ 8 9]
[10 11]
[12 13]
[14 15]]

  • numpy.arange (a, b, c) starting from a digital step size is c, b to the end, array generation
  • numpy.arange (a, b, c) .reshape (m, n): The array dimension becomes m rows and n columns.

 

#reshape (1, -1) is converted into a line:
ARR3 arr.reshape = (1, -1)
Print (ARR3)

Results: [[0 1,234,567,891,011,121,314 15]]

 

#reshape (-1,1) is converted into a:
arr4 arr.reshape = (-1,1)
Print (arr4)

result:

[[ 0]
[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]
[11]
[12]
[13]
[14]
[15]]

Guess you like

Origin www.cnblogs.com/gaojr/p/12176944.html