From dbf241ed13a085c97f7c7a5e32c8f268c5dd3622 Mon Sep 17 00:00:00 2001 From: Montserrat Labrada Date: Sat, 15 Oct 2022 21:46:00 -0400 Subject: [PATCH 1/2] Completed lab-functional programming --- .../your-code/main.ipynb | 647 +++++++++++++++++- 1 file changed, 614 insertions(+), 33 deletions(-) diff --git a/lab-functional-programming/your-code/main.ipynb b/lab-functional-programming/your-code/main.ipynb index 8017d6e..ef16094 100644 --- a/lab-functional-programming/your-code/main.ipynb +++ b/lab-functional-programming/your-code/main.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -146,9 +146,17 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "def divisible2(iterator):\n", " # This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", @@ -159,7 +167,13 @@ " # Sample Output: 2\n", " \n", " # Your code here:\n", - " " + " iterator=iter([1,2,3])\n", + " for item in iterator:\n", + " if item % 2 == 0:\n", + " return item\n", + " return 0\n", + "\n", + "print(divisible2(iterator))" ] }, { @@ -224,9 +238,19 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n" + ] + } + ], "source": [ "def even_iterator(n):\n", " # This function produces an iterator containing all even numbers between 0 and n\n", @@ -237,7 +261,17 @@ " # Sample Output: iter([0, 2, 4])\n", " \n", " # Your code here:\n", - " " + " number=0\n", + " while number < n:\n", + " yield number\n", + " number += 2\n", + " \n", + "\n", + "iterator = even_iterator(5)\n", + "\n", + "for i in iterator:\n", + " print(i)\n", + " " ] }, { @@ -253,7 +287,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -270,12 +304,100 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 8, "metadata": {}, - "outputs": [], + "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", + "
sepal_lengthsepal_widthpetal_lengthpetal_widthiris_type
05.13.51.40.2Iris-setosa
14.93.01.40.2Iris-setosa
24.73.21.30.2Iris-setosa
34.63.11.50.2Iris-setosa
45.03.61.40.2Iris-setosa
\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width iris_type\n", + "0 5.1 3.5 1.4 0.2 Iris-setosa\n", + "1 4.9 3.0 1.4 0.2 Iris-setosa\n", + "2 4.7 3.2 1.3 0.2 Iris-setosa\n", + "3 4.6 3.1 1.5 0.2 Iris-setosa\n", + "4 5.0 3.6 1.4 0.2 Iris-setosa" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris.head()" ] }, { @@ -287,12 +409,35 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0 2.550\n", + "1 2.375\n", + "2 2.350\n", + "3 2.350\n", + "4 2.550\n", + " ... \n", + "145 4.300\n", + "146 3.925\n", + "147 4.175\n", + "148 4.325\n", + "149 3.950\n", + "Length: 150, dtype: float64" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "np.mean(iris, axis=1)\n", + "# Se debe especificar el axis en el que se requiere calcular el promedio" ] }, { @@ -304,12 +449,35 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0 1.887459\n", + "1 1.764051\n", + "2 1.729884\n", + "3 1.656050\n", + "4 1.867485\n", + " ... \n", + "145 1.750714\n", + "146 1.797742\n", + "147 1.772533\n", + "148 1.551411\n", + "149 1.631717\n", + "Length: 150, dtype: float64" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "np.std(iris, axis=1)" ] }, { @@ -321,12 +489,145 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 21, "metadata": {}, - "outputs": [], + "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", + "
sepal_lengthsepal_widthpetal_lengthpetal_width
05.13.51.40.2
14.93.01.40.2
24.73.21.30.2
34.63.11.50.2
45.03.61.40.2
...............
1456.73.05.22.3
1466.32.55.01.9
1476.53.05.22.0
1486.23.45.42.3
1495.93.05.11.8
\n", + "

150 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width\n", + "0 5.1 3.5 1.4 0.2\n", + "1 4.9 3.0 1.4 0.2\n", + "2 4.7 3.2 1.3 0.2\n", + "3 4.6 3.1 1.5 0.2\n", + "4 5.0 3.6 1.4 0.2\n", + ".. ... ... ... ...\n", + "145 6.7 3.0 5.2 2.3\n", + "146 6.3 2.5 5.0 1.9\n", + "147 6.5 3.0 5.2 2.0\n", + "148 6.2 3.4 5.4 2.3\n", + "149 5.9 3.0 5.1 1.8\n", + "\n", + "[150 rows x 4 columns]" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "iris_numeric=iris.select_dtypes(['number'])\n", + "iris_numeric\n" ] }, { @@ -338,9 +639,20 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0.393701" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def cm_to_in(x):\n", " # This function takes in a numeric value in centimeters and converts it to inches\n", @@ -351,7 +663,8 @@ " # Sample Output: 0.393701\n", " \n", " # Your code here:\n", - " " + " return x*0.393701\n", + "cm_to_in(1)" ] }, { @@ -363,12 +676,146 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 24, "metadata": {}, - "outputs": [], + "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", + "
sepal_lengthsepal_widthpetal_lengthpetal_width
02.0078751.3779540.5511810.078740
11.9291351.1811030.5511810.078740
21.8503951.2598430.5118110.078740
31.8110251.2204730.5905520.078740
41.9685051.4173240.5511810.078740
...............
1452.6377971.1811032.0472450.905512
1462.4803160.9842531.9685050.748032
1472.5590571.1811032.0472450.787402
1482.4409461.3385832.1259850.905512
1492.3228361.1811032.0078750.708662
\n", + "

150 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width\n", + "0 2.007875 1.377954 0.551181 0.078740\n", + "1 1.929135 1.181103 0.551181 0.078740\n", + "2 1.850395 1.259843 0.511811 0.078740\n", + "3 1.811025 1.220473 0.590552 0.078740\n", + "4 1.968505 1.417324 0.551181 0.078740\n", + ".. ... ... ... ...\n", + "145 2.637797 1.181103 2.047245 0.905512\n", + "146 2.480316 0.984253 1.968505 0.748032\n", + "147 2.559057 1.181103 2.047245 0.787402\n", + "148 2.440946 1.338583 2.125985 0.905512\n", + "149 2.322836 1.181103 2.007875 0.708662\n", + "\n", + "[150 rows x 4 columns]" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris_inch=iris_numeric.transform(lambda x:cm_to_in(x))\n", + "iris_inch" ] }, { @@ -380,12 +827,144 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 26, "metadata": {}, - "outputs": [], + "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", + "
sepal_lengthsepal_widthpetal_lengthpetal_width
07.15.53.42.2
16.95.03.42.2
26.75.23.32.2
36.65.13.52.2
47.05.63.42.2
...............
1458.75.07.24.3
1468.34.57.03.9
1478.55.07.24.0
1488.25.47.44.3
1497.95.07.13.8
\n", + "

150 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width\n", + "0 7.1 5.5 3.4 2.2\n", + "1 6.9 5.0 3.4 2.2\n", + "2 6.7 5.2 3.3 2.2\n", + "3 6.6 5.1 3.5 2.2\n", + "4 7.0 5.6 3.4 2.2\n", + ".. ... ... ... ...\n", + "145 8.7 5.0 7.2 4.3\n", + "146 8.3 4.5 7.0 3.9\n", + "147 8.5 5.0 7.2 4.0\n", + "148 8.2 5.4 7.4 4.3\n", + "149 7.9 5.0 7.1 3.8\n", + "\n", + "[150 rows x 4 columns]" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Define constant below:\n", - "\n", + "error=2\n", "\n", "def add_constant(x):\n", " # This function adds a global constant to our input.\n", @@ -393,7 +972,9 @@ " # Output: numeric value\n", " \n", " # Your code here:\n", - " " + " return x+error\n", + "iris_constant=iris_numeric.transform(lambda x:add_constant(x))\n", + "iris_constant" ] }, { @@ -442,7 +1023,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -456,9 +1037,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.9.12" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } From 89dee3ecbeafb7821060d99cce05bdb442867c9b Mon Sep 17 00:00:00 2001 From: Montserrat Labrada Date: Sat, 15 Oct 2022 21:46:54 -0400 Subject: [PATCH 2/2] completed q3 --- lab-functional-programming/your-code/Q3.ipynb | 97 +++++++++++++++---- 1 file changed, 79 insertions(+), 18 deletions(-) diff --git a/lab-functional-programming/your-code/Q3.ipynb b/lab-functional-programming/your-code/Q3.ipynb index 75055ac..45e739a 100644 --- a/lab-functional-programming/your-code/Q3.ipynb +++ b/lab-functional-programming/your-code/Q3.ipynb @@ -13,7 +13,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -22,7 +22,7 @@ "[2, 12, 30]" ] }, - "execution_count": 11, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } @@ -44,7 +44,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -53,7 +53,7 @@ "[2, 12, 30]" ] }, - "execution_count": 10, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -85,13 +85,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 5]\n" + ] + } + ], "source": [ "numbers = [1, 4, -1, -100, 0, 5, -99]\n", "\n", - "# Enter your code below" + "# Enter your code below\n", + "\n", + "positive_numbers=list(filter(lambda x: x>0, numbers))\n", + "print(positive_numbers)" ] }, { @@ -113,15 +124,53 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: langdetect in c:\\users\\monla\\anaconda3\\lib\\site-packages (1.0.9)\n", + "Requirement already satisfied: six in c:\\users\\monla\\anaconda3\\lib\\site-packages (from langdetect) (1.16.0)\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], "source": [ - "import langdetect\n", + "pip install langdetect\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['good morning', 'everyone']\n" + ] + }, + { + "data": { + "text/plain": [ + "'good morning everyone'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from langdetect import detect\n", "from functools import reduce\n", "words = ['good morning', '早上好', 'доброго', 'おはようございます', 'everyone', '大家', 'каждый', 'みんな']\n", - "\n", - "# Enter your code below" + "english_words=list(filter(lambda x: detect(x)=='en',words))\n", + "print(english_words)\n", + "' '.join(english_words)" ] }, { @@ -142,7 +191,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -160,9 +209,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ImportError", + "evalue": "cannot import name 'stop_words' from 'sklearn.feature_extraction' (C:\\Users\\monla\\anaconda3\\lib\\site-packages\\sklearn\\feature_extraction\\__init__.py)", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mImportError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [7]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msklearn\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfeature_extraction\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m stop_words\n\u001b[0;32m 2\u001b[0m bow \u001b[38;5;241m=\u001b[39m get_bow_from_docs([\n\u001b[0;32m 3\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m../../lab-bag-of-words/your-code/doc1.txt\u001b[39m\u001b[38;5;124m'\u001b[39m, \n\u001b[0;32m 4\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m../../lab-bag-of-words/your-code/doc2.txt\u001b[39m\u001b[38;5;124m'\u001b[39m, \n\u001b[0;32m 5\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m../../lab-bag-of-words/your-code/doc3.txt\u001b[39m\u001b[38;5;124m'\u001b[39m],\n\u001b[0;32m 6\u001b[0m stop_words\u001b[38;5;241m.\u001b[39mENGLISH_STOP_WORDS\n\u001b[0;32m 7\u001b[0m )\n\u001b[0;32m 9\u001b[0m \u001b[38;5;28mprint\u001b[39m(bow)\n", + "\u001b[1;31mImportError\u001b[0m: cannot import name 'stop_words' from 'sklearn.feature_extraction' (C:\\Users\\monla\\anaconda3\\lib\\site-packages\\sklearn\\feature_extraction\\__init__.py)" + ] + } + ], "source": [ "from sklearn.feature_extraction import stop_words\n", "bow = get_bow_from_docs([\n", @@ -185,7 +246,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -199,9 +260,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.9.12" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }