Python笔记:3.1.2.5numpy数组维度_展平

# -*- coding: utf-8 -*-
"""
Created on Mon May 20 22:02:57 2019

@author: User
"""

import numpy as np

b = np.arange(24).reshape(2,3,4)
print(b)
print(b.ndim)

print('\n展平为1维数组 br = np.ravel(b):')
br = np.ravel(b)
print(br)
print(br.ndim)

print('\n展平为貌似1维数组 brsh = b.reshape(1,1,24):')
brsh = b.reshape(1,1,24)
print(brsh)
print(brsh.ndim)

print('\n flatten 方法与ravel类似 bd = b.reshape(4,6):')
print(b.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]]]
3

展平为1维数组 br = np.ravel(b):
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
1

展平为貌似1维数组 brsh = b.reshape(1,1,24):
[[[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
   23]]]
3

 flatten 方法与ravel类似 bd = b.reshape(4,6):
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
 

你可能感兴趣的:(python学习)