Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
347 changes: 347 additions & 0 deletions your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb

Large diffs are not rendered by default.

207 changes: 207 additions & 0 deletions your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Lambda** is a special Python function type that is **anonymous**. By *anonymous* it means a lambda function does not have name. Lambda functions are embedded inside codes so that you don't call them like calling regular Python functions.\n",
"\n",
"**`Map`** applies a function to all the items in an input list. The function that is applied can be a standard or a lambda function.\n",
"\n",
"For instance, below is an example of multiplying number tuples in a list:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 12, 30]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"items = [(1,2), (3,4), (5,6)]\n",
"\n",
"def multiply(num_tuple):\n",
" return num_tuple[0]*num_tuple[1]\n",
"list(map(multiply, items))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"...is the same as:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 12, 30]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"items = [(1,2), (3,4), (5,6)]\n",
"list(map(lambda item: item[0]*item[1], items))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Why do we sometimes use `lambda` and `map`? Because, as you see in the example above, they make your code really concise by combining 3 lines of code to 1 line.\n",
"\n",
"Besides `map`, there is also **`filter`** that selectively returns elements in an array according to whether you return `True`. There is also **`reduce`** that performs computation on a list of items then returns result.\n",
"\n",
"Here is a [good tutorial](http://book.pythontips.com/en/latest/map_filter.html) about `map`, `filter`, and `reduce`. Read it if you are not familiar with how they are used. Then proceed to the next cell."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the next cell, use `filter` and `lambda` to return a list that contains positive numbers only. The output should be:\n",
"\n",
"```[1, 4, 5]```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numbers = [1, 4, -1, -100, 0, 5, -99]\n",
"\n",
"# Enter your code below"
]
},
{
"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": null,
"metadata": {},
"outputs": [],
"source": [
"import langdetect\n",
"from functools import reduce\n",
"words = ['good morning', '早上好', 'доброго', 'おはようございます', 'everyone', '大家', 'каждый', 'みんな']\n",
"\n",
"# Enter your code below"
]
},
{
"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.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading