From 7ef19f8defb130b309699d0b503c380ec182252a Mon Sep 17 00:00:00 2001 From: marsalmorera Date: Thu, 14 Nov 2024 15:17:10 +0100 Subject: [PATCH] up2date --- your-code/main.ipynb | 830 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 790 insertions(+), 40 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index cdc1acb..96593e4 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -14,12 +14,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "# import numpy and pandas\n", - "\n" + "# Libraries 📚\n", + "import pandas as pd\n", + "import numpy as np \n", + "import scipy.stats as st" ] }, { @@ -39,9 +41,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Run this code:\n", - "\n", - "pokemon = pd.read_csv('../pokemon.csv')" + "df = pd.read_csv('../pokemon.csv')" ] }, { @@ -53,12 +53,173 @@ }, { "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", - "\n" + "df.head(5)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(800, 13)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.shape" ] }, { @@ -70,12 +231,63 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "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", + "
Legendarycount
0False735
1True65
\n", + "
" + ], + "text/plain": [ + " Legendary count\n", + "0 False 735\n", + "1 True 65" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n", - "\n" + "Legen = df.Legendary.value_counts()\n", + "Legen.reset_index()" ] }, { @@ -91,8 +303,52 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n", - "\n" + "legendary = df[df[\"Legendary\"] == True][\"Total\"].dropna()\n", + "nonlegendary = df[df[\"Legendary\"] == False][\"Total\"].dropna()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mean Legendaries: 637.38\n", + "Std Legendaries: 60.94\n" + ] + } + ], + "source": [ + "mean_legendary = legendary.mean().round(2)\n", + "std_legendary = round(legendary.std(), 2)\n", + "\n", + "print(f'Mean Legendaries: {mean_legendary}')\n", + "print(f'Std Legendaries: {std_legendary}')" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mean no legendaries: 417.21\n", + "Std no legendaries: 106.76\n" + ] + } + ], + "source": [ + "mean_nonlegendary = nonlegendary.mean().round(2)\n", + "std_nonlegendary = round(nonlegendary.std(), 2)\n", + "\n", + "print(f'Mean no legendaries: {mean_nonlegendary}')\n", + "print(f'Std no legendaries: {std_nonlegendary}')" ] }, { @@ -106,12 +362,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Test Statistic (t): 25.83\n", + "P-Value: 0.0000\n", + "\n", + "Reject the Null Hypothesis: ... \n" + ] + } + ], "source": [ - "# Your code here:\n", - "\n" + "# Perform two-sample t-test for independent samples\n", + "# I'm not sure which is Null Hypothesis. \n", + "t_stat, p_value = st.ttest_ind(legendary, nonlegendary, equal_var=False) # equal_var True for Welch's test (more robus, relies less on variance)\n", + "print(f\"Test Statistic (t): {t_stat:.2f}\")\n", + "print(f\"P-Value: {p_value:.4f}\")\n", + "print()\n", + "\n", + "# Significance level\n", + "alpha = 0.05\n", + "\n", + "# Decision-Making\n", + "if p_value > alpha:\n", + " print(\"Fail to Reject the Null Hypothesis: ...\")\n", + "else:\n", + " print(\"Reject the Null Hypothesis: ... \")" ] }, { @@ -140,12 +420,159 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "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", + "
Type 1count
0Water112
1Normal98
2Grass70
3Bug69
4Psychic57
5Fire52
6Electric44
7Rock44
8Dragon32
9Ground32
10Ghost32
11Dark31
12Poison28
13Steel27
14Fighting27
15Ice24
16Fairy17
17Flying4
\n", + "
" + ], + "text/plain": [ + " Type 1 count\n", + "0 Water 112\n", + "1 Normal 98\n", + "2 Grass 70\n", + "3 Bug 69\n", + "4 Psychic 57\n", + "5 Fire 52\n", + "6 Electric 44\n", + "7 Rock 44\n", + "8 Dragon 32\n", + "9 Ground 32\n", + "10 Ghost 32\n", + "11 Dark 31\n", + "12 Poison 28\n", + "13 Steel 27\n", + "14 Fighting 27\n", + "15 Ice 24\n", + "16 Fairy 17\n", + "17 Flying 4" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n", - "\n" + "type = df['Type 1'].value_counts()\n", + "type.reset_index()" ] }, { @@ -157,12 +584,185 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "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", + "
Type 1Total
meanstd
0Bug378.93117.88
1Dark445.74109.13
2Dragon550.53146.27
3Electric443.41105.72
4Fairy413.18123.78
5Fighting416.44102.46
6Fire458.08109.76
7Flying485.00161.40
8Ghost439.56110.07
9Grass421.14106.65
10Ground437.50123.91
11Ice433.46108.28
12Normal401.68115.73
13Poison399.1492.36
14Psychic475.95139.03
15Rock453.75108.06
16Steel487.70115.42
17Water430.46113.19
\n", + "
" + ], + "text/plain": [ + " Type 1 Total \n", + " mean std\n", + "0 Bug 378.93 117.88\n", + "1 Dark 445.74 109.13\n", + "2 Dragon 550.53 146.27\n", + "3 Electric 443.41 105.72\n", + "4 Fairy 413.18 123.78\n", + "5 Fighting 416.44 102.46\n", + "6 Fire 458.08 109.76\n", + "7 Flying 485.00 161.40\n", + "8 Ghost 439.56 110.07\n", + "9 Grass 421.14 106.65\n", + "10 Ground 437.50 123.91\n", + "11 Ice 433.46 108.28\n", + "12 Normal 401.68 115.73\n", + "13 Poison 399.14 92.36\n", + "14 Psychic 475.95 139.03\n", + "15 Rock 453.75 108.06\n", + "16 Steel 487.70 115.42\n", + "17 Water 430.46 113.19" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n", - "\n" + "# Calculating with Total. \n", + "statistics = df.groupby('Type 1').agg({'Total': ['mean', 'std']})\n", + "statistics.reset_index().round(2)" ] }, { @@ -174,12 +774,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n", - "\n" + "water_poke = df[df[\"Type 1\"] == 'Water'][\"Total\"].dropna()\n", + "other_poke = df[df[\"Type 1\"] != 'Water'][\"Total\"].dropna()" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Test Statistic (t): -0.46\n", + "P-Value: 0.6434\n", + "\n", + "Fail to Reject the Null Hypothesis: The mean of total points for Water type is equal to the mean of total points for non-Water type.\n" + ] + } + ], + "source": [ + "t_stat, p_value = st.ttest_ind(water_poke, other_poke, equal_var=False) \n", + "print(f\"Test Statistic (t): {t_stat:.2f}\")\n", + "print(f\"P-Value: {p_value:.4f}\")\n", + "print()\n", + "\n", + "# Significance level\n", + "alpha = 0.05\n", + "\n", + "# Decision-Making\n", + "if p_value > alpha:\n", + " print(\"Fail to Reject the Null Hypothesis: The mean of total points for Water type is equal to the mean of total points for non-Water type.\")\n", + "else:\n", + " print(\"Reject the Null Hypothesis: The mean of total points for Water type is not equal to the mean of total points for non-water type. \")" ] }, { @@ -214,8 +846,72 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n", - "\n" + "# Null-Hypothesis: Defense and attack scores are equal. \n", + "# Alternative Hypothesis: Defense and attack scores not equal. " + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "attack = df['Attack']\n", + "defense = df['Defense']" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.7140303479358558e-05" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t_stat, p_value = st.ttest_rel(df['Attack'], df['Defense'])\n", + "p_value " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Test Statistic (t): 4.33\n", + "P-Value: 0.000017\n", + "\n", + "Reject the Null Hypothesis: Defense and attack scores not equal.\n" + ] + } + ], + "source": [ + "# Perform paired t-test\n", + "t_stat, p_value = st.ttest_rel(attack, defense)\n", + "print(f\"Test Statistic (t): {t_stat:.2f}\")\n", + "print(f\"P-Value: {p_value:.6f}\")\n", + "print()\n", + "\n", + "# Significance level\n", + "alpha = 0.05\n", + "\n", + "# Decision-Making\n", + "if p_value > alpha:\n", + " print(\"Fail to Reject the Null Hypothesis: Defense and attack scores are equal.\")\n", + "else:\n", + " print(\"Reject the Null Hypothesis: Defense and attack scores not equal.\")" ] }, { @@ -231,8 +927,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Your conclusions here:\n", - "\n" + "# We reject the null hyphotesis and say that defense and attack scores are not equal. \n" ] }, { @@ -244,14 +939,69 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n", + "spattack = df['Sp. Atk']\n", + "spdefense = df['Sp. Def']\n", "\n" ] }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mean Attack: 72.82\n", + "Mean Defense: 71.9\n" + ] + } + ], + "source": [ + "mean_attack = spattack.mean().round(2)\n", + "mean_defense = spdefense.mean().round(2)\n", + "\n", + "print(f'Mean Attack: {mean_attack}')\n", + "print(f'Mean Defense: {mean_defense}')" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Test Statistic (t): 0.85\n", + "P-Value: 0.39337\n", + "\n", + "Fail to Reject the Null Hypothesis: SP Defense and SP attack scores are equal.\n" + ] + } + ], + "source": [ + "t_stat, p_value = st.ttest_rel(spattack, spdefense)\n", + "print(f\"Test Statistic (t): {t_stat:.2f}\")\n", + "print(f\"P-Value: {p_value:.5f}\")\n", + "print()\n", + "\n", + "# Significance level\n", + "alpha = 0.05\n", + "\n", + "# Decision-Making\n", + "if p_value > alpha:\n", + " print(\"Fail to Reject the Null Hypothesis: SP Defense and SP attack scores are equal.\")\n", + "else:\n", + " print(\"Reject the Null Hypothesis:SP Defense and SP attack scores not equal.\")" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -265,7 +1015,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Your conclusions here:\n", + "# In this case we fail to reject the null hypotesis. \n", "\n" ] }, @@ -354,7 +1104,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "base", "language": "python", "name": "python3" }, @@ -368,7 +1118,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.9" + "version": "3.12.4" } }, "nbformat": 4,