diff --git a/your-code/Q3.ipynb b/your-code/Q3.ipynb index 75055ac..b7b8fc0 100644 --- a/your-code/Q3.ipynb +++ b/your-code/Q3.ipynb @@ -13,7 +13,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -22,7 +22,7 @@ "[2, 12, 30]" ] }, - "execution_count": 11, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -32,6 +32,7 @@ "\n", "def multiply(num_tuple):\n", " return num_tuple[0]*num_tuple[1]\n", + "\n", "list(map(multiply, items))" ] }, @@ -85,12 +86,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 5]\n" + ] + } + ], "source": [ "numbers = [1, 4, -1, -100, 0, 5, -99]\n", "\n", + "positive_numbers = list(filter(lambda x: x > 0, numbers))\n", + "print(positive_numbers)\n", + "\n", "# Enter your code below" ] }, @@ -113,15 +125,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 142, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'good morning everyone'" + ] + }, + "execution_count": 142, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import langdetect\n", "from functools import reduce\n", - "words = ['good morning', '早上好', 'доброго', 'おはようございます', 'everyone', '大家', 'каждый', 'みんな']\n", "\n", - "# Enter your code below" + "words = ['good morning', '早上好', 'доброго', 'おはようございます', \n", + " 'everyone', '大家', 'каждый', 'みんな'] \n", + "lista = []\n", + "for palabra in words:\n", + " if (langdetect.detect(palabra)=='en') == True:\n", + " lista.append(palabra)\n", + " else:\n", + " continue\n", + "\n", + "reduce(lambda x, y: x + ' ' + y, lista)\n", + "\n" ] }, { @@ -199,7 +231,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.8.8" } }, "nbformat": 4, diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 0239d17..6925491 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -31,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -54,7 +54,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -73,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -90,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -100,7 +100,7 @@ "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;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[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[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m: " ] } @@ -113,7 +113,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -146,9 +146,20 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def divisible2(iterator):\n", " # This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", @@ -159,7 +170,18 @@ " # Sample Output: 2\n", " \n", " # Your code here:\n", - " " + " \n", + " for i in iterator:\n", + " if i % 2 == 0:\n", + " d = i\n", + " break\n", + " else:\n", + " d = 0\n", + " \n", + "\n", + " return d\n", + "\n", + "divisible2(iter([1,2,4,6]))" ] }, { @@ -173,15 +195,26 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def firstn(n):\n", - " number = 0\n", - " while number < n:\n", - " yield number\n", - " number = number + 1" + " number = 0\n", + " while number < n:\n", + " yield number\n", + " number = number + 1" ] }, { @@ -193,7 +226,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -224,9 +257,43 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [], + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n", + "6\n", + "8\n", + "10\n", + "12\n", + "14\n", + "16\n", + "18\n", + "20\n", + "22\n", + "24\n", + "26\n", + "28\n", + "30\n", + "32\n", + "34\n", + "36\n", + "38\n", + "40\n", + "42\n", + "44\n", + "46\n", + "48\n" + ] + } + ], "source": [ "def even_iterator(n):\n", " # This function produces an iterator containing all even numbers between 0 and n\n", @@ -237,7 +304,20 @@ " # Sample Output: iter([0, 2, 4])\n", " \n", " # Your code here:\n", - " " + " \n", + " number = 0\n", + " while number < n:\n", + " if number % 2 == 0: # Is it even?\n", + " yield number\n", + " number = number + 1\n", + " else:\n", + " number = number + 1\n", + " \n", + " \n", + "iterator = even_iterator(50)\n", + "\n", + "for i in iterator:\n", + " print(i)" ] }, { @@ -253,7 +333,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -270,12 +350,100 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
sepal_lengthsepal_widthpetal_lengthpetal_widthiris_type
05.13.51.40.2Iris-setosa
14.93.01.40.2Iris-setosa
24.73.21.30.2Iris-setosa
34.63.11.50.2Iris-setosa
45.03.61.40.2Iris-setosa
\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width iris_type\n", + "0 5.1 3.5 1.4 0.2 Iris-setosa\n", + "1 4.9 3.0 1.4 0.2 Iris-setosa\n", + "2 4.7 3.2 1.3 0.2 Iris-setosa\n", + "3 4.6 3.1 1.5 0.2 Iris-setosa\n", + "4 5.0 3.6 1.4 0.2 Iris-setosa" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "iris.head()\n", + "# iris" ] }, { @@ -287,12 +455,29 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 23, "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": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "# iris['sepal_length'].mean()\n", + "\n", + "iris.mean()" ] }, { @@ -304,12 +489,28 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "sepal_length 0.828066\n", + "sepal_width 0.433594\n", + "petal_length 1.764420\n", + "petal_width 0.763161\n", + "dtype: float64" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "# np.std(iris)\n", + "iris.std()\n" ] }, { @@ -321,12 +522,43 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sepal_length float64\n", + "sepal_width float64\n", + "petal_length float64\n", + "petal_width float64\n", + "dtype: object\n" + ] + } + ], "source": [ "# Your code here:\n", - "\n" + "# for col in iris.columns:\n", + "# # print(iris[col].dtypes)\n", + "# if iris[col].dtypes == 'float64':\n", + "# # print(iris[col].dtypes)\n", + "\n", + "# # iris.columns[0]\n", + "\n", + "iris_numeric = iris.select_dtypes(include='float64')\n", + "\n", + "print(iris_numeric.dtypes)\n", + "# print(iris.dtypes)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "# iris.select_dtypes(include='float64')" ] }, { @@ -338,7 +570,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -351,7 +583,9 @@ " # Sample Output: 0.393701\n", " \n", " # Your code here:\n", - " " + " pulg = x * 0.393701 \n", + " \n", + " return pulg" ] }, { @@ -363,12 +597,290 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], + "execution_count": 17, + "metadata": { + "collapsed": true + }, + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \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
...............
1456.73.05.22.3
1466.32.55.01.9
1476.53.05.22.0
1486.23.45.42.3
1495.93.05.11.8
\n", + "

150 rows × 4 columns

\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\n", + ".. ... ... ... ...\n", + "145 6.7 3.0 5.2 2.3\n", + "146 6.3 2.5 5.0 1.9\n", + "147 6.5 3.0 5.2 2.0\n", + "148 6.2 3.4 5.4 2.3\n", + "149 5.9 3.0 5.1 1.8\n", + "\n", + "[150 rows x 4 columns]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iris_numeric" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \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.078740
11.9291351.1811030.5511810.078740
21.8503951.2598430.5118110.078740
31.8110251.2204730.5905520.078740
41.9685051.4173240.5511810.078740
...............
1452.6377971.1811032.0472450.905512
1462.4803160.9842531.9685050.748032
1472.5590571.1811032.0472450.787402
1482.4409461.3385832.1259850.905512
1492.3228361.1811032.0078750.708662
\n", + "

150 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width\n", + "0 2.007875 1.377954 0.551181 0.078740\n", + "1 1.929135 1.181103 0.551181 0.078740\n", + "2 1.850395 1.259843 0.511811 0.078740\n", + "3 1.811025 1.220473 0.590552 0.078740\n", + "4 1.968505 1.417324 0.551181 0.078740\n", + ".. ... ... ... ...\n", + "145 2.637797 1.181103 2.047245 0.905512\n", + "146 2.480316 0.984253 1.968505 0.748032\n", + "147 2.559057 1.181103 2.047245 0.787402\n", + "148 2.440946 1.338583 2.125985 0.905512\n", + "149 2.322836 1.181103 2.007875 0.708662\n", + "\n", + "[150 rows x 4 columns]" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "iris_inch = cm_to_in(iris_numeric)\n", + "iris_inch" ] }, { @@ -380,12 +892,146 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [], + "execution_count": 21, + "metadata": { + "collapsed": true + }, + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \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
...............
1458.75.07.24.3
1468.34.57.03.9
1478.55.07.24.0
1488.25.47.44.3
1497.95.07.13.8
\n", + "

150 rows × 4 columns

\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\n", + ".. ... ... ... ...\n", + "145 8.7 5.0 7.2 4.3\n", + "146 8.3 4.5 7.0 3.9\n", + "147 8.5 5.0 7.2 4.0\n", + "148 8.2 5.4 7.4 4.3\n", + "149 7.9 5.0 7.1 3.8\n", + "\n", + "[150 rows x 4 columns]" + ] + }, + "execution_count": 21, + "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 +1039,11 @@ " # Output: numeric value\n", " \n", " # Your code here:\n", - " " + " y = x + error\n", + " return y\n", + "\n", + "iris_constant = add_constant(iris_numeric)\n", + "iris_constant" ] }, { @@ -407,12 +1057,116 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[7.9, 4.4, 6.9, 2.5]" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "def get_the_max(df):\n", + " # This function returns the maximum value of a dataset\n", + " # input: DataFrame\n", + " # output: List of maximum value of each column\n", + " \n", + " cols = df.columns\n", + " out1 = []\n", + " for col in cols:\n", + " out1.append(max(df[col]))\n", + " \n", + " return out1\n", + "\n", + "get_the_max(iris_numeric)\n", + "# iris.columns" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5.0, 5.0, 5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5.0, 5.5, 4.9, 4.4, 5.1, 5.0, 4.5, 4.4, 5.0, 5.1, 4.8, 5.1, 4.6, 5.3, 5.0, 7.0, 6.4, 6.9, 5.5, 6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 5.0, 5.9, 6.0, 6.1, 5.6, 6.7, 5.6, 5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7, 6.0, 5.7, 5.5, 5.5, 5.8, 6.0, 5.4, 6.0, 6.7, 6.3, 5.6, 5.5, 5.5, 6.1, 5.8, 5.0, 5.6, 5.7, 5.7, 6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3, 6.5, 7.6, 4.9, 7.3, 6.7, 7.2, 6.5, 6.4, 6.8, 5.7, 5.8, 6.4, 6.5, 7.7, 7.7, 6.0, 6.9, 5.6, 7.7, 6.3, 6.7, 7.2, 6.2, 6.1, 6.4, 7.2, 7.4, 7.9, 6.4, 6.3, 6.1, 7.7, 6.3, 6.4, 6.0, 6.9, 6.7, 6.9, 5.8, 6.8, 6.7, 6.7, 6.3, 6.5, 6.2, 5.9]\n" + ] + } + ], + "source": [ + "# maximum value for each ROW \n", + "def get_the_max(df):\n", + " out2 = []\n", + " for i in range(len(df)):\n", + " out2.append(max(df.iloc[i]))\n", + " return out2\n", + "\n", + "print(get_the_max(iris_numeric)) \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [], + "source": [ + "a = get_the_max(iris_numeric)\n", + "b = []\n", + "for item in iris_numeric['sepal_length']:\n", + " b.append(item)\n", + "a == b" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a == b" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5.1" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iris_numeric['sepal_length'][0]" ] }, { @@ -424,14 +1178,39 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The data frame has 150 rows, and 5 columns\n" + ] + } + ], "source": [ "# Your code here:\n", - "\n" + "# cols = iris.columns\n", + "\n", + "total_length = iris.shape[0]\n", + "total_with = iris.shape[1]\n", + "string1 = 'The data frame has'\n", + "string2 = 'rows, and'\n", + "string3 = 'columns'\n", + "\n", + "print(f'{string1} {total_length} {string2} {total_with} {string3}')\n", + "\n", + "# len(iris[cols[0]])" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -456,7 +1235,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.8" } }, "nbformat": 4, diff --git a/your-code/main.ipynb.json b/your-code/main.ipynb.json new file mode 100644 index 0000000..90ce28b --- /dev/null +++ b/your-code/main.ipynb.json @@ -0,0 +1,524 @@ +{ + "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": 1, + "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": 2, + "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": { + "scrolled": true + }, + "outputs": [ + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# After we have iterated through all elements, we will get a StopIteration Error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mStopIteration\u001b[0m: " + ] + } + ], + "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": { + "scrolled": true + }, + "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 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": 9, + "metadata": { + "scrolled": true + }, + "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", + "number = [1, 2, 3]\n", + "\n", + " x%2==0\n", + "\n", + "\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'div' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdiv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'div' is not defined" + ] + } + ], + "source": [ + "print(div)" + ] + }, + { + "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": null, + "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": null, + "metadata": { + "scrolled": false + }, + "outputs": [], + "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": null, + "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", + " \n", + " for x in range (0, n):\n", + " if x%2 == 0:\n", + " yield x\n", + " \n", + " \n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "even_iterator(5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "list(even_iterator(5))" + ] + }, + { + "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": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\n", + "columns.head()" + ] + }, + { + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\n" + ] + }, + { + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\n" + ] + }, + { + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\n" + ] + }, + { + "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": null, + "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", + " " + ] + }, + { + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\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": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Define constant below:\n", + "\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", + " " + ] + }, + { + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\n" + ] + }, + { + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\n" + ] + }, + { + "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 +}