z = np.arange(9).reshape((3,3))for index,value in np.ndenumerate(z):print(index,value)
56、生成一个通用二维Gaussian-like数组
X,Y = np.meshgrid(np.linspace(-1,1,10),np.linspace(-1,1,10))
D = np.sqrt(X*X + Y*Y)
sigma =1
mu =0
G = np.exp(-((D-mu)**2/(2.0*sigma*2)))
57、对于一个二维数组如何在其内部随机放置p个元素
n =10
p =3
data = np.zeros((n,n))
np.put(data,np.random.choice(range(n*n),p,replace=False),1)
58、减去一个矩阵中每一行的平均值
x = np.random.rand(5,10)
y = x-x.mean(axis=1,keepdims=True)
59、如何通过第N列对数组进行排序
z = np.random.randint(0,10,(3,3))
z[z[:1].argsort()]
60、如何检查一个二维数组是否有空列
z = np.random.randint(0,3,(3,10))(z.any(axis=0)).any()
61、从数组中的给定值找出最近的值
z = np.random.random(10)
z1 =0.5print(z[np.abs(z-z1).argmin()])
62、如何用迭代器(iterator)计算两个分别具有形状(1,3)和(3,1)的数组
A = np.arange(3).reshape((3,1))
B = np.arange(3).reshape((1,3))
data = np.nditer([A,B,None])for i,j,k in data:
k = i + j
print(data.operands[2])
63、创建一个具有name属性的数组类
classNameArray(np.ndarray):def__new__(cls,array,name="no name"):
obj = np.asarray(array).view(cls)
obj.name = name
return obj
def__array_finalize__(self,obj):if obj isNone:return
self.info =getattr(obj,"name","no name")
z = NameArray(np.array([1,2,3]),"range_1")
64、一个给定向量,对由第二个向量索引的每个元素加1(重复索引!)
z = np.ones(10)
index = np.random.randint(0,10,20)
z += np.bincount(index,minlength=len(z))
65、根据索引表(I),如何将向量(X)的元素累加到数组(F)
X =[1,2,3,4,5,6]
Y =[1,3,9,3,4,1]
F = np.bincount(x,y)
66、考虑一个dtype=ubyte的(w,h,3)图像,计算其唯一颜色的数量
w,h =16,16
data = np.random.randint(0,2,(h,w,3)).astype(np.ubyte)
F = data[...,0]*(256*256)+ data[...,1]*256+ data[...,2]
n =len(np.unique(F))print(n)
67、一个四维数组,如何一次性计算出最后两个轴(axis)的和
z = np.random.randint(0,1,(3,4,5,6))
sum1 = z.sum(axis=(-2,-1))
68、一个一维向量D,如何使用相同大小的向量S来计算D子集的均值
D = np.random.uniform(0,1,100)
S = np.random.uniform(0,10,100)
D_sum = np.bincount(S,weight=D)
D_counts = np.bincount(S)
D_means = D_sum / D_counts
print(D_means)
69、如何获得点积dot prodcut的对角线
A = np.random.uniform(0,1,(5,5))
B = np.random.uniform(0,1,(5,5))
np.diag(np.dot(A,B))
70、一个向量[1,2,3,4,5],如何建立一个新的向量,在这个新向量中每个值之间有3个连续的零
data = np.array([1,2,3,4,5])
nz =3
data1 = np.zeros(len(data)+(len(data)-1)* nz)
data1[::nz+1]= data
print(data1)
71、一个维度(5,5,3)的数组,如何将其与一个(5,5)的数组相乘
A = np.ones((5,5,3))
B =2* np.ones((5,5))# 1print(A*B[:,:,None])# 2print(A*B.reshape(5,5,1))
72、对一个数组中任意两行做交换
A = np.arange(25).reshape((5,5))
A[[0,1]]= A[[1,0]]
73、一个可以描述10个三角形的triplets,找到可以分隔全部三角形的line segment
faces = np.random.randint(0,100,(10,3))
F = np.roll(faces.repeat(2,axis=1),-1,axis=1)
F = F.reshape(len(F)*3,2)
F = np.sort(F,axis=1)
G = F.view( dtype=[('p0',F.dtype),('p1',F.dtype)])
G = np.unique(G)
Z = np.random.randint(0,2,100)
np.logical_not(Z, out=Z)
Z = np.random.uniform(-1.0,1.0,100)
np.negative(Z, out=Z)
78、两组点集p0和P1去描述一组线(二维)和一个点P,如何计算P到每一条线 i (P0[i],P1[i])的距离
defdistance(P0, P1, p):
T = P1 - P0
L =(T**2).sum(axis=1)
U =-((P0[:,0]-p[...,0])*T[:,0]+(P0[:,1]-p[...,1])*T[:,1])/ L
U = U.reshape(len(U),1)
D = P0 + U*T - p
return np.sqrt((D**2).sum(axis=1))
P0 = np.random.uniform(-10,10,(10,2))
P1 = np.random.uniform(-10,10,(10,2))
p = np.random.uniform(-10,10,(1,2))print(distance(P0, P1, p))
79、考虑两组点集P0和P1去描述一组线(二维)和一组点集P,如何计算每一个点 j(P[j]) 到每一条线 i (P0[i],P1[i])的距离?
P0 = np.random.uniform(-10, 10, (10,2))
P1 = np.random.uniform(-10,10,(10,2))
p = np.random.uniform(-10, 10, (10,2))
print(np.array([distance(P0,P1,p_i) for p_i in p]))
Z = np.arange(1,15,dtype=np.uint32)
R = stride_tricks.as_strided(Z,(11,4),(4,4))
82、计算矩阵的秩
Z = np.random.uniform(0,1,(10,10))# 奇异值分解,S是主对角线矩阵,也是Z的奇异值
U, S, V = np.linalg.svd(Z)
rank = np.sum(S >1e-10)
83、如何找出数组中出现频率最高的值
Z = np.random.randint(0,10,50)print(np.bincount(Z).argmax())
84、从一个10x10的矩阵中提取出连续的3x3区块
Z = np.random.randint(0,5,(10,10))
n =3
i =1+(Z.shape[0]-3)
j =1+(Z.shape[1]-3)
C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides)
85、创建一个满足 Z[i,j] == Z[j,i]的二维数组子类
classSymetric(np.ndarray):def__setitem__(self, index, value):
i,j = index
super(Symetric, self).__setitem__((i,j), value)super(Symetric, self).__setitem__((j,i), value)defsymetric(Z):return np.asarray(Z + Z.T - np.diag(Z.diagonal())).view(Symetric)
S = symetric(np.random.randint(0,10,(5,5)))
S[2,3]=42
86、 考虑p个 nxn 矩阵和一组形状为(n,1)的向量,如何直接计算p个矩阵的乘积(n,1)
p, n =10, 20
M = np.ones((p,n,n))
V = np.ones((p,n,1))
S = np.tensordot(M, V, axes=[[0, 2], [0, 1]])
print(S)
87、对于一个16x16的数组,得到一个区域的和(区域大小为4x4)
Z = np.ones((16,16))
k = 4
S = np.add.reduceat(np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0), np.arange(0, Z.shape[1], k), axis=1)
print(S)
88、如何利用numpy数组实现Game of Life
defiterate(Z):# Count neighbours
N =(Z[0:-2,0:-2]+ Z[0:-2,1:-1]+ Z[0:-2,2:]+
Z[1:-1,0:-2]+ Z[1:-1,2:]+
Z[2:,0:-2]+ Z[2:,1:-1]+ Z[2:,2:])# Apply rules
birth =(N==3)&(Z[1:-1,1:-1]==0)
survive =((N==2)|(N==3))&(Z[1:-1,1:-1]==1)
Z[...]=0
Z[1:-1,1:-1][birth | survive]=1return Z
Z = np.random.randint(0,2,(50,50))for i inrange(100): Z = iterate(Z)print(Z)
89、找到一个数组的第n个最大值
Z = np.arange(10000)
np.random.shuffle(Z)
n =5# Slow
print (Z[np.argsort(Z)[-n:]])# Fast
print (Z[np.argpartition(-Z,n)[:n]])
90、给定任意个数向量,创建笛卡尔积(每一个元素的每一种组合)
defcartesian(arrays):
arrays =[np.asarray(a)for a in arrays]
shape =(len(x)for x in arrays)
ix = np.indices(shape, dtype=int)
ix = ix.reshape(len(arrays),-1).T
for n, arr inenumerate(arrays):
ix[:, n]= arrays[n][ix[:, n]]return ix
print(cartesian(([1,2,3],[4,5],[6,7])))
# Author: Gabe Schwartz
A = np.random.randint(0,5,(8,3))
B = np.random.randint(0,5,(2,2))
C = (A[..., np.newaxis, np.newaxis] == B)
rows = np.where(C.any((3,1)).all(1))[0]
print(rows)
94. 一个10x3的矩阵,如何分解出有不全相同值的行
Z = np.random.randint(0,5,(10,3))print(Z)# solution for arrays of all dtypes (including string arrays and record arrays)
E = np.all(Z[:,1:]== Z[:,:-1], axis=1)
U = Z[~E]print(U)# soluiton for numerical arrays only, will work for any number of columns in Z
U = Z[Z.max(axis=1)!= Z.min(axis=1),:]print(U)
95. 将一个整数向量转换为二进制矩阵
I = np.arange(10)
B =((I.reshape(-1,1)&(2**np.arange(8)))!=0).astype(int)print(B[:,::-1])
96. 给定一个二维数组,提取出唯一的行?
Z = np.random.randint(0,2,(6,3))
T = np.ascontiguousarray(Z).view(np.dtype((np.void, Z.dtype.itemsize * Z.shape[1])))
_, idx = np.unique(T, return_index=True)
uZ = Z[idx]
print(uZ)
A = np.random.uniform(0,1,10)
B = np.random.uniform(0,1,10)
np.einsum('i->', A) # np.sum(A)
np.einsum('i,i->i', A, B) # A * B
np.einsum('i,i', A, B) # np.inner(A, B)
np.einsum('i,j->ij', A, B) # np.outer(A, B)
X = np.asarray([[1.0, 0.0, 3.0, 8.0],
[2.0, 0.0, 1.0, 1.0],
[1.5, 2.5, 1.0, 0.0]])
n =4
M = np.logical_and.reduce(np.mod(X,1)==0, axis=-1)
M &=(X.sum(axis=-1)== n)print(X[M])
100、对于一个一维数组X,计算它boostrapped之后的95%置信区间的平均值
X = np.random.randn(100) # random 1D array
N = 1000 # number of bootstrap samples
idx = np.random.randint(0, X.size, (N, X.size))
means = X[idx].mean(axis=1)
confint = np.percentile(means, [2.5, 97.5])
print(confint)