From 42c5e27e89ad8c6bbf0aa7e4cf179b79b4cbe508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20P=C3=A9rez=20Ortega?= Date: Mon, 5 Jul 2021 00:12:12 -0500 Subject: [PATCH 1/2] Lab terminado --- .DS_Store | Bin 0 -> 6148 bytes .../.ipynb_checkpoints/Q1-checkpoint.ipynb | 253 +++++++ .../.ipynb_checkpoints/Q2-checkpoint.ipynb | 178 +++++ .../.ipynb_checkpoints/Q3-checkpoint.ipynb | 236 +++++++ .../.ipynb_checkpoints/main-checkpoint.ipynb | 650 ++++++++++++++++-- your-code/Q1.ipynb | 102 ++- your-code/Q2.ipynb | 71 +- your-code/Q3.ipynb | 49 +- your-code/main.ipynb | 650 ++++++++++++++++-- 9 files changed, 2066 insertions(+), 123 deletions(-) create mode 100644 .DS_Store create mode 100644 your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb create mode 100644 your-code/.ipynb_checkpoints/Q2-checkpoint.ipynb create mode 100644 your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1968f281020c1f0e7c8808efd9a172edadf70b94 GIT binary patch literal 6148 zcmeHKJ5Iwu5PbtFf@o4w?gc>N2211=DQL(AP+~eGOGqi`+~p8C4JU&)A5e^;r9d;% z%-h+W=e1wq*##iWeR~Tm0L1-u9>}{x zgA;zg+jjkytNLmi`td!?7_aED#|y5p1lr2?ryDv%1K0;vEMV9!>Y zUpi(?1yX@j;7bAhKNPxR4ICWp*TJC2cU_+;s&Q<031W%71`du~p@~z8PL;S~h|@V= zqOJxGj!uWz&U{YnEOA2-+nw{p$|2P;V=9mej1@TdbfNu!MgQggKPF|C3Zw%6N&%TQ zn`X&RirzZ;IPJBC{z(5c)>=8k=n%j}Tk+LNUC}Y?YT)2#bj}-{mB3k5y^ DIvXq# literal 0 HcmV?d00001 diff --git a/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb b/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb new file mode 100644 index 0000000..774ab13 --- /dev/null +++ b/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb @@ -0,0 +1,253 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, create a Python function that wraps your previous solution for the Bag of Words lab.\n", + "\n", + "Requirements:\n", + "\n", + "1. Your function should accept the following parameters:\n", + " * `docs` [REQUIRED] - array of document paths.\n", + " * `stop_words` [OPTIONAL] - array of stop words. The default value is an empty array.\n", + "\n", + "1. Your function should return a Python object that contains the following:\n", + " * `bag_of_words` - array of strings of normalized unique words in the corpus.\n", + " * `term_freq` - array of the term-frequency vectors." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# Import required libraries\n", + "import os\n", + "import glob\n", + "import re\n", + "from sklearn.feature_extraction import _stop_words\n", + "\n", + "# Define function\n", + "def get_bow_from_docs(docs, stop_words = []):\n", + " \n", + " # In the function, first define the variables you will use such as `corpus`, `bag_of_words`, and `term_freq`.\n", + " corpus = []\n", + " bag_of_words = []\n", + " term_freq = []\n", + " corpus_l = []\n", + " term_freq_b = []\n", + " \n", + " \"\"\"\n", + " Loop `docs` and read the content of each doc into a string in `corpus`.\n", + " Remember to convert the doc content to lowercases and remove punctuation.\n", + " \"\"\"\n", + " for index in range(len(docs)):\n", + " with open(docs[index], \"r\") as file:\n", + " for text in file:\n", + " stripped_text = text.strip()\n", + " corpus.append(stripped_text)\n", + " corpus = [char.lower().replace('.', '') for char in corpus]\n", + " \n", + " \"\"\"\n", + " Loop `corpus`. Append the terms in each doc into the `bag_of_words` array. The terms in `bag_of_words` \n", + " should be unique which means before adding each term you need to check if it's already added to the array.\n", + " In addition, check if each term is in the `stop_words` array. Only append the term to `bag_of_words`\n", + " if it is not a stop word.\n", + " \"\"\"\n", + " for index in range(len(corpus)):\n", + " words = re.split(\" \", (corpus[index]))\n", + " for word in words:\n", + " if word in bag_of_words or word in stop_words:\n", + " pass\n", + " else:\n", + " bag_of_words.append(word)\n", + "\n", + " \"\"\"\n", + " Loop `corpus` again. For each doc string, count the number of occurrences of each term in `bag_of_words`. \n", + " Create an array for each doc's term frequency and append it to `term_freq`.\n", + " \"\"\"\n", + " for index in range(len(corpus)):\n", + " corpus_l.append((corpus[index]))\n", + " \n", + " for index in range(len(corpus_l)):\n", + " for word in bag_of_words:\n", + " if word in corpus_l[index].split():\n", + " term_freq_b.append(1)\n", + " else:\n", + " term_freq_b.append(0)\n", + " term_freq.append(term_freq_b)\n", + " \n", + " \n", + " # Now return your output as an object\n", + " return {\n", + " \"bag_of_words\": bag_of_words,\n", + " \"term_freq\": term_freq\n", + " }\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Test your function without stop words. You should see the output like below:\n", + "\n", + "```{'bag_of_words': ['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at'], 'term_freq': [[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": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'bag_of_words': ['ironhack', 'is', 'cool', 'i', 'am', 'a', 'student', 'at', 'love'], 'term_freq': [[1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1]]}\n" + ] + } + ], + "source": [ + "# Define doc paths array\n", + "\n", + "pwd = '/Users/diego.perez97/Documents/IronHack/Module 1/Week 1/Day 2/lab-string-operations/your-code'\n", + "os.chdir(pwd)\n", + "file_extention = '.txt'\n", + "file_names = [file for file in glob.glob(f'*{file_extention}')]\n", + "docs = []\n", + "\n", + "for file in file_names:\n", + " docs.append(file)\n", + "\n", + "# Obtain BoW from your function\n", + "bow = get_bow_from_docs(docs)\n", + "\n", + "# Print BoW\n", + "print(bow)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If your attempt above is successful, nice work done!\n", + "\n", + "Now test your function again with the stop words. In the previous lab we defined the stop words in a large array. In this lab, we'll import the stop words from Scikit-Learn." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.feature_extraction import _stop_words" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You should have seen a large list of words that looks like:\n", + "\n", + "```frozenset({'across', 'mine', 'cannot', ...})```\n", + "\n", + "`frozenset` is a type of Python object that is immutable. In this lab you can use it just like an array without conversion." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, test your function with supplying `stop_words.ENGLISH_STOP_WORDS` as the second parameter." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "ename": "KeyError", + "evalue": "0", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mstop_words\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_stop_words\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mENGLISH_STOP_WORDS\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mbow\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_bow_from_docs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbow\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstop_words\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mget_bow_from_docs\u001b[0;34m(docs, stop_words)\u001b[0m\n\u001b[1;32m 20\u001b[0m \"\"\"\n\u001b[1;32m 21\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mindex\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdocs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 22\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdocs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"r\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mfile\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 23\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mtext\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mfile\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0mstripped_text\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mKeyError\u001b[0m: 0" + ] + } + ], + "source": [ + "stop_words = list(_stop_words.ENGLISH_STOP_WORDS)\n", + "bow = get_bow_from_docs(docs, stop_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'bag_of_words': ['ironhack', 'is', 'cool', 'i', 'am', 'a', 'student', 'at', 'love'], 'term_freq': [[1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1]]}\n" + ] + } + ], + "source": [ + "print(bow)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You should have seen:\n", + "\n", + "```{'bag_of_words': ['ironhack', 'cool', 'love', 'student'], 'term_freq': [[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]}```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.ipynb_checkpoints/Q2-checkpoint.ipynb b/your-code/.ipynb_checkpoints/Q2-checkpoint.ipynb new file mode 100644 index 0000000..313b5ac --- /dev/null +++ b/your-code/.ipynb_checkpoints/Q2-checkpoint.ipynb @@ -0,0 +1,178 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we want to enhance the `get_bow_from_docs` function so that it will work with HTML webpages. In HTML, there are a lot of messy codes such as HTML tags, Javascripts, [unicodes](https://www.w3schools.com/charsets/ref_utf_misc_symbols.asp) that will mess up your bag of words. We need to clean up those junk before generating BoW.\n", + "\n", + "Next, what you will do is to define several new functions each of which is specialized to clean up the HTML codes in one aspect. For instance, you can have a `strip_html_tags` function to remove all HTML tags, a `remove_punctuation` function to remove all punctuation, a `to_lower_case` function to convert string to lowercase, and a `remove_unicode` function to remove all unicodes.\n", + "\n", + "Then in your `get_bow_from_doc` function, you will call each of those functions you created to clean up the HTML before you generate the corpus.\n", + "\n", + "Note: Please use Python string operations and regular expression only in this lab. Do not use extra libraries such as `beautifulsoup` because otherwise you loose the purpose of practicing." + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [], + "source": [ + "# Define your string handling functions below\n", + "# Minimal 3 functions\n", + "\n", + "# 1.- Remove all HTML tags:\n", + "def strip_html_tags():\n", + " return()\n", + "\n", + "# 2.- Remove all punctuation:\n", + "def remove_punctuation():\n", + " return()\n", + "\n", + "# 3.- Convert string to lowercase:\n", + "def to_lower_case():\n", + " return\n", + "\n", + "# 4.- Remove all unicodes:\n", + "def remove_unicode(): \n", + " return()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, paste your previously written `get_bow_from_docs` function below. Call your functions above at the appropriate place." + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [], + "source": [ + "# Define function\n", + "def get_bow_from_docs(docs, stop_words = []):\n", + " \n", + " # In the function, first define the variables you will use such as `corpus`, `bag_of_words`, and `term_freq`.\n", + " corpus = []\n", + " bag_of_words = []\n", + " term_freq = []\n", + " corpus_l = []\n", + " term_freq_b = []\n", + " \n", + " \"\"\"\n", + " Loop `docs` and read the content of each doc into a string in `corpus`.\n", + " Remember to convert the doc content to lowercases and remove punctuation.\n", + " \"\"\"\n", + " for index in range(len(docs)):\n", + " with open(docs[index], \"r\") as file:\n", + " for text in file:\n", + " stripped_text = text.strip()\n", + " corpus.append(stripped_text)\n", + " corpus = [char.lower().replace('.', '') for char in corpus]\n", + " \n", + " \"\"\"\n", + " Loop `corpus`. Append the terms in each doc into the `bag_of_words` array. The terms in `bag_of_words` \n", + " should be unique which means before adding each term you need to check if it's already added to the array.\n", + " In addition, check if each term is in the `stop_words` array. Only append the term to `bag_of_words`\n", + " if it is not a stop word.\n", + " \"\"\"\n", + " for index in range(len(corpus)):\n", + " words = re.split(\" \", (corpus[index]))\n", + " for word in words:\n", + " if word in bag_of_words or word in stop_words:\n", + " pass\n", + " else:\n", + " bag_of_words.append(word)\n", + "\n", + " \"\"\"\n", + " Loop `corpus` again. For each doc string, count the number of occurrences of each term in `bag_of_words`. \n", + " Create an array for each doc's term frequency and append it to `term_freq`.\n", + " \"\"\"\n", + " for index in range(len(corpus)):\n", + " corpus_l.append((corpus[index]))\n", + " \n", + " for index in range(len(corpus_l)):\n", + " for word in bag_of_words:\n", + " if word in corpus_l[index].split():\n", + " term_freq_b.append(1)\n", + " else:\n", + " term_freq_b.append(0)\n", + " term_freq.append(term_freq_b)\n", + " \n", + " \n", + " # Now return your output as an object\n", + " return {\n", + " \"bag_of_words\": bag_of_words,\n", + " \"term_freq\": term_freq\n", + " }" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, read the content from the three HTML webpages in the `your-codes` directory to test your function." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.feature_extraction import stop_words\n", + "bow = get_bow_from_docs([\n", + " 'www.coursereport.com_ironhack.html',\n", + " 'en.wikipedia.org_Data_analysis.html',\n", + " 'www.lipsum.com.html'\n", + " ],\n", + " stop_words.ENGLISH_STOP_WORDS\n", + ")\n", + "\n", + "print(bow)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Do you see any problem in the output? How do you improve the output?\n", + "\n", + "A good way to improve your codes is to look into the HTML data sources and try to understand where the messy output came from. A good data analyst always learns about the data in depth in order to perform the job well.\n", + "\n", + "Spend 20-30 minutes to improve your functions or until you feel you are good at string operations. This lab is just a practice so you don't need to stress yourself out. If you feel you've practiced enough you can stop and move on the next challenge question." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb b/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb new file mode 100644 index 0000000..6c13378 --- /dev/null +++ b/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb @@ -0,0 +1,236 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Lambda** is a special Python function type that is **anonymous**. By *anonymous* it means a lambda function does not have name. Lambda functions are embedded inside codes so that you don't call them like calling regular Python functions.\n", + "\n", + "**`Map`** applies a function to all the items in an input list. The function that is applied can be a standard or a lambda function.\n", + "\n", + "For instance, below is an example of multiplying number tuples in a list:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 12, 30]" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "items = [(1,2), (3,4), (5,6)]\n", + "\n", + "def multiply(num_tuple):\n", + " return num_tuple[0]*num_tuple[1]\n", + "list(map(multiply, items))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...is the same as:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 12, 30]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "items = [(1,2), (3,4), (5,6)]\n", + "list(map(lambda item: item[0]*item[1], items))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Why do we sometimes use `lambda` and `map`? Because, as you see in the example above, they make your code really concise by combining 3 lines of code to 1 line.\n", + "\n", + "Besides `map`, there is also **`filter`** that selectively returns elements in an array according to whether you return `True`. There is also **`reduce`** that performs computation on a list of items then returns result.\n", + "\n", + "Here is a [good tutorial](http://book.pythontips.com/en/latest/map_filter.html) about `map`, `filter`, and `reduce`. Read it if you are not familiar with how they are used. Then proceed to the next cell." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the next cell, use `filter` and `lambda` to return a list that contains positive numbers only. The output should be:\n", + "\n", + "```[1, 4, 5]```" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "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\n", + "greater_than_zero = list(filter(lambda x: x > 0, numbers))\n", + "print(greater_than_zero)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, use `reduce` and `lambda` to return a string that only contains English terms. The English terms are separated with a whitespace ` `.\n", + "\n", + "Hints: \n", + "\n", + "* If your Jupyter Notebook cannot import `langdetect`, you need to install it with `pip install langdetect`. If Jupyter Notebook still cannot find the library, try install with `python3 -m pip install langdetect`. This is because you need to install `langdetect` in the same Python run environment where Jupyter Notebook is running.\n", + "\n", + "* If a word is English, `langdetect.detect(word)=='en'` will return `True`.\n", + "\n", + "Your output should read:\n", + "\n", + "```'good morning everyone'```" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "import langdetect\n", + "from functools import reduce\n", + "words = ['good morning', '早上好', 'доброго', 'おはようございます', 'everyone', '大家', 'каждый', 'みんな']\n", + "\n", + "# Enter your code below" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "good morning everyone\n" + ] + } + ], + "source": [ + "english_words_1 = list(filter(lambda word: langdetect.detect(word)=='en', words))\n", + "english_words_2 = reduce((lambda a, b: a + \" \" + b), english_words_1)\n", + "print(english_words_2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Question" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, if you still have time, convert your response in Q2 by using `lambda`, `map`, `filter`, or `reduce` where applicable. Enter your function in the cell below.\n", + "\n", + "As you write Python functions, generally you don't want to make your function too long. Long functions are difficult to read and difficult to debug. If a function is getting too long, consider breaking it into sever shorter functions where the main function calls other functions. If anything goes wrong, you can output debug messages in each of the functions to check where exactly the error is." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Enter your code below" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Test your function below with the Bag of Words lab docs (it's easier for you to debug your code). Your output should be:\n", + "\n", + "```{'bag_of_words': ['ironhack', 'cool', 'love', 'student'], 'term_freq': [[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]}```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.feature_extraction import stop_words\n", + "bow = get_bow_from_docs([\n", + " '../../lab-bag-of-words/your-code/doc1.txt', \n", + " '../../lab-bag-of-words/your-code/doc2.txt', \n", + " '../../lab-bag-of-words/your-code/doc3.txt'],\n", + " stop_words.ENGLISH_STOP_WORDS\n", + ")\n", + "\n", + "print(bow)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb index 0239d17..3ec8755 100644 --- a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -98,10 +98,10 @@ "evalue": "", "output_type": "error", "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# After we have iterated through all elements, we will get a StopIteration Error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mStopIteration\u001b[0m: " + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# After we have iterated through all elements, we will get a StopIteration Error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mStopIteration\u001b[0m: " ] } ], @@ -113,7 +113,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -146,7 +146,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -159,7 +159,32 @@ " # Sample Output: 2\n", " \n", " # Your code here:\n", - " " + " x = []\n", + " for i in iterator:\n", + " if i % 2 == 0 and i != 0:\n", + " x.append(i)\n", + " return(x[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iterator = iter(list(range(101)))\n", + "divisible2(iterator)" ] }, { @@ -173,7 +198,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -193,7 +218,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -224,7 +249,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -237,7 +262,39 @@ " # Sample Output: iter([0, 2, 4])\n", " \n", " # Your code here:\n", - " " + " number = 0\n", + " while number <= n:\n", + " yield number\n", + " number = number + 2" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n", + "6\n", + "8\n", + "10\n", + "12\n", + "14\n", + "16\n", + "18\n" + ] + } + ], + "source": [ + "iterator = even_iterator(19)\n", + "\n", + "for i in iterator:\n", + " print(i)" ] }, { @@ -253,12 +310,12 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "columns = ['sepal_length', 'sepal_width', 'petal_length','petal_width','iris_type']\n", - "iris = pd.read_csv(\"https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\", names=columns)" + "iris = pd.read_csv(\"https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\", names = columns)" ] }, { @@ -270,12 +327,100 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 14, "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": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris.head()" ] }, { @@ -287,12 +432,28 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "sepal_length 5.843333\n", + "sepal_width 3.054000\n", + "petal_length 3.758667\n", + "petal_width 1.198667\n", + "dtype: float64" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris.mean()" ] }, { @@ -304,12 +465,28 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "sepal_length 0.828066\n", + "sepal_width 0.433594\n", + "petal_length 1.764420\n", + "petal_width 0.763161\n", + "dtype: float64" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris.std()" ] }, { @@ -321,12 +498,96 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 17, "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", + "
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
\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" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']\n", + "iris_numeric = iris.select_dtypes(include = numerics)\n", + "iris_numeric.head()" ] }, { @@ -338,11 +599,11 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ - "def cm_to_in(x):\n", + "def cm_to_in(x_cm):\n", " # This function takes in a numeric value in centimeters and converts it to inches\n", " # Input: numeric value\n", " # Output: float\n", @@ -351,7 +612,8 @@ " # Sample Output: 0.393701\n", " \n", " # Your code here:\n", - " " + " x_in = x_cm*0.393701\n", + " return(x_in)" ] }, { @@ -363,12 +625,95 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 19, "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", + "
sepal_lengthsepal_widthpetal_lengthpetal_width
02.0078751.3779540.5511810.07874
11.9291351.1811030.5511810.07874
21.8503951.2598430.5118110.07874
31.8110251.2204730.5905520.07874
41.9685051.4173240.5511810.07874
\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width\n", + "0 2.007875 1.377954 0.551181 0.07874\n", + "1 1.929135 1.181103 0.551181 0.07874\n", + "2 1.850395 1.259843 0.511811 0.07874\n", + "3 1.811025 1.220473 0.590552 0.07874\n", + "4 1.968505 1.417324 0.551181 0.07874" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris_inch = cm_to_in(iris_numeric)\n", + "iris_inch.head()" ] }, { @@ -380,20 +725,113 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "# Define constant below:\n", "\n", + "error = 2\n", "\n", - "def add_constant(x):\n", + "def add_constant(x_w_error):\n", " # This function adds a global constant to our input.\n", " # Input: numeric value\n", " # Output: numeric value\n", " \n", " # Your code here:\n", - " " + " x_w_o_error = x_w_error + error\n", + " return(x_w_o_error)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "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", + "
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
\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" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iris_constant = add_constant(iris_numeric)\n", + "iris_constant.head()" ] }, { @@ -407,12 +845,41 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "# Your code here:\n", - "\n" + "\n", + "def max_value(x_row):\n", + " x_max = max(x_row)\n", + " return(x_max)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 5.1\n", + "1 4.9\n", + "2 4.7\n", + "3 4.6\n", + "4 5.0\n", + "dtype: float64" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iris_max = iris_numeric.apply(max_value, axis = 1)\n", + "iris_max.head()" ] }, { @@ -424,12 +891,115 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 28, "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", + "
sepal_lengthsepal_widthpetal_lengthpetal_widthtotal_lengthtotal_width
05.13.51.40.26.53.7
14.93.01.40.26.33.2
24.73.21.30.26.03.4
34.63.11.50.26.13.3
45.03.61.40.26.43.8
\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width total_length \\\n", + "0 5.1 3.5 1.4 0.2 6.5 \n", + "1 4.9 3.0 1.4 0.2 6.3 \n", + "2 4.7 3.2 1.3 0.2 6.0 \n", + "3 4.6 3.1 1.5 0.2 6.1 \n", + "4 5.0 3.6 1.4 0.2 6.4 \n", + "\n", + " total_width \n", + "0 3.7 \n", + "1 3.2 \n", + "2 3.4 \n", + "3 3.3 \n", + "4 3.8 " + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris_numeric['total_length'] = iris_numeric.apply(lambda x: x['sepal_length'] + x['petal_length'], axis = 1)\n", + "iris_numeric['total_width'] = iris_numeric.apply(lambda x: x['sepal_width'] + x['petal_width'], axis = 1)\n", + "iris_numeric.head()" ] }, { @@ -456,7 +1026,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.5" } }, "nbformat": 4, diff --git a/your-code/Q1.ipynb b/your-code/Q1.ipynb index 8b07d3d..8261421 100644 --- a/your-code/Q1.ipynb +++ b/your-code/Q1.ipynb @@ -19,41 +19,65 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Import required libraries\n", + "import os\n", + "import glob\n", + "import re\n", + "from sklearn.feature_extraction import _stop_words\n", "\n", "# Define function\n", - "def get_bow_from_docs(docs, stop_words=[]):\n", + "def get_bow_from_docs(docs, stop_words = []):\n", " \n", " # In the function, first define the variables you will use such as `corpus`, `bag_of_words`, and `term_freq`.\n", - " \n", - " \n", + " corpus = []\n", + " bag_of_words = []\n", + " term_freq = []\n", + " corpus_l = []\n", + " term_freq_b = []\n", " \n", " \"\"\"\n", " Loop `docs` and read the content of each doc into a string in `corpus`.\n", " Remember to convert the doc content to lowercases and remove punctuation.\n", " \"\"\"\n", - "\n", - " \n", - " \n", + " for index in range(len(docs)):\n", + " with open(docs[index], \"r\") as file:\n", + " for text in file:\n", + " stripped_text = text.strip()\n", + " corpus.append(stripped_text)\n", + " corpus = [char.lower().replace('.', '') for char in corpus]\n", + " \n", " \"\"\"\n", " Loop `corpus`. Append the terms in each doc into the `bag_of_words` array. The terms in `bag_of_words` \n", " should be unique which means before adding each term you need to check if it's already added to the array.\n", " In addition, check if each term is in the `stop_words` array. Only append the term to `bag_of_words`\n", " if it is not a stop word.\n", " \"\"\"\n", + " for index in range(len(corpus)):\n", + " words = re.split(\" \", (corpus[index]))\n", + " for word in words:\n", + " if word in bag_of_words or word in stop_words:\n", + " pass\n", + " else:\n", + " bag_of_words.append(word)\n", "\n", - " \n", - " \n", - " \n", " \"\"\"\n", " Loop `corpus` again. For each doc string, count the number of occurrences of each term in `bag_of_words`. \n", " Create an array for each doc's term frequency and append it to `term_freq`.\n", " \"\"\"\n", - "\n", + " for index in range(len(corpus)):\n", + " corpus_l.append((corpus[index]))\n", + " \n", + " for index in range(len(corpus_l)):\n", + " for word in bag_of_words:\n", + " if word in corpus_l[index].split():\n", + " term_freq_b.append(1)\n", + " else:\n", + " term_freq_b.append(0)\n", + " term_freq.append(term_freq_b)\n", " \n", " \n", " # Now return your output as an object\n", @@ -75,13 +99,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'bag_of_words': ['ironhack', 'is', 'cool', 'i', 'am', 'a', 'student', 'at', 'love'], 'term_freq': [[1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1]]}\n" + ] + } + ], "source": [ "# Define doc paths array\n", + "\n", + "pwd = '/Users/diego.perez97/Documents/IronHack/Module 1/Week 1/Day 2/lab-string-operations/your-code'\n", + "os.chdir(pwd)\n", + "file_extention = '.txt'\n", + "file_names = [file for file in glob.glob(f'*{file_extention}')]\n", "docs = []\n", "\n", + "for file in file_names:\n", + " docs.append(file)\n", + "\n", "# Obtain BoW from your function\n", "bow = get_bow_from_docs(docs)\n", "\n", @@ -100,12 +140,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "from sklearn.feature_extraction import stop_words\n", - "print(stop_words.ENGLISH_STOP_WORDS)" + "from sklearn.feature_extraction import _stop_words" ] }, { @@ -128,12 +167,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ - "bow = get_bow_from_docs(bow, stop_words.ENGLISH_STOP_WORDS)\n", - "\n", + "stop_words = list(_stop_words.ENGLISH_STOP_WORDS)\n", + "bow = get_bow_from_docs(docs, stop_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'bag_of_words': ['ironhack', 'cool', 'student', 'love'], 'term_freq': [[1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1], [1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1], [1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1]]}\n" + ] + } + ], + "source": [ "print(bow)" ] }, @@ -145,13 +200,6 @@ "\n", "```{'bag_of_words': ['ironhack', 'cool', 'love', 'student'], 'term_freq': [[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]}```" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -170,7 +218,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.9.5" } }, "nbformat": 4, diff --git a/your-code/Q2.ipynb b/your-code/Q2.ipynb index f50f442..313b5ac 100644 --- a/your-code/Q2.ipynb +++ b/your-code/Q2.ipynb @@ -20,7 +20,23 @@ "outputs": [], "source": [ "# Define your string handling functions below\n", - "# Minimal 3 functions\n" + "# Minimal 3 functions\n", + "\n", + "# 1.- Remove all HTML tags:\n", + "def strip_html_tags():\n", + " return()\n", + "\n", + "# 2.- Remove all punctuation:\n", + "def remove_punctuation():\n", + " return()\n", + "\n", + "# 3.- Convert string to lowercase:\n", + "def to_lower_case():\n", + " return\n", + "\n", + "# 4.- Remove all unicodes:\n", + "def remove_unicode(): \n", + " return()" ] }, { @@ -36,19 +52,62 @@ "metadata": {}, "outputs": [], "source": [ - "def get_bow_from_docs(docs, stop_words=[]):\n", + "# Define function\n", + "def get_bow_from_docs(docs, stop_words = []):\n", + " \n", " # In the function, first define the variables you will use such as `corpus`, `bag_of_words`, and `term_freq`.\n", " corpus = []\n", " bag_of_words = []\n", " term_freq = []\n", + " corpus_l = []\n", + " term_freq_b = []\n", + " \n", + " \"\"\"\n", + " Loop `docs` and read the content of each doc into a string in `corpus`.\n", + " Remember to convert the doc content to lowercases and remove punctuation.\n", + " \"\"\"\n", + " for index in range(len(docs)):\n", + " with open(docs[index], \"r\") as file:\n", + " for text in file:\n", + " stripped_text = text.strip()\n", + " corpus.append(stripped_text)\n", + " corpus = [char.lower().replace('.', '') for char in corpus]\n", + " \n", + " \"\"\"\n", + " Loop `corpus`. Append the terms in each doc into the `bag_of_words` array. The terms in `bag_of_words` \n", + " should be unique which means before adding each term you need to check if it's already added to the array.\n", + " In addition, check if each term is in the `stop_words` array. Only append the term to `bag_of_words`\n", + " if it is not a stop word.\n", + " \"\"\"\n", + " for index in range(len(corpus)):\n", + " words = re.split(\" \", (corpus[index]))\n", + " for word in words:\n", + " if word in bag_of_words or word in stop_words:\n", + " pass\n", + " else:\n", + " bag_of_words.append(word)\n", + "\n", + " \"\"\"\n", + " Loop `corpus` again. For each doc string, count the number of occurrences of each term in `bag_of_words`. \n", + " Create an array for each doc's term frequency and append it to `term_freq`.\n", + " \"\"\"\n", + " for index in range(len(corpus)):\n", + " corpus_l.append((corpus[index]))\n", + " \n", + " for index in range(len(corpus_l)):\n", + " for word in bag_of_words:\n", + " if word in corpus_l[index].split():\n", + " term_freq_b.append(1)\n", + " else:\n", + " term_freq_b.append(0)\n", + " term_freq.append(term_freq_b)\n", " \n", - " # write your codes here\n", " \n", + " # Now return your output as an object\n", " return {\n", " \"bag_of_words\": bag_of_words,\n", " \"term_freq\": term_freq\n", - " }\n", - " " + " }" ] }, { @@ -111,7 +170,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.9.5" } }, "nbformat": 4, diff --git a/your-code/Q3.ipynb b/your-code/Q3.ipynb index 75055ac..6c13378 100644 --- a/your-code/Q3.ipynb +++ b/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,23 @@ }, { "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", + "greater_than_zero = list(filter(lambda x: x > 0, numbers))\n", + "print(greater_than_zero)" ] }, { @@ -113,7 +123,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ @@ -124,6 +134,25 @@ "# Enter your code below" ] }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "good morning everyone\n" + ] + } + ], + "source": [ + "english_words_1 = list(filter(lambda word: langdetect.detect(word)=='en', words))\n", + "english_words_2 = reduce((lambda a, b: a + \" \" + b), english_words_1)\n", + "print(english_words_2)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -142,7 +171,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -199,7 +228,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.9.5" } }, "nbformat": 4, diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 0239d17..3ec8755 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -98,10 +98,10 @@ "evalue": "", "output_type": "error", "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# After we have iterated through all elements, we will get a StopIteration Error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mStopIteration\u001b[0m: " + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# After we have iterated through all elements, we will get a StopIteration Error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mStopIteration\u001b[0m: " ] } ], @@ -113,7 +113,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -146,7 +146,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -159,7 +159,32 @@ " # Sample Output: 2\n", " \n", " # Your code here:\n", - " " + " x = []\n", + " for i in iterator:\n", + " if i % 2 == 0 and i != 0:\n", + " x.append(i)\n", + " return(x[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iterator = iter(list(range(101)))\n", + "divisible2(iterator)" ] }, { @@ -173,7 +198,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -193,7 +218,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -224,7 +249,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -237,7 +262,39 @@ " # Sample Output: iter([0, 2, 4])\n", " \n", " # Your code here:\n", - " " + " number = 0\n", + " while number <= n:\n", + " yield number\n", + " number = number + 2" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n", + "6\n", + "8\n", + "10\n", + "12\n", + "14\n", + "16\n", + "18\n" + ] + } + ], + "source": [ + "iterator = even_iterator(19)\n", + "\n", + "for i in iterator:\n", + " print(i)" ] }, { @@ -253,12 +310,12 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "columns = ['sepal_length', 'sepal_width', 'petal_length','petal_width','iris_type']\n", - "iris = pd.read_csv(\"https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\", names=columns)" + "iris = pd.read_csv(\"https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\", names = columns)" ] }, { @@ -270,12 +327,100 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 14, "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": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris.head()" ] }, { @@ -287,12 +432,28 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "sepal_length 5.843333\n", + "sepal_width 3.054000\n", + "petal_length 3.758667\n", + "petal_width 1.198667\n", + "dtype: float64" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris.mean()" ] }, { @@ -304,12 +465,28 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "sepal_length 0.828066\n", + "sepal_width 0.433594\n", + "petal_length 1.764420\n", + "petal_width 0.763161\n", + "dtype: float64" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris.std()" ] }, { @@ -321,12 +498,96 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 17, "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", + "
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
\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" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']\n", + "iris_numeric = iris.select_dtypes(include = numerics)\n", + "iris_numeric.head()" ] }, { @@ -338,11 +599,11 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ - "def cm_to_in(x):\n", + "def cm_to_in(x_cm):\n", " # This function takes in a numeric value in centimeters and converts it to inches\n", " # Input: numeric value\n", " # Output: float\n", @@ -351,7 +612,8 @@ " # Sample Output: 0.393701\n", " \n", " # Your code here:\n", - " " + " x_in = x_cm*0.393701\n", + " return(x_in)" ] }, { @@ -363,12 +625,95 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 19, "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", + "
sepal_lengthsepal_widthpetal_lengthpetal_width
02.0078751.3779540.5511810.07874
11.9291351.1811030.5511810.07874
21.8503951.2598430.5118110.07874
31.8110251.2204730.5905520.07874
41.9685051.4173240.5511810.07874
\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width\n", + "0 2.007875 1.377954 0.551181 0.07874\n", + "1 1.929135 1.181103 0.551181 0.07874\n", + "2 1.850395 1.259843 0.511811 0.07874\n", + "3 1.811025 1.220473 0.590552 0.07874\n", + "4 1.968505 1.417324 0.551181 0.07874" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris_inch = cm_to_in(iris_numeric)\n", + "iris_inch.head()" ] }, { @@ -380,20 +725,113 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "# Define constant below:\n", "\n", + "error = 2\n", "\n", - "def add_constant(x):\n", + "def add_constant(x_w_error):\n", " # This function adds a global constant to our input.\n", " # Input: numeric value\n", " # Output: numeric value\n", " \n", " # Your code here:\n", - " " + " x_w_o_error = x_w_error + error\n", + " return(x_w_o_error)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "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", + "
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
\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" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iris_constant = add_constant(iris_numeric)\n", + "iris_constant.head()" ] }, { @@ -407,12 +845,41 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "# Your code here:\n", - "\n" + "\n", + "def max_value(x_row):\n", + " x_max = max(x_row)\n", + " return(x_max)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 5.1\n", + "1 4.9\n", + "2 4.7\n", + "3 4.6\n", + "4 5.0\n", + "dtype: float64" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iris_max = iris_numeric.apply(max_value, axis = 1)\n", + "iris_max.head()" ] }, { @@ -424,12 +891,115 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 28, "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", + "
sepal_lengthsepal_widthpetal_lengthpetal_widthtotal_lengthtotal_width
05.13.51.40.26.53.7
14.93.01.40.26.33.2
24.73.21.30.26.03.4
34.63.11.50.26.13.3
45.03.61.40.26.43.8
\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width total_length \\\n", + "0 5.1 3.5 1.4 0.2 6.5 \n", + "1 4.9 3.0 1.4 0.2 6.3 \n", + "2 4.7 3.2 1.3 0.2 6.0 \n", + "3 4.6 3.1 1.5 0.2 6.1 \n", + "4 5.0 3.6 1.4 0.2 6.4 \n", + "\n", + " total_width \n", + "0 3.7 \n", + "1 3.2 \n", + "2 3.4 \n", + "3 3.3 \n", + "4 3.8 " + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris_numeric['total_length'] = iris_numeric.apply(lambda x: x['sepal_length'] + x['petal_length'], axis = 1)\n", + "iris_numeric['total_width'] = iris_numeric.apply(lambda x: x['sepal_width'] + x['petal_width'], axis = 1)\n", + "iris_numeric.head()" ] }, { @@ -456,7 +1026,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.5" } }, "nbformat": 4, From 6609e296411b2560e8178d77b6e246de19c4bf05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20P=C3=A9rez=20Ortega?= Date: Wed, 21 Jul 2021 21:42:42 -0500 Subject: [PATCH 2/2] Lab terminado --- .DS_Store | Bin 6148 -> 6148 bytes .../.ipynb_checkpoints/Q1-checkpoint.ipynb | 35 ++---------------- .../.ipynb_checkpoints/Q2-checkpoint.ipynb | 27 ++++++++------ .../.ipynb_checkpoints/Q3-checkpoint.ipynb | 32 +++++++++------- .../.ipynb_checkpoints/main-checkpoint.ipynb | 15 ++------ your-code/Q1.ipynb | 16 ++++---- your-code/Q2.ipynb | 27 ++++++++------ your-code/Q3.ipynb | 32 +++++++++------- your-code/main.ipynb | 15 ++------ 9 files changed, 90 insertions(+), 109 deletions(-) diff --git a/.DS_Store b/.DS_Store index 1968f281020c1f0e7c8808efd9a172edadf70b94..c08fc5e5676e93c6c0e80b8cea942c2f403de312 100644 GIT binary patch delta 304 zcmZoMXfc=|#>B)qu~2NHo+2a5!~p9}j17!HMo;?0gTnPm<;4X_Ir&Kp3=Aid3UV@w zOAHLIGcqx=u(GjpaBy*O@p8omXXKX$mn4>y7CR*tMT2+&i6t3HPcQVcdHI20no!NJMF880ASU2SM; zsH0$LS*xQ^ZE0knqhMleR$I%-A+Bm@>zR;SSyf$ATQ>vfSRi0zgwPE9P#Q+foVZVn yr!2TAFDE}Q9i(vM$8MI*>>T_Yz<}8J@jLTmei1_fknRSMEEq8`Yz`3F!VCb^x>GFx delta 66 zcmZoMXfc=|#>CJ*u~2NHo+2aD!~pBb1|lqz`I%BSw=g%eY@Wb8k!dqK2R{c;(dI_x W@640=MGP4kCfo2xZ;laJ!3+R*yb@~w diff --git a/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb b/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb index 774ab13..9a0c5b4 100644 --- a/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb @@ -106,14 +106,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'bag_of_words': ['ironhack', 'is', 'cool', 'i', 'am', 'a', 'student', 'at', 'love'], 'term_freq': [[1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1]]}\n" + "{'bag_of_words': [], 'term_freq': []}\n" ] } ], "source": [ "# Define doc paths array\n", "\n", - "pwd = '/Users/diego.perez97/Documents/IronHack/Module 1/Week 1/Day 2/lab-string-operations/your-code'\n", + "pwd = '/Users/diegoperezo97/Documents/Ironhack – Data Analytics Bootcamp/Module 1/Week 1/Day 4/lab-functional-programming/your-code'\n", "os.chdir(pwd)\n", "file_extention = '.txt'\n", "file_names = [file for file in glob.glob(f'*{file_extention}')]\n", @@ -169,20 +169,7 @@ "cell_type": "code", "execution_count": 4, "metadata": {}, - "outputs": [ - { - "ename": "KeyError", - "evalue": "0", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mstop_words\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_stop_words\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mENGLISH_STOP_WORDS\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mbow\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_bow_from_docs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbow\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstop_words\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m\u001b[0m in \u001b[0;36mget_bow_from_docs\u001b[0;34m(docs, stop_words)\u001b[0m\n\u001b[1;32m 20\u001b[0m \"\"\"\n\u001b[1;32m 21\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mindex\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdocs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 22\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdocs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"r\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mfile\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 23\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mtext\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mfile\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0mstripped_text\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mKeyError\u001b[0m: 0" - ] - } - ], + "outputs": [], "source": [ "stop_words = list(_stop_words.ENGLISH_STOP_WORDS)\n", "bow = get_bow_from_docs(docs, stop_words)" @@ -197,7 +184,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'bag_of_words': ['ironhack', 'is', 'cool', 'i', 'am', 'a', 'student', 'at', 'love'], 'term_freq': [[1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1]]}\n" + "{'bag_of_words': [], 'term_freq': []}\n" ] } ], @@ -213,20 +200,6 @@ "\n", "```{'bag_of_words': ['ironhack', 'cool', 'love', 'student'], 'term_freq': [[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]}```" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/your-code/.ipynb_checkpoints/Q2-checkpoint.ipynb b/your-code/.ipynb_checkpoints/Q2-checkpoint.ipynb index 313b5ac..d5185e6 100644 --- a/your-code/.ipynb_checkpoints/Q2-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/Q2-checkpoint.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -48,7 +48,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -119,9 +119,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ImportError", + "evalue": "cannot import name 'stop_words' from 'sklearn.feature_extraction' (/usr/local/lib/python3.9/site-packages/sklearn/feature_extraction/__init__.py)", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0msklearn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfeature_extraction\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mstop_words\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m bow = get_bow_from_docs([\n\u001b[1;32m 3\u001b[0m \u001b[0;34m'www.coursereport.com_ironhack.html'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m'en.wikipedia.org_Data_analysis.html'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m'www.lipsum.com.html'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mImportError\u001b[0m: cannot import name 'stop_words' from 'sklearn.feature_extraction' (/usr/local/lib/python3.9/site-packages/sklearn/feature_extraction/__init__.py)" + ] + } + ], "source": [ "from sklearn.feature_extraction import stop_words\n", "bow = get_bow_from_docs([\n", @@ -145,13 +157,6 @@ "\n", "Spend 20-30 minutes to improve your functions or until you feel you are good at string operations. This lab is just a practice so you don't need to stress yourself out. If you feel you've practiced enough you can stop and move on the next challenge question." ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb b/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb index 6c13378..73102a1 100644 --- a/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb @@ -123,7 +123,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -136,14 +136,14 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "good morning everyone\n" + "everyone\n" ] } ], @@ -171,7 +171,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -189,11 +189,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ImportError", + "evalue": "cannot import name 'stop_words' from 'sklearn.feature_extraction' (/usr/local/lib/python3.9/site-packages/sklearn/feature_extraction/__init__.py)", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0msklearn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfeature_extraction\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mstop_words\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m bow = get_bow_from_docs([\n\u001b[1;32m 3\u001b[0m \u001b[0;34m'../../lab-bag-of-words/your-code/doc1.txt'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m'../../lab-bag-of-words/your-code/doc2.txt'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m '../../lab-bag-of-words/your-code/doc3.txt'],\n", + "\u001b[0;31mImportError\u001b[0m: cannot import name 'stop_words' from 'sklearn.feature_extraction' (/usr/local/lib/python3.9/site-packages/sklearn/feature_extraction/__init__.py)" + ] + } + ], "source": [ "from sklearn.feature_extraction import stop_words\n", + "\n", "bow = get_bow_from_docs([\n", " '../../lab-bag-of-words/your-code/doc1.txt', \n", " '../../lab-bag-of-words/your-code/doc2.txt', \n", @@ -203,13 +216,6 @@ "\n", "print(bow)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb index 3ec8755..8f68f2f 100644 --- a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -858,7 +858,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 23, "metadata": {}, "outputs": [ { @@ -872,7 +872,7 @@ "dtype: float64" ] }, - "execution_count": 24, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } @@ -891,7 +891,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -989,7 +989,7 @@ "4 3.8 " ] }, - "execution_count": 28, + "execution_count": 24, "metadata": {}, "output_type": "execute_result" } @@ -1001,13 +1001,6 @@ "iris_numeric['total_width'] = iris_numeric.apply(lambda x: x['sepal_width'] + x['petal_width'], axis = 1)\n", "iris_numeric.head()" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/your-code/Q1.ipynb b/your-code/Q1.ipynb index 8261421..9a0c5b4 100644 --- a/your-code/Q1.ipynb +++ b/your-code/Q1.ipynb @@ -19,7 +19,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -99,21 +99,21 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'bag_of_words': ['ironhack', 'is', 'cool', 'i', 'am', 'a', 'student', 'at', 'love'], 'term_freq': [[1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1]]}\n" + "{'bag_of_words': [], 'term_freq': []}\n" ] } ], "source": [ "# Define doc paths array\n", "\n", - "pwd = '/Users/diego.perez97/Documents/IronHack/Module 1/Week 1/Day 2/lab-string-operations/your-code'\n", + "pwd = '/Users/diegoperezo97/Documents/Ironhack – Data Analytics Bootcamp/Module 1/Week 1/Day 4/lab-functional-programming/your-code'\n", "os.chdir(pwd)\n", "file_extention = '.txt'\n", "file_names = [file for file in glob.glob(f'*{file_extention}')]\n", @@ -140,7 +140,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -167,7 +167,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -177,14 +177,14 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'bag_of_words': ['ironhack', 'cool', 'student', 'love'], 'term_freq': [[1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1], [1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1], [1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1]]}\n" + "{'bag_of_words': [], 'term_freq': []}\n" ] } ], diff --git a/your-code/Q2.ipynb b/your-code/Q2.ipynb index 313b5ac..d5185e6 100644 --- a/your-code/Q2.ipynb +++ b/your-code/Q2.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -48,7 +48,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -119,9 +119,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ImportError", + "evalue": "cannot import name 'stop_words' from 'sklearn.feature_extraction' (/usr/local/lib/python3.9/site-packages/sklearn/feature_extraction/__init__.py)", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0msklearn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfeature_extraction\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mstop_words\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m bow = get_bow_from_docs([\n\u001b[1;32m 3\u001b[0m \u001b[0;34m'www.coursereport.com_ironhack.html'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m'en.wikipedia.org_Data_analysis.html'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m'www.lipsum.com.html'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mImportError\u001b[0m: cannot import name 'stop_words' from 'sklearn.feature_extraction' (/usr/local/lib/python3.9/site-packages/sklearn/feature_extraction/__init__.py)" + ] + } + ], "source": [ "from sklearn.feature_extraction import stop_words\n", "bow = get_bow_from_docs([\n", @@ -145,13 +157,6 @@ "\n", "Spend 20-30 minutes to improve your functions or until you feel you are good at string operations. This lab is just a practice so you don't need to stress yourself out. If you feel you've practiced enough you can stop and move on the next challenge question." ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/your-code/Q3.ipynb b/your-code/Q3.ipynb index 6c13378..73102a1 100644 --- a/your-code/Q3.ipynb +++ b/your-code/Q3.ipynb @@ -123,7 +123,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -136,14 +136,14 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "good morning everyone\n" + "everyone\n" ] } ], @@ -171,7 +171,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -189,11 +189,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ImportError", + "evalue": "cannot import name 'stop_words' from 'sklearn.feature_extraction' (/usr/local/lib/python3.9/site-packages/sklearn/feature_extraction/__init__.py)", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0msklearn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfeature_extraction\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mstop_words\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m bow = get_bow_from_docs([\n\u001b[1;32m 3\u001b[0m \u001b[0;34m'../../lab-bag-of-words/your-code/doc1.txt'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m'../../lab-bag-of-words/your-code/doc2.txt'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m '../../lab-bag-of-words/your-code/doc3.txt'],\n", + "\u001b[0;31mImportError\u001b[0m: cannot import name 'stop_words' from 'sklearn.feature_extraction' (/usr/local/lib/python3.9/site-packages/sklearn/feature_extraction/__init__.py)" + ] + } + ], "source": [ "from sklearn.feature_extraction import stop_words\n", + "\n", "bow = get_bow_from_docs([\n", " '../../lab-bag-of-words/your-code/doc1.txt', \n", " '../../lab-bag-of-words/your-code/doc2.txt', \n", @@ -203,13 +216,6 @@ "\n", "print(bow)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 3ec8755..8f68f2f 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -858,7 +858,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 23, "metadata": {}, "outputs": [ { @@ -872,7 +872,7 @@ "dtype: float64" ] }, - "execution_count": 24, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } @@ -891,7 +891,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -989,7 +989,7 @@ "4 3.8 " ] }, - "execution_count": 28, + "execution_count": 24, "metadata": {}, "output_type": "execute_result" } @@ -1001,13 +1001,6 @@ "iris_numeric['total_width'] = iris_numeric.apply(lambda x: x['sepal_width'] + x['petal_width'], axis = 1)\n", "iris_numeric.head()" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {