{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "sOW3cWiycXj3" }, "source": [ "# Python Course 1 - Numpy exercises\n", "\n", "Reference: adapted from \n", "\n", "The following exercises will help you get better with numpy. They have three levels of difficulty. Don't be discouraged if you can't solve everything. If you have trouble, consult the solutions.\n", "\n", "This is the exercise sheet." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Cf_6ZKuPcXj7" }, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "Ie_GrVLucXj9" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "JeldsooKcXkE" }, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "P9f4UUPmcXkF" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "ylzBy9ejcXkJ" }, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "Yn9KtvcNcXkL" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "jpscY2mCcXkP" }, "source": [ "#### 4. How to find the memory size of any array (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "pnDjgPfwcXkR" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "GTNvekedcXkV" }, "source": [ "#### 5. How to get the documentation of the numpy add function? (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "BA9PSnH-cXkW" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "uxyiGB98cXka" }, "source": [ "#### 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "jAuspRNDcXkb" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "FXjliBxjcXkf" }, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "vYR6qT_qcXkg" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "7AyO685CcXkj" }, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "RbwLCVjTcXkl" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "6DdmxcQDcXkp" }, "source": [ "#### 9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "bfGA3tIFcXkr" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "BZqucrF_cXkt" }, "source": [ "#### 10. Find indices of non-zero elements from \\[1,2,0,0,4,0\\] (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "Q_zmrQfgcXku" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "_0NPfVIZcXkx" }, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "lZKInzZ5cXky" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "moaibqgdcXk1" }, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "collapsed": true, "id": "G29TH_FncXk2" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "VXTXYktccXk5" }, "source": [ "#### 13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "sczpnuSQcXk6" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "_f18Z-2DcXk-" }, "source": [ "#### 14. Create a random vector of size 30 and find the mean value (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "bzyVRkVJcXk_" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "HkBVYcTRcXlA" }, "source": [ "#### 15. Create a 2d array with 1 on the border and 0 inside (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "62mosB-ncXlD" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "AHPQoBwPcXlF" }, "source": [ "#### 16. How to add a border (filled with 0's) around an existing array? (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "d8BM3jF6cXlG" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "E0Hmow5ZcXlJ" }, "source": [ "#### 17. What is the result of the following expression? (★☆☆)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "u0XDwg3HcXlL" }, "source": [ "```python\n", "0 * np.nan\n", "np.nan == np.nan\n", "np.inf > np.nan\n", "np.nan - np.nan\n", "np.nan in set([np.nan])\n", "0.3 == 3 * 0.1\n", "```" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "DOXd8eHvcXlM" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Ea2Fe_c7cXlO" }, "source": [ "#### 18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "AYxJ_X79cXlO" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "O5FIWj9icXlR" }, "source": [ "#### 19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "qleRMchQcXlT" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "m1IIY-QVcXlV" }, "source": [ "#### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "LNQwjQLycXlV" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "MOfvgt4HcXlY" }, "source": [ "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "HfY5vj15cXlY" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "-oDOudvTcXlc" }, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "AXr0pdQ7cXld" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "wT6Eu-YkcXlg" }, "source": [ "#### 23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "collapsed": true, "id": "mslu_qrUcXlh" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "8I4apGA5cXlk" }, "source": [ "#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "LlLi4xPOcXlk" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "-FNxW0rkcXlm" }, "source": [ "#### 25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "RXPh0TS5cXln" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "xSQBCfuJcXlo" }, "source": [ "#### 26. What is the output of the following script? (★☆☆)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "9nPzwwiIcXlp" }, "source": [ "```python\n", "# Author: Jake VanderPlas\n", "\n", "print(sum(range(5),-1))\n", "from numpy import *\n", "print(sum(range(5),-1))\n", "```" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "mgLfOx-TcXlp" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "2E7AuFqncXlr" }, "source": [ "#### 27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "pK6-hAh0cXls" }, "source": [ "```python\n", "Z**Z\n", "2 << Z >> 2\n", "Z <- Z\n", "1j*Z\n", "Z/1/1\n", "ZZ\n", "```" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "8R1B4L5hcXlt" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "rMtNJZQMcXlu" }, "source": [ "#### 28. What are the result of the following expressions?" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "e5QqCszhcXlu" }, "source": [ "```python\n", "np.array(0) / np.array(0)\n", "np.array(0) // np.array(0)\n", "np.array([np.nan]).astype(int).astype(float)\n", "```" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "fLddxeD-cXlv" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "jPkdhE08cXlx" }, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "iqYY27VQcXly" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "tGTOp9G4cXl0" }, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "SLkyIGDRcXl0" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "fnjNeh_QcXl2" }, "source": [ "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "hQbEKHJHcXl2" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "l8Jtw5AWcXl3" }, "source": [ "#### 32. Is the following expressions true? (★☆☆)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "yBL3wC_3cXl5" }, "source": [ "```python\n", "np.sqrt(-1) == np.emath.sqrt(-1)\n", "```" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "D67pqgekcXl5" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "cpUJddFicXl9" }, "source": [ "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "collapsed": true, "id": "kilVl30hcXl9" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "YBfXPaf7cXmA" }, "source": [ "#### 34. How to get all the dates corresponding to the month of July 2016? (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "K-5SNYsfcXmA" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "y7naf3RbcXmD" }, "source": [ "#### 35. How to compute ((A+B)\\*(-A/2)) in place (without copy)? (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "brUjhQZBcXmD" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "fJfzsOI2cXmE" }, "source": [ "#### 36. Extract the integer part of a random array using 5 different methods (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "qVk0zo0xcXmF" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "DthRnIiwcXmH" }, "source": [ "#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "xuL-x4ZtcXmI" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "gBBD5i1NcXmK" }, "source": [ "#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "f0RIcXsocXmL" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "9kTjKPn5cXmL" }, "source": [ "#### 39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "YemNIsuDcXmM" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "jR0mnD1hcXmN" }, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "Ul950trBcXmX" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "5zR2NOVPcXmZ" }, "source": [ "#### 41. How to sum a small array faster than np.sum? (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "fjgpR-xPcXmZ" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "a2zaHtvEcXmb" }, "source": [ "#### 42. Consider two random array A and B, check if they are equal (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "Sc-x7YqVcXmb" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "4-_jslgAcXmc" }, "source": [ "#### 43. Make an array immutable (read-only) (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "CyOzn5tZcXmd" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "hBk_6juacXmh" }, "source": [ "#### 44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "pceHUEmTcXmi" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "heWNnu1DcXmk" }, "source": [ "#### 45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "1kF2cDQwcXmk" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "oPJxzEzIcXml" }, "source": [ "#### 46. Create a structured array with `x` and `y` coordinates covering the \\[0,1\\]x\\[0,1\\] area (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "sLuDUYNFcXmm" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "LYLtDAYQcXmn" }, "source": [ "#### 47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj))" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "-aQYY_ibcXmo" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "gvmNqYXfcXmp" }, "source": [ "#### 48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "dJ-rwX4RcXmq" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "u5niBelWcXmr" }, "source": [ "#### 49. How to print all the values of an array? (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "LOAoz2zGcXmr" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "qCb4dk0PcXmt" }, "source": [ "#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "gzKj25KncXmt" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "3tvN4J8DcXmu" }, "source": [ "#### 51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "lpSZOqGIcXmu" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "S6llW12ScXmw" }, "source": [ "#### 52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "lg6KpVlDcXmw" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "WEVKm5JqcXmy" }, "source": [ "#### 53. How to convert a float (32 bits) array into an integer (32 bits) in place?" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "resM5ToWcXmz" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "njtqjimscXm0" }, "source": [ "#### 54. How to read the following file? (★★☆)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "cfyfoCFlcXm0" }, "source": [ "```\n", "1, 2, 3, 4, 5\n", "6, , , 7, 8\n", " , , 9,10,11\n", "```" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "-ljydHpgcXm2" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "6jnmJogNcXm3" }, "source": [ "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "FplG-ZC5cXm4" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "pvWk7nO3cXm5" }, "source": [ "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "hSusLD32cXm5" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "hfiuSwMdcXm7" }, "source": [ "#### 57. How to randomly place p elements in a 2D array? (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "zGDcXtmEcXm7" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "wCZ25idrcXm8" }, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "yun_ne8ZcXm8" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "hSJFlHCRcXm-" }, "source": [ "#### 59. How to sort an array by the nth column? (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "XwGN3B4-cXm-" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "73lA8XGGcXm-" }, "source": [ "#### 60. How to tell if a given 2D array has null columns? (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "g-3XfwZAcXnA" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "8v04HLW0cXnC" }, "source": [ "#### 61. Find the nearest value from a given value in an array (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "0oNKteXTcXnC" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "5AD9V_kjcXnE" }, "source": [ "#### 62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "4F9aUQ-7cXnF" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "CY33FdG1cXnG" }, "source": [ "#### 63. Create an array class that has a name attribute (★★☆)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "THNRtbTAcXnG" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "pWp3snCHcXnH" }, "source": [ "#### 64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "miV2vu9vcXnI" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "H2oytTHocXnJ" }, "source": [ "#### 65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "QEf9oVZkcXnJ" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "ub8LH_mxcXnM" }, "source": [ "#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "MiOxiy-LcXnN" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "D4GmsJu2cXnO" }, "source": [ "#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "hwGItKP7cXnO" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "d7hnN6jKcXnQ" }, "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? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "vZcSWZFXcXnQ" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "6V3MHHGmcXnR" }, "source": [ "#### 69. How to get the diagonal of a dot product? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "duVPCuH6cXnT" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "8lTqEdWmcXnV" }, "source": [ "#### 70. Consider the vector \\[1, 2, 3, 4, 5\\], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "SYh4Xen7cXnW" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "ZlM-EujXcXnX" }, "source": [ "#### 71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "collapsed": true, "id": "ochz9t2acXnY" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "rS9_b_5VcXnY" }, "source": [ "#### 72. How to swap two rows of an array? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "je7bqNxucXnZ" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "4Jp83LRhcXna" }, "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 (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "R-muTouicXna" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "ttFnebEjcXnb" }, "source": [ "#### 74. Given an array C that is a bincount, how to produce an array A such that np.bincount(A) == C? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "U20nNNMGcXnb" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Kp_f4-TPcXnc" }, "source": [ "#### 75. How to compute averages using a sliding window over an array? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "6mw9ss7ycXnd" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "VXCBHPMycXne" }, "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\\]) (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "9IMaKLK3cXnf" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "q7XgALVPcXnf" }, "source": [ "#### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "MLX0fVWacXng" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "tLj99saacXni" }, "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\\])? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "tqJnsxakcXni" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "5cvOjraacXni" }, "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\\])? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "9W3SkGRYcXni" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "HOzIlLvDcXnk" }, "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) (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "4GdDD6FGcXnk" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "-wq4qLl8cXnl" }, "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\\]\\]? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "KdsXW920cXnl" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "L2TN3jCicXnn" }, "source": [ "#### 82. Compute a matrix rank (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "L_VVi7xtcXnn" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "MvJTkSXScXnn" }, "source": [ "#### 83. How to find the most frequent value in an array?" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "GDr_JOQNcXno" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "lmBDpyaNcXnp" }, "source": [ "#### 84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "XKLxvNgbcXnp" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "sAAJ2MxycXnq" }, "source": [ "#### 85. Create a 2D array subclass such that Z\\[i,j\\] == Z\\[j,i\\] (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "9yiUt-sScXnq" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "_VkHYRdPcXnr" }, "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)) (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "Ylo4yKqqcXnr" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "BipacrxxcXnt" }, "source": [ "#### 87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "0TRs964VcXnt" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "ncRAvUowcXnt" }, "source": [ "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "17OaI9SMcXnu" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "EKgEWnq_cXnv" }, "source": [ "#### 89. How to get the n largest values of an array (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "u6VCqxaEcXnv" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "5T-Hpsh2cXnw" }, "source": [ "#### 90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "cwv8xDzDcXnx", "scrolled": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "GD2oK1V2cXny" }, "source": [ "#### 91. How to create a record array from a regular array? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "sLcJFnH-cXny" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "eU8OUSPhcXnz" }, "source": [ "#### 92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "6Cw-OL7XcXnz" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "pr-6ta-lcXn0" }, "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? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "ESxG0q2xcXn1" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "O9HxhT5ycXn2" }, "source": [ "#### 94. Considering a 10x3 matrix, extract rows with unequal values (e.g. \\[2,2,3\\]) (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "0Xam3kXWcXn3" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Sb4Z5NiFcXn3" }, "source": [ "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "7YJGIA2lcXn3" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "KwYvaZwHcXn4" }, "source": [ "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "nTfqz_93cXn5" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "pWOlbD5fcXn6" }, "source": [ "#### 97. Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "t2WgY3hocXn7" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "K6DmP3kjcXn7" }, "source": [ "#### 98. Considering a path described by two vectors (X,Y), how to sample it using equidistant samples (★★★)?" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "collapsed": true, "id": "-apO0eppcXn7" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "LAlTku9WcXn-" }, "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. (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "IpO7iSs_cXn-" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "BwqHkmtQcXoA" }, "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). (★★★)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "6xqK5BmNcXoA" }, "outputs": [], "source": [] } ], "metadata": { "colab": { "name": "Copy of python-tutorial-2.ipynb", "provenance": [ { "file_id": "1VYYwGsDSak_UAZ7q8myIem9P61iPXuZA", "timestamp": 1568710720018 } ], "version": "0.3.2" }, "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.3" } }, "nbformat": 4, "nbformat_minor": 1 }