Numpy练习100题--难度★★★

时间:2021-10-16 18:33:11
  1. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★)

  2. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)

  3. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★★)

  4. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)

  5. Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices? (★★★)

  6. How to get the diagonal of a dot product? (★★★)

  7. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★)

  8. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)

  9. How to swap two rows of an array? (★★★)

  10. Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles (★★★)

  11. Given an array C that is a bincount, how to produce an array A such that np.bincount(A) == C? (★★★)

  12. How to compute averages using a sliding window over an array? (★★★)

  13. 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]) (★★★)

  14. How to negate a boolean, or to change the sign of a float inplace? (★★★)

  15. 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])? (★★★)

  16. 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])? (★★★)

  17. 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) (★★★)

  18. 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]]? (★★★)

  19. Compute a matrix rank (★★★)

  20. How to find the most frequent value in an array?

  21. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)

  22. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)

  23. 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)) (★★★)

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

  25. How to implement the Game of Life using numpy arrays? (★★★)

  26. How to get the n largest values of an array (★★★)

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

  28. How to create a record array from a regular array? (★★★)

  29. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★)

  30. 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? (★★★)

  31. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) (★★★)

  32. Convert a vector of ints into a matrix binary representation (★★★)

  33. Given a two dimensional array, how to extract unique rows? (★★★)

  34. Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function (★★★)

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

  36. 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. (★★★)

  37. 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). (★★★)