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_)
数学中两个集合之间的笛卡尔积