From a7c84d5fed330bd6ae5cf7bd2e2bcbe26f61ed75 Mon Sep 17 00:00:00 2001 From: Francesco Terenzi Date: Thu, 8 Apr 2021 20:51:14 +0200 Subject: [PATCH] Updated question 37 --- 100_Numpy_exercises.ipynb | 226 ++++++++++++++++-- ...mpy_exercises_with_hints_with_solutions.md | 32 ++- 100_Numpy_exercises_with_solutions.md | 32 ++- 100_Numpy_random.ipynb | 27 +-- source/exercises100.ktx | 3 +- 5 files changed, 258 insertions(+), 62 deletions(-) diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index 4fd1119..5ef0925 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -2,6 +2,7 @@ "cells": [ { "cell_type": "markdown", + "id": "8d5f9376", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -17,6 +18,7 @@ }, { "cell_type": "markdown", + "id": "f3991601", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -24,6 +26,7 @@ }, { "cell_type": "markdown", + "id": "00eb0a43", "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": "71de66b9", "metadata": {}, "outputs": [], "source": [ @@ -41,6 +45,7 @@ }, { "cell_type": "markdown", + "id": "7bfd9bff", "metadata": {}, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" @@ -49,12 +54,14 @@ { "cell_type": "code", "execution_count": null, + "id": "87399bef", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "cb191df0", "metadata": {}, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" @@ -63,12 +70,14 @@ { "cell_type": "code", "execution_count": null, + "id": "6bddfd8d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "8d29e132", "metadata": {}, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" @@ -77,12 +86,14 @@ { "cell_type": "code", "execution_count": null, + "id": "98d87f02", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "8ccac176", "metadata": {}, "source": [ "#### 4. How to find the memory size of any array (★☆☆)" @@ -91,12 +102,14 @@ { "cell_type": "code", "execution_count": null, + "id": "2a350784", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "cfdb15e5", "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": "14c16b65", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "aa575121", "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": "ba8cdb73", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "182022f5", "metadata": {}, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" @@ -133,12 +150,14 @@ { "cell_type": "code", "execution_count": null, + "id": "9aeebf5e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "cb876890", "metadata": {}, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" @@ -147,12 +166,14 @@ { "cell_type": "code", "execution_count": null, + "id": "0f76940e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "eb4808bf", "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": "1b315e4c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "6b963906", "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": "fb8e5d22", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "32162d7a", "metadata": {}, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" @@ -189,12 +214,14 @@ { "cell_type": "code", "execution_count": null, + "id": "2a1f76ee", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "40e9c864", "metadata": {}, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" @@ -203,12 +230,14 @@ { "cell_type": "code", "execution_count": null, + "id": "348d9b8b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "ccddf92a", "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": "5cb70d41", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "1e721bf5", "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": "820bf543", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "bd1601a4", "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": "55b2da2b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "97a1cbff", "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": "40bb1714", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "bab40feb", "metadata": {}, "source": [ "#### 17. What is the result of the following expression? (★☆☆)\n", @@ -281,12 +318,14 @@ { "cell_type": "code", "execution_count": null, + "id": "c4e3b9f0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "73e147e5", "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": "748b0220", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "c76e859b", "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": "f6ebaefc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "d2ae73d8", "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": "7bf0d74b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "1028425d", "metadata": {}, "source": [ "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" @@ -337,12 +382,14 @@ { "cell_type": "code", "execution_count": null, + "id": "79512e34", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "b2fbd122", "metadata": {}, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" @@ -351,12 +398,14 @@ { "cell_type": "code", "execution_count": null, + "id": "87641a2c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "2509c57c", "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": "52f8acea", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "ded9a899", "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": "3c298bde", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "fa460ccc", "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": "9f2f8d69", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "4dd2ddbb", "metadata": {}, "source": [ "#### 26. What is the output of the following script? (★☆☆)\n", @@ -414,12 +469,14 @@ { "cell_type": "code", "execution_count": null, + "id": "c430369b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "b9f9c658", "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": "d143bbc3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "9a17682c", "metadata": {}, "source": [ "#### 28. What are the result of the following expressions?\n", @@ -455,12 +514,14 @@ { "cell_type": "code", "execution_count": null, + "id": "ab7599cf", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "fa411d12", "metadata": {}, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" @@ -469,12 +530,14 @@ { "cell_type": "code", "execution_count": null, + "id": "9f12134e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "7b52f26a", "metadata": {}, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" @@ -483,12 +546,14 @@ { "cell_type": "code", "execution_count": null, + "id": "7fe39358", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "a6749c05", "metadata": {}, "source": [ "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" @@ -497,12 +562,14 @@ { "cell_type": "code", "execution_count": null, + "id": "ccdab4e3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "1f25789b", "metadata": {}, "source": [ "#### 32. Is the following expressions true? (★☆☆)\n", @@ -514,12 +581,14 @@ { "cell_type": "code", "execution_count": null, + "id": "52400a9b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "3eecd35d", "metadata": {}, "source": [ "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" @@ -528,12 +597,14 @@ { "cell_type": "code", "execution_count": null, + "id": "0cf79900", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "2b1ecb5e", "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": "0ec271db", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "f7104e3c", "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": "d6bfbf1a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "b2a99af4", "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": "2f4c5c3b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "edef1564", "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": "5aa93285", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "34fd06e6", "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": "30fcb1e1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "914fee46", "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": "1d3cdc81", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "bfc54e3b", "metadata": {}, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" @@ -626,12 +709,14 @@ { "cell_type": "code", "execution_count": null, + "id": "e530dc04", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "ef014831", "metadata": {}, "source": [ "#### 41. How to sum a small array faster than np.sum? (★★☆)" @@ -640,12 +725,14 @@ { "cell_type": "code", "execution_count": null, + "id": "1156b032", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "3313ce07", "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": "1317970d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "fc9a5834", "metadata": {}, "source": [ "#### 43. Make an array immutable (read-only) (★★☆)" @@ -668,12 +757,14 @@ { "cell_type": "code", "execution_count": null, + "id": "bf473a1d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "9e90f64c", "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": "9037151a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "d55519be", "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": "203c13e6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "ab364b3f", "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": "c3b70ace", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "a5ec75bc", "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": "c1c8772e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "ae730f76", "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": "c31c676d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "71e1d392", "metadata": {}, "source": [ "#### 49. How to print all the values of an array? (★★☆)" @@ -752,12 +853,14 @@ { "cell_type": "code", "execution_count": null, + "id": "6d49ec36", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "f5b9005b", "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": "3b1263ef", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "8ed1cfac", "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": "ac621bf9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "2ef9e21c", "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": "4984f42a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "caaf1c14", "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": "10e1a512", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "7e5080c5", "metadata": {}, "source": [ "#### 54. How to read the following file? (★★☆)\n", @@ -827,12 +938,14 @@ { "cell_type": "code", "execution_count": null, + "id": "00bf9c38", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "a0c35578", "metadata": {}, "source": [ "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" @@ -841,12 +954,14 @@ { "cell_type": "code", "execution_count": null, + "id": "f64d2874", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "64c4d38a", "metadata": {}, "source": [ "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" @@ -855,12 +970,14 @@ { "cell_type": "code", "execution_count": null, + "id": "a4cc553c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "8455392b", "metadata": {}, "source": [ "#### 57. How to randomly place p elements in a 2D array? (★★☆)" @@ -869,12 +986,14 @@ { "cell_type": "code", "execution_count": null, + "id": "8fbe4fb8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "6b89b37f", "metadata": {}, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" @@ -883,12 +1002,14 @@ { "cell_type": "code", "execution_count": null, + "id": "a59d5bd0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "cab7bfe5", "metadata": {}, "source": [ "#### 59. How to sort an array by the nth column? (★★☆)" @@ -897,12 +1018,14 @@ { "cell_type": "code", "execution_count": null, + "id": "ef0f4e9a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "f7180832", "metadata": {}, "source": [ "#### 60. How to tell if a given 2D array has null columns? (★★☆)" @@ -911,12 +1034,14 @@ { "cell_type": "code", "execution_count": null, + "id": "3d261dd9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "df0fc6b0", "metadata": {}, "source": [ "#### 61. Find the nearest value from a given value in an array (★★☆)" @@ -925,12 +1050,14 @@ { "cell_type": "code", "execution_count": null, + "id": "38f683dc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "38d042c0", "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": "ce3f3e39", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "fb5f1468", "metadata": {}, "source": [ "#### 63. Create an array class that has a name attribute (★★☆)" @@ -953,12 +1082,14 @@ { "cell_type": "code", "execution_count": null, + "id": "2e62ec16", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "26246179", "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": "331b42fb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "f090f30a", "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": "ce79532a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "c190e2c2", "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": "417894c2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "ec743250", "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": "1459ed28", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "3f0f49bb", "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": "4019dd26", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "9f58b3ee", "metadata": {}, "source": [ "#### 69. How to get the diagonal of a dot product? (★★★)" @@ -1037,12 +1178,14 @@ { "cell_type": "code", "execution_count": null, + "id": "eeeb4a9e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "c54f9dac", "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": "debab1b2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "bc01953d", "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": "6d090e70", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "b0baef43", "metadata": {}, "source": [ "#### 72. How to swap two rows of an array? (★★★)" @@ -1079,12 +1226,14 @@ { "cell_type": "code", "execution_count": null, + "id": "50cd4d2a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "4fa94835", "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": "6865bea3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "7e5e349b", "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": "77566eb3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "41b933ae", "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": "615b277b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "151a2312", "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": "8c38e25f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "da1ab0c7", "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": "558a7ee2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "f0b73c9b", "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": "60113555", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "e93b01c0", "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": "501e422c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "ba44a416", "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": "ce182c62", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "a73b0984", "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": "3888023d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "c6abb5dc", "metadata": {}, "source": [ "#### 82. Compute a matrix rank (★★★)" @@ -1219,12 +1386,14 @@ { "cell_type": "code", "execution_count": null, + "id": "877df10d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "c935778c", "metadata": {}, "source": [ "#### 83. How to find the most frequent value in an array?" @@ -1233,12 +1402,14 @@ { "cell_type": "code", "execution_count": null, + "id": "ec8edd02", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "afb79a63", "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": "f122d621", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "dd42e7c9", "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": "a88da7d1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "af05a48c", "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": "99d208ee", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "a00fe56e", "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": "71eb8301", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "88a439b1", "metadata": {}, "source": [ "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" @@ -1303,12 +1482,14 @@ { "cell_type": "code", "execution_count": null, + "id": "58d00342", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "5857f23f", "metadata": {}, "source": [ "#### 89. How to get the n largest values of an array (★★★)" @@ -1317,12 +1498,14 @@ { "cell_type": "code", "execution_count": null, + "id": "2bcc9c8b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "f36eb793", "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": "6c02e6e6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "e70c8050", "metadata": {}, "source": [ "#### 91. How to create a record array from a regular array? (★★★)" @@ -1345,12 +1530,14 @@ { "cell_type": "code", "execution_count": null, + "id": "3d13a2d5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "2a5aefce", "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": "f905531d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "4218947c", "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": "9df857c8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "d1a3499b", "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": "457835a5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "92abcbb3", "metadata": {}, "source": [ "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" @@ -1401,12 +1594,14 @@ { "cell_type": "code", "execution_count": null, + "id": "143ab446", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "533d9ad0", "metadata": {}, "source": [ "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" @@ -1415,12 +1610,14 @@ { "cell_type": "code", "execution_count": null, + "id": "ce13ac74", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "45b80432", "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": "41cf9dd9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "c95c5032", "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": "c7259ce6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "c8512547", "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": "e4a0e867", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", + "id": "6de8ff95", "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": "f62a28f2", "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_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index a8eabc6..4730a3d 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) @@ -355,8 +354,7 @@ print(np.trunc(Z)) `hint: np.arange` ```python -Z = np.zeros((5,5)) -Z += np.arange(5) +Z = np.tile(np.arange(0, 5), (5,1)) print(Z) ``` #### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆) @@ -472,7 +470,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? (★★☆) @@ -681,10 +679,24 @@ print(F) # Author: Fisher Wang w, h = 256, 256 -I = np.random.randint(0, 4, (w, h, 3)).astype(np.ubyte) +I = np.random.randint(0, 4, (h, w, 3)).astype(np.ubyte) colors = np.unique(I.reshape(-1, 3), axis=0) n = len(colors) print(n) + +# Faster version +# Author: Mark Setchell +# https://stackoverflow.com/a/59671950/2836621 + +w, h = 256, 256 +I = np.random.randint(0,4,(h,w,3), dtype=np.uint8) + +# View each pixel as a single 24-bit integer, rather than three 8-bit bytes +I24 = np.dot(I.astype(np.uint32),[1,256,65536]) + +# Count unique colours +n = len(np.unique(I24)) +print(n) ``` #### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★) `hint: sum(axis=(-2,-1))` @@ -778,7 +790,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 4c13301..5b21657 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) @@ -355,8 +354,7 @@ print(np.trunc(Z)) ```python -Z = np.zeros((5,5)) -Z += np.arange(5) +Z = np.tile(np.arange(0, 5), (5,1)) print(Z) ``` #### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆) @@ -472,7 +470,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? (★★☆) @@ -681,10 +679,24 @@ print(F) # Author: Fisher Wang w, h = 256, 256 -I = np.random.randint(0, 4, (w, h, 3)).astype(np.ubyte) +I = np.random.randint(0, 4, (h, w, 3)).astype(np.ubyte) colors = np.unique(I.reshape(-1, 3), axis=0) n = len(colors) print(n) + +# Faster version +# Author: Mark Setchell +# https://stackoverflow.com/a/59671950/2836621 + +w, h = 256, 256 +I = np.random.randint(0,4,(h,w,3), dtype=np.uint8) + +# View each pixel as a single 24-bit integer, rather than three 8-bit bytes +I24 = np.dot(I.astype(np.uint32),[1,256,65536]) + +# Count unique colours +n = len(np.unique(I24)) +print(n) ``` #### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★) @@ -1179,4 +1191,4 @@ idx = np.random.randint(0, X.size, (N, X.size)) means = X[idx].mean(axis=1) confint = np.percentile(means, [2.5, 97.5]) print(confint) -``` +``` \ No newline at end of file diff --git a/100_Numpy_random.ipynb b/100_Numpy_random.ipynb index b05f89d..f7dbbc3 100644 --- a/100_Numpy_random.ipynb +++ b/100_Numpy_random.ipynb @@ -2,6 +2,7 @@ "cells": [ { "cell_type": "markdown", + "id": "68fdc73a", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -17,6 +18,7 @@ }, { "cell_type": "markdown", + "id": "fb354250", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -24,6 +26,7 @@ }, { "cell_type": "markdown", + "id": "a6d059d8", "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": "390f283b", "metadata": {}, "outputs": [], "source": [ @@ -42,6 +46,7 @@ { "cell_type": "code", "execution_count": null, + "id": "ac498ee7", "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 8ea7c91..7a856a5 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -451,8 +451,7 @@ Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆) hint: np.arange < a37 -Z = np.zeros((5,5)) -Z += np.arange(5) +Z = np.tile(np.arange(0, 5), (5,1)) print(Z) < q38