Numpy 索引,切片,分割,迭代

Numpy 索引,切片,分割,迭代

  • 索引
  • 切片
  • 分割
  • 迭代

索引

访问倒数第二行的最后一个值

a = dataset[-2,-1]

切片

从第二行,第二列处创建一个2x2的矩阵,并计算平均值。

subsection = dataset[1:3, 1:3]
np.mean(subsection)

第7行,每隔一个元素取一个值。

elem = dataset[6, ::2]

在这里插入图片描述

最后一行,逆序

row = dataset[-1, ::-1]

分割

将数据水平分割为三个部分,如果无法以给定的数量进行分割,将会报错。

split = np.hsplit(dataset, (3))

将数据垂直分割为两个部分

ver_split = np.vsplit(split[0], (2))

迭代

迭代所有元素

curr_index = 0
for x in np.nditer(dataset):
    print(x, curr_index)
    curr_index += 1

方法二:

for index,value in np.ndenumerate(dataset):
    print(index, value)

你可能感兴趣的:(Numpy,numpy,python)