diff --git a/lab-functional-programming/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb b/lab-functional-programming/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb new file mode 100644 index 0000000..9908ce6 --- /dev/null +++ b/lab-functional-programming/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb @@ -0,0 +1,216 @@ +{ + "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": 69, + "metadata": {}, + "outputs": [], + "source": [ + "# Import required libraries\n", + "import re\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", + " corpus = []\n", + "\n", + "# Write your code here\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", + " for i in range(len(docs)):\n", + " doc1 =[]\n", + " doc = open(docs[i], 'r').read()\n", + " doc1 = re.findall(r'\\b\\w+\\b', doc.lower())\n", + " corpus.append(doc1)\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", + " bag_of_words = list(set([word for doc in corpus for word in doc])-set(stop_words))\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", + " term_freq = [[1 if word in doc else 0 for word in bag_of_words] for doc in corpus]\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": 70, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'bag_of_words': ['cool', 'is', 'a', 'love', 'i', 'student', 'at', 'ironhack', 'am'], 'term_freq': [[1, 1, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 0, 1, 0, 1, 1, 1, 1, 1]]}\n" + ] + } + ], + "source": [ + "# Define doc paths array\n", + "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']\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": 71, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mDEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.\u001b[0m\n", + "Defaulting to user installation because normal site-packages is not writeable\n", + "Requirement already satisfied: sklearn in /Users/robertomargain/Library/Python/2.7/lib/python/site-packages (0.0)\n", + "Requirement already satisfied: scikit-learn in /Users/robertomargain/Library/Python/2.7/lib/python/site-packages (from sklearn) (0.20.4)\n", + "Requirement already satisfied: numpy>=1.8.2 in /Users/robertomargain/Library/Python/2.7/lib/python/site-packages (from scikit-learn->sklearn) (1.16.6)\n", + "Requirement already satisfied: scipy>=0.13.3 in /Users/robertomargain/Library/Python/2.7/lib/python/site-packages (from scikit-learn->sklearn) (1.2.3)\n", + "frozenset({'cannot', 'etc', 'whether', 'against', 'mine', 'whence', 'it', 'such', 'wherever', 'go', 'became', 'hence', 'few', 'describe', 'of', 'inc', 'fifty', 'an', 'most', 'along', 'fifteen', 'con', 'two', 'behind', 'us', 'then', 'either', 'found', 'until', 'so', 'had', 'seem', 'whither', 'last', 'she', 'should', 'whom', 'everyone', 'thus', 'everywhere', 'any', 'first', 'thence', 'up', 'per', 'perhaps', 'somehow', 'side', 'nowhere', 'cry', 'through', 'or', 'beside', 're', 'as', 'fire', 'each', 'ours', 'however', 'will', 'whereby', 'none', 'couldnt', 'do', 'no', 'may', 'thick', 'done', 'hereupon', 'further', 'not', 'whereupon', 'call', 'without', 'four', 'namely', 'system', 'hereby', 'about', 'move', 'above', 'made', 'sixty', 'detail', 'nine', 'her', 'sometime', 'on', 'next', 'i', 'whatever', 'has', 'ourselves', 'noone', 'mill', 'when', 'hers', 'both', 'another', 'upon', 'afterwards', 'below', 'you', 'me', 'many', 'latter', 'seemed', 'fill', 'who', 'anyhow', 'why', 'there', 'still', 'towards', 'he', 'empty', 'my', 'see', 'might', 'among', 'become', 'him', 'all', 'other', 'between', 'whenever', 'be', 'ever', 'since', 'thru', 'get', 'back', 'three', 'nor', 'where', 'give', 'once', 'very', 'its', 'what', 'least', 'almost', 'seeming', 'several', 'some', 'besides', 'via', 'this', 'else', 'although', 'moreover', 'around', 'here', 'due', 'show', 'over', 'eleven', 'less', 'formerly', 'am', 'meanwhile', 'twelve', 'are', 'seems', 'for', 'keep', 'which', 'front', 'name', 'anywhere', 'part', 'was', 'eight', 'neither', 'ie', 'within', 'his', 'former', 'herein', 'too', 'by', 'hereafter', 'throughout', 'always', 'out', 'put', 'and', 'how', 'amongst', 'whereafter', 'from', 'can', 'also', 'after', 'off', 'beyond', 'whose', 'bill', 'somewhere', 'mostly', 'could', 'de', 'those', 'something', 'they', 'please', 'five', 'nobody', 'before', 'again', 'under', 'someone', 'one', 'even', 'never', 'full', 'forty', 'nothing', 'the', 'yourselves', 'itself', 'a', 'whereas', 'whoever', 'we', 'in', 'but', 'their', 'un', 'would', 'becomes', 'everything', 'latterly', 'while', 'amoungst', 'myself', 'them', 'rather', 'now', 'anyway', 'themselves', 'nevertheless', 'being', 'himself', 'if', 'own', 'yours', 'six', 'serious', 'becoming', 'beforehand', 'more', 'interest', 'already', 'same', 'anything', 'have', 'often', 'except', 'thereafter', 'enough', 'together', 'indeed', 'sometimes', 'at', 'alone', 'only', 'than', 'sincere', 'onto', 'bottom', 'wherein', 'anyone', 'thin', 'with', 'ten', 'these', 'across', 'to', 'were', 'been', 'therein', 'though', 'much', 'yourself', 'therefore', 'eg', 'hasnt', 'ltd', 'amount', 'thereupon', 'otherwise', 'cant', 'is', 'co', 'twenty', 'during', 'your', 'take', 'toward', 'whole', 'thereby', 'hundred', 'well', 'third', 'find', 'elsewhere', 'every', 'top', 'our', 'yet', 'down', 'into', 'herself', 'because', 'others', 'must', 'that'})\n" + ] + } + ], + "source": [ + "!pip install sklearn\n", + "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": 72, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'bag_of_words': ['cool', 'ironhack', 'student', 'love'], 'term_freq': [[1, 1, 0, 0], [0, 1, 0, 1], [0, 1, 1, 0]]}\n" + ] + } + ], + "source": [ + "bow = get_bow_from_docs(docs, _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 (ipykernel)", + "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.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/lab-functional-programming/your-code/.ipynb_checkpoints/Q2-checkpoint.ipynb b/lab-functional-programming/your-code/.ipynb_checkpoints/Q2-checkpoint.ipynb new file mode 100644 index 0000000..3be3df2 --- /dev/null +++ b/lab-functional-programming/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 (ipykernel)", + "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.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/lab-functional-programming/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/lab-functional-programming/your-code/.ipynb_checkpoints/main-checkpoint.ipynb new file mode 100644 index 0000000..6982bc9 --- /dev/null +++ b/lab-functional-programming/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -0,0 +1,1024 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Before your start:\n", + "- Read the README.md file\n", + "- Comment as much as you can and use the resources in the README.md file\n", + "- Happy learning!" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import pandas as pd" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 1 - Iterators, Generators and `yield`. \n", + "\n", + "In iterator in Python is an object that represents a stream of data. However, iterators contain a countable number of values. We traverse through the iterator and return one value at a time. All iterators support a `next` function that allows us to traverse through the iterator. We can create an iterator using the `iter` function that comes with the base package of Python. Below is an example of an iterator." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "# We first define our iterator:\n", + "\n", + "iterator = iter([1,2,3])\n", + "\n", + "# We can now iterate through the object using the next function\n", + "\n", + "print(next(iterator))" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "# We continue to iterate through the iterator.\n", + "\n", + "print(next(iterator))" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "print(next(iterator))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "ename": "StopIteration", + "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: " + ] + } + ], + "source": [ + "# After we have iterated through all elements, we will get a StopIteration Error\n", + "\n", + "print(next(iterator))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "# We can also iterate through an iterator using a for loop like this:\n", + "# Note: we cannot go back directly in an iterator once we have traversed through the elements. \n", + "# This is why we are redefining the iterator below\n", + "\n", + "iterator = iter([1,2,3])\n", + "\n", + "for i in iterator:\n", + " print(i)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, write a function that takes an iterator and returns the first element in the iterator and returns the first element in the iterator that is divisible by 2. Assume that all iterators contain only numeric data. If we have not found a single element that is divisible by 2, return zero." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "def divisible2(iterator):\n", + " # This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", + " # Input: Iterable\n", + " # Output: Integer\n", + " \n", + " # Sample Input: iter([1,2,3])\n", + " # Sample Output: 2\n", + " \n", + " # Your code here:\n", + " value = 0\n", + " for ele in iterator:\n", + " if ele%2 == 0:\n", + " value = ele\n", + " return value" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisible2([1,2,3])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Generators\n", + "\n", + "It is quite difficult to create your own iterator since you would have to implement a `next` function. Generators are functions that enable us to create iterators. The difference between a function and a generator is that instead of using `return`, we use `yield`. For example, below we have a function that returns an iterator containing the numbers 0 through n:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "def firstn(n):\n", + " number = 0\n", + " while number < n:\n", + " yield number\n", + " number = number + 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we pass 5 to the function, we will see that we have a iterator containing the numbers 0 through 4." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n" + ] + } + ], + "source": [ + "iterator = firstn(5)\n", + "\n", + "for i in iterator:\n", + " print(i)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, create a generator that takes a number and returns an iterator containing all even numbers between 0 and the number you passed to the generator." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "def even_iterator(n):\n", + " # This function produces an iterator containing all even numbers between 0 and n\n", + " # Input: integer\n", + " # Output: iterator\n", + " \n", + " # Sample Input: 5\n", + " # Sample Output: iter([0, 2, 4])\n", + " \n", + " # Your code here:\n", + " number = 0\n", + " while number < n:\n", + " if number % 2 == 0:\n", + " yield number\n", + " number = number +1\n" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n" + ] + } + ], + "source": [ + "iterator = even_iterator(5)\n", + "for i in iterator:\n", + " print(i)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 2 - Applying Functions to DataFrames\n", + "\n", + "In this challenge, we will look at how to transform cells or entire columns at once.\n", + "\n", + "First, let's load a dataset. We will download the famous Iris classification dataset in the cell below." + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "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)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's look at the dataset using the `head` function." + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
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": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "iris.head()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's start off by using built-in functions. Try to apply the numpy mean function and describe what happens in the comments of the code." + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/fromnumeric.py:3438: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError. Select only valid columns before calling the reduction.\n", + " return mean(axis=axis, dtype=dtype, out=out, **kwargs)\n" + ] + }, + { + "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": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "\n", + "np.mean(iris)\n", + "\n", + "# Result: np.mean() automatically calculates the mean of each \"numeric\" column" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, we'll apply the standard deviation function in numpy (`np.std`). Describe what happened in the comments." + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/fromnumeric.py:3579: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError. Select only valid columns before calling the reduction.\n", + " return std(axis=axis, dtype=dtype, out=out, ddof=ddof, **kwargs)\n" + ] + }, + { + "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": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "np.std(iris)\n", + "# Result: np.std() automatically calculates the standard deviation of each \"numeric\" column, however in the future this would raise an error given iris_type is non-numeric" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The measurements are in centimeters. Let's convert them all to inches. First, we will create a dataframe that contains only the numeric columns. Assign this new dataframe to `iris_numeric`." + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "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
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": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "iris_numeric= iris.drop(pd.DataFrame(iris.iris_type).columns,axis=1)\n", + "iris_numeric.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, we will write a function that converts centimeters to inches in the cell below. Recall that 1cm = 0.393701in." + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "def cm_to_in(x):\n", + " # This function takes in a numeric value in centimeters and converts it to inches\n", + " # Input: numeric value\n", + " # Output: float\n", + " \n", + " # Sample Input: 1.0\n", + " # Sample Output: 0.393701\n", + " \n", + " # Your code here:\n", + " return x*0.393701" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now convert all columns in `iris_numeric` to inches in the cell below. We like to think of functional transformations as immutable. Therefore, save the transformed data in a dataframe called `iris_inch`." + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "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
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": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "iris_inch = cm_to_in(iris_numeric)\n", + "iris_inch.head()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We have just found that the original measurements were off by a constant. Define the global constant `error` and set it to 2. Write a function that uses the global constant and adds it to each cell in the dataframe. Apply this function to `iris_numeric` and save the result in `iris_constant`." + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "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": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Define constant below:\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", + " \n", + " # Your code here:\n", + " return x + error\n", + "\n", + "iris_constant = add_constant(iris_numeric)\n", + "iris_constant.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Challenge - Applying Functions to Columns\n", + "\n", + "Read more about applying functions to either rows or columns [here](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html) and write a function that computes the maximum value for each row of `iris_numeric`" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "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", + " ... \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": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "iris_numeric.apply(np.max)\n", + "iris_numeric.apply(np.max, axis=1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Compute the combined lengths for each row and the combined widths for each row using a function. Assign these values to new columns `total_length` and `total_width`." + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "iris_numeric['total_length'] = iris_numeric.sepal_length + iris_numeric.petal_length\n", + "iris_numeric['total_width'] = iris_numeric.sepal_width + iris_numeric.petal_width" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
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": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iris_numeric.head()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/lab-functional-programming/your-code/Q1.ipynb b/lab-functional-programming/your-code/Q1.ipynb index 8b07d3d..9908ce6 100644 --- a/lab-functional-programming/your-code/Q1.ipynb +++ b/lab-functional-programming/your-code/Q1.ipynb @@ -19,25 +19,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 69, "metadata": {}, "outputs": [], "source": [ "# Import required libraries\n", + "import re\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", + " corpus = []\n", + "\n", + "# Write your code here\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", + " for i in range(len(docs)):\n", + " doc1 =[]\n", + " doc = open(docs[i], 'r').read()\n", + " doc1 = re.findall(r'\\b\\w+\\b', doc.lower())\n", + " corpus.append(doc1)\n", " \n", " \"\"\"\n", " Loop `corpus`. Append the terms in each doc into the `bag_of_words` array. The terms in `bag_of_words` \n", @@ -46,15 +53,15 @@ " if it is not a stop word.\n", " \"\"\"\n", "\n", - " \n", + " bag_of_words = list(set([word for doc in corpus for word in doc])-set(stop_words))\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", + " term_freq = [[1 if word in doc else 0 for word in bag_of_words] for doc in corpus]\n", " \n", " # Now return your output as an object\n", " return {\n", @@ -75,12 +82,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 70, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'bag_of_words': ['cool', 'is', 'a', 'love', 'i', 'student', 'at', 'ironhack', 'am'], 'term_freq': [[1, 1, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 0, 1, 0, 1, 1, 1, 1, 1]]}\n" + ] + } + ], "source": [ "# Define doc paths array\n", - "docs = []\n", + "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']\n", "\n", "# Obtain BoW from your function\n", "bow = get_bow_from_docs(docs)\n", @@ -100,12 +115,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mDEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.\u001b[0m\n", + "Defaulting to user installation because normal site-packages is not writeable\n", + "Requirement already satisfied: sklearn in /Users/robertomargain/Library/Python/2.7/lib/python/site-packages (0.0)\n", + "Requirement already satisfied: scikit-learn in /Users/robertomargain/Library/Python/2.7/lib/python/site-packages (from sklearn) (0.20.4)\n", + "Requirement already satisfied: numpy>=1.8.2 in /Users/robertomargain/Library/Python/2.7/lib/python/site-packages (from scikit-learn->sklearn) (1.16.6)\n", + "Requirement already satisfied: scipy>=0.13.3 in /Users/robertomargain/Library/Python/2.7/lib/python/site-packages (from scikit-learn->sklearn) (1.2.3)\n", + "frozenset({'cannot', 'etc', 'whether', 'against', 'mine', 'whence', 'it', 'such', 'wherever', 'go', 'became', 'hence', 'few', 'describe', 'of', 'inc', 'fifty', 'an', 'most', 'along', 'fifteen', 'con', 'two', 'behind', 'us', 'then', 'either', 'found', 'until', 'so', 'had', 'seem', 'whither', 'last', 'she', 'should', 'whom', 'everyone', 'thus', 'everywhere', 'any', 'first', 'thence', 'up', 'per', 'perhaps', 'somehow', 'side', 'nowhere', 'cry', 'through', 'or', 'beside', 're', 'as', 'fire', 'each', 'ours', 'however', 'will', 'whereby', 'none', 'couldnt', 'do', 'no', 'may', 'thick', 'done', 'hereupon', 'further', 'not', 'whereupon', 'call', 'without', 'four', 'namely', 'system', 'hereby', 'about', 'move', 'above', 'made', 'sixty', 'detail', 'nine', 'her', 'sometime', 'on', 'next', 'i', 'whatever', 'has', 'ourselves', 'noone', 'mill', 'when', 'hers', 'both', 'another', 'upon', 'afterwards', 'below', 'you', 'me', 'many', 'latter', 'seemed', 'fill', 'who', 'anyhow', 'why', 'there', 'still', 'towards', 'he', 'empty', 'my', 'see', 'might', 'among', 'become', 'him', 'all', 'other', 'between', 'whenever', 'be', 'ever', 'since', 'thru', 'get', 'back', 'three', 'nor', 'where', 'give', 'once', 'very', 'its', 'what', 'least', 'almost', 'seeming', 'several', 'some', 'besides', 'via', 'this', 'else', 'although', 'moreover', 'around', 'here', 'due', 'show', 'over', 'eleven', 'less', 'formerly', 'am', 'meanwhile', 'twelve', 'are', 'seems', 'for', 'keep', 'which', 'front', 'name', 'anywhere', 'part', 'was', 'eight', 'neither', 'ie', 'within', 'his', 'former', 'herein', 'too', 'by', 'hereafter', 'throughout', 'always', 'out', 'put', 'and', 'how', 'amongst', 'whereafter', 'from', 'can', 'also', 'after', 'off', 'beyond', 'whose', 'bill', 'somewhere', 'mostly', 'could', 'de', 'those', 'something', 'they', 'please', 'five', 'nobody', 'before', 'again', 'under', 'someone', 'one', 'even', 'never', 'full', 'forty', 'nothing', 'the', 'yourselves', 'itself', 'a', 'whereas', 'whoever', 'we', 'in', 'but', 'their', 'un', 'would', 'becomes', 'everything', 'latterly', 'while', 'amoungst', 'myself', 'them', 'rather', 'now', 'anyway', 'themselves', 'nevertheless', 'being', 'himself', 'if', 'own', 'yours', 'six', 'serious', 'becoming', 'beforehand', 'more', 'interest', 'already', 'same', 'anything', 'have', 'often', 'except', 'thereafter', 'enough', 'together', 'indeed', 'sometimes', 'at', 'alone', 'only', 'than', 'sincere', 'onto', 'bottom', 'wherein', 'anyone', 'thin', 'with', 'ten', 'these', 'across', 'to', 'were', 'been', 'therein', 'though', 'much', 'yourself', 'therefore', 'eg', 'hasnt', 'ltd', 'amount', 'thereupon', 'otherwise', 'cant', 'is', 'co', 'twenty', 'during', 'your', 'take', 'toward', 'whole', 'thereby', 'hundred', 'well', 'third', 'find', 'elsewhere', 'every', 'top', 'our', 'yet', 'down', 'into', 'herself', 'because', 'others', 'must', 'that'})\n" + ] + } + ], "source": [ - "from sklearn.feature_extraction import stop_words\n", - "print(stop_words.ENGLISH_STOP_WORDS)" + "!pip install sklearn\n", + "from sklearn.feature_extraction import _stop_words\n", + "print(_stop_words.ENGLISH_STOP_WORDS)" ] }, { @@ -128,11 +158,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'bag_of_words': ['cool', 'ironhack', 'student', 'love'], 'term_freq': [[1, 1, 0, 0], [0, 1, 0, 1], [0, 1, 1, 0]]}\n" + ] + } + ], "source": [ - "bow = get_bow_from_docs(bow, stop_words.ENGLISH_STOP_WORDS)\n", + "bow = get_bow_from_docs(docs, _stop_words.ENGLISH_STOP_WORDS)\n", "\n", "print(bow)" ] @@ -156,7 +194,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -170,7 +208,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.9.7" } }, "nbformat": 4, diff --git a/lab-functional-programming/your-code/Q2.ipynb b/lab-functional-programming/your-code/Q2.ipynb index f50f442..3be3df2 100644 --- a/lab-functional-programming/your-code/Q2.ipynb +++ b/lab-functional-programming/your-code/Q2.ipynb @@ -97,7 +97,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -111,7 +111,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.9.7" } }, "nbformat": 4, diff --git a/lab-functional-programming/your-code/main.ipynb b/lab-functional-programming/your-code/main.ipynb index 8017d6e..6982bc9 100644 --- a/lab-functional-programming/your-code/main.ipynb +++ b/lab-functional-programming/your-code/main.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 36, "metadata": {}, "outputs": [], "source": [ @@ -31,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -146,7 +146,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ @@ -159,7 +159,31 @@ " # Sample Output: 2\n", " \n", " # Your code here:\n", - " " + " value = 0\n", + " for ele in iterator:\n", + " if ele%2 == 0:\n", + " value = ele\n", + " return value" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisible2([1,2,3])" ] }, { @@ -224,7 +248,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ @@ -237,7 +261,32 @@ " # Sample Output: iter([0, 2, 4])\n", " \n", " # Your code here:\n", - " " + " number = 0\n", + " while number < n:\n", + " if number % 2 == 0:\n", + " yield number\n", + " number = number +1\n" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n" + ] + } + ], + "source": [ + "iterator = even_iterator(5)\n", + "for i in iterator:\n", + " print(i)" ] }, { @@ -253,7 +302,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 39, "metadata": {}, "outputs": [], "source": [ @@ -270,12 +319,99 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 40, "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": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "iris.head()\n" ] }, { @@ -287,12 +423,38 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/fromnumeric.py:3438: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError. Select only valid columns before calling the reduction.\n", + " return mean(axis=axis, dtype=dtype, out=out, **kwargs)\n" + ] + }, + { + "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": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "\n", + "np.mean(iris)\n", + "\n", + "# Result: np.mean() automatically calculates the mean of each \"numeric\" column" ] }, { @@ -304,12 +466,36 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/fromnumeric.py:3579: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError. Select only valid columns before calling the reduction.\n", + " return std(axis=axis, dtype=dtype, out=out, ddof=ddof, **kwargs)\n" + ] + }, + { + "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": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "np.std(iris)\n", + "# Result: np.std() automatically calculates the standard deviation of each \"numeric\" column, however in the future this would raise an error given iris_type is non-numeric" ] }, { @@ -321,12 +507,94 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 54, "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": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "iris_numeric= iris.drop(pd.DataFrame(iris.iris_type).columns,axis=1)\n", + "iris_numeric.head()" ] }, { @@ -338,7 +606,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 56, "metadata": {}, "outputs": [], "source": [ @@ -351,7 +619,7 @@ " # Sample Output: 0.393701\n", " \n", " # Your code here:\n", - " " + " return x*0.393701" ] }, { @@ -363,12 +631,94 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 57, "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": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "iris_inch = cm_to_in(iris_numeric)\n", + "iris_inch.head()\n" ] }, { @@ -380,12 +730,93 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 58, "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": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Define constant below:\n", - "\n", + "error = 2\n", "\n", "def add_constant(x):\n", " # This function adds a global constant to our input.\n", @@ -393,7 +824,10 @@ " # Output: numeric value\n", " \n", " # Your code here:\n", - " " + " return x + error\n", + "\n", + "iris_constant = add_constant(iris_numeric)\n", + "iris_constant.head()" ] }, { @@ -407,12 +841,35 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 70, "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": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "iris_numeric.apply(np.max)\n", + "iris_numeric.apply(np.max, axis=1)" ] }, { @@ -424,25 +881,128 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 73, "metadata": {}, "outputs": [], "source": [ "# Your code here:\n", - "\n" + "iris_numeric['total_length'] = iris_numeric.sepal_length + iris_numeric.petal_length\n", + "iris_numeric['total_width'] = iris_numeric.sepal_width + iris_numeric.petal_width" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 74, "metadata": {}, - "outputs": [], - "source": [] + "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": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iris_numeric.head()" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -456,7 +1016,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.9.7" } }, "nbformat": 4,