Python numpy,数组的形状(shape属性),多维数组

 

demo.py(shape形状,reshape改变形状,多维数组):

# coding=utf-8
import numpy as np


# shape属性(形状)
t1 = np.array([1, 2, 3, 4, 5])  # 一维数组
print(t1.shape)   # (5,)  5个元素


t2 = np.array([[1, 2, 3], [4, 5, 6]])  # 二维数组
print(t2.shape)   # (2, 3)  2行3列


# reshape改变数组的形状
t3 = np.arange(12)
t4 = t3.reshape((3, 4))   # 改变形状,改成3行4列
print(t4)
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
'''


t5 = t4.reshape((t4.shape[0] * t4.shape[1],))  # 将多维数组转成一维数组(复杂方法)
print(t5)  # [ 0  1  2  3  4  5  6  7  8  9 10 11]

t6 = t4.flatten()   # 将多维数组转成一维数组(简单方法)
print(t6)  # [ 0  1  2  3  4  5  6  7  8  9 10 11]

 

 

你可能感兴趣的:(Python+,python,numpy,shape)