numpy 100题练习 <二>

2022-04-02 19:52:53 浏览数 (1)

上次发的numpy 100题练习 <一>不知道大家学的咋样了大概又放在收藏夹里吃灰了吧,我们加班加点终于把后一半给翻译出来啦~希望各位观众老爷们喜欢~

Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆) 创建一个表示位置(x,y)和颜色(r,g,b)的结构化数组

代码语言:javascript复制
Z = np.zeros(10, [ ('position', [ ('x', float, 1),
                                  ('y', float, 1)]),
                   ('color',    [ ('r', float, 1),
                                  ('g', float, 1),
                                  ('b', float, 1)])])
print(Z)

Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆) 对一个表示坐标形状为(100,2)的随机向量,找到点与点的距离

代码语言:javascript复制
Z = np.random.random((10,2))
X,Y = np.atleast_2d(Z[:,0], Z[:,1])
D = np.sqrt( (X-X.T)**2   (Y-Y.T)**2)
print (D)
# 方法2,用scipy会快很多
import scipy
import scipy.spatial
D = scipy.spatial.distance.cdist(Z,Z)
print (D)

How to convert a float (32 bits) array into an integer (32 bits) in place?(★★☆) 如何将32位的浮点数(float)转换为对应的整数(integer)?

代码语言:javascript复制
Z = np.arange(10, dtype=np.int32)
Z = Z.astype(np.float32, copy=False)
print (Z)

How to read the following file? (★★☆) 如何读取以下文件?

代码语言:javascript复制
1, 2, 3, 4, 5
6,  ,  , 7, 8
 ,  , 9,10,11
代码语言:javascript复制
Z = np.genfromtxt(s, delimiter=",", dtype=np.int)
print(Z)

What is the equivalent of enumerate for numpy arrays? (★★☆) 对于numpy数组,enumerate的等价操作是什么?

代码语言:javascript复制
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])

Generate a generic 2D Gaussian-like array (★★☆) 生成一个通用的二维Gaussian-like数组

代码语言:javascript复制
X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10))
D = np.sqrt(X*X Y*Y)
sigma, mu = 1.0, 0.0
G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) )
print (G)

How to randomly place p elements in a 2D array? (★★☆) 对一个二维数组,如何在其内部随机放置p个元素?

代码语言:javascript复制
n = 10
p = 3
Z = np.zeros((n,n))
np.put(Z, np.random.choice(range(n*n), p, replace=False),1)
print (Z)

Subtract the mean of each row of a matrix (★★☆) 减去一个矩阵中的每一行的平均值

代码语言:javascript复制
X = np.random.rand(5, 10)
# numpy最新版本的方法
Y = X - X.mean(axis=1, keepdims=True)
print(Y)
# numpy之前版本的方法
Y = X - X.mean(axis=1).reshape(-1, 1)
print (Y)

How to I sort an array by the nth column? (★★☆) 如何通过第n列对一个数组进行排序?

代码语言:javascript复制
Z = np.random.randint(0,10,(3,3))
print (Z)
print (Z[Z[:,1].argsort()])

How to tell if a given 2D array has null columns? (★★☆) 如何检查一个二维数组是否有空列?

代码语言:javascript复制
Z = np.random.randint(0,3,(3,10))
print ((~Z.any(axis=0)).any())

Find the nearest value from a given value in an array (★★☆) 从数组中的给定值中找出最近的值

代码语言:javascript复制
Z = np.random.uniform(0,1,10)
z = 0.5
m = Z.flat[np.abs(Z - z).argmin()]
print (m)

Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆) 如何用迭代器(iterator)计算两个分别具有形状(1,3)和(3,1)的数组?

代码语言:javascript复制
A = np.arange(3).reshape(3,1)
B = np.arange(3).reshape(1,3)
it = np.nditer([A,B,None])
for x,y,z in it:
    z[...] = x   y
print (it.operands[2])

Create an array class that has a name attribute (★★☆) 创建一个具有name属性的数组类

代码语言:javascript复制
class NamedArray(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 is None: return
        self.info = getattr(obj, 'name', "no name")

Z = NamedArray(np.arange(10), "range_10")
print (Z.name)

Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★) 考虑一个给定的向量,如何对由第二个向量索引的每个元素加1(小心重复的索引)?

代码语言:javascript复制
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)

How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★) 根据索引列表(I),如何将向量(X)的元素累加到数组(F)?

代码语言:javascript复制
X = [1,2,3,4,5,6]
I = [1,3,9,3,4,1]
F = np.bincount(I,X)
print (F)

Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★★) 考虑一个(dtype=ubyte) 的 (w,h,3)图像,计算其唯一颜色的数量

代码语言:javascript复制
w,h = 16,16
I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte)
F = I[...,0]*(256*256)   I[...,1]*256  I[...,2]
n = len(np.unique(F))
print (n)

Considering a four dimensions array, how to get sum over the last two axis at once? (★★★) 考虑一个四维数组,如何一次性计算出最后两个轴(axis)的和?

代码语言:javascript复制
A = np.random.randint(0,10,(3,4,3,4))
sum = A.sum(axis=(-2,-1))
print (sum)
# 方法2
sum = A.reshape(A.shape[:-2]   (-1,)).sum(axis=-1)
print (sum)

Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices? (★★★) 考虑一个一维向量D,如何使用相同大小的向量S来计算D子集的均值?

代码语言:javascript复制
D = np.random.uniform(0,1,100)
S = np.random.randint(0,10,100)
D_sums = np.bincount(S, weights=D)
D_counts = np.bincount(S)
D_means = D_sums / D_counts
print (D_means)
# 方法2
import pandas as pd
print(pd.Series(D).groupby(S).mean())

How to get the diagonal of a dot product? (★★★) 如何获得点积 dot prodcut的对角线元素?

代码语言:javascript复制
A = np.random.uniform(0,1,(5,5))
B = np.random.uniform(0,1,(5,5))
# 比较慢
np.diag(np.dot(A, B))
# 方法2,快一点
np.sum(A * B.T, axis=1)
# 方法3,快很多
np.einsum("ij,ji->i", A, B)

Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★) 考虑一个向量[1,2,3,4,5],如何建立一个新的向量,在这个新向量中每个值之间有3个连续的零?(★★★)

代码语言:javascript复制
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)

Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★) 考虑一个维度(5,5,3)的数组,如何将其与一个(5,5)的数组相乘?

代码语言:javascript复制
A = np.ones((5,5,3))
B = 2*np.ones((5,5))
print (A * B[:,:,None])

How to swap two rows of an array? (★★★) 如何对一个数组中任意两行做交换?

代码语言:javascript复制
A = np.arange(25).reshape(5,5)
A[[0,1]] = A[[1,0]]
print (A)

Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles (★★★) 考虑一个可以描述10个三角形的triplets,找到可以分割全部三角形的line segment

代码语言:javascript复制
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)
print (G)

Given an array C that is a bincount, how to produce an array A such that np.bincount(A) == C? (★★★) 给定一个二进制的数组C,如何产生一个数组A满足np.bincount(A)==C

代码语言:javascript复制
C = np.bincount([1,1,2,3,4,4,6])
A = np.repeat(np.arange(len(C)), C)
print (A)

How to compute averages using a sliding window over an array? (★★★) 如何通过滑动窗口计算一个数组的平均数?

代码语言:javascript复制
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))

Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) (★★★) 考虑一个一维数组Z,构造一个2维数组,第一行是(Z[0],Z[1],Z[2]),后面每一行都后移一个元素(最后一行是(Z[-3],Z[-2],Z[-1])) (提示: from numpy.lib import stride_tricks)

代码语言:javascript复制
from numpy.lib import stride_tricks

def rolling(a, window):
    shape = (a.size - window   1, window)
    strides = (a.itemsize, a.itemsize)
    return stride_tricks.as_strided(a, shape=shape, strides=strides)
Z = rolling(np.arange(10), 3)
print (Z)

How to negate a boolean, or to change the sign of a float inplace? (★★★) 如何对布尔值取反,或者原位(in-place)改变浮点数的符号(sign)?

代码语言:javascript复制
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)

Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to compute distance from p to each line i (P0[i],P1[i])? (★★★) 考虑两组点集P0和P1去描述一组线(二维)和一个点p,如何计算点p到每一条线 i (P0[i],P1[i])的距离?

代码语言:javascript复制
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))

Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])? (★★★)

考虑两组点集P0和P1去描述一组线(二维)和一组点集P,如何计算每一个点 j(P[j]) 到每一条线 i (P0[i],P1[i])的距离?

代码语言:javascript复制
# 接上一题的函数
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]))

Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a fill value when necessary) (★★★) 对于任意一个数组,写一个可以给定中心元素和固定形状,输出数组子集的功能(如果有需要,可自动填充数值)

代码语言:javascript复制
Z = np.random.randint(0,10,(10,10))
shape = (5,5)
fill  = 0
position = (1,1)

R = np.ones(shape, dtype=Z.dtype)*fill
P  = np.array(list(position)).astype(int)
Rs = np.array(list(R.shape)).astype(int)
Zs = np.array(list(Z.shape)).astype(int)

R_start = np.zeros((len(shape),)).astype(int)
R_stop  = np.array(list(shape)).astype(int)
Z_start = (P-Rs//2)
Z_stop  = (P Rs//2) Rs%2

R_start = (R_start - np.minimum(Z_start,0)).tolist()
Z_start = (np.maximum(Z_start,0)).tolist()
R_stop = np.maximum(R_start, (R_stop - np.maximum(Z_stop-Zs,0))).tolist()
Z_stop = (np.minimum(Z_stop,Zs)).tolist()

r = [slice(start,stop) for start,stop in zip(R_start,R_stop)]
z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)]
R[r] = Z[z]
print (Z)
print (R)

Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]? (★★★) 考虑一个数组Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14],如何生成一个数组R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ...,[11,12,13,14]]? (提示: stride_tricks.as_strided)

代码语言:javascript复制
Z = np.arange(1,15,dtype=np.uint32)
R = stride_tricks.as_strided(Z,(11,4),(4,4))
print (R)

Compute a matrix rank (★★★) 计算一个矩阵的秩

代码语言:javascript复制
Z = np.random.uniform(0,1,(10,10))
U, S, V = np.linalg.svd(Z) # 奇异值分解
rank = np.sum(S > 1e-10)
print (rank)

How to find the most frequent value in an array?(★★★) 如何找到一个数组中出现频率最高的值?

代码语言:javascript复制
Z = np.random.randint(0,10,50)
print (np.bincount(Z).argmax())

Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★) 从一个10x10的矩阵中提取出连续的3x3区块

代码语言:javascript复制
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)

Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★) 创建一个满足 Z[i,j] == Z[j,i]的子类

代码语言:javascript复制
class Symetric(np.ndarray):
    def __setitem__(self, index, value):
        i,j = index
        super(Symetric, self).__setitem__((i,j), value)
        super(Symetric, self).__setitem__((j,i), value)

def symetric(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
print (S)

Consider a set of p matrices wich shape (n,n) and a set of p vectors with shape (n,1). How to compute the sum of of the p matrix products at once? (result has shape (n,1)) (★★★) 考虑p个 nxn 矩阵和p个形状为(n,1)的向量,如何直接计算p个矩阵的乘积的和(答案的形状是(n,1))?

代码语言:javascript复制
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)

Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★) 对于一个16x16的数组,如何得到一个区域(block-sum)的和(区域大小为4x4)?

代码语言:javascript复制
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)

How to implement the Game of Life using numpy arrays? (★★★) 如何利用numpy数组实现Game of Life?

代码语言:javascript复制
def iterate(Z):
    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:])

    # 游戏规则
    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)

How to get the n largest values of an array (★★★) 如何找到一个数组的第n个最大值?

代码语言:javascript复制
Z = np.arange(10000)
np.random.shuffle(Z)
n = 5

# 慢
print (Z[np.argsort(Z)[-n:]])
# 方法2,快
print (Z[np.argpartition(-Z,n)[:n]])

Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★) 给定任意个数向量,创建笛卡尔积(每一个元素的每一种组合)

代码语言:javascript复制
def cartesian(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 in enumerate(arrays):
        ix[:, n] = arrays[n][ix[:, n]]

    return ix

print (cartesian(([1, 2, 3], [4, 5], [6, 7])))

How to create a record array from a regular array? (★★★) 如何从一个正常数组创建记录数组(record array)?

代码语言:javascript复制
Z = np.array([("Hello", 2.5, 3),
              ("World", 3.6, 2)])
R = np.core.records.fromarrays(Z.T, 
                               names='col1, col2, col3',
                               formats = 'S8, f8, i8')
print (R)

Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★) 考虑一个大向量Z, 用三种不同的方法计算它的立方

代码语言:javascript复制
x = np.random.rand()
np.power(x,3)
# 方法2
x*x*x
# 方法3
np.einsum('i,i,i->i',x,x,x)

Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain elements of each row of B regardless of the order of the elements in B? (★★★) 考虑两个形状分别为(8,3) 和(2,2)的数组A和B. 如何在数组A中找到满足包含B中元素的行?(不考虑B中每行元素顺序)?

代码语言:javascript复制
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)

Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) (★★★) 考虑一个10x3的矩阵,分解出有不全相同值的行 (如 [2,2,3])

代码语言:javascript复制
Z = np.random.randint(0,5,(10,3))
print (Z)

# 方法一,适用于所有类型的数组,包括字符数组以及record array
E = np.all(Z[:,1:] == Z[:,:-1], axis=1)
U = Z[~E]
print (U)
# 方法2,只适用于数字类型数组
U = Z[Z.max(axis=1) != Z.min(axis=1),:]
print (U)

Convert a vector of ints into a matrix binary representation (★★★) 将一个整数向量转换为matrix binary的表现形式 (★★★)

代码语言:javascript复制
I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128])
B = ((I.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int)
print(B[:,::-1])
# 方法2
print (np.unpackbits(I[:, np.newaxis], axis=1))

Given a two dimensional array, how to extract unique rows? (★★★) 给定一个二维数组,如何提取出唯一的(unique)行?

代码语言:javascript复制
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)

Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function (★★★) 考虑两个向量A和B,写出用einsum等式对应的inner, outer, sum, mul函数

代码语言:javascript复制
A = np.random.uniform(0,1,10)
B = np.random.uniform(0,1,10)
print ('sum')
print (np.einsum('i->', A))# np.sum(A)
print ('A * B')
print (np.einsum('i,i->i', A, B)) # A * B
print ('inner')
print (np.einsum('i,i', A, B))    # np.inner(A, B)
print ('outer')
print (np.einsum('i,j->ij', A, B))    # np.outer(A, B)

Considering a path described by two vectors (X,Y), how to sample it using equidistant samples (★★★)? 考虑一个由两个向量描述的路径(X,Y),如何用等距样例(equidistant samples)对其进行采样(sample)?

代码语言:javascript复制
phi = np.arange(0, 10*np.pi, 0.1)
a = 1
x = a*phi*np.cos(phi)
y = a*phi*np.sin(phi)

dr = (np.diff(x)**2   np.diff(y)**2)**.5 # segment lengths
r = np.zeros_like(x)
r[1:] = np.cumsum(dr)                # integrate path
r_int = np.linspace(0, r.max(), 200) # regular spaced path
x_int = np.interp(r_int, r, x)       # integrate path
y_int = np.interp(r_int, r, y)

Given an integer n and a 2D array X, select from X the rows which can be interpreted as draws from a multinomial distribution with n degrees, i.e., the rows which only contain integers and which sum to n. (★★★) 给定一个整数n和一个2D数组X,从X中选出行,满足n次多项式分布,即行内只有整数且相加为n

代码语言:javascript复制
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])

Compute bootstrapped 95% confidence intervals for the mean of a 1D array X,i.e. resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means. (★★★ 对于一个一维数组X,计算它boostrapped之后的95%置信区间的平均值。

代码语言:javascript复制
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)

摩拳擦掌想做题试试手感的

0 人点赞