numpy100问51-100问

也和上一篇一样,只做一个代码留存

51-75问哔哩哔哩视频地址 https://www.bilibili.com/video/BV12h411d71j

76-100问哔哩哔哩视频地址 https://www.bilibili.com/video/BV1pf4y197NE

import numpy as np
# Z = np.zeros(10, [ ('position', [ ('x', float, 1),
#                                   ('y', float, 1)]),
#                    ('color',    [ ('r', float, 1),
#                                   ('g', float, 1),
#                                   ('b', float, 1)])])
# print (Z)

# Z = np.random.random((10,2))
# print(Z)
# X,Y = np.atleast_2d(Z[:,0], Z[:,1])
# print(X,'\n',Y)
# D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2)
# print (D)

# floor ceil round,trunc,.astype()

# Z = np.arange(9).reshape(3,3)
# for index, value in np.ndenumerate(Z):
#     print (index, value)
# for index in np.ndindex(Z.shape):
#     print (index, Z[index])

# n = 10
# p = 3
# Z = np.zeros((n,n))
# np.put(Z, np.random.choice(range(n*n), p, replace=False),1)
# print (Z)

# X = np.random.rand(5, 10)
# # Recent versions of numpy
# print(X.mean(axis=1, keepdims=True))
# Y = X - X.mean(axis=1, keepdims=True)
# print(Y)
# print(X.mean(axis=1).reshape(-1, 1))
# Y = X - X.mean(axis=1).reshape(-1, 1)
# print (Y)

# Z = np.random.randint(0,10,(3,3))
# print (Z)
# print(Z[:,1].argsort())
# print (Z[Z[:,1].argsort()]) #Z[[1 0 2]]

# Z = np.random.randint(0,3,(3,10))
# print(Z)
# print ((~Z.any(axis=0)).any())

# Z = np.random.uniform(0,1,(2,10))
# print(Z)
# z = 0.5
# m = Z.flat[np.abs(Z - z).argmin()]
# print (m)
# print(np.abs(Z - z))
# print(np.abs(Z - z).argmin())
# m1 = Z[np.abs(Z - z).argmin()]   #argmin()默认吧数组给展开了,所以返会的下标只是一个数,不是一个索引坐标,不能处理二维及以上数组
# print(m1)

# Z = np.ones(10)
# I = np.random.randint(0,len(Z),20)
# Z += np.bincount(I, minlength=len(Z))
# print(Z)
# # 方法2
# np.add.at(Z, I, 1)
# print(Z)

# A = np.random.uniform(0,1,(5,5))
# B = np.random.uniform(0,1,(5,5))
# # slow version
# print(np.diag(np.dot(A, B)))
# # 方法2
# # Fast version
# print(np.sum(A * B.T, axis=1))
# # 方法3
# # Faster version
# print(np.einsum("ij,ji->i", A, B))

# Z = np.array([1,2,3,4,5])
# nz = 3
# Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))
# Z0[::nz+1] = Z
# print (Z0)
# Z0[0::nz+1] = Z
# print(Z0)

# A = np.arange(25).reshape(5,5)
# A[[0,1]] = A[[1,0]]
# print (A)

# C = np.bincount([1,1,2,3,4,4,6])
# A = np.repeat(np.arange(len(C)), C)
# print (A)


# def moving_average(a, n=3) :
#     ret = np.cumsum(a, dtype=float)
#     ret[n:] = ret[n:] - ret[:-n]
#     return ret[n - 1:] / n
# Z = np.arange(20)

# print(moving_average(Z, n=3))

# Z = np.random.randint(0,2,100)
# print(Z)
# print(np.logical_not(Z, out=Z))
# Z = np.random.uniform(-1.0,1.0,100)
# print(Z)
# print(np.negative(Z, out=Z)) #对数组中每一个元素取相反数。

# def distance(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))

# 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))
# print (R)

# Z = np.random.uniform(0,1,(10,10))
# U, S, V = np.linalg.svd(Z) # Singular Value Decomposition
# print(S > 1e-10)
# rank = np.sum(S > 1e-10)
# print (rank)

# Z = np.random.randint(0,10,10)
# print(Z)
# print (np.bincount(Z).argmax())

# 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)
# print (C)

# 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)
# # It works, because:
# # M is (p,n,n)
# # V is (p,n,1)
# # Thus, summing over the paired axes 0 and 0 (of M and V independently),
# # and 2 and 1, to remain with a (n,1) vector.

# def iterate(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] = 1
#     return Z
# Z = np.random.randint(0,2,(50,50))
# for i in range(100): Z = iterate(Z)
# print (Z)

# Z = np.arange(10000)
# np.random.shuffle(Z)
# n = 5
# print (Z[np.argsort(Z)[-n]])
# print (Z[np.argpartition(-Z,n)[:n]])

# 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)
# print(E)
# U = Z[~E]
# print (U)
# # 方法2
# # 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)

Z = np.random.randint(0,2,(6,3))
print(Z)
T = np.ascontiguousarray(Z).view(np.dtype((np.void, Z.dtype.itemsize * Z.shape[1])))
print(T)
_, idx = np.unique(T, return_index=True)
uZ = Z[idx]
print (uZ)

numpy100问51-75问

numpy100问76-100

猜你喜欢

转载自blog.csdn.net/qq_43657442/article/details/108007112