Numpy中花式索引和shape用法

原文转载自我的博客benym.cn

总结一下最近学习中容易出现问题的地方❗️ ❗️ ❗️

代码

from numpy import *

randMat = random.randint(0, 10, (4, 3))
print("原矩阵:\n", randMat)
# 索引从0开始计数
print("输出第一行的所有数据:\n", randMat[0, :])
print("输出第二列的所有数据:\n", randMat[:, 1])
print("输出矩阵第1和第3行的所有数据:\n", randMat[[0, 2], :])
print("行数:\n",randMat.shape[0])
print("列数:\n",randMat.shape[1])
print("维数:\n",randMat.shape)

运行结果

原矩阵:
 [[3 5 1]
 [1 3 0]
 [0 2 1]
 [0 2 9]]
输出第一行的所有数据:
 [3 5 1]
输出第二列的所有数据:
 [5 3 2 2]
输出矩阵第1和第3行的所有数据:
 [[3 5 1]
 [0 2 1]]
行数:
 4
列数:
 3
维数:
 (4, 3)

猜你喜欢

转载自blog.csdn.net/qq_34438435/article/details/81873897