From af8d83ac8947e04b6033d19e6c14ecc106fb76a3 Mon Sep 17 00:00:00 2001 From: "Nicolas P. Rougier" Date: Tue, 18 Feb 2025 08:13:08 +0100 Subject: [PATCH] Update generated files --- 100_Numpy_exercises.ipynb | 408 +++++++++--------- ...mpy_exercises_with_hints_with_solutions.md | 28 +- 100_Numpy_exercises_with_solutions.md | 28 +- 100_Numpy_random.ipynb | 10 +- 4 files changed, 263 insertions(+), 211 deletions(-) diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index f7efe47..d7623b0 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "6293cc50", + "id": "d46e26d4", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "a167c052", + "id": "d391e44c", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "57fc0ee5", + "id": "1ce7c53f", "metadata": {}, "source": [ "Run the `initialize.py` module, then for each question you can query the\n", @@ -36,7 +36,7 @@ { "cell_type": "code", "execution_count": null, - "id": "3d46fba2", + "id": "2e6ba9b2", "metadata": {}, "outputs": [], "source": [ @@ -45,7 +45,7 @@ }, { "cell_type": "markdown", - "id": "3c1222ff", + "id": "dcc6b1ac", "metadata": {}, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" @@ -54,14 +54,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f52e6a0b", + "id": "6c52078e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9d9f4753", + "id": "118ded29", "metadata": {}, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" @@ -70,14 +70,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b7fa5fa1", + "id": "023a2c52", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3e4c23c6", + "id": "172b913a", "metadata": {}, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" @@ -86,14 +86,14 @@ { "cell_type": "code", "execution_count": null, - "id": "083c5c46", + "id": "df54025a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c38bbe82", + "id": "95ee9e07", "metadata": {}, "source": [ "#### 4. How to find the memory size of any array (★☆☆)" @@ -102,14 +102,14 @@ { "cell_type": "code", "execution_count": null, - "id": "02b04021", + "id": "5c5b0950", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "67f893d4", + "id": "45fca154", "metadata": {}, "source": [ "#### 5. How to get the documentation of the numpy add function from the command line? (★☆☆)" @@ -118,14 +118,14 @@ { "cell_type": "code", "execution_count": null, - "id": "40e6b70d", + "id": "fcb1248a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e0c739c6", + "id": "674e8019", "metadata": {}, "source": [ "#### 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)" @@ -134,14 +134,14 @@ { "cell_type": "code", "execution_count": null, - "id": "bcceacf7", + "id": "452df2a7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fbebedab", + "id": "7685e337", "metadata": {}, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" @@ -150,14 +150,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3d117d54", + "id": "10847d11", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ad0bff02", + "id": "54c913ba", "metadata": {}, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" @@ -166,14 +166,14 @@ { "cell_type": "code", "execution_count": null, - "id": "22d23470", + "id": "34722e83", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9f59d7e1", + "id": "8e72fd08", "metadata": {}, "source": [ "#### 9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)" @@ -182,14 +182,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7449e82d", + "id": "e18eef56", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f374c4cf", + "id": "088424fc", "metadata": {}, "source": [ "#### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)" @@ -198,14 +198,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1a84bed8", + "id": "9a402d06", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9c4707f5", + "id": "3f347de9", "metadata": {}, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" @@ -214,14 +214,14 @@ { "cell_type": "code", "execution_count": null, - "id": "29dc1fd3", + "id": "e38459c2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3bac294a", + "id": "24e98f25", "metadata": {}, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" @@ -230,14 +230,14 @@ { "cell_type": "code", "execution_count": null, - "id": "eadfae67", + "id": "94752bb3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "466c2989", + "id": "7cbca2cc", "metadata": {}, "source": [ "#### 13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)" @@ -246,14 +246,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9a74dbfc", + "id": "c0643b1f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "53a8edb2", + "id": "6cab4cbb", "metadata": {}, "source": [ "#### 14. Create a random vector of size 30 and find the mean value (★☆☆)" @@ -262,14 +262,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6691530b", + "id": "e922a524", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cbaad31c", + "id": "72053be8", "metadata": {}, "source": [ "#### 15. Create a 2d array with 1 on the border and 0 inside (★☆☆)" @@ -278,14 +278,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ad8308e3", + "id": "6bc84dfe", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c5ecc267", + "id": "86b0206b", "metadata": {}, "source": [ "#### 16. How to add a border (filled with 0's) around an existing array? (★☆☆)" @@ -294,14 +294,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9d5c78c4", + "id": "41e66e3e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ef909b72", + "id": "98a45574", "metadata": {}, "source": [ "#### 17. What is the result of the following expression? (★☆☆)\n", @@ -318,14 +318,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3b4985f2", + "id": "7eb533d5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "502738ac", + "id": "1486619a", "metadata": {}, "source": [ "#### 18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)" @@ -334,14 +334,14 @@ { "cell_type": "code", "execution_count": null, - "id": "faed6e2b", + "id": "2b9fb59c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a65da660", + "id": "319287fd", "metadata": {}, "source": [ "#### 19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)" @@ -350,14 +350,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ed150400", + "id": "299f5e60", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9f7f9c49", + "id": "fb1686ed", "metadata": {}, "source": [ "#### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (★☆☆)" @@ -366,14 +366,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3782b5a9", + "id": "22888cc4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "66bfda35", + "id": "137ea278", "metadata": {}, "source": [ "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" @@ -382,14 +382,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4c07477c", + "id": "9cb81fe1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d5bcfc08", + "id": "9319f5a1", "metadata": {}, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" @@ -398,14 +398,14 @@ { "cell_type": "code", "execution_count": null, - "id": "cea6c993", + "id": "fcdc0459", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f9a0911e", + "id": "f1902fef", "metadata": {}, "source": [ "#### 23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)" @@ -414,14 +414,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d165044b", + "id": "7445cb2d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "654805b1", + "id": "3f34bd2a", "metadata": {}, "source": [ "#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)" @@ -430,14 +430,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6a42619f", + "id": "ec8bd6da", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "994c25df", + "id": "b1919ba3", "metadata": {}, "source": [ "#### 25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)" @@ -446,14 +446,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e4b39c03", + "id": "5b63b63b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b7d15d1a", + "id": "fb1daa27", "metadata": {}, "source": [ "#### 26. What is the output of the following script? (★☆☆)\n", @@ -469,14 +469,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4c95bc3f", + "id": "52a34d3d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a34f0d08", + "id": "89b7e7a7", "metadata": {}, "source": [ "#### 27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)\n", @@ -493,14 +493,14 @@ { "cell_type": "code", "execution_count": null, - "id": "564658fc", + "id": "451181cf", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c34e94bf", + "id": "8c7601c6", "metadata": {}, "source": [ "#### 28. What are the result of the following expressions? (★☆☆)\n", @@ -514,14 +514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "56663562", + "id": "52983a0b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "546f5e7b", + "id": "06c9164d", "metadata": {}, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" @@ -530,14 +530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f725a6d2", + "id": "4cce0af8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "eadca856", + "id": "52235994", "metadata": {}, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" @@ -546,14 +546,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e0270b5b", + "id": "27eff6b1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ca16ca22", + "id": "7e6f7d78", "metadata": {}, "source": [ "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" @@ -562,14 +562,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7d7208ef", + "id": "1bdde6e1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c7871d0d", + "id": "b001277e", "metadata": {}, "source": [ "#### 32. Is the following expressions true? (★☆☆)\n", @@ -581,14 +581,14 @@ { "cell_type": "code", "execution_count": null, - "id": "acc3701a", + "id": "d8673bdf", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e0433e30", + "id": "b9a5ff84", "metadata": {}, "source": [ "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" @@ -597,14 +597,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9c4d2152", + "id": "f33f9cc2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9f52a2ef", + "id": "f8e96e58", "metadata": {}, "source": [ "#### 34. How to get all the dates corresponding to the month of July 2016? (★★☆)" @@ -613,14 +613,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d6c34dec", + "id": "3927a8e8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4a64e059", + "id": "557c163e", "metadata": {}, "source": [ "#### 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)" @@ -629,14 +629,14 @@ { "cell_type": "code", "execution_count": null, - "id": "49267f60", + "id": "3c2f59ac", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8a98c2a8", + "id": "14c05852", "metadata": {}, "source": [ "#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)" @@ -645,14 +645,14 @@ { "cell_type": "code", "execution_count": null, - "id": "368130b3", + "id": "8b7d2937", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5a2e3d85", + "id": "003b8721", "metadata": {}, "source": [ "#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)" @@ -661,14 +661,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1d974e8f", + "id": "63c75db0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9720e2d5", + "id": "8e71dc21", "metadata": {}, "source": [ "#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)" @@ -677,14 +677,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e5dd8fc9", + "id": "f6aefd94", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c6f2c912", + "id": "b7f03449", "metadata": {}, "source": [ "#### 39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)" @@ -693,14 +693,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6aaa27d3", + "id": "a27cd904", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a36d6ee4", + "id": "043156c5", "metadata": {}, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" @@ -709,14 +709,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e589d216", + "id": "6ae5eb58", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0fb4ad68", + "id": "4f7e8f3f", "metadata": {}, "source": [ "#### 41. How to sum a small array faster than np.sum? (★★☆)" @@ -725,14 +725,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a1ba05a0", + "id": "88790af3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "024ac365", + "id": "13d1ca52", "metadata": {}, "source": [ "#### 42. Consider two random array A and B, check if they are equal (★★☆)" @@ -741,14 +741,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7db3846d", + "id": "ca6dd579", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c68d2401", + "id": "cf7cca6d", "metadata": {}, "source": [ "#### 43. Make an array immutable (read-only) (★★☆)" @@ -757,14 +757,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7828539d", + "id": "cbbb8917", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "55df6280", + "id": "37b0828f", "metadata": {}, "source": [ "#### 44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)" @@ -773,14 +773,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f1c86ea0", + "id": "a89969af", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2f60f1ec", + "id": "b7d7dd80", "metadata": {}, "source": [ "#### 45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)" @@ -789,14 +789,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d9f48f1c", + "id": "8403a440", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "084e8ad4", + "id": "b47e5837", "metadata": {}, "source": [ "#### 46. Create a structured array with `x` and `y` coordinates covering the [0,1]x[0,1] area (★★☆)" @@ -805,14 +805,14 @@ { "cell_type": "code", "execution_count": null, - "id": "58b47c13", + "id": "42c72e94", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d7b9bc42", + "id": "8c7946ee", "metadata": {}, "source": [ "#### 47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)" @@ -821,14 +821,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a24824fb", + "id": "26b93f06", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "65ebc11b", + "id": "3e898e5f", "metadata": {}, "source": [ "#### 48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)" @@ -837,14 +837,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e45d38fb", + "id": "afd1ae18", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9b317a98", + "id": "6fe6133b", "metadata": {}, "source": [ "#### 49. How to print all the values of an array? (★★☆)" @@ -853,14 +853,14 @@ { "cell_type": "code", "execution_count": null, - "id": "309cdeb9", + "id": "5764ec92", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b580f9d0", + "id": "7ee2dacc", "metadata": {}, "source": [ "#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)" @@ -869,14 +869,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ebfb6714", + "id": "742518ef", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "602e6252", + "id": "cfeda6ba", "metadata": {}, "source": [ "#### 51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)" @@ -885,14 +885,14 @@ { "cell_type": "code", "execution_count": null, - "id": "cf002b3a", + "id": "9fcbd031", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "78bcd92f", + "id": "b97e75b5", "metadata": {}, "source": [ "#### 52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)" @@ -901,14 +901,14 @@ { "cell_type": "code", "execution_count": null, - "id": "fea7f9a6", + "id": "c58a45b6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "343c5a85", + "id": "291e0bb9", "metadata": {}, "source": [ "#### 53. How to convert a float (32 bits) array into an integer (32 bits) in place?" @@ -917,14 +917,14 @@ { "cell_type": "code", "execution_count": null, - "id": "fc1b687b", + "id": "2f3e6c00", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a69e1ace", + "id": "1e305e92", "metadata": {}, "source": [ "#### 54. How to read the following file? (★★☆)\n", @@ -938,14 +938,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1c7957d4", + "id": "753024cb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c23bd80d", + "id": "94a8ad69", "metadata": {}, "source": [ "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" @@ -954,14 +954,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d95d9578", + "id": "ae45249f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2153a5ec", + "id": "72b340c9", "metadata": {}, "source": [ "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" @@ -970,14 +970,14 @@ { "cell_type": "code", "execution_count": null, - "id": "61122d1e", + "id": "a80e5c9b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2e76d589", + "id": "86550d61", "metadata": {}, "source": [ "#### 57. How to randomly place p elements in a 2D array? (★★☆)" @@ -986,14 +986,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0c2e4fcc", + "id": "db9f2582", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8b3933eb", + "id": "153f8cfa", "metadata": {}, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" @@ -1002,14 +1002,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a306c9ef", + "id": "8301cb12", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ea8807c0", + "id": "f4038090", "metadata": {}, "source": [ "#### 59. How to sort an array by the nth column? (★★☆)" @@ -1018,14 +1018,14 @@ { "cell_type": "code", "execution_count": null, - "id": "cc89a699", + "id": "eb5745df", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "00fa00a1", + "id": "20d2a462", "metadata": {}, "source": [ "#### 60. How to tell if a given 2D array has null columns? (★★☆)" @@ -1034,14 +1034,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3436e165", + "id": "b92b7316", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5a65c7a2", + "id": "bb2aa49b", "metadata": {}, "source": [ "#### 61. Find the nearest value from a given value in an array (★★☆)" @@ -1050,14 +1050,14 @@ { "cell_type": "code", "execution_count": null, - "id": "73f149d6", + "id": "8b62e59c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "dd83e8af", + "id": "1a950e1a", "metadata": {}, "source": [ "#### 62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)" @@ -1066,14 +1066,14 @@ { "cell_type": "code", "execution_count": null, - "id": "29a709a1", + "id": "cad1d01b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6c042efa", + "id": "165b3a20", "metadata": {}, "source": [ "#### 63. Create an array class that has a name attribute (★★☆)" @@ -1082,14 +1082,14 @@ { "cell_type": "code", "execution_count": null, - "id": "45b3d2e0", + "id": "666bf983", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0a075c3d", + "id": "87f85b12", "metadata": {}, "source": [ "#### 64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★)" @@ -1098,14 +1098,14 @@ { "cell_type": "code", "execution_count": null, - "id": "36c50470", + "id": "03ee4c88", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0fb2f0c5", + "id": "1cda27c4", "metadata": {}, "source": [ "#### 65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)" @@ -1114,14 +1114,14 @@ { "cell_type": "code", "execution_count": null, - "id": "58aa5f82", + "id": "aee2c782", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d89dc506", + "id": "1db205db", "metadata": {}, "source": [ "#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★☆)" @@ -1130,14 +1130,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8fb43a49", + "id": "43204f35", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "535a5d81", + "id": "ebaf1222", "metadata": {}, "source": [ "#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)" @@ -1146,14 +1146,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f954ab9d", + "id": "aabbbab9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "eee66d3c", + "id": "2dfb4033", "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? (★★★)" @@ -1162,14 +1162,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8929773a", + "id": "7a9c2287", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f90d4c40", + "id": "7035c626", "metadata": {}, "source": [ "#### 69. How to get the diagonal of a dot product? (★★★)" @@ -1178,14 +1178,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b47d392d", + "id": "f6af16c1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a8a97e7d", + "id": "8f73f0e5", "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? (★★★)" @@ -1194,14 +1194,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ee6c6c37", + "id": "b0cabc11", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d6a30d60", + "id": "bafaa79e", "metadata": {}, "source": [ "#### 71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)" @@ -1210,14 +1210,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e9703f2e", + "id": "d441e30b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bac359a9", + "id": "80a9ac82", "metadata": {}, "source": [ "#### 72. How to swap two rows of an array? (★★★)" @@ -1226,14 +1226,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f1b432b1", + "id": "1aee2791", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "620c9f83", + "id": "5e53d68c", "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 (★★★)" @@ -1242,14 +1242,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4e595ffc", + "id": "2f975322", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a76c55a2", + "id": "f85da603", "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? (★★★)" @@ -1258,14 +1258,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2d2ae65a", + "id": "8c476a73", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d550c8e2", + "id": "a2330e6c", "metadata": {}, "source": [ "#### 75. How to compute averages using a sliding window over an array? (★★★)" @@ -1274,14 +1274,14 @@ { "cell_type": "code", "execution_count": null, - "id": "841c457c", + "id": "35e5e9f5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6b170b00", + "id": "87698de8", "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]) (★★★)" @@ -1290,14 +1290,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d9a0cf4e", + "id": "753763ed", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e94a5c1d", + "id": "b8417b15", "metadata": {}, "source": [ "#### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★)" @@ -1306,14 +1306,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c0f9504f", + "id": "3cad31b2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4eb8b5c1", + "id": "37a49831", "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])? (★★★)" @@ -1322,14 +1322,14 @@ { "cell_type": "code", "execution_count": null, - "id": "487f47ec", + "id": "df95dce8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3df99948", + "id": "f5ae032f", "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])? (★★★)" @@ -1338,14 +1338,14 @@ { "cell_type": "code", "execution_count": null, - "id": "dc2df37b", + "id": "f5ae2e9b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "11e972c2", + "id": "6bc35ba2", "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) (★★★)" @@ -1354,14 +1354,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0291247f", + "id": "2ad8db75", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "93cfeab3", + "id": "d17818f7", "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]]? (★★★)" @@ -1370,14 +1370,14 @@ { "cell_type": "code", "execution_count": null, - "id": "360de791", + "id": "97deb72f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4c15ceff", + "id": "67f526e6", "metadata": {}, "source": [ "#### 82. Compute a matrix rank (★★★)" @@ -1386,14 +1386,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9a4272d2", + "id": "d70bed5b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1171cb84", + "id": "0a3fb55f", "metadata": {}, "source": [ "#### 83. How to find the most frequent value in an array?" @@ -1402,14 +1402,14 @@ { "cell_type": "code", "execution_count": null, - "id": "33e951c8", + "id": "b0647d07", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "04f472aa", + "id": "cb0ac341", "metadata": {}, "source": [ "#### 84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)" @@ -1418,14 +1418,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7aade14a", + "id": "91798923", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "52836346", + "id": "c2a9da0d", "metadata": {}, "source": [ "#### 85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)" @@ -1434,14 +1434,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4df70318", + "id": "f9b027ff", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "78c43ee5", + "id": "718ac868", "metadata": {}, "source": [ "#### 86. Consider a set of p matrices with 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)) (★★★)" @@ -1450,14 +1450,14 @@ { "cell_type": "code", "execution_count": null, - "id": "32fd2c3b", + "id": "4de05217", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "158f9945", + "id": "a6eda6f8", "metadata": {}, "source": [ "#### 87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)" @@ -1466,14 +1466,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5a9b808f", + "id": "286b8db5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2e0ad860", + "id": "625aa611", "metadata": {}, "source": [ "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" @@ -1482,14 +1482,14 @@ { "cell_type": "code", "execution_count": null, - "id": "574acff5", + "id": "a01c1ff2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "69a892fa", + "id": "ca3dde4a", "metadata": {}, "source": [ "#### 89. How to get the n largest values of an array (★★★)" @@ -1498,14 +1498,14 @@ { "cell_type": "code", "execution_count": null, - "id": "21b2bd48", + "id": "5d14148c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b55ccacc", + "id": "e233805e", "metadata": {}, "source": [ "#### 90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)" @@ -1514,14 +1514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4b645da4", + "id": "17638e13", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "89187a23", + "id": "c163f115", "metadata": {}, "source": [ "#### 91. How to create a record array from a regular array? (★★★)" @@ -1530,14 +1530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "da94c71a", + "id": "da402514", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "aa70d4ca", + "id": "049c006e", "metadata": {}, "source": [ "#### 92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★)" @@ -1546,14 +1546,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7cff1bf5", + "id": "96ce278a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5f79dd00", + "id": "6e652635", "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? (★★★)" @@ -1562,14 +1562,14 @@ { "cell_type": "code", "execution_count": null, - "id": "20c105b2", + "id": "f429d6d4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a80570cd", + "id": "33c4755d", "metadata": {}, "source": [ "#### 94. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) (★★★)" @@ -1578,14 +1578,14 @@ { "cell_type": "code", "execution_count": null, - "id": "84db8de1", + "id": "29507586", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ba154f43", + "id": "c9fe158d", "metadata": {}, "source": [ "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" @@ -1594,14 +1594,14 @@ { "cell_type": "code", "execution_count": null, - "id": "cfa96e1b", + "id": "58209fb7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "420e2343", + "id": "f60086c9", "metadata": {}, "source": [ "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" @@ -1610,14 +1610,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4680dfd0", + "id": "787b6e4c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "89fef2f3", + "id": "0f5e1299", "metadata": {}, "source": [ "#### 97. Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function (★★★)" @@ -1626,14 +1626,14 @@ { "cell_type": "code", "execution_count": null, - "id": "bb9c14b2", + "id": "cd2a8214", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7ad5cc5f", + "id": "a04dd898", "metadata": {}, "source": [ "#### 98. Considering a path described by two vectors (X,Y), how to sample it using equidistant samples (★★★)?" @@ -1642,14 +1642,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0899d1d5", + "id": "12d9c30f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2b69272e", + "id": "b45bf2ae", "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. (★★★)" @@ -1658,14 +1658,14 @@ { "cell_type": "code", "execution_count": null, - "id": "55c8b034", + "id": "f4179f4a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "59ab5516", + "id": "d92b6592", "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). (★★★)" @@ -1674,7 +1674,7 @@ { "cell_type": "code", "execution_count": null, - "id": "450877d8", + "id": "256ebe30", "metadata": {}, "outputs": [], "source": [] diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index 59e60fc..68ed74c 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -202,7 +202,7 @@ color = np.dtype([("r", np.ubyte), `hint:` ```python -Z = np.dot(np.ones((5,3)), np.ones((3,2))) +Z = np.matmul(np.ones((5, 3)), np.ones((3, 2))) print(Z) # Alternative solution, in Python 3.5 and above @@ -894,6 +894,32 @@ 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])) + +# Author: Yang Wu (Broadcasting) +def distance_points_to_lines(p: np.ndarray, p_1: np.ndarray, p_2: np.ndarray) -> np.ndarray: + x_0, y_0 = p.T # Shape -> (n points, ) + x_1, y_1 = p_1.T # Shape -> (n lines, ) + x_2, y_2 = p_2.T # Shape -> (n lines, ) + + # Displacement vector coordinates from p_1 -> p_2 + dx = x_2 - x_1 # Shape -> (n lines, ) + dy = y_2 - y_1 # Shape -> (n lines, ) + + # The 'cross product' term + cross_term = x_2 * y_1 - y_2 * x_1 # Shape -> (n lines, ) + + # Broadcast x_0, y_0 (n points, 1) and dx, dy, cross_term (1, n lines) -> (n points, n lines) + numerator = np.abs( + dy[np.newaxis, :] * x_0[:, np.newaxis] + - dx[np.newaxis, :] * y_0[:, np.newaxis] + + cross_term[np.newaxis, :] + ) + denominator = np.sqrt(dx**2 + dy**2) # Shape -> (n lines, ) + + # Shape (n points, n lines) / (1, n_lines) -> (n points, n lines) + return numerator / denominator[np.newaxis, :] + +distance_points_to_lines(p, P0, P1) ``` #### 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) (★★★) `hint: minimum maximum` diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index 78fd8e9..18271eb 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -202,7 +202,7 @@ color = np.dtype([("r", np.ubyte), ```python -Z = np.dot(np.ones((5,3)), np.ones((3,2))) +Z = np.matmul(np.ones((5, 3)), np.ones((3, 2))) print(Z) # Alternative solution, in Python 3.5 and above @@ -894,6 +894,32 @@ 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])) + +# Author: Yang Wu (Broadcasting) +def distance_points_to_lines(p: np.ndarray, p_1: np.ndarray, p_2: np.ndarray) -> np.ndarray: + x_0, y_0 = p.T # Shape -> (n points, ) + x_1, y_1 = p_1.T # Shape -> (n lines, ) + x_2, y_2 = p_2.T # Shape -> (n lines, ) + + # Displacement vector coordinates from p_1 -> p_2 + dx = x_2 - x_1 # Shape -> (n lines, ) + dy = y_2 - y_1 # Shape -> (n lines, ) + + # The 'cross product' term + cross_term = x_2 * y_1 - y_2 * x_1 # Shape -> (n lines, ) + + # Broadcast x_0, y_0 (n points, 1) and dx, dy, cross_term (1, n lines) -> (n points, n lines) + numerator = np.abs( + dy[np.newaxis, :] * x_0[:, np.newaxis] + - dx[np.newaxis, :] * y_0[:, np.newaxis] + + cross_term[np.newaxis, :] + ) + denominator = np.sqrt(dx**2 + dy**2) # Shape -> (n lines, ) + + # Shape (n points, n lines) / (1, n_lines) -> (n points, n lines) + return numerator / denominator[np.newaxis, :] + +distance_points_to_lines(p, P0, P1) ``` #### 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) (★★★) diff --git a/100_Numpy_random.ipynb b/100_Numpy_random.ipynb index f17e08f..dfac932 100644 --- a/100_Numpy_random.ipynb +++ b/100_Numpy_random.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "e06c1964", + "id": "ed5b154a", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "c108c1c4", + "id": "f21bc71d", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "2aefe09b", + "id": "7a3d8374", "metadata": {}, "source": [ "Run the `initialize.py` module, then call a random question with `pick()` an hint towards its solution with\n", @@ -36,7 +36,7 @@ { "cell_type": "code", "execution_count": null, - "id": "55c8f855", + "id": "a3aaf46b", "metadata": {}, "outputs": [], "source": [ @@ -46,7 +46,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8f1e8a4a", + "id": "6db78cf9", "metadata": {}, "outputs": [], "source": [