NumPy百题(五)

上一篇:NumPy百题(四)

81.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)
NumPy百题(五)

82.Compute a matrix rank

计算一个矩阵的秩
NumPy百题(五)

83.How to find the most frequent value in an array

如何找到一个数组中出现频率最高的值
NumPy百题(五)

84.Extract all the contiguous 3x3 blocks from a random 10x10 matrix

从一个10x10的矩阵中提取出连续的3x3区块
NumPy百题(五)

85.Create a 2D array subclass such that Z[i,j] == Z[j,i]

创建一个满足 Z[i,j] == Z[j,i]的子类
NumPy百题(五)

86.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))
NumPy百题(五)

87.Consider a 16x16 array, how to get the block-sum (block size is 4x4)

对于一个16x16的数组,如何得到一个区域(block-sum)的和(区域大小为4x4)
NumPy百题(五)

88.How to implement the Game of Life using numpy arrays

如何利用numpy数组实现Game of Life
NumPy百题(五)

89.How to get the n largest values of an array

如何找到一个数组的第n个最大值
NumPy百题(五)

numpy.random.shuffle()

numpy.random,shuffle(x)是进行原地洗牌,直接改变x的值,而无返回值。
对于多维度的array来说,只对第一维进行洗牌,比如一个 3×33×3 的array,只对行之间进行洗牌,而行内内容保持不变。
NumPy百题(五)

90.Given an arbitrary number of vectors, build the cartesian product (every combinations of every item)

给定任意个数向量,创建笛卡尔积(每一个元素的每一种组合)
NumPy百题(五)

91.How to create a record array from a regular array

如何从一个正常数组创建记录数组(record array)
NumPy百题(五)

92.Consider a large vector Z, compute Z to the power of 3 using 3 different methods

考虑一个大向量Z, 用三种不同的方法计算它的立方
NumPy百题(五)

93.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中每行元素顺序)
NumPy百题(五)

94.Considering a 10x3 matrix, extract rows with unequal values

考虑一个10x3的矩阵,分解出有不全相同值的行
NumPy百题(五)

95.Convert a vector of ints into a matrix binary representation

将一个整数向量转换为matrix binary的表现形式
NumPy百题(五)

96.Given a two dimensional array, how to extract unique rows

给定一个二维数组,如何提取出唯一的(unique)行
NumPy百题(五)

97.Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function

考虑两个向量A和B,写出用einsum等式对应的inner, outer, sum, mul函数
NumPy百题(五)

98.Considering a path described by two vectors (X,Y), how to sample it using equidistant samples

考虑一个由两个向量描述的路径(X,Y),如何用等距样例(equidistant samples)对其进行采样(sample)
NumPy百题(五)

99.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
NumPy百题(五)

100.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%置信区间的平均值
NumPy百题(五)