From 207a8a5675b4d0d3c111c1f4dc5cbf7cfb76f739 Mon Sep 17 00:00:00 2001 From: perikoloso Date: Mon, 16 Oct 2023 21:02:48 +0200 Subject: [PATCH] [Pere] map filter reduce lab --- your-code/main.ipynb | 283 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 236 insertions(+), 47 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index a2958dd..c1f1cca 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,11 +12,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ - "# Import reduce from functools, numpy and pandas" + "# Import reduce from functools, numpy and pandas\n", + "\n", + "import numpy as np\n", + "from functools import reduce\n", + "import pandas as pd" ] }, { @@ -32,7 +36,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -54,11 +58,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "del prophet[0:568]\n" ] }, { @@ -70,11 +74,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['PROPHET\\n\\n|Almustafa,',\n", + " 'the{7}',\n", + " 'chosen',\n", + " 'and',\n", + " 'the\\nbeloved,',\n", + " 'who',\n", + " 'was',\n", + " 'a',\n", + " 'dawn',\n", + " 'unto']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "prophet[0:10]" ] }, { @@ -88,7 +112,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -96,13 +120,18 @@ " '''\n", " Input: A string\n", " Output: The string with references removed\n", - " \n", + "\n", " Example:\n", " Input: 'the{7}'\n", " Output: 'the'\n", " '''\n", - " \n", - " # your code here" + " clean_word = ''\n", + " if \"{\" in x:\n", + " v_index = x.index(\"{\")\n", + " clean_word = x[0:v_index]\n", + " else:\n", + " clean_word = x\n", + " return clean_word\n" ] }, { @@ -114,11 +143,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "prophet_reference = list(map(reference,prophet))\n" ] }, { @@ -130,7 +159,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -143,8 +172,7 @@ " Input: 'the\\nbeloved'\n", " Output: ['the', 'beloved']\n", " '''\n", - " \n", - " # your code here" + " return x.split(\"\\n\")" ] }, { @@ -156,11 +184,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "prophet_line = list(map(line_break,prophet_reference))" ] }, { @@ -172,11 +200,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "prophet_flat= [x for i in prophet_line for x in i]" ] }, { @@ -190,7 +218,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -210,8 +238,15 @@ " '''\n", " \n", " word_list = ['and', 'the', 'a', 'an']\n", + " if x in word_list:\n", + " return False\n", + " else:\n", + " return True\n", " \n", - " # your code here" + " \n", + "prophet_filter = list(filter(word_filter,prophet_flat))\n", + "\n", + " " ] }, { @@ -232,15 +267,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "def word_filter_case(x):\n", " \n", " word_list = ['and', 'the', 'a', 'an']\n", + " \n", + " if x.lower() in word_list:\n", + " return False\n", + " else:\n", + " return True\n", " \n", - " # your code here" + " \n", + "prophet_filter = list(filter(word_filter_case,prophet_flat))" ] }, { @@ -256,7 +297,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -270,7 +311,8 @@ " Output: 'John Smith'\n", " '''\n", " \n", - " # your code here" + " output = a +\" \"+b\n", + " return output" ] }, { @@ -282,11 +324,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "prophet_string = reduce(concat_space,prophet_filter)" ] }, { @@ -302,7 +344,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, "outputs": [], "source": [ @@ -318,11 +360,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "import pandas as pd\n", + "import numpy as np" ] }, { @@ -334,10 +377,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "df = pd.read_csv(url)" + ] }, { "cell_type": "markdown", @@ -348,11 +393,146 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 37, + "metadata": {}, + "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", + "
Noyearmonthdayhourpm2.5DEWPTEMPPREScbwdIwsIsIr
012010110NaN-21-11.01021.0NW1.7900
122010111NaN-21-12.01020.0NW4.9200
232010112NaN-21-11.01019.0NW6.7100
342010113NaN-21-14.01019.0NW9.8400
452010114NaN-20-12.01018.0NW12.9700
\n", + "
" + ], + "text/plain": [ + " No year month day hour pm2.5 DEWP TEMP PRES cbwd Iws Is Ir\n", + "0 1 2010 1 1 0 NaN -21 -11.0 1021.0 NW 1.79 0 0\n", + "1 2 2010 1 1 1 NaN -21 -12.0 1020.0 NW 4.92 0 0\n", + "2 3 2010 1 1 2 NaN -21 -11.0 1019.0 NW 6.71 0 0\n", + "3 4 2010 1 1 3 NaN -21 -14.0 1019.0 NW 9.84 0 0\n", + "4 5 2010 1 1 4 NaN -20 -12.0 1018.0 NW 12.97 0 0" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.head()" ] }, { @@ -364,7 +544,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, "outputs": [], "source": [ @@ -378,7 +558,9 @@ " Output: 2.0\n", " '''\n", " \n", - " # your code here" + " hours = x/24\n", + " return hours\n", + "\n" ] }, { @@ -390,11 +572,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "df[[\"Iws\"]] = hourly(df[[\"Iws\"]])\n", + "df[[\"Is\"]] = hourly(df[[\"Is\"]])\n", + "df[[\"Ir\"]] = hourly(df[[\"Ir\"]])\n", + "\n", + "pm25_hourly = df\n", + "\n" ] }, { @@ -421,7 +608,9 @@ " Input: pd.Series([1,2,3,4])\n", " Output: 0.3726779962\n", " '''\n", - " \n", + " sd = \n", + "\n", + " return sd\n", " # your code here" ] } @@ -442,7 +631,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.11.4" }, "toc": { "base_numbering": 1,