PYTHON-Array knowledge

1.shape

# 1.shape 
# One-dimensional array 
a = [1,2,3,4,5,6,7,8,9,10,11,12 ] 
b = np.array (a) 

print (b.shape [0 ]) # outermost elements 12 
# Print (b.shape [. 1]) times the outer #, # IndexError: OUT index tuple of Range 

# Why not a.shape [0], because the 'list' object has no attribute 'shape' 

# Two-dimensional array 
a = [[1,2,3,4], [5,6,7,8], [ 9,10,11,12 ]] 
b = np.array (a)
 print (b)
 print (b.shape [0], b.shape [1]) # 3 outermost layers, 4 inner ones
#output:
12 [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] 3 4
 

2.reshape

# 2.reshape 
a = [1,2,3,4,5,6,7,8,9,10,11,12 ] 
b = np.array (a) .reshape (2,6) # 2 lines 6 Column 
print (b)
 print (a) 
b = np.array (a) .reshape (2,3,2) # 2 rows and 3 columns of two matrices 
print (b)
 print (np.array (a)) 

# reshape The newly generated array and the original array share the same memory, no matter which changes will affect each other.
# Output: 
[[1 2 3 4 5 6 ] 
 [ 7 8 9 10 11 12 ]] 
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] 
[[[ 1 2 ] 
  [ 3 4 ] 
  [ 5 6 ]] 

 [[ 7 8 ] 
  [ 9 10 ] 
  [ 11 12 ]]] 
[ 1 2 3 4 5 6 7 8 9 10 11 12]
# 3.reshape (-1,1) is interpreted as: -1 row == no row; 1 == 1 column, then this is 1 column vector 
a = [1,2,3,4,5,6,7 , 8,9,10,11,12 ] 
b = np.array (a) .reshape (-1,1 ) # 12 * 1
 print (b) 

a = [1,2,3,4,5,6, 7,8,9,10,11,12 ] 
b = np.array (a) .reshape (-1,2 ) # 6 * 2
 print (b) 

a = [1,2,3,4,5,6 , 7,8,9,10,11,12 ] 
b = np.array (a) .reshape (1, -1 ) # 1 * 12
 print (b) 

a = [1,2,3,4,5, 6,7,8,9,10,11,12 ] 
b = np.array (a) .reshape (2, -1 ) # 2 * 6
 print (b)
#Result : 
[[1 ] 
 [ 2 ] 
 [ 3 ] 
 [ 4 ] 
 [ 5 ] 
 [ 6 ] 
 [ 7 ] 
 [ 8 ] 
 [ 9 ] 
 [ 10 ] 
 [ 11 ] 
 [ 12 ]] 
[[ 1 2 ] 
 [ 3 4 ] 
 [ 5 6 ] 
 [ 7 8 ] 
 [ 9 10 ] 
 [ 11 12 ]] 
[[1  2  3  4  5  6  7  8  9 10 11 12]]
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]]
>>> 

 

3. When I was studying this, I seemed to refer to that website. Some of them did not remember. If there is an infringement of your copyright, delete it!

 

Guess you like

Origin www.cnblogs.com/xiao-yu-/p/12727899.html