From 9a94d012675fae78565255370224148bde6740e3 Mon Sep 17 00:00:00 2001 From: Salvador Carrasco Date: Mon, 8 Nov 2021 12:40:39 -0600 Subject: [PATCH] Lab modificado por Chava --- .../.ipynb_checkpoints/Q1-checkpoint.ipynb | 178 ++++++ .../.ipynb_checkpoints/Q2-checkpoint.ipynb | 119 ++++ .../.ipynb_checkpoints/Q3-checkpoint.ipynb | 285 +++++++++ .../.ipynb_checkpoints/main-checkpoint.ipynb | 554 +++++++++++++++++- your-code/Q1.ipynb | 4 +- your-code/Q2.ipynb | 4 +- your-code/Q3.ipynb | 94 ++- your-code/main.ipynb | 554 +++++++++++++++++- 8 files changed, 1724 insertions(+), 68 deletions(-) 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/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb b/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb new file mode 100644 index 0000000..8b07d3d --- /dev/null +++ b/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb @@ -0,0 +1,178 @@ +{ + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Import required libraries\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", + " \n", + " \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", + " \"\"\"\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", + "\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", + " \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": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Define doc paths array\n", + "docs = []\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": null, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.feature_extraction import stop_words\n", + "print(stop_words.ENGLISH_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": null, + "metadata": {}, + "outputs": [], + "source": [ + "bow = get_bow_from_docs(bow, stop_words.ENGLISH_STOP_WORDS)\n", + "\n", + "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": [] + } + ], + "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.6.6" + } + }, + "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..f50f442 --- /dev/null +++ b/your-code/.ipynb_checkpoints/Q2-checkpoint.ipynb @@ -0,0 +1,119 @@ +{ + "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" + ] + }, + { + "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": [ + "def get_bow_from_docs(docs, stop_words=[]):\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", + " \n", + " # write your codes here\n", + " \n", + " return {\n", + " \"bag_of_words\": bag_of_words,\n", + " \"term_freq\": term_freq\n", + " }\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.6.6" + } + }, + "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..46132f6 --- /dev/null +++ b/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb @@ -0,0 +1,285 @@ +{ + "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": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 12, 30]" + ] + }, + "execution_count": 11, + "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": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 12, 30]" + ] + }, + "execution_count": 10, + "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": [ + { + "data": { + "text/plain": [ + "[1, 4, 5]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "numbers = [1, 4, -1, -100, 0, 5, -99]\n", + "\n", + "# Enter your code below\n", + "\n", + "list(filter(lambda x:x>0,numbers))" + ] + }, + { + "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": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting langdetect\n", + " Downloading langdetect-1.0.9.tar.gz (981 kB)\n", + "Requirement already satisfied: six in c:\\users\\salva\\anaconda3\\lib\\site-packages (from langdetect) (1.15.0)\n", + "Building wheels for collected packages: langdetect\n", + " Building wheel for langdetect (setup.py): started\n", + " Building wheel for langdetect (setup.py): finished with status 'done'\n", + " Created wheel for langdetect: filename=langdetect-1.0.9-py3-none-any.whl size=993221 sha256=415206f6605e133bb17a67876ca60da8f69bb7c5c82e8c22425572f27fbcc528\n", + " Stored in directory: c:\\users\\salva\\appdata\\local\\pip\\cache\\wheels\\13\\c7\\b0\\79f66658626032e78fc1a83103690ef6797d551cb22e56e734\n", + "Successfully built langdetect\n", + "Installing collected packages: langdetect\n", + "Successfully installed langdetect-1.0.9\n" + ] + } + ], + "source": [ + "!pip install langdetect" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "good morning\n", + "everyone\n", + "['good morning', 'everyone']\n" + ] + }, + { + "data": { + "text/plain": [ + "'good morning everyone'" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import langdetect\n", + "from functools import reduce\n", + "words = ['good morning', '早上好', 'доброго', 'おはようございます', 'everyone', '大家', 'каждый', 'みんな']\n", + "\n", + "# Enter your code below\n", + "word_english=[]\n", + "for word in words:\n", + " if langdetect.detect(word)=='en':\n", + " word_english.append(word)\n", + " print(word)\n", + " \n", + "filt_list=[word for word in words if langdetect.detect(word)=='en']\n", + "print(filt_list)\n", + "english_words2= reduce(lambda x,y: x+ ' '+ y, filt_list)\n", + "english_words2\n", + " \n", + "#product = reduce((lambda x, y: x * y), [1, 2, 3, 4])\n", + "#list(reduce((lambda x:word)(for word in words:if langdetect.detect(word)=='en')))\n", + "\n", + "#list(reduce(filter(lambda item: item == (langdetect.detect(word)=='en'),words)))\n", + "#word= reduce(filter((lambda x:x==langdetect.detect(word)=='en', words))\n", + "\n", + "\n" + ] + }, + { + "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": 80, + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb index 0239d17..679dc52 100644 --- a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -146,7 +146,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -159,7 +159,33 @@ " # Sample Output: 2\n", " \n", " # Your code here:\n", - " " + " a=0\n", + " for i in iterator:\n", + " if i%2==0:\n", + " a=i\n", + " break\n", + " return a\n", + " print(iterator)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisible2([1,2,3])" ] }, { @@ -224,7 +250,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -237,7 +263,71 @@ " # Sample Output: iter([0, 2, 4])\n", " \n", " # Your code here:\n", - " " + " \n", + " for i in range(n):\n", + " if i%2==0:\n", + " yield i" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iterator=even_iterator(5)\n", + "iterator.__next__()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iterator.__next__()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iterator.__next__()" ] }, { @@ -270,12 +360,100 @@ }, { "cell_type": "code", - "execution_count": 15, + "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", + " \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": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "import pandas as pd\n", + "iris.head()" ] }, { @@ -289,10 +467,27 @@ "cell_type": "code", "execution_count": 16, "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": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "import numpy as np\n", + "\n", + "np.mean(iris)" ] }, { @@ -306,10 +501,25 @@ "cell_type": "code", "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "sepal_length 0.825301\n", + "sepal_width 0.432147\n", + "petal_length 1.758529\n", + "petal_width 0.760613\n", + "dtype: float64" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "np.std(iris)\n" ] }, { @@ -321,12 +531,94 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 25, "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": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "iris_numeric=iris._get_numeric_data()\n", + "iris_numeric.head()" ] }, { @@ -338,9 +630,20 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0.393701" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def cm_to_in(x):\n", " # This function takes in a numeric value in centimeters and converts it to inches\n", @@ -351,6 +654,10 @@ " # Sample Output: 0.393701\n", " \n", " # Your code here:\n", + " a=x*0.393701\n", + " return a\n", + "\n", + "cm_to_in(1)\n", " " ] }, @@ -363,12 +670,95 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 31, "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": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris_inch=cm_to_in(iris_numeric)\n", + "iris_inch.head()" ] }, { @@ -380,19 +770,104 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 33, "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
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": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Define constant below:\n", - "\n", + "error=2\n", "\n", "def add_constant(x):\n", " # This function adds a global constant to our input.\n", " # Input: numeric value\n", " # Output: numeric value\n", + " b=x+error\n", + " return b\n", " \n", " # Your code here:\n", + "iris_constant=add_constant(iris_numeric)\n", + "iris_constant.head()\n", " " ] }, @@ -407,12 +882,35 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 34, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0 5.1\n", + "1 4.9\n", + "2 4.7\n", + "3 4.6\n", + "4 5.0\n", + " ... \n", + "145 6.7\n", + "146 6.3\n", + "147 6.5\n", + "148 6.2\n", + "149 5.9\n", + "Length: 150, dtype: float64" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris_numeric.apply(max,axis=1)" ] }, { @@ -456,9 +954,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.8" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/your-code/Q1.ipynb b/your-code/Q1.ipynb index 8b07d3d..daa7832 100644 --- a/your-code/Q1.ipynb +++ b/your-code/Q1.ipynb @@ -170,9 +170,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.8.8" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/your-code/Q2.ipynb b/your-code/Q2.ipynb index f50f442..b305082 100644 --- a/your-code/Q2.ipynb +++ b/your-code/Q2.ipynb @@ -111,9 +111,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.8.8" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/your-code/Q3.ipynb b/your-code/Q3.ipynb index 75055ac..46132f6 100644 --- a/your-code/Q3.ipynb +++ b/your-code/Q3.ipynb @@ -85,13 +85,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 5]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "numbers = [1, 4, -1, -100, 0, 5, -99]\n", "\n", - "# Enter your code below" + "# Enter your code below\n", + "\n", + "list(filter(lambda x:x>0,numbers))" ] }, { @@ -113,15 +126,80 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting langdetect\n", + " Downloading langdetect-1.0.9.tar.gz (981 kB)\n", + "Requirement already satisfied: six in c:\\users\\salva\\anaconda3\\lib\\site-packages (from langdetect) (1.15.0)\n", + "Building wheels for collected packages: langdetect\n", + " Building wheel for langdetect (setup.py): started\n", + " Building wheel for langdetect (setup.py): finished with status 'done'\n", + " Created wheel for langdetect: filename=langdetect-1.0.9-py3-none-any.whl size=993221 sha256=415206f6605e133bb17a67876ca60da8f69bb7c5c82e8c22425572f27fbcc528\n", + " Stored in directory: c:\\users\\salva\\appdata\\local\\pip\\cache\\wheels\\13\\c7\\b0\\79f66658626032e78fc1a83103690ef6797d551cb22e56e734\n", + "Successfully built langdetect\n", + "Installing collected packages: langdetect\n", + "Successfully installed langdetect-1.0.9\n" + ] + } + ], + "source": [ + "!pip install langdetect" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "good morning\n", + "everyone\n", + "['good morning', 'everyone']\n" + ] + }, + { + "data": { + "text/plain": [ + "'good morning everyone'" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import langdetect\n", "from functools import reduce\n", "words = ['good morning', '早上好', 'доброго', 'おはようございます', 'everyone', '大家', 'каждый', 'みんな']\n", "\n", - "# Enter your code below" + "# Enter your code below\n", + "word_english=[]\n", + "for word in words:\n", + " if langdetect.detect(word)=='en':\n", + " word_english.append(word)\n", + " print(word)\n", + " \n", + "filt_list=[word for word in words if langdetect.detect(word)=='en']\n", + "print(filt_list)\n", + "english_words2= reduce(lambda x,y: x+ ' '+ y, filt_list)\n", + "english_words2\n", + " \n", + "#product = reduce((lambda x, y: x * y), [1, 2, 3, 4])\n", + "#list(reduce((lambda x:word)(for word in words:if langdetect.detect(word)=='en')))\n", + "\n", + "#list(reduce(filter(lambda item: item == (langdetect.detect(word)=='en'),words)))\n", + "#word= reduce(filter((lambda x:x==langdetect.detect(word)=='en', words))\n", + "\n", + "\n" ] }, { @@ -199,9 +277,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.8.8" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 0239d17..679dc52 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -146,7 +146,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -159,7 +159,33 @@ " # Sample Output: 2\n", " \n", " # Your code here:\n", - " " + " a=0\n", + " for i in iterator:\n", + " if i%2==0:\n", + " a=i\n", + " break\n", + " return a\n", + " print(iterator)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisible2([1,2,3])" ] }, { @@ -224,7 +250,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -237,7 +263,71 @@ " # Sample Output: iter([0, 2, 4])\n", " \n", " # Your code here:\n", - " " + " \n", + " for i in range(n):\n", + " if i%2==0:\n", + " yield i" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iterator=even_iterator(5)\n", + "iterator.__next__()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iterator.__next__()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iterator.__next__()" ] }, { @@ -270,12 +360,100 @@ }, { "cell_type": "code", - "execution_count": 15, + "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", + " \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": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "import pandas as pd\n", + "iris.head()" ] }, { @@ -289,10 +467,27 @@ "cell_type": "code", "execution_count": 16, "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": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "import numpy as np\n", + "\n", + "np.mean(iris)" ] }, { @@ -306,10 +501,25 @@ "cell_type": "code", "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "sepal_length 0.825301\n", + "sepal_width 0.432147\n", + "petal_length 1.758529\n", + "petal_width 0.760613\n", + "dtype: float64" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "np.std(iris)\n" ] }, { @@ -321,12 +531,94 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 25, "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": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "iris_numeric=iris._get_numeric_data()\n", + "iris_numeric.head()" ] }, { @@ -338,9 +630,20 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0.393701" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def cm_to_in(x):\n", " # This function takes in a numeric value in centimeters and converts it to inches\n", @@ -351,6 +654,10 @@ " # Sample Output: 0.393701\n", " \n", " # Your code here:\n", + " a=x*0.393701\n", + " return a\n", + "\n", + "cm_to_in(1)\n", " " ] }, @@ -363,12 +670,95 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 31, "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": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris_inch=cm_to_in(iris_numeric)\n", + "iris_inch.head()" ] }, { @@ -380,19 +770,104 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 33, "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
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": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Define constant below:\n", - "\n", + "error=2\n", "\n", "def add_constant(x):\n", " # This function adds a global constant to our input.\n", " # Input: numeric value\n", " # Output: numeric value\n", + " b=x+error\n", + " return b\n", " \n", " # Your code here:\n", + "iris_constant=add_constant(iris_numeric)\n", + "iris_constant.head()\n", " " ] }, @@ -407,12 +882,35 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 34, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0 5.1\n", + "1 4.9\n", + "2 4.7\n", + "3 4.6\n", + "4 5.0\n", + " ... \n", + "145 6.7\n", + "146 6.3\n", + "147 6.5\n", + "148 6.2\n", + "149 5.9\n", + "Length: 150, dtype: float64" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "iris_numeric.apply(max,axis=1)" ] }, { @@ -456,9 +954,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.8" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }