From 869a6dc1f1d28e421dc36a152429d442d977118c Mon Sep 17 00:00:00 2001 From: JasonZhang Date: Sun, 24 Jan 2021 17:02:47 +0800 Subject: [PATCH] FEAT change #61 to find 3 nearest values in order --- 100_Numpy_exercises.ipynb | 228 ++++++++++++++++-- 100_Numpy_exercises.md | 2 +- 100_Numpy_exercises_with_hints.md | 4 +- ...mpy_exercises_with_hints_with_solutions.md | 24 +- 100_Numpy_exercises_with_solutions.md | 20 +- 100_Numpy_random.ipynb | 27 +-- source/exercises100.ktx | 11 +- 7 files changed, 245 insertions(+), 71 deletions(-) diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index bef43000..a62bb75e 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -2,6 +2,7 @@ "cells": [ { "cell_type": "markdown", + "id": "advanced-beaver", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -17,6 +18,7 @@ }, { "cell_type": "markdown", + "id": "greenhouse-scientist", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -24,6 +26,7 @@ }, { "cell_type": "markdown", + "id": "superior-routine", "metadata": {}, "source": [ "Run the `initialize.py` module, then for each question you can query the\n", @@ -33,6 +36,7 @@ { "cell_type": "code", "execution_count": null, + "id": "automated-discharge", "metadata": {}, "outputs": [], "source": [ @@ -41,6 +45,7 @@ }, { "cell_type": "markdown", + "id": "identical-exemption", "metadata": {}, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" @@ -49,12 +54,14 @@ { "cell_type": "code", "execution_count": null, + "id": "governmental-debut", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "manufactured-cargo", "metadata": {}, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" @@ -63,12 +70,14 @@ { "cell_type": "code", "execution_count": null, + "id": "joint-frost", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "affiliated-uruguay", "metadata": {}, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" @@ -77,12 +86,14 @@ { "cell_type": "code", "execution_count": null, + "id": "welcome-briefs", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "intelligent-moldova", "metadata": {}, "source": [ "#### 4. How to find the memory size of any array (★☆☆)" @@ -91,12 +102,14 @@ { "cell_type": "code", "execution_count": null, + "id": "renewable-respect", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "modular-circulation", "metadata": {}, "source": [ "#### 5. How to get the documentation of the numpy add function from the command line? (★☆☆)" @@ -105,12 +118,14 @@ { "cell_type": "code", "execution_count": null, + "id": "daily-reason", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "together-attack", "metadata": {}, "source": [ "#### 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)" @@ -119,12 +134,14 @@ { "cell_type": "code", "execution_count": null, + "id": "ambient-source", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "known-cheese", "metadata": {}, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" @@ -133,12 +150,14 @@ { "cell_type": "code", "execution_count": null, + "id": "paperback-mentor", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "pointed-disclosure", "metadata": {}, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" @@ -147,12 +166,14 @@ { "cell_type": "code", "execution_count": null, + "id": "heard-camera", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "dress-citation", "metadata": {}, "source": [ "#### 9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)" @@ -161,12 +182,14 @@ { "cell_type": "code", "execution_count": null, + "id": "unlikely-magnet", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "identified-headset", "metadata": {}, "source": [ "#### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)" @@ -175,12 +198,14 @@ { "cell_type": "code", "execution_count": null, + "id": "shaped-ordering", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "nuclear-migration", "metadata": {}, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" @@ -189,12 +214,14 @@ { "cell_type": "code", "execution_count": null, + "id": "ahead-extraction", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "manual-automation", "metadata": {}, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" @@ -203,12 +230,14 @@ { "cell_type": "code", "execution_count": null, + "id": "loving-recycling", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "regulated-consent", "metadata": {}, "source": [ "#### 13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)" @@ -217,12 +246,14 @@ { "cell_type": "code", "execution_count": null, + "id": "internal-honduras", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "fourth-calvin", "metadata": {}, "source": [ "#### 14. Create a random vector of size 30 and find the mean value (★☆☆)" @@ -231,12 +262,14 @@ { "cell_type": "code", "execution_count": null, + "id": "considerable-surfing", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "running-grade", "metadata": {}, "source": [ "#### 15. Create a 2d array with 1 on the border and 0 inside (★☆☆)" @@ -245,12 +278,14 @@ { "cell_type": "code", "execution_count": null, + "id": "alleged-royalty", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "wooden-twelve", "metadata": {}, "source": [ "#### 16. How to add a border (filled with 0's) around an existing array? (★☆☆)" @@ -259,12 +294,14 @@ { "cell_type": "code", "execution_count": null, + "id": "technical-garden", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "norman-procedure", "metadata": {}, "source": [ "#### 17. What is the result of the following expression? (★☆☆)\n", @@ -281,12 +318,14 @@ { "cell_type": "code", "execution_count": null, + "id": "other-briefing", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "indoor-programmer", "metadata": {}, "source": [ "#### 18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)" @@ -295,12 +334,14 @@ { "cell_type": "code", "execution_count": null, + "id": "secret-singer", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "victorian-apparatus", "metadata": {}, "source": [ "#### 19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)" @@ -309,12 +350,14 @@ { "cell_type": "code", "execution_count": null, + "id": "active-cornell", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "inner-market", "metadata": {}, "source": [ "#### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?" @@ -323,12 +366,14 @@ { "cell_type": "code", "execution_count": null, + "id": "instant-vision", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "constitutional-determination", "metadata": {}, "source": [ "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" @@ -337,12 +382,14 @@ { "cell_type": "code", "execution_count": null, + "id": "twelve-focus", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "circular-hello", "metadata": {}, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" @@ -351,12 +398,14 @@ { "cell_type": "code", "execution_count": null, + "id": "eleven-violence", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "blank-handy", "metadata": {}, "source": [ "#### 23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)" @@ -365,12 +414,14 @@ { "cell_type": "code", "execution_count": null, + "id": "joined-uruguay", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "northern-asset", "metadata": {}, "source": [ "#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)" @@ -379,12 +430,14 @@ { "cell_type": "code", "execution_count": null, + "id": "architectural-nicholas", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "cathedral-dairy", "metadata": {}, "source": [ "#### 25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)" @@ -393,12 +446,14 @@ { "cell_type": "code", "execution_count": null, + "id": "dangerous-initial", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "adapted-title", "metadata": {}, "source": [ "#### 26. What is the output of the following script? (★☆☆)\n", @@ -414,12 +469,14 @@ { "cell_type": "code", "execution_count": null, + "id": "third-rachel", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "lined-franklin", "metadata": {}, "source": [ "#### 27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)\n", @@ -436,12 +493,14 @@ { "cell_type": "code", "execution_count": null, + "id": "sustainable-purchase", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "configured-boundary", "metadata": {}, "source": [ "#### 28. What are the result of the following expressions?\n", @@ -455,12 +514,14 @@ { "cell_type": "code", "execution_count": null, + "id": "accredited-double", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "sharing-single", "metadata": {}, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" @@ -469,12 +530,14 @@ { "cell_type": "code", "execution_count": null, + "id": "norman-attribute", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "included-program", "metadata": {}, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" @@ -483,12 +546,14 @@ { "cell_type": "code", "execution_count": null, + "id": "turkish-clarity", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "precise-entity", "metadata": {}, "source": [ "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" @@ -497,12 +562,14 @@ { "cell_type": "code", "execution_count": null, + "id": "stunning-fundamentals", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "designed-middle", "metadata": {}, "source": [ "#### 32. Is the following expressions true? (★☆☆)\n", @@ -514,12 +581,14 @@ { "cell_type": "code", "execution_count": null, + "id": "after-emerald", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "continuing-vampire", "metadata": {}, "source": [ "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" @@ -528,12 +597,14 @@ { "cell_type": "code", "execution_count": null, + "id": "informal-marsh", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "cleared-triumph", "metadata": {}, "source": [ "#### 34. How to get all the dates corresponding to the month of July 2016? (★★☆)" @@ -542,12 +613,14 @@ { "cell_type": "code", "execution_count": null, + "id": "deadly-martial", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "collectible-explorer", "metadata": {}, "source": [ "#### 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)" @@ -556,12 +629,14 @@ { "cell_type": "code", "execution_count": null, + "id": "elect-active", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "prostate-balance", "metadata": {}, "source": [ "#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)" @@ -570,12 +645,14 @@ { "cell_type": "code", "execution_count": null, + "id": "conscious-naples", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "amber-cleanup", "metadata": {}, "source": [ "#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)" @@ -584,12 +661,14 @@ { "cell_type": "code", "execution_count": null, + "id": "identical-steering", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "given-champion", "metadata": {}, "source": [ "#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)" @@ -598,12 +677,14 @@ { "cell_type": "code", "execution_count": null, + "id": "transsexual-sucking", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "neutral-forest", "metadata": {}, "source": [ "#### 39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)" @@ -612,12 +693,14 @@ { "cell_type": "code", "execution_count": null, + "id": "surprising-seafood", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "magnetic-surgery", "metadata": {}, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" @@ -626,12 +709,14 @@ { "cell_type": "code", "execution_count": null, + "id": "northern-determination", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "fleet-review", "metadata": {}, "source": [ "#### 41. How to sum a small array faster than np.sum? (★★☆)" @@ -640,12 +725,14 @@ { "cell_type": "code", "execution_count": null, + "id": "binding-mayor", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "greatest-dragon", "metadata": {}, "source": [ "#### 42. Consider two random array A and B, check if they are equal (★★☆)" @@ -654,12 +741,14 @@ { "cell_type": "code", "execution_count": null, + "id": "alert-labor", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "handy-society", "metadata": {}, "source": [ "#### 43. Make an array immutable (read-only) (★★☆)" @@ -668,12 +757,14 @@ { "cell_type": "code", "execution_count": null, + "id": "identified-liquid", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "harmful-little", "metadata": {}, "source": [ "#### 44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)" @@ -682,12 +773,14 @@ { "cell_type": "code", "execution_count": null, + "id": "liable-lecture", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "otherwise-volunteer", "metadata": {}, "source": [ "#### 45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)" @@ -696,12 +789,14 @@ { "cell_type": "code", "execution_count": null, + "id": "likely-output", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "representative-google", "metadata": {}, "source": [ "#### 46. Create a structured array with `x` and `y` coordinates covering the [0,1]x[0,1] area (★★☆)" @@ -710,12 +805,14 @@ { "cell_type": "code", "execution_count": null, + "id": "creative-albany", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "suspected-sapphire", "metadata": {}, "source": [ "#### 47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj))" @@ -724,12 +821,14 @@ { "cell_type": "code", "execution_count": null, + "id": "normal-breathing", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "muslim-hotel", "metadata": {}, "source": [ "#### 48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)" @@ -738,12 +837,14 @@ { "cell_type": "code", "execution_count": null, + "id": "tight-textbook", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "polish-chart", "metadata": {}, "source": [ "#### 49. How to print all the values of an array? (★★☆)" @@ -752,12 +853,14 @@ { "cell_type": "code", "execution_count": null, + "id": "unlike-compensation", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "pleasant-scotland", "metadata": {}, "source": [ "#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)" @@ -766,12 +869,14 @@ { "cell_type": "code", "execution_count": null, + "id": "genuine-correspondence", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "covered-sapphire", "metadata": {}, "source": [ "#### 51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)" @@ -780,12 +885,14 @@ { "cell_type": "code", "execution_count": null, + "id": "changed-metadata", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "verified-finland", "metadata": {}, "source": [ "#### 52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)" @@ -794,12 +901,14 @@ { "cell_type": "code", "execution_count": null, + "id": "dirty-patio", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "pursuant-restaurant", "metadata": {}, "source": [ "#### 53. How to convert a float (32 bits) array into an integer (32 bits) in place?" @@ -808,12 +917,14 @@ { "cell_type": "code", "execution_count": null, + "id": "immediate-imperial", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "enormous-division", "metadata": {}, "source": [ "#### 54. How to read the following file? (★★☆)\n", @@ -827,12 +938,14 @@ { "cell_type": "code", "execution_count": null, + "id": "sought-representation", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "composite-lemon", "metadata": {}, "source": [ "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" @@ -841,12 +954,14 @@ { "cell_type": "code", "execution_count": null, + "id": "framed-brother", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "female-voluntary", "metadata": {}, "source": [ "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" @@ -855,12 +970,14 @@ { "cell_type": "code", "execution_count": null, + "id": "plain-bargain", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "reasonable-nomination", "metadata": {}, "source": [ "#### 57. How to randomly place p elements in a 2D array? (★★☆)" @@ -869,12 +986,14 @@ { "cell_type": "code", "execution_count": null, + "id": "heated-arbor", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "artistic-orbit", "metadata": {}, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" @@ -883,12 +1002,14 @@ { "cell_type": "code", "execution_count": null, + "id": "flush-india", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "executive-accident", "metadata": {}, "source": [ "#### 59. How to sort an array by the nth column? (★★☆)" @@ -897,12 +1018,14 @@ { "cell_type": "code", "execution_count": null, + "id": "returning-taxation", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "fifteen-individual", "metadata": {}, "source": [ "#### 60. How to tell if a given 2D array has null columns? (★★☆)" @@ -911,26 +1034,30 @@ { "cell_type": "code", "execution_count": null, + "id": "verbal-march", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "further-comparative", "metadata": {}, "source": [ - "#### 61. Find the nearest value from a given value in an array (★★☆)" + "#### 61. Find the 3 nearest values in order from a given value in an array (★★☆)" ] }, { "cell_type": "code", "execution_count": null, + "id": "excited-battery", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "casual-house", "metadata": {}, "source": [ "#### 62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)" @@ -939,12 +1066,14 @@ { "cell_type": "code", "execution_count": null, + "id": "consistent-measurement", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "professional-banana", "metadata": {}, "source": [ "#### 63. Create an array class that has a name attribute (★★☆)" @@ -953,12 +1082,14 @@ { "cell_type": "code", "execution_count": null, + "id": "advisory-system", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "czech-relief", "metadata": {}, "source": [ "#### 64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★)" @@ -967,12 +1098,14 @@ { "cell_type": "code", "execution_count": null, + "id": "interpreted-rental", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "bigger-bullet", "metadata": {}, "source": [ "#### 65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)" @@ -981,12 +1114,14 @@ { "cell_type": "code", "execution_count": null, + "id": "intended-reference", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "common-houston", "metadata": {}, "source": [ "#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★★)" @@ -995,12 +1130,14 @@ { "cell_type": "code", "execution_count": null, + "id": "minor-favorite", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "dedicated-tiger", "metadata": {}, "source": [ "#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)" @@ -1009,12 +1146,14 @@ { "cell_type": "code", "execution_count": null, + "id": "broken-understanding", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "pending-klein", "metadata": {}, "source": [ "#### 68. Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices? (★★★)" @@ -1023,12 +1162,14 @@ { "cell_type": "code", "execution_count": null, + "id": "provincial-digit", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "iraqi-galaxy", "metadata": {}, "source": [ "#### 69. How to get the diagonal of a dot product? (★★★)" @@ -1037,12 +1178,14 @@ { "cell_type": "code", "execution_count": null, + "id": "super-biology", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "twelve-pound", "metadata": {}, "source": [ "#### 70. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★)" @@ -1051,12 +1194,14 @@ { "cell_type": "code", "execution_count": null, + "id": "middle-implement", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "fifteen-accordance", "metadata": {}, "source": [ "#### 71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)" @@ -1065,12 +1210,14 @@ { "cell_type": "code", "execution_count": null, + "id": "single-cosmetic", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "appointed-fitting", "metadata": {}, "source": [ "#### 72. How to swap two rows of an array? (★★★)" @@ -1079,12 +1226,14 @@ { "cell_type": "code", "execution_count": null, + "id": "color-branch", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "controlling-peter", "metadata": {}, "source": [ "#### 73. Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles (★★★)" @@ -1093,12 +1242,14 @@ { "cell_type": "code", "execution_count": null, + "id": "vulnerable-project", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "diagnostic-vessel", "metadata": {}, "source": [ "#### 74. Given a sorted array C that corresponds to a bincount, how to produce an array A such that np.bincount(A) == C? (★★★)" @@ -1107,12 +1258,14 @@ { "cell_type": "code", "execution_count": null, + "id": "genetic-marriage", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "closed-garage", "metadata": {}, "source": [ "#### 75. How to compute averages using a sliding window over an array? (★★★)" @@ -1121,12 +1274,14 @@ { "cell_type": "code", "execution_count": null, + "id": "moving-watson", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "electronic-biography", "metadata": {}, "source": [ "#### 76. 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]) (★★★)" @@ -1135,12 +1290,14 @@ { "cell_type": "code", "execution_count": null, + "id": "flying-barbados", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "trained-police", "metadata": {}, "source": [ "#### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★)" @@ -1149,12 +1306,14 @@ { "cell_type": "code", "execution_count": null, + "id": "caroline-single", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "expressed-venture", "metadata": {}, "source": [ "#### 78. 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])? (★★★)" @@ -1163,12 +1322,14 @@ { "cell_type": "code", "execution_count": null, + "id": "informal-knife", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "alleged-field", "metadata": {}, "source": [ "#### 79. 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])? (★★★)" @@ -1177,12 +1338,14 @@ { "cell_type": "code", "execution_count": null, + "id": "amber-billy", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "small-silver", "metadata": {}, "source": [ "#### 80. 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) (★★★)" @@ -1191,12 +1354,14 @@ { "cell_type": "code", "execution_count": null, + "id": "unauthorized-rachel", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "criminal-technology", "metadata": {}, "source": [ "#### 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]]? (★★★)" @@ -1205,12 +1370,14 @@ { "cell_type": "code", "execution_count": null, + "id": "unsigned-packing", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "spiritual-encounter", "metadata": {}, "source": [ "#### 82. Compute a matrix rank (★★★)" @@ -1219,12 +1386,14 @@ { "cell_type": "code", "execution_count": null, + "id": "another-stewart", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "stuck-promise", "metadata": {}, "source": [ "#### 83. How to find the most frequent value in an array?" @@ -1233,12 +1402,14 @@ { "cell_type": "code", "execution_count": null, + "id": "french-mercury", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "color-xerox", "metadata": {}, "source": [ "#### 84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)" @@ -1247,12 +1418,14 @@ { "cell_type": "code", "execution_count": null, + "id": "worth-profile", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "extended-replacement", "metadata": {}, "source": [ "#### 85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)" @@ -1261,12 +1434,14 @@ { "cell_type": "code", "execution_count": null, + "id": "bibliographic-storm", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "fixed-washington", "metadata": {}, "source": [ "#### 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)) (★★★)" @@ -1275,12 +1450,14 @@ { "cell_type": "code", "execution_count": null, + "id": "difficult-teaching", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "available-portfolio", "metadata": {}, "source": [ "#### 87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)" @@ -1289,12 +1466,14 @@ { "cell_type": "code", "execution_count": null, + "id": "painful-translation", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "banner-television", "metadata": {}, "source": [ "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" @@ -1303,12 +1482,14 @@ { "cell_type": "code", "execution_count": null, + "id": "documentary-bracelet", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "assured-domain", "metadata": {}, "source": [ "#### 89. How to get the n largest values of an array (★★★)" @@ -1317,12 +1498,14 @@ { "cell_type": "code", "execution_count": null, + "id": "animal-monkey", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "bound-forty", "metadata": {}, "source": [ "#### 90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)" @@ -1331,12 +1514,14 @@ { "cell_type": "code", "execution_count": null, + "id": "fantastic-sacrifice", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "wicked-victim", "metadata": {}, "source": [ "#### 91. How to create a record array from a regular array? (★★★)" @@ -1345,12 +1530,14 @@ { "cell_type": "code", "execution_count": null, + "id": "associate-punishment", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "initial-increase", "metadata": {}, "source": [ "#### 92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★)" @@ -1359,12 +1546,14 @@ { "cell_type": "code", "execution_count": null, + "id": "exempt-desert", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "ambient-bridge", "metadata": {}, "source": [ "#### 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? (★★★)" @@ -1373,12 +1562,14 @@ { "cell_type": "code", "execution_count": null, + "id": "premium-favor", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "forbidden-islam", "metadata": {}, "source": [ "#### 94. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) (★★★)" @@ -1387,12 +1578,14 @@ { "cell_type": "code", "execution_count": null, + "id": "geological-atlas", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "ranking-vacation", "metadata": {}, "source": [ "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" @@ -1401,12 +1594,14 @@ { "cell_type": "code", "execution_count": null, + "id": "logical-cologne", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "cordless-christian", "metadata": {}, "source": [ "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" @@ -1415,12 +1610,14 @@ { "cell_type": "code", "execution_count": null, + "id": "guilty-gather", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "smooth-paradise", "metadata": {}, "source": [ "#### 97. Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function (★★★)" @@ -1429,12 +1626,14 @@ { "cell_type": "code", "execution_count": null, + "id": "focal-dietary", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "returning-missouri", "metadata": {}, "source": [ "#### 98. Considering a path described by two vectors (X,Y), how to sample it using equidistant samples (★★★)?" @@ -1443,12 +1642,14 @@ { "cell_type": "code", "execution_count": null, + "id": "asian-luxury", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "dangerous-familiar", "metadata": {}, "source": [ "#### 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. (★★★)" @@ -1457,12 +1658,14 @@ { "cell_type": "code", "execution_count": null, + "id": "simplified-cleanup", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "organizational-grenada", "metadata": {}, "source": [ "#### 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). (★★★)" @@ -1471,30 +1674,13 @@ { "cell_type": "code", "execution_count": null, + "id": "fossil-generation", "metadata": {}, "outputs": [], "source": [] } ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.7.7" - } - }, + "metadata": {}, "nbformat": 4, - "nbformat_minor": 4 + "nbformat_minor": 5 } diff --git a/100_Numpy_exercises.md b/100_Numpy_exercises.md index 5f55706b..2eb97694 100644 --- a/100_Numpy_exercises.md +++ b/100_Numpy_exercises.md @@ -168,7 +168,7 @@ np.sqrt(-1) == np.emath.sqrt(-1) #### 60. How to tell if a given 2D array has null columns? (★★☆) -#### 61. Find the nearest value from a given value in an array (★★☆) +#### 61. Find the 3 nearest values in order from a given value in an array (★★☆) #### 62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆) diff --git a/100_Numpy_exercises_with_hints.md b/100_Numpy_exercises_with_hints.md index 4a107b9b..71405d3c 100644 --- a/100_Numpy_exercises_with_hints.md +++ b/100_Numpy_exercises_with_hints.md @@ -168,8 +168,8 @@ np.sqrt(-1) == np.emath.sqrt(-1) `hint: argsort` #### 60. How to tell if a given 2D array has null columns? (★★☆) `hint: any, ~` -#### 61. Find the nearest value from a given value in an array (★★☆) -`hint: np.abs, argmin, flat` +#### 61. Find the 3 nearest values in order from a given value in an array (★★☆) +`hint: np.abs, argpartition, flat, argsort` #### 62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆) `hint: np.nditer` #### 63. Create an array class that has a name attribute (★★☆) diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index bd311ce5..7540a4b5 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -193,10 +193,10 @@ print(Z) `hint: np.dtype` ```python -color = np.dtype([("r", np.ubyte, 1), - ("g", np.ubyte, 1), - ("b", np.ubyte, 1), - ("a", np.ubyte, 1)]) +color = np.dtype([("r", np.ubyte), + ("g", np.ubyte), + ("b", np.ubyte), + ("a", np.ubyte)]) ``` #### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆) `hint:` @@ -333,7 +333,6 @@ print(Z) ```python A = np.ones(3)*1 B = np.ones(3)*2 -C = np.ones(3)*3 np.add(A,B,out=B) np.divide(A,2,out=A) np.negative(A,out=A) @@ -472,7 +471,7 @@ for dtype in [np.float32, np.float64]: ```python np.set_printoptions(threshold=float("inf")) -Z = np.zeros((16,16)) +Z = np.zeros((40,40)) print(Z) ``` #### 50. How to find the closest value (to a given scalar) in a vector? (★★☆) @@ -612,14 +611,15 @@ print(Z[Z[:,1].argsort()]) Z = np.random.randint(0,3,(3,10)) print((~Z.any(axis=0)).any()) ``` -#### 61. Find the nearest value from a given value in an array (★★☆) -`hint: np.abs, argmin, flat` +#### 61. Find the 3 nearest values in order from a given value in an array (★★☆) +`hint: np.abs, argpartition, flat, argsort` ```python -Z = np.random.uniform(0,1,10) +Z = np.random.uniform(0, 1, 10) z = 0.5 -m = Z.flat[np.abs(Z - z).argmin()] -print(m) +m = Z.flat[np.abs(Z - z).ravel().argpartition(3)[:3]] +ordered_m = m[np.argsort(np.abs(m - z))] +print(ordered_m) ``` #### 62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆) `hint: np.nditer` @@ -778,7 +778,7 @@ G = F.view( dtype=[('p0',F.dtype),('p1',F.dtype)] ) G = np.unique(G) print(G) ``` -#### 74. Given an array C that is a bincount, how to produce an array A such that np.bincount(A) == C? (★★★) +#### 74. Given a sorted array C that corresponds to a bincount, how to produce an array A such that np.bincount(A) == C? (★★★) `hint: np.repeat` ```python diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index 5f40a6a3..0e6955f6 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -193,10 +193,10 @@ print(Z) ```python -color = np.dtype([("r", np.ubyte, 1), - ("g", np.ubyte, 1), - ("b", np.ubyte, 1), - ("a", np.ubyte, 1)]) +color = np.dtype([("r", np.ubyte), + ("g", np.ubyte), + ("b", np.ubyte), + ("a", np.ubyte)]) ``` #### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆) @@ -333,7 +333,6 @@ print(Z) ```python A = np.ones(3)*1 B = np.ones(3)*2 -C = np.ones(3)*3 np.add(A,B,out=B) np.divide(A,2,out=A) np.negative(A,out=A) @@ -472,7 +471,7 @@ for dtype in [np.float32, np.float64]: ```python np.set_printoptions(threshold=float("inf")) -Z = np.zeros((16,16)) +Z = np.zeros((40,40)) print(Z) ``` #### 50. How to find the closest value (to a given scalar) in a vector? (★★☆) @@ -612,14 +611,15 @@ print(Z[Z[:,1].argsort()]) Z = np.random.randint(0,3,(3,10)) print((~Z.any(axis=0)).any()) ``` -#### 61. Find the nearest value from a given value in an array (★★☆) +#### 61. Find the 3 nearest values in order from a given value in an array (★★☆) ```python -Z = np.random.uniform(0,1,10) +Z = np.random.uniform(0, 1, 10) z = 0.5 -m = Z.flat[np.abs(Z - z).argmin()] -print(m) +m = Z.flat[np.abs(Z - z).ravel().argpartition(3)[:3]] +ordered_m = m[np.argsort(np.abs(m - z))] +print(ordered_m) ``` #### 62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆) diff --git a/100_Numpy_random.ipynb b/100_Numpy_random.ipynb index b05f89d9..083b7949 100644 --- a/100_Numpy_random.ipynb +++ b/100_Numpy_random.ipynb @@ -2,6 +2,7 @@ "cells": [ { "cell_type": "markdown", + "id": "equipped-certificate", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -17,6 +18,7 @@ }, { "cell_type": "markdown", + "id": "mighty-tract", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -24,6 +26,7 @@ }, { "cell_type": "markdown", + "id": "matched-payroll", "metadata": {}, "source": [ "Run the `initialize.py` module, then call a random question with `pick()` an hint towards its solution with\n", @@ -33,6 +36,7 @@ { "cell_type": "code", "execution_count": null, + "id": "treated-aluminum", "metadata": {}, "outputs": [], "source": [ @@ -42,6 +46,7 @@ { "cell_type": "code", "execution_count": null, + "id": "meaningful-anime", "metadata": {}, "outputs": [], "source": [ @@ -49,25 +54,7 @@ ] } ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.7.7" - } - }, + "metadata": {}, "nbformat": 4, - "nbformat_minor": 4 + "nbformat_minor": 5 } diff --git a/source/exercises100.ktx b/source/exercises100.ktx index 5f392a77..dee80278 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -778,16 +778,17 @@ Z = np.random.randint(0,3,(3,10)) print((~Z.any(axis=0)).any()) < q61 -Find the nearest value from a given value in an array (★★☆) +Find the 3 nearest values in order from a given value in an array (★★☆) < h61 -hint: np.abs, argmin, flat +hint: np.abs, argpartition, flat, argsort < a61 -Z = np.random.uniform(0,1,10) +Z = np.random.uniform(0, 1, 10) z = 0.5 -m = Z.flat[np.abs(Z - z).argmin()] -print(m) +m = Z.flat[np.abs(Z - z).ravel().argpartition(3)[:3]] +ordered_m = m[np.argsort(np.abs(m - z))] +print(ordered_m) < q62 Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)