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)
在這裏插入圖片描述

82.Compute a matrix rank

計算一個矩陣的秩
在這裏插入圖片描述

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

如何找到一個數組中出現頻率最高的值
在這裏插入圖片描述

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

從一個10x10的矩陣中提取出連續的3x3區塊
在這裏插入圖片描述

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

創建一個滿足 Z[i,j] == Z[j,i]的子類
在這裏插入圖片描述

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))
在這裏插入圖片描述

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

對於一個16x16的數組,如何得到一個區域(block-sum)的和(區域大小爲4x4)
在這裏插入圖片描述

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

如何利用numpy數組實現Game of Life
在這裏插入圖片描述

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

如何找到一個數組的第n個最大值
在這裏插入圖片描述

numpy.random.shuffle()

numpy.random,shuffle(x)是進行原地洗牌,直接改變x的值,而無返回值。
對於多維度的array來說,只對第一維進行洗牌,比如一個 3×33×3 的array,只對行之間進行洗牌,而行內內容保持不變。
在這裏插入圖片描述

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

給定任意個數向量,創建笛卡爾積(每一個元素的每一種組合)
在這裏插入圖片描述

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

如何從一個正常數組創建記錄數組(record array)
在這裏插入圖片描述

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

考慮一個大向量Z, 用三種不同的方法計算它的立方
在這裏插入圖片描述

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中每行元素順序)
在這裏插入圖片描述

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

考慮一個10x3的矩陣,分解出有不全相同值的行
在這裏插入圖片描述

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

將一個整數向量轉換爲matrix binary的表現形式
在這裏插入圖片描述

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

給定一個二維數組,如何提取出唯一的(unique)行
在這裏插入圖片描述

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

考慮兩個向量A和B,寫出用einsum等式對應的inner, outer, sum, mul函數
在這裏插入圖片描述

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

考慮一個由兩個向量描述的路徑(X,Y),如何用等距樣例(equidistant samples)對其進行採樣(sample)
在這裏插入圖片描述

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
在這裏插入圖片描述

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%置信區間的平均值
在這裏插入圖片描述

發佈了82 篇原創文章 · 獲贊 122 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章