From 26343be02ee4d0f42b26ecedd72b6aefadb33513 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 20 Nov 2023 00:54:19 +0000 Subject: [PATCH] lab done --- your-code/challenge-1.ipynb | 312 ++++++++++++++++++++++++++++++++---- your-code/challenge-2.ipynb | 223 ++++++++++++++++++++++++-- 2 files changed, 490 insertions(+), 45 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index c1bb43d..c6c052d 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": 2, "metadata": {}, "outputs": [], "source": [ "# Import libraries\n", - "import pandas as pd" + "import pandas as pd\n", + "import scipy.stats as st" ] }, { @@ -38,11 +39,155 @@ }, { "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", + "pokemon = pd.read_csv(\"Pokemon.csv\")\n", + "pokemon.head()" ] }, { @@ -58,7 +203,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -75,9 +220,15 @@ " \"\"\"\n", " results = {}\n", "\n", - " # Your code here\n", - " \n", - " return results" + " for feature in features:\n", + " feature_s1 = s1[feature]\n", + " feature_s2 = s2[feature]\n", + " \n", + " comparisson = st.ttest_ind(feature_s1, feature_s2, equal_var = False, alternative = \"less\")\n", + " \n", + " results[feature] = comparisson[1]\n", + "\n", + " return results\n" ] }, { @@ -101,11 +252,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'HP': 5.013455854017642e-14,\n", + " 'Attack': 1.260186224618323e-16,\n", + " 'Defense': 2.4134992474596658e-11,\n", + " 'Sp. Atk': 7.757307056119906e-22,\n", + " 'Sp. Def': 1.1474663932026413e-15,\n", + " 'Speed': 5.245081559412255e-19,\n", + " 'Total': 4.678977167978723e-47}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "feature_s1 = pokemon[pokemon[\"Legendary\"] == False]\n", + "feature_s2 = pokemon[pokemon[\"Legendary\"] == True]\n", + "\n", + "t_test_features(feature_s1, feature_s2)" ] }, { @@ -117,11 +289,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'\\nThey do have some significantly differences, especially on \"HP\", \"Sp. Atk\" and \"Speed\".\\n'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your comment here" + "# Your comment here\n", + "\"\"\"\n", + "They do have some significantly differences, especially on \"HP\", \"Sp. Atk\" and \"Speed\".\n", + "\"\"\"" ] }, { @@ -133,11 +319,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'HP': 0.07275848917109812,\n", + " 'Attack': 0.8763902051639113,\n", + " 'Defense': 0.2838855505862713,\n", + " 'Sp. Atk': 0.9383391701144781,\n", + " 'Sp. Def': 0.09414936146322876,\n", + " 'Speed': 0.9988036703134393,\n", + " 'Total': 0.7184311046029161}" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "generation_s1 = pokemon[pokemon[\"Generation\"] == 1]\n", + "generation_s2 = pokemon[pokemon[\"Generation\"] == 2]\n", + "\n", + "t_test_features(generation_s1, generation_s2)" ] }, { @@ -153,7 +360,11 @@ "metadata": {}, "outputs": [], "source": [ - "# Your comment here" + "# Your comment here\n", + "\"\"\"\n", + "There is no strong evidence to suggest a significant difference between Generation 1 and Generation 2 Pokémon for \n", + "these attributes. \n", + "\"\"\"" ] }, { @@ -165,11 +376,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'HP': 0.943428050723103,\n", + " 'Attack': 0.9999253371092702,\n", + " 'Defense': 0.9999999860107298,\n", + " 'Sp. Atk': 0.9999306189170717,\n", + " 'Sp. Def': 0.9999463469453275,\n", + " 'Speed': 0.9878914835909045,\n", + " 'Total': 0.9999999442147175}" + ] + }, + "execution_count": 11, + "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 +417,11 @@ "metadata": {}, "outputs": [], "source": [ - "# Your comment here" + "# Your comment here\n", + "\"\"\"\n", + "There is no strong evidence to suggest a significant difference between Pokémon with single types and those with \n", + "two types for any of the attributes being tested. \n", + "\"\"\"" ] }, { @@ -199,11 +435,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "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}\")" ] }, { @@ -219,13 +469,17 @@ "metadata": {}, "outputs": [], "source": [ - "# Your comment here" + "# Your comment here\n", + "\"\"\" \n", + "Because of the low values of \"Attack\" vs. \"Defense\" we cannot reject the null hypothesis that the values are different,\n", + "but because of the higher values of \"Sp. Atk\" vs. \"Sp. Def\", we can reject it.\n", + "\"\"\"" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -239,7 +493,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.11.4" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index 1f0e335..f73b3fe 100755 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -17,21 +17,166 @@ }, { "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": 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": [ - "# Load the data:\n" + "# Load the data:\n", + "pokemon = pd.read_csv(\"Pokemon.csv\")\n", + "pokemon.head()" ] }, { @@ -58,12 +203,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "18" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here\n", - "\n", + "unique_types = pd.concat([pokemon[\"Type 1\"], pokemon[\"Type 2\"]]).dropna().unique()\n", + "unique_types\n", "\n", "len(unique_types) # you should see 19" ] @@ -85,13 +242,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "18" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "pokemon_totals = []\n", "\n", "# Your code here\n", + "pokemon_totals = []\n", + "\n", + "for pokemon_type in unique_types:\n", + " type_totals = pokemon[pokemon[\"Type 1\"] == pokemon_type][\"Total\"]\n", + " pokemon_totals.append(type_totals)\n", + "\n", + "len(pokemon_totals)\n", "\n", "len(pokemon_totals) # you should see 18" ] @@ -111,11 +286,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "F_onewayResult(statistic=4.63876748166055, pvalue=2.077215448842098e-09)" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "st.f_oneway(*pokemon_totals)" ] }, { @@ -131,13 +318,17 @@ "metadata": {}, "outputs": [], "source": [ - "# Your comment here" + "# Your comment here\n", + "\"\"\"\n", + "The p-value suggests that there is 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", + "\"\"\"" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -151,7 +342,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.11.4" } }, "nbformat": 4,