ソースを参照

Adding exercises to the data structures tutorial

Marc Garcia 7 年 前
コミット
5dcd8f5ed8
3 ファイル変更98 行追加0 行削除
  1. 88 0
      02_Data_structures.ipynb
  2. 5 0
      solutions/data_structures_1.py
  3. 5 0
      solutions/data_structures_2.py

+ 88 - 0
02_Data_structures.ipynb

@@ -39,6 +39,16 @@
    "metadata": {},
    "outputs": [],
    "source": [
+    "# Access by slice\n",
+    "python_list[2:5]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
     "# Loop\n",
     "for number in python_list:\n",
     "    print(number)"
@@ -251,6 +261,45 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
+    "<div class=\"alert alert-success\">\n",
+    "    <p><b>EXERCISE 1:</b> Fibonacci numbers</p>\n",
+    "    <p>Tasks:\n",
+    "        <ul>\n",
+    "            <li>Create a Series with the first 10 Fibonacci numbers.</li>\n",
+    "            <li>Compute the squared number of each of them.</li> \n",
+    "            <li>Compute the sum of the last 4.</li>\n",
+    "        </ul>\n",
+    "    </p>\n",
+    "    <p>Hints:\n",
+    "        <ul>\n",
+    "            <li>You can compute the Fibonacci numbers with Python, or just write the first 10 manually.</li>\n",
+    "            <li>The Fibonacci sequence defines each element as the sum of the two previous, starting by <code>[1, 1]</code>.</li>\n",
+    "            <li>In Python you can compute the square of a number with <code>x ** 2</code>.</li>\n",
+    "        </ul>\n",
+    "    </p>\n",
+    "</div>"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "%load solutions/data_structures_1.py"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
     "### Data types"
    ]
   },
@@ -408,6 +457,45 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
+    "<div class=\"alert alert-success\">\n",
+    "    <p><b>EXERCISE 2:</b> Performance of int vs float</p>\n",
+    "    <p>Tasks:\n",
+    "        <ul>\n",
+    "            <li>Create a Series of 10 million of unsigned integers of 8 bits.</li>\n",
+    "            <li>Create a Series of 10 million of floats of 64 bits.</li> \n",
+    "            <li>Compare the performance when computing the mean in both Series.</li>\n",
+    "        </ul>\n",
+    "    </p>\n",
+    "    <p>Hints:\n",
+    "        <ul>\n",
+    "            <li>Signed integers are able to represent positive and negative numbers. Unsigned only positives and the number zero.</li>\n",
+    "            <li>Unsigned integers have the prefix `u` (`uint` vs `int`).</li>\n",
+    "            <li>NumPy types have often as a suffix the number of bits (e.g. `uint32`, `float16`).</li>\n",
+    "        </ul>\n",
+    "    </p>\n",
+    "</div>"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "%load solutions/data_structures_1.py"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
     "### pandas DataFrame"
    ]
   },

+ 5 - 0
solutions/data_structures_1.py

@@ -0,0 +1,5 @@
+fibonacci_numbers = [1, 1]
+[fibonacci_numbers.append(sum(fibonacci_numbers[-2:])) for i in range(8)]
+fibonacci_numbers = pandas.Series(fibonacci_numbers)
+
+(fibonacci_numbers ** 2)[-4:].sum()

+ 5 - 0
solutions/data_structures_2.py

@@ -0,0 +1,5 @@
+uint8_series = pandas.Series(numpy.empty(100_000_000, dtype=numpy.uint8))
+float64_series = pandas.Series(numpy.empty(100_000_000, dtype=numpy.float64))
+
+%timeit uint8_series.mean()
+%timeit float64_series.mean()