numpy中的数组切片 高级索引

参考:NumPy 高级索引 | 菜鸟教程NumPy 高级索引 NumPy 比一般的 Python 序列提供更多的索引方式。除了之前看到的用整数和切片的索引外,数组可以由整数数组索引、布尔索引及花式索引。 整数数组索引 以下实例获取数组中 (0,0),(1,1) 和 (2,0) 位置处的元素。 实例 [mycode3 type='python'] import numpy as np x = np.array([[1, 2], [3,..https://www.runoob.com/numpy/numpy-advanced-indexing.html

1. 数组的创建

#define_array.py
import numpy as np

a1 = np.zeros((7,12),dtype=int)#all 0
print(a1)
a2 = np.ones((12,3),dtype=int)#all 1
print(a2)
a3 = np.eye(7)# E(3)
print(a3)
a4 = np.empty([3,7],dtype=int)#any value
print(a4)
a5 = np.random.randint(0, 5, (7,7))#0~5int, 7x7 2darray
print(a5)

y1 = np.array([3, 4, 5])
print(y1)
a6 = np.diag(y1)#y is the diag
print(a6)

a7 = np.arange(0, 12, 2)# start from 0 step 2, stop the arry before 30
print(a7)
a8 = a7.reshape(2, 3) # reshape the oned array to be 3x4
print(a8)

运行结果:

 

2.整数数组做矩阵索引

源码1:

运行结果:

 源码2:

运行结果:

bool型索引源码3:

 运行结果:

 补集索引源码4:

 运行结果:

花式数组索引源码5:

运行结果:

用一维数组索引二维数组的行,源码6:

运行结果:

 倒序索引数组源码7:

运行结果:

 

 笛卡尔积索引源码8:

运行结果:

3、传入多个索引数组(要使用 np.ix_)

数学中两个集合之间的笛卡尔积

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值