From a2ae4dc656c1afb1098dfebe1fb4d738154cf502 Mon Sep 17 00:00:00 2001 From: mosijituo049 Date: Mon, 22 Jun 2026 23:30:54 +0200 Subject: [PATCH] submit lab --- lab-hypothesis-testing.ipynb | 433 +++++++++++++++-------------------- 1 file changed, 189 insertions(+), 244 deletions(-) diff --git a/lab-hypothesis-testing.ipynb b/lab-hypothesis-testing.ipynb index 0cc26d5..33cb9df 100644 --- a/lab-hypothesis-testing.ipynb +++ b/lab-hypothesis-testing.ipynb @@ -38,7 +38,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -51,241 +51,20 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 8, "metadata": {}, "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", - "
NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
0BulbasaurGrassPoison4549496565451False
1IvysaurGrassPoison6062638080601False
2VenusaurGrassPoison808283100100801False
3Mega VenusaurGrassPoison80100123122120801False
4CharmanderFireNaN3952436050651False
....................................
795DiancieRockFairy50100150100150506True
796Mega DiancieRockFairy501601101601101106True
797Hoopa ConfinedPsychicGhost8011060150130706True
798Hoopa UnboundPsychicDark8016060170130806True
799VolcanionFireWater8011012013090706True
\n", - "

800 rows × 11 columns

\n", - "
" - ], - "text/plain": [ - " Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def \\\n", - "0 Bulbasaur Grass Poison 45 49 49 65 65 \n", - "1 Ivysaur Grass Poison 60 62 63 80 80 \n", - "2 Venusaur Grass Poison 80 82 83 100 100 \n", - "3 Mega Venusaur Grass Poison 80 100 123 122 120 \n", - "4 Charmander Fire NaN 39 52 43 60 50 \n", - ".. ... ... ... .. ... ... ... ... \n", - "795 Diancie Rock Fairy 50 100 150 100 150 \n", - "796 Mega Diancie Rock Fairy 50 160 110 160 110 \n", - "797 Hoopa Confined Psychic Ghost 80 110 60 150 130 \n", - "798 Hoopa Unbound Psychic Dark 80 160 60 170 130 \n", - "799 Volcanion Fire Water 80 110 120 130 90 \n", - "\n", - " Speed Generation Legendary \n", - "0 45 1 False \n", - "1 60 1 False \n", - "2 80 1 False \n", - "3 80 1 False \n", - "4 65 1 False \n", - ".. ... ... ... \n", - "795 50 6 True \n", - "796 110 6 True \n", - "797 70 6 True \n", - "798 80 6 True \n", - "799 70 6 True \n", - "\n", - "[800 rows x 11 columns]" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "['Name', 'Type 1', 'Type 2', 'HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed', 'Generation', 'Legendary']\n" + ] } ], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/data-bootcamp-v4/data/main/pokemon.csv\")\n", - "df" + "print(df.columns.tolist())" ] }, { @@ -297,11 +76,45 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-statistic: 3.3349632905124063\n", + "p-value: 0.0007993609745420597\n" + ] + } + ], + "source": [ + "#code here\n", + "dragon_hp = df[df['Type 1'] == 'Dragon']['HP']\n", + "grass_hp = df[df['Type 1'] == 'Grass']['HP']\n", + "\n", + "# Welch's t-test (recommended)\n", + "t_stat, p_two_sided = st.ttest_ind(\n", + " dragon_hp,\n", + " grass_hp,\n", + " equal_var=False\n", + ")\n", + "\n", + "# Convert to one-sided p-value\n", + "if t_stat > 0:\n", + " p_one_sided = p_two_sided / 2\n", + "else:\n", + " p_one_sided = 1 - p_two_sided / 2\n", + "\n", + "print(\"t-statistic:\", t_stat)\n", + "print(\"p-value:\", p_one_sided)" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "#code here" + "Since the p-value (0.000799) is less than the significance level of 0.05, we reject the null hypothesis. There is sufficient statistical evidence to conclude that Dragon-type Pokémon have a higher average HP stat than Grass-type Pokémon." ] }, { @@ -313,11 +126,77 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HP:\n", + " t-statistic = 8.9814\n", + " p-value = 0.000000\n", + " Significant difference\n", + "\n", + "Attack:\n", + " t-statistic = 10.4381\n", + " p-value = 0.000000\n", + " Significant difference\n", + "\n", + "Defense:\n", + " t-statistic = 7.6371\n", + " p-value = 0.000000\n", + " Significant difference\n", + "\n", + "Sp. Atk:\n", + " t-statistic = 13.4174\n", + " p-value = 0.000000\n", + " Significant difference\n", + "\n", + "Sp. Def:\n", + " t-statistic = 10.0157\n", + " p-value = 0.000000\n", + " Significant difference\n", + "\n", + "Speed:\n", + " t-statistic = 11.4750\n", + " p-value = 0.000000\n", + " Significant difference\n", + "\n" + ] + } + ], "source": [ - "#code here" + "#code here\n", + "from scipy.stats import ttest_ind\n", + "\n", + "stats_cols = ['HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed']\n", + "\n", + "legendary = df[df['Legendary'] == True]\n", + "non_legendary = df[df['Legendary'] == False]\n", + "\n", + "for stat in stats_cols:\n", + " t_stat, p_value = ttest_ind(\n", + " legendary[stat],\n", + " non_legendary[stat],\n", + " equal_var=False # Welch's t-test\n", + " )\n", + "\n", + " print(f'{stat}:')\n", + " print(f' t-statistic = {t_stat:.4f}')\n", + " print(f' p-value = {p_value:.6f}')\n", + "\n", + " if p_value < 0.05:\n", + " print(' Significant difference\\n')\n", + " else:\n", + " print(' No significant difference\\n')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Using independent two-sample t-tests at the 5% significance level, all six stats (HP, Attack, Defense, Sp. Atk, Sp. Def, and Speed) showed statistically significant differences between Legendary and Non-Legendary Pokémon (all p-values < 0.001). Therefore, we conclude that Legendary Pokémon have significantly different overall stats compared with Non-Legendary Pokémon." ] }, { @@ -337,7 +216,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -453,7 +332,7 @@ "4 624.0 262.0 1.9250 65500.0 " ] }, - "execution_count": 5, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -483,22 +362,88 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def euclidean_distance(x1, y1, x2, y2):\n", + " return np.sqrt((x1 - x2)**2 + (y1 - y2)**2)" + ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-statistic: 37.992330214201516\n", + "p-value: 3.0064957768592614e-301\n" + ] + } + ], + "source": [ + "df['dist_school'] = euclidean_distance(\n", + " df['longitude'],\n", + " df['latitude'],\n", + " -118,\n", + " 34\n", + ")\n", + "\n", + "df['dist_hospital'] = euclidean_distance(\n", + " df['longitude'],\n", + " df['latitude'],\n", + " -122,\n", + " 37\n", + ")\n", + "\n", + "df['Close'] = (\n", + " (df['dist_school'] < 0.5) |\n", + " (df['dist_hospital'] < 0.5)\n", + ")\n", + "\n", + "close_prices = df[df['Close']]['median_house_value']\n", + "far_prices = df[~df['Close']]['median_house_value']\n", + "\n", + "t_stat, p_value = ttest_ind(\n", + " close_prices,\n", + " far_prices,\n", + " equal_var=False\n", + ")\n", + "\n", + "print(\"t-statistic:\", t_stat)\n", + "print(\"p-value:\", p_value)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Appropriate Test\n", + "\n", + "An independent two-sample t-test (Welch's t-test) was used to compare house prices between neighborhoods that are close to either a school or a hospital and those that are not.\n", + "\n", + "Hypotheses\n", + "\n", + "H₀: The average house price is the same for houses close to a school or hospital and houses farther away.\n", + "H₁: Houses close to a school or hospital have higher average prices.\n", + "\n", + "Results\n", + "\n", + "t-statistic = 37.992\n", + "p-value = 3.01 × 10⁻³⁰¹\n", + "\n", + "Conclusion\n", + "\n", + "Since the p-value is far below the 5% significance level (0.05), we reject the null hypothesis. There is extremely strong statistical evidence that houses located close to either a school or a hospital have higher average prices than houses located farther away. Therefore, the data supports the claim that proximity to a school or hospital is associated with more expensive houses." + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "3.10.14", "language": "python", "name": "python3" }, @@ -512,7 +457,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.10.14" } }, "nbformat": 4,