From 9ddab441898711bc9267e00efc79b970593d04da Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 21 Aug 2023 00:51:09 +0100 Subject: [PATCH] lab done --- your-code/challenge-1.ipynb | 415 +++++++++++++++++++++++++++++++++--- your-code/challenge-2.ipynb | 198 +++++++++++++++-- 2 files changed, 568 insertions(+), 45 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index c1bb43d..89a6433 100755 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -19,12 +19,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Import libraries\n", - "import pandas as pd" + "import pandas as pd\n", + "import scipy.stats as st" ] }, { @@ -38,11 +39,266 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \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
..........................................
795719DiancieRockFairy60050100150100150506True
796719DiancieMega DiancieRockFairy700501601101601101106True
797720HoopaHoopa ConfinedPsychicGhost6008011060150130706True
798720HoopaHoopa UnboundPsychicDark6808016060170130806True
799721VolcanionFireWater6008011012013090706True
\n", + "

800 rows × 13 columns

\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", + "795 719 Diancie Rock Fairy 600 50 100 150 \n", + "796 719 DiancieMega Diancie Rock Fairy 700 50 160 110 \n", + "797 720 HoopaHoopa Confined Psychic Ghost 600 80 110 60 \n", + "798 720 HoopaHoopa Unbound Psychic Dark 680 80 160 60 \n", + "799 721 Volcanion Fire Water 600 80 110 120 \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 \n", + ".. ... ... ... ... ... \n", + "795 100 150 50 6 True \n", + "796 160 110 110 6 True \n", + "797 150 130 70 6 True \n", + "798 170 130 80 6 True \n", + "799 130 90 70 6 True \n", + "\n", + "[800 rows x 13 columns]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "pokemon= pd.read_csv(\"Pokemon.csv\")\n", + "pokemon" ] }, { @@ -58,7 +314,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ @@ -76,7 +332,10 @@ " results = {}\n", "\n", " # Your code here\n", - " \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" ] }, @@ -101,11 +360,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'HP': 1.0026911708035284e-13,\n", + " 'Attack': 2.520372449236646e-16,\n", + " 'Defense': 4.826998494919331e-11,\n", + " 'Sp. Atk': 1.5514614112239816e-21,\n", + " 'Sp. Def': 2.2949327864052826e-15,\n", + " 'Speed': 1.0490163118824507e-18,\n", + " 'Total': 9.357954335957444e-47}" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "t_test_features(pokemon[pokemon[\"Legendary\"]],pokemon[pokemon[\"Legendary\"]==False])\n" ] }, { @@ -117,11 +394,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'it appears that there are significant differences between the stats of Legendary and non-Legendary Pokémon in each of the \\nspecified features. the p-value are extremly small indicate the strong evidence aganist the null hypothesis'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your comment here" + "# Your comment here\n", + "\"\"\"it appears that there are significant differences between the stats of Legendary and non-Legendary Pokémon in each of the \n", + "specified features. the p-value are extremly small indicate the strong evidence aganist the null hypothesis\"\"\" " ] }, { @@ -133,11 +423,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'HP': 0.14551697834219626,\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": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "t_test_features(pokemon[pokemon[\"Generation\"]==1],pokemon[pokemon[\"Generation\"]==2])\n" ] }, { @@ -153,7 +461,9 @@ "metadata": {}, "outputs": [], "source": [ - "# Your comment here" + "# Your comment here\n", + "\"\"\"it appears that there are no significant differences in several stats between these two generations, \n", + "except for the \"Speed\" stat.\"\"\"" ] }, { @@ -165,11 +475,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'HP': 0.11314389855379413,\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.1157056505229964e-07}" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "type_s1 = pokemon[~pokemon[\"Type 2\"].isnull()]\n", + "type_s2 = pokemon[pokemon[\"Type 2\"].isnull()]\n", + "\n", + "t_test_features(type_s1, type_s2)" ] }, { @@ -185,7 +516,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your comment here" + "# Your comment here\n", + "\"\"\" based on the output there are significant differences between these two groups\"\"\"" ] }, { @@ -199,11 +531,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Difference Attack vs. Defense: TtestResult(statistic=4.325566393330478, pvalue=1.7140303479358558e-05, df=799)\n", + "Difference Sp. Atk vs. Sp. Def: TtestResult(statistic=0.853986188453353, pvalue=0.3933685997548122, df=799)\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "attack_defense = st.ttest_rel(pokemon[\"Attack\"], pokemon[\"Defense\"])\n", + "sp_atk_def = st.ttest_rel(pokemon[\"Sp. Atk\"], pokemon[\"Sp. Def\"])\n", + "\n", + "print(f\"Difference Attack vs. Defense: {attack_defense}\")\n", + "print(f\"Difference Sp. Atk vs. Sp. Def: {sp_atk_def}\")" ] }, { @@ -215,11 +561,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "' There is a significant difference between the means of Attack and Defense stats.\\nThere is no significant difference between the means of Special Attack and Special Defense stats.'" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your comment here" + "# Your comment here\n", + "\"\"\" There is a significant difference between the means of Attack and Defense stats.\n", + "There is no significant difference between the means of Special Attack and Special Defense stats.\"\"\"" ] } ], @@ -239,7 +598,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 index 1f0e335..9835311 100755 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -17,21 +17,130 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Import libraries\n", - "import pandas as pd" + "import pandas as pd\n", + "import scipy.stats as st" ] }, { "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", + "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison3184549496565451False
12IvysaurGrassPoison4056062638080601False
23VenusaurGrassPoison525808283100100801False
\n", + "
" + ], + "text/plain": [ + " # Name Type 1 Type 2 Total HP Attack Defense Sp. Atk Sp. Def \\\n", + "0 1 Bulbasaur Grass Poison 318 45 49 49 65 65 \n", + "1 2 Ivysaur Grass Poison 405 60 62 63 80 80 \n", + "2 3 Venusaur Grass Poison 525 80 82 83 100 100 \n", + "\n", + " Speed Generation Legendary \n", + "0 45 1 False \n", + "1 60 1 False \n", + "2 80 1 False " + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Load the data:\n" + "# Load the data:\n", + "pokemon = pd.read_csv(\"Pokemon.csv\")\n", + "pokemon.head(3)" ] }, { @@ -58,12 +167,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "19" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here\n", - "\n", + "unique_types = pd.concat([pokemon[\"Type 1\"], pokemon[\"Type 2\"]]).unique()\n", + "unique_types\n", "\n", "len(unique_types) # you should see 19" ] @@ -85,13 +206,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "18" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "pokemon_totals = []\n", "\n", "# Your code here\n", + "for i in unique_types:\n", + " if type(i) == str: \n", + " total = pokemon['Total'][(pokemon['Type 1'] == i) | (pokemon['Type 2'] == i)]\n", + " pokemon_totals.append(total)\n", "\n", "len(pokemon_totals) # you should see 18" ] @@ -111,11 +247,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "F_onewayResult(statistic=6.6175382960055344, pvalue=2.6457458815984803e-15)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "import scipy.stats as st\n", + "st.f_oneway(*pokemon_totals)" ] }, { @@ -127,11 +276,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'\\nThe result suggest there is a strong evidence to reject the null hypothesis. \\nThe null hypothesis assumes that there is no significant difference between the means of the groups.\\n'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your comment here" + "# Your comment here\n", + "\"\"\"\n", + "The result suggest there is a strong evidence to reject the null hypothesis. \n", + "The null hypothesis assumes that there is no significant difference between the means of the groups.\n", + "\"\"\"" ] } ], @@ -151,7 +315,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.9" } }, "nbformat": 4,