From b859d5d679512107eea1b5d22dbd38fe824b5387 Mon Sep 17 00:00:00 2001 From: Giorgio Giao Date: Sat, 5 Mar 2022 10:56:08 +0000 Subject: [PATCH] Lab completed --- your-code/challenge-1.ipynb | 269 ++++++++++++++++++++++++++++++++---- your-code/challenge-2.ipynb | 251 ++++++++++++++++++++++++++++----- 2 files changed, 460 insertions(+), 60 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index c574eba..86b612b 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -33,12 +33,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Durante un tiempo no estuvo segura de si su marido era su marido.'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "str_list = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", - "# Your code here:\n" + "# Your code here:\n", + "\" \".join(str_list)+\".\"" ] }, { @@ -50,12 +62,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Grocery list: Bananas, bread, Brownie Mix.'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", - "# Your code here:\n" + "# Your code here:\n", + "grocery_list = []\n", + "for i in range(0,(len(food_list)-1)):\n", + " if (food_list[i])[0].lower() == 'b':\n", + " grocery_list.append(food_list[i])\n", + " \n", + "\"Grocery list: \"+\", \".join(grocery_list)+\".\"" ] }, { @@ -69,9 +98,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'The area of the circle with radius: 4.5 is: 63.61725123519331'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import math\n", "\n", @@ -90,7 +130,11 @@ " # Your code here:\n", " return pi * (x**2)\n", " \n", - "# Your output string here:\n" + "# Your output string here:\n", + "\n", + "#string1 + ' ' + str(radius) + ' ' + string2 + ' ' + str(area(radius, pi = math.pi))\n", + "\n", + "f'{string1} {radius} {string2} {area(radius, pi = math.pi)}'" ] }, { @@ -106,9 +150,61 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'Some': 2,\n", + " 'say': 3,\n", + " 'the': 1,\n", + " 'world': 1,\n", + " 'will': 1,\n", + " 'end': 1,\n", + " 'in': 2,\n", + " 'fire': 2,\n", + " 'ice': 2,\n", + " 'From': 1,\n", + " 'what': 1,\n", + " 'I': 4,\n", + " 've': 1,\n", + " 'tasted': 1,\n", + " 'of': 2,\n", + " 'desire': 1,\n", + " 'hold': 1,\n", + " 'with': 1,\n", + " 'those': 1,\n", + " 'who': 1,\n", + " 'favor': 1,\n", + " 'But': 1,\n", + " 'if': 1,\n", + " 'it': 1,\n", + " 'had': 1,\n", + " 'to': 1,\n", + " 'perish': 1,\n", + " 'twice': 1,\n", + " 'think': 1,\n", + " 'know': 1,\n", + " 'enough': 1,\n", + " 'hate': 1,\n", + " 'To': 1,\n", + " 'that': 1,\n", + " 'for': 1,\n", + " 'destruction': 1,\n", + " 'Is': 1,\n", + " 'also': 1,\n", + " 'great': 1,\n", + " 'And': 1,\n", + " 'would': 1,\n", + " 'suffice': 1}" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "poem = \"\"\"Some say the world will end in fire,\n", "Some say in ice.\n", @@ -120,7 +216,19 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "import re\n", + "\n", + "poem_no_ponct = re.sub(\"\\W\",\" \",poem)\n", + "\n", + "poem_word_list = poem_no_ponct.split()\n", + "\n", + "poem_count_dict = {}\n", + "\n", + "for word in poem_word_list:\n", + " poem_count_dict[word] = poem_word_list.count(word)\n", + " \n", + "poem_count_dict" ] }, { @@ -132,9 +240,67 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'angry',\n", + " 'apple',\n", + " 'beheld',\n", + " 'beneath',\n", + " 'bore',\n", + " 'both',\n", + " 'bright',\n", + " 'day',\n", + " 'deceitful',\n", + " 'did',\n", + " 'end',\n", + " 'fears',\n", + " 'foe',\n", + " 'friend',\n", + " 'garden',\n", + " 'glad',\n", + " 'grew',\n", + " 'grow',\n", + " 'had',\n", + " 'he',\n", + " 'i',\n", + " 'into',\n", + " 'knew',\n", + " 'mine',\n", + " 'morning',\n", + " 'my',\n", + " 'night',\n", + " 'not',\n", + " 'outstretched',\n", + " 'pole',\n", + " 'see',\n", + " 'shine',\n", + " 'smiles',\n", + " 'soft',\n", + " 'stole',\n", + " 'sunned',\n", + " 'tears',\n", + " 'that',\n", + " 'till',\n", + " 'told',\n", + " 'tree',\n", + " 'veild',\n", + " 'was',\n", + " 'waterd',\n", + " 'when',\n", + " 'wiles',\n", + " 'with',\n", + " 'wrath'}" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n", "\n", @@ -158,7 +324,13 @@ "In the morning glad I see; \n", "My foe outstretched beneath the tree.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "#remove all ponctuation and substitute with spaces | all letters to lower case\n", + "poem_no_ponct = (re.sub(\"\\W\",\" \",poem)).lower()\n", + "#convert string to list with split, will remove all spaces\n", + "poem_word_list = poem_no_ponct.split()\n", + "#create sets for the lists (remove duplicated words) and remove black list from poem\n", + "set(poem_word_list).difference(set(blacklist))\n" ] }, { @@ -172,16 +344,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T\n", + "P\n" + ] + } + ], "source": [ "import re\n", "\n", "poem = \"\"\"The apparition of these faces in the crowd;\n", "Petals on a wet, black bough.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "#for loop in each letter of the poem and check if it is capital\n", + "for letter in poem:\n", + " if letter.isupper() == True:\n", + " print(letter)" ] }, { @@ -193,13 +378,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "123abc\n", + "abc123\n", + "JohnSmith1\n", + "ABBY4\n" + ] + } + ], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "#create for loop for each word of the list\n", + "#check if the word contains a number\n", + "for word in data:\n", + " if re.search('\\d',word):\n", + " print(word)\n", + " " ] }, { @@ -215,18 +417,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "123abc\n", + "abc123\n", + "JohnSmith1\n" + ] + } + ], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", - "# Your code here:\n" + "# Your code here:\n", + "for word in data:\n", + " if re.search('\\d' and '[a-z]', word):\n", + " print(word)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -240,7 +455,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.9.7" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index 6873bd2..2154b75 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -72,11 +72,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']" + "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']\n", + "\n", + "#import os\n", + "#txt_files = [file for file in os.listdir(\"/Users/giorgiogiao/Desktop/LABS_IRONHACK/lab-string-operations/your-code/\") if file.endswith('.txt')]" ] }, { @@ -88,11 +91,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "# Write your code here\n" + "# Write your code here\n", + "import pandas as pd\n", + "\n", + "data = [pd.read_csv(\"/Users/giorgiogiao/Desktop/LABS_IRONHACK/lab-string-operations/your-code/\"+ f) for f in docs]\n", + "all_data = pd.concat(data, ignore_index=True)\n", + "corpus = list(all_data.columns)" ] }, { @@ -104,10 +112,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "display(corpus)" + ] }, { "cell_type": "markdown", @@ -132,11 +152,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['ironhack is cool', 'i love ironhack', 'i am a student at ironhack']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Write your code here" + "# Write your code here\n", + "import re\n", + "special_charact = [re.findall('\\W', letter) for sentence in corpus for letter in sentence]\n", + "\n", + "corpus = [(re.sub(\"\\d\" and '[.]' ,\"\",sentence)).lower() for sentence in corpus]\n", + "corpus" ] }, { @@ -148,10 +184,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "bag_of_words = []" + ] }, { "cell_type": "markdown", @@ -166,11 +204,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Write your code here\n" + "# Write your code here\n", + "\n", + "for sentence in corpus:\n", + " for word in sentence.split():\n", + " if (word in bag_of_words) == False:\n", + " bag_of_words.append(word)\n" ] }, { @@ -186,10 +229,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + } + ], + "source": [ + "print(bag_of_words)" + ] }, { "cell_type": "markdown", @@ -200,28 +253,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "# Write your code here\n" + "# Write your code here\n", + "term_freq =[]\n", + "\n", + "for sentence in corpus:\n", + " term_freq1 =[]\n", + " for word in bag_of_words:\n", + " \n", + " if word in sentence.split():\n", + " term_freq1.append(1)\n", + " else:\n", + " term_freq1.append(0)\n", + " term_freq.append(term_freq1)\n" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 9, "metadata": {}, + "outputs": [], "source": [ - "Print `term_freq`. You should see:\n", + "# Print `term_freq`. You should see:\n", "\n", - "```[[1, 1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 0, 0, 1, 0, 1, 1, 1, 1]]```" + "#```[[1, 1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 0, 0, 1, 0, 1, 1, 1, 1]]```" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1, 1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 0, 0, 1, 0, 1, 1, 1, 1]]\n" + ] + } + ], + "source": [ + "print(term_freq)" + ] }, { "cell_type": "markdown", @@ -256,13 +332,62 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'cool', 'love', 'student']\n", + "[[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]\n" + ] + } + ], "source": [ "stop_words = ['all', 'six', 'less', 'being', 'indeed', 'over', 'move', 'anyway', 'fifty', 'four', 'not', 'own', 'through', 'yourselves', 'go', 'where', 'mill', 'only', 'find', 'before', 'one', 'whose', 'system', 'how', 'somewhere', 'with', 'thick', 'show', 'had', 'enough', 'should', 'to', 'must', 'whom', 'seeming', 'under', 'ours', 'has', 'might', 'thereafter', 'latterly', 'do', 'them', 'his', 'around', 'than', 'get', 'very', 'de', 'none', 'cannot', 'every', 'whether', 'they', 'front', 'during', 'thus', 'now', 'him', 'nor', 'name', 'several', 'hereafter', 'always', 'who', 'cry', 'whither', 'this', 'someone', 'either', 'each', 'become', 'thereupon', 'sometime', 'side', 'two', 'therein', 'twelve', 'because', 'often', 'ten', 'our', 'eg', 'some', 'back', 'up', 'namely', 'towards', 'are', 'further', 'beyond', 'ourselves', 'yet', 'out', 'even', 'will', 'what', 'still', 'for', 'bottom', 'mine', 'since', 'please', 'forty', 'per', 'its', 'everything', 'behind', 'un', 'above', 'between', 'it', 'neither', 'seemed', 'ever', 'across', 'she', 'somehow', 'be', 'we', 'full', 'never', 'sixty', 'however', 'here', 'otherwise', 'were', 'whereupon', 'nowhere', 'although', 'found', 'alone', 're', 'along', 'fifteen', 'by', 'both', 'about', 'last', 'would', 'anything', 'via', 'many', 'could', 'thence', 'put', 'against', 'keep', 'etc', 'amount', 'became', 'ltd', 'hence', 'onto', 'or', 'con', 'among', 'already', 'co', 'afterwards', 'formerly', 'within', 'seems', 'into', 'others', 'while', 'whatever', 'except', 'down', 'hers', 'everyone', 'done', 'least', 'another', 'whoever', 'moreover', 'couldnt', 'throughout', 'anyhow', 'yourself', 'three', 'from', 'her', 'few', 'together', 'top', 'there', 'due', 'been', 'next', 'anyone', 'eleven', 'much', 'call', 'therefore', 'interest', 'then', 'thru', 'themselves', 'hundred', 'was', 'sincere', 'empty', 'more', 'himself', 'elsewhere', 'mostly', 'on', 'fire', 'am', 'becoming', 'hereby', 'amongst', 'else', 'part', 'everywhere', 'too', 'herself', 'former', 'those', 'he', 'me', 'myself', 'made', 'twenty', 'these', 'bill', 'cant', 'us', 'until', 'besides', 'nevertheless', 'below', 'anywhere', 'nine', 'can', 'of', 'your', 'toward', 'my', 'something', 'and', 'whereafter', 'whenever', 'give', 'almost', 'wherever', 'is', 'describe', 'beforehand', 'herein', 'an', 'as', 'itself', 'at', 'have', 'in', 'seem', 'whence', 'ie', 'any', 'fill', 'again', 'hasnt', 'inc', 'thereby', 'thin', 'no', 'perhaps', 'latter', 'meanwhile', 'when', 'detail', 'same', 'wherein', 'beside', 'also', 'that', 'other', 'take', 'which', 'becomes', 'you', 'if', 'nobody', 'see', 'though', 'may', 'after', 'upon', 'most', 'hereupon', 'eight', 'but', 'serious', 'nothing', 'such', 'why', 'a', 'off', 'whereby', 'third', 'i', 'whole', 'noone', 'sometimes', 'well', 'amoungst', 'yours', 'their', 'rather', 'without', 'so', 'five', 'the', 'first', 'whereas', 'once']\n", "\n", - "# Write your code below\n" + "# Write your code below\n", + "\n", + "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']\n", + "\n", + "import pandas as pd\n", + "\n", + "data = [pd.read_csv(\"/Users/giorgiogiao/Desktop/LABS_IRONHACK/lab-string-operations/your-code/\"+ f) for f in docs]\n", + "all_data = pd.concat(data, ignore_index=True)\n", + "corpus = list(all_data.columns)\n", + "\n", + "import re\n", + "special_charact = [re.findall('\\W', letter) for sentence in corpus for letter in sentence]\n", + "\n", + "corpus = [(re.sub(\"\\d\" and '[.]' ,\"\",sentence)).lower() for sentence in corpus]\n", + "\n", + "bag_of_word = []\n", + "\n", + "for sentence in corpus:\n", + " for word in sentence.split():\n", + " if (word in bag_of_words) == False:\n", + " bag_of_words.append(word)\n", + "\n", + "for word in stop_words:\n", + " if (word in bag_of_words) == True:\n", + " \n", + " while (word in bag_of_words) == True:\n", + " bag_of_words.remove(word)\n", + "print(bag_of_words)\n", + "\n", + "term_freq =[]\n", + "\n", + "for sentence in corpus:\n", + " term_freq1 =[]\n", + " for word in bag_of_words:\n", + " \n", + " if word in sentence.split():\n", + " term_freq1.append(1)\n", + " else:\n", + " term_freq1.append(0)\n", + " term_freq.append(term_freq1)\n", + "print(term_freq) " ] }, { @@ -296,15 +421,75 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: sklearn in /Users/giorgiogiao/opt/anaconda3/lib/python3.9/site-packages (0.0)\n", + "Requirement already satisfied: scikit-learn in /Users/giorgiogiao/opt/anaconda3/lib/python3.9/site-packages (from sklearn) (0.24.2)\n", + "Requirement already satisfied: joblib>=0.11 in /Users/giorgiogiao/opt/anaconda3/lib/python3.9/site-packages (from scikit-learn->sklearn) (1.1.0)\n", + "Requirement already satisfied: scipy>=0.19.1 in /Users/giorgiogiao/opt/anaconda3/lib/python3.9/site-packages (from scikit-learn->sklearn) (1.7.1)\n", + "Requirement already satisfied: numpy>=1.13.3 in /Users/giorgiogiao/opt/anaconda3/lib/python3.9/site-packages (from scikit-learn->sklearn) (1.20.3)\n", + "Requirement already satisfied: threadpoolctl>=2.0.0 in /Users/giorgiogiao/opt/anaconda3/lib/python3.9/site-packages (from scikit-learn->sklearn) (2.2.0)\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "pip install sklearn" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n" + ] + } + ], + "source": [ + "data = [pd.read_csv(\"/Users/giorgiogiao/Desktop/LABS_IRONHACK/lab-string-operations/your-code/\"+ f) for f in docs]\n", + "all_data = pd.concat(data, ignore_index=True)\n", + "corpus1 = list(all_data.columns)\n", + "print(corpus1)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'ironhack': 3, 'is': 4, 'cool': 2, 'love': 5, 'am': 0, 'student': 6, 'at': 1}\n", + "[[0 0 1 1 1 0 0]\n", + " [0 0 0 1 0 1 0]\n", + " [1 1 0 1 0 0 1]]\n" + ] + } + ], + "source": [ + "from sklearn.feature_extraction.text import CountVectorizer\n", + "vectorizer = CountVectorizer()\n", + "bag = vectorizer.fit_transform(corpus1)\n", + "print(vectorizer.vocabulary_)\n", + "print(bag.toarray())\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -318,7 +503,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.9.7" } }, "nbformat": 4,