{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "lamda.ipynb", "provenance": [], "collapsed_sections": [], "authorship_tag": "ABX9TyMOyD+4tvDOYCdm1AxRsxK1", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "code", "source": [ "" ], "metadata": { "id": "W6rzI8z8wH43" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import nltk\n", "nltk.download('stopwords')\n", "\n", "import pandas as pd\n", "import re\n", "import string\n", "pd.set_option('display.max_colwidth', 200)\n", "\n", "stopwords = nltk.corpus.stopwords.words('english')\n", "\n", "data = pd.DataFrame({\n", " 'label' : [ 'one', 'second'],\n", " 'body_text' : [ 'The Democratic Party has a more complicated relationship with Donald Trump than it likes to admit.', \\\n", " 'It wants voters to remember the nonstop chaos of his administration, his Twitter rants, \\\n", " how he debased the presidency on Jan. 6 and won’t stop lying about the 2020 election results.']\n", "})\n", "data" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 208 }, "id": "EYAVniDWwH0r", "outputId": "cd18dd2d-9720-40de-f8b9-2137b99c70d0" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "[nltk_data] Downloading package stopwords to /root/nltk_data...\n", "[nltk_data] Package stopwords is already up-to-date!\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ " label \\\n", "0 one \n", "1 second \n", "\n", " body_text \n", "0 The Democratic Party has a more complicated relationship with Donald Trump than it likes to admit. \n", "1 It wants voters to remember the nonstop chaos of his administration, his Twitter rants, how he debased the presidency on Jan. 6 and won’t stop lying about the 2020 election res... " ], "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", "
labelbody_text
0oneThe Democratic Party has a more complicated relationship with Donald Trump than it likes to admit.
1secondIt wants voters to remember the nonstop chaos of his administration, his Twitter rants, how he debased the presidency on Jan. 6 and won’t stop lying about the 2020 election res...
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 7 } ] }, { "cell_type": "code", "source": [ "def clean_text(text):\n", " text = \"\".join([word for word in text if word not in string.punctuation])\n", " tokens = re.split('\\W+', text)\n", " text = [word for word in tokens if word not in stopwords]\n", " return text\n", "\n", "data['body_text_nostop'] = data['body_text'].apply(lambda x: clean_text(x.lower()))\n", "\n", "data.head()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 208 }, "id": "kCVV5Vk4wHxk", "outputId": "a03da592-c3c2-4aed-c741-8d1c527d461a" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " label \\\n", "0 one \n", "1 second \n", "\n", " body_text \\\n", "0 The Democratic Party has a more complicated relationship with Donald Trump than it likes to admit. \n", "1 It wants voters to remember the nonstop chaos of his administration, his Twitter rants, how he debased the presidency on Jan. 6 and won’t stop lying about the 2020 election res... \n", "\n", " body_text_nostop \n", "0 [democratic, party, complicated, relationship, donald, trump, likes, admit] \n", "1 [wants, voters, remember, nonstop, chaos, administration, twitter, rants, debased, presidency, jan, 6, stop, lying, 2020, election, results] " ], "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", "
labelbody_textbody_text_nostop
0oneThe Democratic Party has a more complicated relationship with Donald Trump than it likes to admit.[democratic, party, complicated, relationship, donald, trump, likes, admit]
1secondIt wants voters to remember the nonstop chaos of his administration, his Twitter rants, how he debased the presidency on Jan. 6 and won’t stop lying about the 2020 election res...[wants, voters, remember, nonstop, chaos, administration, twitter, rants, debased, presidency, jan, 6, stop, lying, 2020, election, results]
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 8 } ] }, { "cell_type": "code", "source": [ "ps = nltk.PorterStemmer()\n", "\n", "# stem is one of attribute or function of PorterStemmer of NLTK\n", "def stemming(tokenized_text):\n", " text = [ps.stem(word) for word in tokenized_text]\n", " return text\n", "\n", "data['body_text_stemmed'] = data['body_text_nostop'].apply(lambda x: stemming(x))\n", "\n", "data.head()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 226 }, "id": "RvUyDVsjwHub", "outputId": "6826f2d2-1028-47b3-e72c-00f3254c789e" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " label \\\n", "0 one \n", "1 second \n", "\n", " body_text \\\n", "0 The Democratic Party has a more complicated relationship with Donald Trump than it likes to admit. \n", "1 It wants voters to remember the nonstop chaos of his administration, his Twitter rants, how he debased the presidency on Jan. 6 and won’t stop lying about the 2020 election res... \n", "\n", " body_text_nostop \\\n", "0 [democratic, party, complicated, relationship, donald, trump, likes, admit] \n", "1 [wants, voters, remember, nonstop, chaos, administration, twitter, rants, debased, presidency, jan, 6, stop, lying, 2020, election, results] \n", "\n", " body_text_stemmed \n", "0 [democrat, parti, complic, relationship, donald, trump, like, admit] \n", "1 [want, voter, rememb, nonstop, chao, administr, twitter, rant, debas, presid, jan, 6, stop, lie, 2020, elect, result] " ], "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", "
labelbody_textbody_text_nostopbody_text_stemmed
0oneThe Democratic Party has a more complicated relationship with Donald Trump than it likes to admit.[democratic, party, complicated, relationship, donald, trump, likes, admit][democrat, parti, complic, relationship, donald, trump, like, admit]
1secondIt wants voters to remember the nonstop chaos of his administration, his Twitter rants, how he debased the presidency on Jan. 6 and won’t stop lying about the 2020 election res...[wants, voters, remember, nonstop, chaos, administration, twitter, rants, debased, presidency, jan, 6, stop, lying, 2020, election, results][want, voter, rememb, nonstop, chao, administr, twitter, rant, debas, presid, jan, 6, stop, lie, 2020, elect, result]
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 10 } ] }, { "cell_type": "code", "source": [ "" ], "metadata": { "id": "o4c1_C6IwHo9" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "bx1rb2bR43Ep", "outputId": "cd3920f6-3f3d-4dd9-f03b-17ed32338982" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "15\n" ] } ], "source": [ "x = lambda a : a + 10\n", "print(x(5))" ] }, { "cell_type": "code", "source": [ "y=lambda x: x+3\n", "y(5)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ZiHuYN6E95eM", "outputId": "fd5adf97-9f18-4dfe-e5b1-9a84ce3b57ba" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "8" ] }, "metadata": {}, "execution_count": 100 } ] }, { "cell_type": "code", "source": [ "(lambda x: x+3)(5)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "cpuZg-s4gtjQ", "outputId": "194d9300-c969-4e36-f517-01ce7bf5a31d" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "8" ] }, "metadata": {}, "execution_count": 102 } ] }, { "cell_type": "code", "source": [ "def add(x):\n", " return x+3\n", "\n", "add(5)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "F9amlr6Z95hw", "outputId": "121a289b-8475-4df8-a356-f0a1c8238310" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "8" ] }, "metadata": {}, "execution_count": 103 } ] }, { "cell_type": "code", "source": [ "" ], "metadata": { "id": "M6ID7Ag3BNMo" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "dff=pd.DataFrame([[1, 2, 3, 4],\n", " [5, 6, 7, 8],\n", " [np.square(6), np.sqrt(144), 22, 34]])\n", "\n", "dff.index=[0,1,2]\n", "dff.columns=['A', 'B', 'C','D']\n", "\n", "dff" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "D07j5IEoFiF1", "outputId": "a95a0984-eb93-4b32-b360-dbfee5db5eaf" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " A B C D\n", "0 1 2.0 3 4\n", "1 5 6.0 7 8\n", "2 36 12.0 22 34" ], "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", "
ABCD
012.034
156.078
23612.02234
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 11 } ] }, { "cell_type": "code", "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "df=pd.DataFrame([[1, 2, 3, 4],\n", " [5, 6, 7, 8],\n", " [9, 10, 11, 12]],\n", " index=[0,1,2],\n", " columns=['A', 'B', 'C','D'])\n", "\n", "df" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "40nKkQcPBNHT", "outputId": "842a0669-611f-4ec6-b6c6-66d42123ede3" }, "execution_count": 9, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " A B C D\n", "0 1 2 3 4\n", "1 5 6 7 8\n", "2 9 10 11 12" ], "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", "
ABCD
01234
15678
29101112
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 9 } ] }, { "cell_type": "code", "source": [ "df2 = df.apply(lambda x : x + 10)\n", "print(df2)\n", "df3 = df + 10\n", "print(df3)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "u84odJwIElIq", "outputId": "763dd972-c693-4794-f804-b6fcfd620d03" }, "execution_count": 5, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " A B C D\n", "0 11 12 13 14\n", "1 15 16 17 18\n", "2 19 20 21 22\n", " A B C D\n", "0 11 12 13 14\n", "1 15 16 17 18\n", "2 19 20 21 22\n" ] } ] }, { "cell_type": "code", "source": [ "df.at[1, 'A'] =np.square(df.at[1, 'A'])\n", "df.at[0, 'A'] = 20\n", "\n", "df" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "936Lyb9LJtp3", "outputId": "5c0e3b63-d8e2-46eb-f2b7-5ba88a0022c1" }, "execution_count": 10, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " A B C D\n", "0 20 2 3 4\n", "1 25 6 7 8\n", "2 9 10 11 12" ], "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", "
ABCD
020234
125678
29101112
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 10 } ] }, { "cell_type": "code", "source": [ "df2[\"A\"] = df2[\"A\"].apply(lambda x: x-2)\n", "df2" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "JEt_HqZ7FCfr", "outputId": "d5f69c28-ba3a-4357-98c4-7bc706015d1d" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " A B C D\n", "0 9 12 13 14\n", "1 13 16 17 18\n", "2 17 30 32 44" ], "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", "
ABCD
09121314
113161718
217303244
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 77 } ] }, { "cell_type": "code", "source": [ "df2 = df.apply(lambda x: np.square(x) if x.name in ['A','B'] else x)\n", "df2" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "VgE-eqtpFWIB", "outputId": "5ee5e765-9efa-4b81-a7d9-51890a85e4d4" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " A B C D\n", "0 400 4 3 4\n", "1 625 36 7 8\n", "2 81 400 22 34" ], "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", "
ABCD
0400434
16253678
2814002234
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 78 } ] }, { "cell_type": "code", "source": [ "# Using DataFrame.map() to Single Column\n", "df['A'] = df['A'].map(lambda A: A/2.)\n", "df\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "HQRurBUC95ug", "outputId": "a6497f96-0dfa-494f-a1a3-2b661718b7e7" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " A B C D\n", "0 10.0 2 3 4\n", "1 12.5 6 7 8\n", "2 4.5 20 22 34" ], "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", "
ABCD
010.0234
112.5678
24.5202234
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 79 } ] }, { "cell_type": "code", "source": [ "" ], "metadata": { "id": "Z_hrLX7tMYzU" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# Using DataFrame.assign() and Lambda\n", "df2 = df.assign(B=lambda df: df.B/2)\n", "df2" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "Z1pDlYPO95xL", "outputId": "02fc7b24-666d-4bb4-bce6-d59f62a1fb83" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " A B C D\n", "0 10.0 1.0 3 4\n", "1 12.5 3.0 7 8\n", "2 4.5 10.0 22 34" ], "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", "
ABCD
010.01.034
112.53.078
24.510.02234
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 81 } ] }, { "cell_type": "code", "source": [ "" ], "metadata": { "id": "lY83XOwQ95z5" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# importing pandas and numpy\n", "import pandas as pd\n", "import numpy as np\n", "\n", "# crete a sample dataframe\n", "data = pd.DataFrame({\n", " 'age' : [ 10, 22, 13, 21, 12, 11, 17],\n", " 'section' : [ 'A', 'B', 'C', 'B', 'B', 'A', 'A'],\n", " 'city' : [ 'Gurgaon', 'Delhi', 'Mumbai', 'Delhi', 'Mumbai', 'Delhi', 'Mumbai'],\n", " 'gender' : [ 'M', 'F', 'F', 'M', 'M', 'M', 'F'],\n", " 'favourite_color' : [ 'red', np.NAN, 'yellow', np.NAN, 'black', 'green', 'red']\n", "})\n", "\n", "# view the data\n", "data" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "id": "GteCCb_f952t", "outputId": "7a9af371-5e31-4fd6-bafa-2fe9f6492cd1" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " age section city gender favourite_color\n", "0 10 A Gurgaon M red\n", "1 22 B Delhi F NaN\n", "2 13 C Mumbai F yellow\n", "3 21 B Delhi M NaN\n", "4 12 B Mumbai M black\n", "5 11 A Delhi M green\n", "6 17 A Mumbai F red" ], "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
agesectioncitygenderfavourite_color
010AGurgaonMred
122BDelhiFNaN
213CMumbaiFyellow
321BDelhiMNaN
412BMumbaiMblack
511ADelhiMgreen
617AMumbaiFred
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 88 } ] }, { "cell_type": "code", "source": [ "len(data)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Fqs3VPr7Nhuc", "outputId": "1e4acc99-d15b-47b3-8ea9-1849a4f53727" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "7" ] }, "metadata": {}, "execution_count": 91 } ] }, { "cell_type": "code", "source": [ "for i in range(len(data)):\n", " data.at[i, 'age']= i * 10\n", "\n", "data" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "id": "hM2d889P955Z", "outputId": "183d4863-8eaf-4023-d77f-22d0396ef3fb" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " age section city gender favourite_color\n", "0 0 A Gurgaon M red\n", "1 10 B Delhi F NaN\n", "2 20 C Mumbai F yellow\n", "3 30 B Delhi M NaN\n", "4 40 B Mumbai M black\n", "5 50 A Delhi M green\n", "6 60 A Mumbai F red" ], "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
agesectioncitygenderfavourite_color
00AGurgaonMred
110BDelhiFNaN
220CMumbaiFyellow
330BDelhiMNaN
440BMumbaiMblack
550ADelhiMgreen
660AMumbaiFred
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 90 } ] }, { "cell_type": "code", "source": [ "" ], "metadata": { "id": "YVZQZvsB958z" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "" ], "metadata": { "id": "KnjMvwQA96Bd" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "" ], "metadata": { "id": "VeLqOt8l96Em" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "x = lambda a, b : a * b\n", "print(x(5, 6))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "aDqPyIqu5CuI", "outputId": "7d5d3782-2ee6-4605-8b6f-11d3e045e5c1" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "30\n" ] } ] }, { "cell_type": "code", "source": [ "x = lambda a, b, c : a + b + c\n", "print(x(5, 6, 2))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8uPKSARX5Cxa", "outputId": "d17f1751-33f6-4c6c-ca98-dddcd6ad46de" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "13\n" ] } ] }, { "cell_type": "code", "source": [ "def myfunc(n):\n", " return lambda a : a * n\n", "\n", "mydoubler = myfunc(2)\n", "\n", "print(mydoubler(11))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "nuxBpR5m5C2A", "outputId": "88533027-bb58-43c2-a99e-06a285fda9c3" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "22\n" ] } ] }, { "cell_type": "code", "source": [ "(lambda x: x + 1)(2)" ], "metadata": { "id": "lOLySgDB5C69", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "0e4bd7c4-f4cd-4176-deb1-a547f5ee07d1" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "3" ] }, "metadata": {}, "execution_count": 5 } ] }, { "cell_type": "code", "source": [ "add_one = lambda x: x + 1\n", "add_one(2)" ], "metadata": { "id": "fd-X7WcP5C90", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "e4ddb639-5785-4fe2-c7ee-7e33d44f48c0" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "3" ] }, "metadata": {}, "execution_count": 6 } ] }, { "cell_type": "code", "source": [ "full_name = lambda first, last: f'Full name: {first.title()} {last.title()}'\n", "full_name('guido', 'van rossum')" ], "metadata": { "id": "W_MPkFRf5DA0", "colab": { "base_uri": "https://localhost:8080/", "height": 36 }, "outputId": "376169c9-ed9e-4a7f-898f-dbb7332ab6d3" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Full name: Guido Van Rossum'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 7 } ] }, { "cell_type": "code", "source": [ "x =\"Cyrus Hi\"\n", "\n", "# lambda gets pass to print\n", "(lambda x : print(x))(x)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NJDzXvUDH-6-", "outputId": "7b5b2730-ab9f-45b4-96c2-2b2cd5c7fbf2" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Cyrus Hi\n" ] } ] }, { "cell_type": "code", "source": [ "# Program to filter out only the even items from a list\n", "my_list = [1, 5, 4, 6, 8, 11, 3, 12, 0]\n", "\n", "new_list = list(filter(lambda x: (x%2 == 0) , my_list))\n", "\n", "print(new_list)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NK3MLUGHH-49", "outputId": "0a0cc8b9-236a-469d-e064-237dbaaeb7f0" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[4, 6, 8, 12, 0]\n" ] } ] }, { "cell_type": "code", "source": [ "# Program to double each item in a list using map()\n", "\n", "my_list = [1, 5, 4, 6, 8, 11, 3, 12, 100]\n", "\n", "new_list = list(map(lambda x: x * 2 , my_list))\n", "\n", "print(new_list)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-DuIxrjWH-1z", "outputId": "94531726-2597-4ade-9582-f430bc64118b" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[2, 10, 8, 12, 16, 22, 6, 24, 200]\n" ] } ] }, { "cell_type": "code", "source": [ "sequences = [10,2,8,7,5,4,3,11,0, 1]\n", "filtered_result = filter (lambda x: x > 4, sequences) \n", "print(list(filtered_result))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "QW5ait4tH-zb", "outputId": "555cd116-b5ff-4943-8ff8-4901797d024b" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[10, 8, 7, 5, 11]\n" ] } ] }, { "cell_type": "code", "source": [ "square = lambda x : x * x\n", "square(5)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "uyUaoQ_qH-xB", "outputId": "c7f3c588-9143-4cc7-858a-64958e194558" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "25" ] }, "metadata": {}, "execution_count": 15 } ] }, { "cell_type": "code", "source": [ "greet = lambda name: print('Hello', name, '?') \n", "greet('Cyrus')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "IgFkJHyQH-t7", "outputId": "eb00ecd8-7a26-4fe8-92a6-2df9a9acddcb" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Hello Cyrus ?\n" ] } ] }, { "cell_type": "markdown", "source": [ "`Lambda` are generally used when a function is needed temporarily for a short period of time, often to be used inside another function such as filter, map and reduce.
\n", "Using `lambda` function, you can define a function and call it immediately at the end of definition. This can’t be done with def functions" ], "metadata": { "id": "qclRG0orMrj_" } }, { "cell_type": "code", "source": [ "print((lambda x: x if(x > 10) else 10)(5))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rn30HBAAMsVf", "outputId": "f0dc936e-4d83-44b7-a607-12f7e88ffe51" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "10\n" ] } ] } ] }