From 05a39fe8e5cef2fcdce7699bd6816a99f15bbf6a Mon Sep 17 00:00:00 2001 From: Ricardo Mendes Date: Sat, 20 May 2023 18:52:40 +0100 Subject: [PATCH] msg --- your-code/challenge-1.ipynb | 324 +++++++++++++++++++++++++++++++++--- your-code/challenge-2.ipynb | 222 ++++++++++++++++++++++-- 2 files changed, 502 insertions(+), 44 deletions(-) mode change 100755 => 100644 your-code/challenge-1.ipynb mode change 100755 => 100644 your-code/challenge-2.ipynb diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb old mode 100755 new mode 100644 index c1bb43d..bfb7d67 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -19,12 +19,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Import libraries\n", - "import pandas as pd" + "import pandas as pd\n", + "import scipy.stats as st" ] }, { @@ -38,11 +39,156 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison3184549496565451False
12IvysaurGrassPoison4056062638080601False
23VenusaurGrassPoison525808283100100801False
33VenusaurMega VenusaurGrassPoison62580100123122120801False
44CharmanderFireNaN3093952436050651False
\n", + "
" + ], + "text/plain": [ + " # Name Type 1 Type 2 Total HP Attack Defense \\\n", + "0 1 Bulbasaur Grass Poison 318 45 49 49 \n", + "1 2 Ivysaur Grass Poison 405 60 62 63 \n", + "2 3 Venusaur Grass Poison 525 80 82 83 \n", + "3 3 VenusaurMega Venusaur Grass Poison 625 80 100 123 \n", + "4 4 Charmander Fire NaN 309 39 52 43 \n", + "\n", + " Sp. Atk Sp. Def Speed Generation Legendary \n", + "0 65 65 45 1 False \n", + "1 80 80 60 1 False \n", + "2 100 100 80 1 False \n", + "3 122 120 80 1 False \n", + "4 60 50 65 1 False " + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "\n", + "data = pd.read_csv(\"Pokemon.csv\")\n", + "data.head()" ] }, { @@ -58,7 +204,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -76,8 +222,13 @@ " results = {}\n", "\n", " # Your code here\n", + " for feature in features:\n", + " stat, p_value = st.ttest_ind(s1[feature], s2[feature], equal_var = False)\n", + " results[feature] = p_value\n", + " \n", + " return results\n", " \n", - " return results" + " " ] }, { @@ -101,11 +252,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'HP': 1.0026911708035284e-13,\n", + " 'Attack': 2.520372449236646e-16,\n", + " 'Defense': 4.8269984949193316e-11,\n", + " 'Sp. Atk': 1.5514614112239812e-21,\n", + " 'Sp. Def': 2.2949327864052826e-15,\n", + " 'Speed': 1.049016311882451e-18,\n", + " 'Total': 9.357954335957446e-47}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "s1 = data[data[\"Legendary\"]==True]\n", + "s2 = data[data[\"Legendary\"]==False]\n", + "\n", + "t_test_features(s1,s2)" ] }, { @@ -117,11 +290,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'\\nWe can reject the null hypothesis for all features. Meaning that Legendary and non-Legendary pokemons have significantly different stats on each feature.\\n'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your comment here" + "# Your comment here\n", + "\n", + "\"\"\"\n", + "We can reject the null hypothesis for all features. \n", + "Meaning that Legendary and non-Legendary pokemons have significantly different stats on each feature.\n", + "\"\"\"" ] }, { @@ -133,11 +322,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'HP': 0.14551697834219623,\n", + " 'Attack': 0.24721958967217725,\n", + " 'Defense': 0.5677711011725426,\n", + " 'Sp. Atk': 0.12332165977104388,\n", + " 'Sp. Def': 0.18829872292645752,\n", + " 'Speed': 0.00239265937312135,\n", + " 'Total': 0.5631377907941676}" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "s1 = data[data[\"Generation\"]==1]\n", + "s2 = data[data[\"Generation\"]==2]\n", + "\n", + "t_test_features(s1,s2)" ] }, { @@ -153,7 +364,12 @@ "metadata": {}, "outputs": [], "source": [ - "# Your comment here" + "# Your comment here\n", + "\n", + "\"\"\"\n", + "We can only reject the null hypothesis for the feature \"Speed\".\n", + "Again, Generation 1 pokemons and Generation 2 pokemons have significantly different stats only on \"Speed\"\n", + "\"\"\"" ] }, { @@ -165,11 +381,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'HP': 0.11314389855379414,\n", + " 'Attack': 0.00014932578145948305,\n", + " 'Defense': 2.7978540411514693e-08,\n", + " 'Sp. Atk': 0.00013876216585667907,\n", + " 'Sp. Def': 0.00010730610934512779,\n", + " 'Speed': 0.02421703281819093,\n", + " 'Total': 1.1157056505229961e-07}" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "s1 = data[data[\"Type 2\"].isna()]\n", + "s2 = data[data[\"Type 2\"].notna()]\n", + "\n", + "t_test_features(s1,s2)\n" ] }, { @@ -185,7 +423,12 @@ "metadata": {}, "outputs": [], "source": [ - "# Your comment here" + "# Your comment here\n", + "\"\"\"\n", + "We can reject the null hypothesis for all features except \"HP\".\n", + "Meaning tha single-type pokemons and double-type pokemon have significantly different stats \n", + "on each feature, except \"HP\"\n", + "\"\"\"" ] }, { @@ -199,11 +442,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Attack vs Defense: 1.7140303479358558e-05\n", + "Sp. Atk vs Sp. Def: 0.3933685997548122\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "# We are comparing the same population so we use t-test\n", + "\n", + "s1 = data[\"Attack\"]\n", + "s2 = data[\"Defense\"]\n", + "\n", + "stat, p_value = st.ttest_rel(s1, s2)\n", + "print(\"Attack vs Defense: \",p_value)\n", + "\n", + "s1 = data[\"Sp. Atk\"]\n", + "s2 = data[\"Sp. Def\"]\n", + "\n", + "stat, p_value = st.ttest_rel(s1, s2)\n", + "print(\"Sp. Atk vs Sp. Def: \",p_value)\n" ] }, { @@ -219,13 +485,17 @@ "metadata": {}, "outputs": [], "source": [ - "# Your comment here" + "# Your comment here\n", + "\n", + "\"\"\"\n", + "We can reject the null hypothesis for \"Attack vs Defense\" but cannot reject for \"Sp. Atk vs Sp. Def\".\n", + "\"\"\"" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -239,7 +509,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb old mode 100755 new mode 100644 index 1f0e335..1cfd95c --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -17,7 +17,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -27,11 +27,156 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison3184549496565451False
12IvysaurGrassPoison4056062638080601False
23VenusaurGrassPoison525808283100100801False
33VenusaurMega VenusaurGrassPoison62580100123122120801False
44CharmanderFireNaN3093952436050651False
\n", + "
" + ], + "text/plain": [ + " # Name Type 1 Type 2 Total HP Attack Defense \\\n", + "0 1 Bulbasaur Grass Poison 318 45 49 49 \n", + "1 2 Ivysaur Grass Poison 405 60 62 63 \n", + "2 3 Venusaur Grass Poison 525 80 82 83 \n", + "3 3 VenusaurMega Venusaur Grass Poison 625 80 100 123 \n", + "4 4 Charmander Fire NaN 309 39 52 43 \n", + "\n", + " Sp. Atk Sp. Def Speed Generation Legendary \n", + "0 65 65 45 1 False \n", + "1 80 80 60 1 False \n", + "2 100 100 80 1 False \n", + "3 122 120 80 1 False \n", + "4 60 50 65 1 False " + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Load the data:\n" + "# Load the data:\n", + "\n", + "data = pd.read_csv(\"Pokemon.csv\")\n", + "data.head()" ] }, { @@ -58,14 +203,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "19" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here\n", "\n", - "\n", - "len(unique_types) # you should see 19" + "unique_types = list(set(data[\"Type 1\"]).union(set(data[\"Type 2\"])))\n", + "len(unique_types)" ] }, { @@ -85,15 +241,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "18" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "pokemon_totals = []\n", "\n", "# Your code here\n", "\n", - "len(pokemon_totals) # you should see 18" + "for pokemon_type in unique_types:\n", + " if type(pokemon_type) is str:\n", + " pokemon_totals.append(data[(data[\"Type 1\"]==pokemon_type) | (data[\"Type 2\"]==pokemon_type)][\"Total\"])\n", + "\n", + "len(pokemon_totals)" ] }, { @@ -111,11 +282,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "F_onewayResult(statistic=6.6175382960055344, pvalue=2.6457458815984803e-15)" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "import scipy.stats as st\n", + "\n", + "st.f_oneway(*pokemon_totals)" ] }, { @@ -131,13 +316,16 @@ "metadata": {}, "outputs": [], "source": [ - "# Your comment here" + "# Your comment here\n", + "\"\"\"\n", + "There is a significant difference in the Totals of each pokemon type\n", + "\"\"\"" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -151,7 +339,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.9" } }, "nbformat": 4,