{ "cells": [ { "cell_type": "markdown", "metadata": { "_uuid": "7dac2651304bbdd274a99291c17f3be1e84c222b" }, "source": [ "## Adapted from :\n", "\n", "### 100 numpy exercises\n", "\n", "This is a collection of exercises that have been collected in the numpy mailing list, on stack overflow and in the numpy documentation. The goal of this collection is to offer a quick reference for both old and new users but also to provide a set of exercises for those who teach.\n", "\n", "\n", "If you find an error or think you've a better way to solve some of them, feel free to open an issue at " ] }, { "cell_type": "markdown", "metadata": { "_uuid": "bf7265d79ded8d1b32c6ac6cf2ba89d6eba96461" }, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "1f42e0ab79d92a23b119e23ab8dda63393447466", "collapsed": true, "jupyter": { "outputs_hidden": true } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "2a9c567c3500905ffd716f33d72f2ce455931c6b" }, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "f92ca9022729c7247a92da557f1898051c1bf21d" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "77978f9afb5f957440f180861ccb06c2f644c317" }, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "beeb301d90b0fd6cb0f015969c969870bee981e8" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "c75ba6ea73f0a23f837c1553a0f564fbb8748557" }, "source": [ "#### 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "a916d1332f7e1d7499fd9de3bb422651711df025" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "118478fd9f22dc99584c1517078f1cdd4a4b36fc" }, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "ac7cbc58129168a298298ae6d3a756a2a94c2a5d" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "1c153cfc18b4c413b602d9fa073a4c049f1e703b" }, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "f1498235d2137116b1a7fabc6626dc8549235aba" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "647011edcdd237ef4bfa0ec4ce3a108805bbd11f" }, "source": [ "#### 9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "d6bf86992dbc3539332876b2caf973bafdb9a790" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "c7d5733bee779737b5ad4250b257d0a1423ffbff" }, "source": [ "#### 10. Find indices of non-zero elements from \\[1,2,0,0,4,0\\] (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "e6e0544a4326eaf8f3ea6390a16422a9844d48fc" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "ab5c1aaf0377ab923c6dbb7452671c181544734a" }, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "0e10bd7b228cff4c485d80142fa5afcea12eb0e1" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "9c3d53fd277d07937bc04838c4ca57a0aaaec498" }, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "77840458ba47b330dd84bab7a119e85299856c57", "collapsed": true, "jupyter": { "outputs_hidden": true } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "886bef95cd1aa9af4133902b45b50127b5da54cb" }, "source": [ "#### 13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "a8c13dc011c6afb824c3a7031330b6bd1a77e497" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "07db1e47dcff92113a6d4c696e92a5578b20d89f" }, "source": [ "#### 14. Create a random vector of size 30 and find the mean value (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "a6533f70b28d583e6e1926a0e3e668b465d6ef6c" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "2234dffb1dffc779d1d24f269da232a560848ad0" }, "source": [ "#### 15. Create a 2d array with 1 on the border and 0 inside (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "03bef7c11ab2593768ce88331a92c0c7cc10d463" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "6e24eff853c1ead319eba0adf0efdf31a58c6944" }, "source": [ "#### 16. How to add a border (filled with 0's) around an existing array? (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "573dd1238aef49fed4a9cfeb645c09119a307916" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "e22023622e0ec70b97ca378772dae42cff45d73c" }, "source": [ "#### 18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "f1aaa690119ec31eb516c96023250ff10f2800be" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "2e1e85761e1eafa4cd8986a3aa28763f2b8197e2" }, "source": [ "#### 19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "467504e8796a218a3faa0a787775d8aa577f4c32" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "87e644b2696a16879dcf96b35b17a48db550cd04" }, "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": null, "metadata": { "_uuid": "eabdb2c5e23366c763f0cc39939ee68866c3f111" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "87fb9e8995db8ee27d3c209c840f3f2c6acb9bc5" }, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "70b01b18cef5e31ffcc4c0219859e607e7a6d998" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "659926bf5bf80842361a658fd0f84bf24084fbf3" }, "source": [ "#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "8be73ba1bd9778d487ac20ef5658c7b8e57c3d67" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "0a65567844ac53a18dbb387a8694da2b1c78294a" }, "source": [ "#### 25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "d48512adb16f06371f651dd8dc1ae9ea1c3586b1" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "dad4e194cbf30833bbc7dba6714e300239088610" }, "source": [ "#### 26. What is the output of the following script? (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "094e225f24cf70ad8c7caeb1dcbd6788e5af562a" }, "outputs": [], "source": [ "# Author: Jake VanderPlas\n", "\n", "print(sum(range(5),-1))\n", "from numpy import *\n", "print(sum(range(5),-1))" ] }, { "cell_type": "markdown", "metadata": { "_uuid": "c13814936adf93d11212e3e85e67a37e48b1fd72" }, "source": [ "#### 27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "2d41e8fd1c2a4970ea55fcf50524a01e083d7f01" }, "outputs": [], "source": [ "Z**Z\n", "2 << Z >> 2\n", "Z <- Z\n", "1j*Z\n", "Z/1/1\n", "ZZ" ] }, { "cell_type": "markdown", "metadata": { "_uuid": "87cfa6cec4f5f3efa4d92b7d01be3e15e92eb8d9" }, "source": [ "#### 28. What are the result of the following expressions?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "8b9ea83b2b3a14de49b957a490ded427b2fdf473" }, "outputs": [], "source": [ "print(np.array(0) / np.array(0))\n", "print(np.array(0) // np.array(0))\n", "print(np.array([np.nan]).astype(int).astype(float))" ] }, { "cell_type": "markdown", "metadata": { "_uuid": "748fc33413222da461b2aac2a9fcabff3e18e057" }, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "b9e1b1bef8f15c72cc7ce5e2ce14def4a98ed41c" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "e49ca0d2249b4a5cf25b264e42c6feb3057889ed" }, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "b7a1b2802e4c4a2ac5baf737e2065c55bef226b6" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "861564e1209df602ba9acf76b21764166d941f64" }, "source": [ "#### 36. Extract the integer part of a random array using 5 different methods (★★☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "26071c39ddc28c65fe06329b480504455890609e" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "1acff22fb998865b58d5a9b37b0a5ce5b7f13387" }, "source": [ "#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "728c78dc7d9477e3300f58d589fbbc888df3cecb" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "c52ceff787ef50f22325107cb5f5b1bfac61858f" }, "source": [ "#### 39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "bcdb9a3b3a6a47768f53ea8e25b70070566c87eb" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "34f5d6e2e269678714f1e5aafec8a92d6411ecd3" }, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "650bd640404dd8cfdbeb0d91b8fc5ea844ee60a8" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "0502011982ec9de8f998bdcc76ff2d00dc9bad6e" }, "source": [ "#### 42. Consider two random array A and B, check if they are equal (★★☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "bc542c3112954260eac0bcf08bc8b0ae2ce0ae33" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "353f854c6c99f2889b0d31cc69bc1fd7fccd0fe1" }, "source": [ "#### 45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "fd9c6de99887f58497e8880f38b8aab24beaef6a" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "f7790f5d373c9f35e4407da026b1f432678e6a54" }, "source": [ "#### 46. Create a structured array with `x` and `y` coordinates covering the \\[0,1\\]x\\[0,1\\] area (★★☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "d41b7ebb14c22e1a447c4fed771bcc806a2e30b1" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "879952e7f6a37f63e8ebadf4baa516325eeb60c6" }, "source": [ "#### 48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "c0582da4b3a82183e3230a0249e9a07bb9329698" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "3cd7d90b37471536a03b27117c01d82a1ed1439c" }, "source": [ "#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "2d0087af5d396d4b823122485c006f0e4a8f5b86" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "ad23d8b28a5016197192b4a6ecdfa61d3c54f49f" }, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "62937a93af1f4bd2ee201a0a28d92ad2e39aeadf" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "_uuid": "c446326eba80f10ee44e69c38975a02b76272c66" }, "source": [ "#### 61. Find the nearest value from a given value in an array (★★☆)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "475de3550f77c7407940a0b31eeb5016286445c4" }, "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.8.8" } }, "nbformat": 4, "nbformat_minor": 4 }