{ "cells": [ { "cell_type": "markdown", "metadata": { "cell_id": "00000-62ede752-ff02-42bd-8e68-beda6bd479d8", "deepnote_cell_type": "markdown", "tags": [] }, "source": [ "[![View on GitHub](https://img.shields.io/badge/GitHub-View_on_GitHub-blue?logo=GitHub)](https://github.com/khuyentran1401/Data-science/blob/master/python/functools%20example.ipynb)\n", "\n", "[](https://deepnote.com/project/Data-science-hxlyJpi-QrKFJziQgoMSmQ/%2FData-science%2Fpython%2Ffunctools%20example.ipynb)" ] }, { "cell_type": "markdown", "metadata": { "cell_id": "00000-41252bfc-c56f-42cc-b3c8-67f4eb02b58f", "deepnote_cell_type": "markdown" }, "source": [ "# functools.partial" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2021-11-12T23:04:49.279883Z", "start_time": "2021-11-12T23:04:49.216809Z" }, "allow_embed": "code_output", "cell_id": "00002-72aa08e6-76c4-4d79-9d9c-10e2e21c6946", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 88, "execution_start": 1636760107587, "source_hash": "147300f6" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
alcoholmalic_acidashalcalinity_of_ashmagnesiumtotal_phenolsflavanoidsnonflavanoid_phenolsproanthocyaninscolor_intensityhueod280/od315_of_diluted_winesprolinetarget
014.231.712.4315.6127.02.803.060.282.295.641.043.921065.00
113.201.782.1411.2100.02.652.760.261.284.381.053.401050.00
213.162.362.6718.6101.02.803.240.302.815.681.033.171185.00
314.371.952.5016.8113.03.853.490.242.187.800.863.451480.00
413.242.592.8721.0118.02.802.690.391.824.321.042.93735.00
514.201.762.4515.2112.03.273.390.341.976.751.052.851450.00
614.391.872.4514.696.02.502.520.301.985.251.023.581290.00
714.062.152.6117.6121.02.602.510.311.255.051.063.581295.00
814.831.642.1714.097.02.802.980.291.985.201.082.851045.00
913.861.352.2716.098.02.983.150.221.857.221.013.551045.00
\n", "
" ], "text/plain": [ " alcohol malic_acid ash alcalinity_of_ash magnesium total_phenols \\\n", "0 14.23 1.71 2.43 15.6 127.0 2.80 \n", "1 13.20 1.78 2.14 11.2 100.0 2.65 \n", "2 13.16 2.36 2.67 18.6 101.0 2.80 \n", "3 14.37 1.95 2.50 16.8 113.0 3.85 \n", "4 13.24 2.59 2.87 21.0 118.0 2.80 \n", "5 14.20 1.76 2.45 15.2 112.0 3.27 \n", "6 14.39 1.87 2.45 14.6 96.0 2.50 \n", "7 14.06 2.15 2.61 17.6 121.0 2.60 \n", "8 14.83 1.64 2.17 14.0 97.0 2.80 \n", "9 13.86 1.35 2.27 16.0 98.0 2.98 \n", "\n", " flavanoids nonflavanoid_phenols proanthocyanins color_intensity hue \\\n", "0 3.06 0.28 2.29 5.64 1.04 \n", "1 2.76 0.26 1.28 4.38 1.05 \n", "2 3.24 0.30 2.81 5.68 1.03 \n", "3 3.49 0.24 2.18 7.80 0.86 \n", "4 2.69 0.39 1.82 4.32 1.04 \n", "5 3.39 0.34 1.97 6.75 1.05 \n", "6 2.52 0.30 1.98 5.25 1.02 \n", "7 2.51 0.31 1.25 5.05 1.06 \n", "8 2.98 0.29 1.98 5.20 1.08 \n", "9 3.15 0.22 1.85 7.22 1.01 \n", "\n", " od280/od315_of_diluted_wines proline target \n", "0 3.92 1065.0 0 \n", "1 3.40 1050.0 0 \n", "2 3.17 1185.0 0 \n", "3 3.45 1480.0 0 \n", "4 2.93 735.0 0 \n", "5 2.85 1450.0 0 \n", "6 3.58 1290.0 0 \n", "7 3.58 1295.0 0 \n", "8 2.85 1045.0 0 \n", "9 3.55 1045.0 0 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.datasets import load_wine\n", "import pandas as pd \n", "\n", "X, y = load_wine(as_frame=True, return_X_y=True)\n", "df = X.merge(y, left_index=True, right_index=True)\n", "df.head(10)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2021-11-12T23:11:23.304259Z", "start_time": "2021-11-12T23:11:23.296809Z" }, "allow_embed": true, "cell_id": "00003-37bc24f0-0ae0-420f-acb6-642b601426b5", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 0, "execution_start": 1636760203860, "source_hash": "b322cb21" }, "outputs": [], "source": [ "def get_count_above_threshold_per_col_df(\n", " threshold: str, column: str, df: pd.DataFrame\n", "):\n", " return (df[column] > threshold).sum()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2021-11-12T23:11:26.384349Z", "start_time": "2021-11-12T23:11:26.374164Z" }, "allow_embed": true, "cell_id": "00004-244d7dc0-daa4-4d45-ae37-4692dc4a8f18", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 15, "execution_start": 1636760205441, "source_hash": "f640ce5a" }, "outputs": [ { "data": { "text/plain": [ "82" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get number of rows above 98 in the magnesium column\n", "get_count_above_threshold_per_col_df(98, 'magnesium', df)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-11-12T23:11:28.718214Z", "start_time": "2021-11-12T23:11:28.708481Z" }, "allow_embed": true, "cell_id": "00005-d87026ff-31f8-449b-a8af-18a49a2afb04", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 8, "execution_start": 1636760207572, "source_hash": "a9a3744" }, "outputs": [ { "data": { "text/plain": [ "92" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get number of rows above 13 in the alcohol column\n", "get_count_above_threshold_per_col_df(13, 'alcohol', df)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2021-11-12T23:11:34.965076Z", "start_time": "2021-11-12T23:11:34.959485Z" }, "allow_embed": true, "cell_id": "00006-51c1886e-2131-49c5-b956-295c3288758e", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 4, "execution_start": 1636760213673, "source_hash": "c5d10393" }, "outputs": [], "source": [ "from functools import partial\n", "\n", "get_count_above_threshold_per_col = partial(\n", " get_count_above_threshold_per_col_df, df=df\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-11-12T23:11:41.789160Z", "start_time": "2021-11-12T23:11:41.778856Z" }, "allow_embed": "code_output", "cell_id": "00007-5ef93021-09a4-4e24-9e05-19862df9da47", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 9, "execution_start": 1636760217872, "source_hash": "87a7f7c3" }, "outputs": [ { "data": { "text/plain": [ "92" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get_count_above_threshold_per_col(13, 'alcohol')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "allow_embed": true, "cell_id": "00008-d7abbd9a-28b5-488d-b0f8-8c73b200bd89", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 103, "execution_start": 1636760221517, "source_hash": "546a5539", "tags": [] }, "outputs": [ { "data": { "text/plain": [ "178" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get_count_above_threshold_per_col(13, 'magnesium')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "allow_embed": true, "cell_id": "00009-14738bb8-c0a6-464b-9cc9-6fe358a123a4", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 1, "execution_start": 1636760272922, "source_hash": "6afa7724", "tags": [] }, "outputs": [], "source": [ "get_count_above_threshold_magnesium = partial(\n", " get_count_above_threshold_per_col, column='magnesium'\n", ")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "allow_embed": true, "cell_id": "00010-d9dd8e58-a650-4517-ad87-2a4953852533", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 23, "execution_start": 1636761816058, "source_hash": "84202d42", "tags": [] }, "outputs": [ { "data": { "text/plain": [ "82" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get_count_above_threshold_magnesium(98)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "allow_embed": "code_output", "cell_id": "00011-ec88f820-fbf6-430c-9d42-1f16ee67e90d", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 38, "execution_start": 1636761817037, "source_hash": "6e0a591a", "tags": [] }, "outputs": [ { "data": { "text/plain": [ "178" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get_count_above_threshold_magnesium(5)" ] }, { "cell_type": "markdown", "metadata": { "cell_id": "00008-5f9692b8-de28-4e3f-a6a2-6fb4c0c15d19", "deepnote_cell_type": "markdown" }, "source": [ "# functools.singledispatch" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "ExecuteTime": { "end_time": "2021-11-12T23:15:05.503720Z", "start_time": "2021-11-12T23:15:05.495774Z" }, "allow_embed": true, "cell_id": "00010-be650320-6b55-43dd-af9a-a18f4eacf845", "deepnote_cell_type": "code" }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "\n", "def create_data(data):\n", " if isinstance(data, dict):\n", " create_data_from_dict(data)\n", "\n", " if isinstance(data, list):\n", " create_data_from_list(data)\n", "\n", " else:\n", " NotImplementedError(f\"Type {type(data)} is unsupported\")\n", "\n", "\n", "def create_data_from_dict(data: dict):\n", " print(f\"Data is created from a {type(data)}.\")\n", " return pd.DataFrame(data)\n", "\n", "\n", "def create_data_from_list(data: list):\n", " print(f\"Data is created from a {type(data)}.\")\n", " return pd.DataFrame(data, columns=[\"a\", \"b\"])\n" ] }, { "cell_type": "code", "execution_count": 83, "id": "642e537c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data is created from a .\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ab
014
125
236
\n", "
" ], "text/plain": [ " a b\n", "0 1 4\n", "1 2 5\n", "2 3 6" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\"a\": [1, 2, 3], \"b\": [4, 5, 6]}\n", "create_data(data)" ] }, { "cell_type": "code", "execution_count": 84, "id": "26088f18", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data is created from a .\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ab
014
125
236
\n", "
" ], "text/plain": [ " a b\n", "0 1 4\n", "1 2 5\n", "2 3 6" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data2 = [(1, 4), (2, 5), (3, 6)]\n", "create_data(data2)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "ExecuteTime": { "end_time": "2021-11-12T23:19:36.971963Z", "start_time": "2021-11-12T23:19:36.963779Z" }, "allow_embed": "code_output", "cell_id": "00011-07903296-5e50-4276-9a25-1bb201ae2072", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 1, "execution_start": 1636769116464, "source_hash": "ff9a12f0" }, "outputs": [], "source": [ "from functools import singledispatch\n", "\n", "@singledispatch\n", "def create_data(data):\n", " raise NotImplementedError(f\"Type {type(data)} is unsupported\")\n", "\n", "@create_data.register\n", "def create_data_from_dict(data: dict): \n", " print(f\"Data is created from a {type(data)}.\")\n", " return pd.DataFrame(data)\n", "\n", "@create_data.register\n", "def create_data_from_list(data: list):\n", " print(f\"Data is created from a {type(data)}.\")\n", " return pd.DataFrame(data, columns=[\"a\", \"b\"])" ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "ExecuteTime": { "end_time": "2021-11-12T23:19:42.134874Z", "start_time": "2021-11-12T23:19:42.127504Z" }, "allow_embed": "code_output", "cell_id": "00012-0aeab9ba-2ff5-40e1-90fb-7bd5443d0dd1", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 308, "execution_start": 1636771200194, "source_hash": "efc0b339" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data is created from a .\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ab
014
125
236
\n", "
" ], "text/plain": [ " a b\n", "0 1 4\n", "1 2 5\n", "2 3 6" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\"a\": [1, 2, 3], \"b\": [4, 5, 6]}\n", "\n", "create_data(data)" ] }, { "cell_type": "code", "execution_count": 87, "id": "e6248b88", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data is created from a .\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ab
014
125
236
\n", "
" ], "text/plain": [ " a b\n", "0 1 4\n", "1 2 5\n", "2 3 6" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data2 = [(1, 4), (2, 5), (3, 6)]\n", "create_data(data2)" ] }, { "cell_type": "code", "execution_count": 80, "id": "b586023c", "metadata": {}, "outputs": [ { "ename": "NotImplementedError", "evalue": "Type is unsupported", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNotImplementedError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn [80], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m data3 \u001b[39m=\u001b[39m ((\u001b[39m1\u001b[39m, \u001b[39m4\u001b[39m), (\u001b[39m2\u001b[39m, \u001b[39m5\u001b[39m), (\u001b[39m3\u001b[39m, \u001b[39m6\u001b[39m))\n\u001b[0;32m----> 2\u001b[0m create_data(data3)\n", "File \u001b[0;32m/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/functools.py:877\u001b[0m, in \u001b[0;36msingledispatch..wrapper\u001b[0;34m(*args, **kw)\u001b[0m\n\u001b[1;32m 873\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m args:\n\u001b[1;32m 874\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mTypeError\u001b[39;00m(\u001b[39mf\u001b[39m\u001b[39m'\u001b[39m\u001b[39m{\u001b[39;00mfuncname\u001b[39m}\u001b[39;00m\u001b[39m requires at least \u001b[39m\u001b[39m'\u001b[39m\n\u001b[1;32m 875\u001b[0m \u001b[39m'\u001b[39m\u001b[39m1 positional argument\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[0;32m--> 877\u001b[0m \u001b[39mreturn\u001b[39;00m dispatch(args[\u001b[39m0\u001b[39;49m]\u001b[39m.\u001b[39;49m\u001b[39m__class__\u001b[39;49m)(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkw)\n", "Cell \u001b[0;32mIn [77], line 5\u001b[0m, in \u001b[0;36mcreate_data\u001b[0;34m(data)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[39m@singledispatch\u001b[39m\n\u001b[1;32m 4\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mcreate_data\u001b[39m(data):\n\u001b[0;32m----> 5\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mNotImplementedError\u001b[39;00m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mType \u001b[39m\u001b[39m{\u001b[39;00m\u001b[39mtype\u001b[39m(data)\u001b[39m}\u001b[39;00m\u001b[39m is unsupported\u001b[39m\u001b[39m\"\u001b[39m)\n", "\u001b[0;31mNotImplementedError\u001b[0m: Type is unsupported" ] } ], "source": [ "data3 = ((1, 4), (2, 5), (3, 6))\n", "create_data(data3)" ] }, { "cell_type": "code", "execution_count": 90, "id": "fa484bc9", "metadata": {}, "outputs": [], "source": [ "@singledispatch\n", "def create_data(data):\n", " raise NotImplementedError(f\"Type {type(data)} is unsupported\")\n", "\n", "@create_data.register(dict)\n", "def create_data_from_dict(data): \n", " print(f\"Data is created from a {type(data)}.\")\n", " return pd.DataFrame(data)\n", "\n", "@create_data.register(list)\n", "@create_data.register(tuple)\n", "def create_data_from_list(data):\n", " print(f\"Data is created from a {type(data)}.\")\n", " return pd.DataFrame(data, columns=[\"a\", \"b\"])" ] }, { "cell_type": "code", "execution_count": 91, "id": "00707005", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data is created from a .\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ab
014
125
236
\n", "
" ], "text/plain": [ " a b\n", "0 1 4\n", "1 2 5\n", "2 3 6" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data3 = ((1, 4), (2, 5), (3, 6))\n", "create_data(data3)" ] } ], "metadata": { "deepnote": {}, "deepnote_execution_queue": [], "deepnote_notebook_id": "1feaac36-bf5e-4524-910f-26219867e5d8", "kernelspec": { "display_name": "venv", "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.9.15 (main, Oct 11 2022, 21:39:54) \n[Clang 14.0.0 (clang-1400.0.29.102)]" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "vscode": { "interpreter": { "hash": "478cc5ef0f338997730d9103983e193fe6c3c8e58abb3c8c0481de8a2f51bddc" } } }, "nbformat": 4, "nbformat_minor": 5 }