From dc4000e5d318e4bc5d1f5b91b394237344e9364c Mon Sep 17 00:00:00 2001 From: claireley Date: Thu, 4 Jun 2026 15:08:29 +0200 Subject: [PATCH] Solved lab --- lab-hypothesis-testing.ipynb | 136 ++++++++++++++++++++++++++++------- 1 file changed, 112 insertions(+), 24 deletions(-) diff --git a/lab-hypothesis-testing.ipynb b/lab-hypothesis-testing.ipynb index 0cc26d5..de47de6 100644 --- a/lab-hypothesis-testing.ipynb +++ b/lab-hypothesis-testing.ipynb @@ -38,7 +38,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 46, "metadata": {}, "outputs": [ { @@ -278,7 +278,7 @@ "[800 rows x 11 columns]" ] }, - "execution_count": 3, + "execution_count": 46, "metadata": {}, "output_type": "execute_result" } @@ -297,11 +297,26 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 53, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ - "#code here" + "#code here\n", + "df_dragon = df[df[\"Type 1\"]==\"Dragon\"][\"HP\"]\n", + "df_grass = df[df[\"Type 1\"]==\"Grass\"][\"HP\"]\n", + "t_stat, p_value = st.ttest_ind(df_dragon,df_grass, equal_var=False, alternative=\"greater\")\n", + "print(p_value < 0.05)\n", + "\n", + "#p-value is less than alpha (0.05), \n", + "#reject the null hypothesis that HP stats are the same, accept alternative that Dragon have greater average HP stats" ] }, { @@ -313,11 +328,63 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 55, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HP\n", + "True\n", + "Attack\n", + "True\n", + "Defense\n", + "True\n", + "Sp. Atk\n", + "True\n", + "Sp. Def\n", + "True\n", + "Speed\n", + "True\n" + ] + }, + { + "data": { + "text/plain": [ + "{'HP': (np.float64(8.981370483625046), np.float64(1.0026911708035284e-13)),\n", + " 'Attack': (np.float64(10.438133539322203), np.float64(2.520372449236646e-16)),\n", + " 'Defense': (np.float64(7.637078164784618),\n", + " np.float64(4.8269984949193316e-11)),\n", + " 'Sp. Atk': (np.float64(13.417449984138461),\n", + " np.float64(1.5514614112239812e-21)),\n", + " 'Sp. Def': (np.float64(10.015696613114878),\n", + " np.float64(2.2949327864052826e-15)),\n", + " 'Speed': (np.float64(11.47504444631443), np.float64(1.049016311882451e-18))}" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#code here" + "from scipy.stats import ttest_ind\n", + "\n", + "df_legendary = df[df[\"Legendary\"] == 1][['HP','Attack','Defense', 'Sp. Atk', 'Sp. Def', 'Speed']]\n", + "df_nonlegendary = df[df[\"Legendary\"] == 0][['HP','Attack','Defense', 'Sp. Atk', 'Sp. Def', 'Speed']]\n", + "\n", + "results = {}\n", + "\n", + "for col in df_legendary.columns:\n", + " t_stat, p_value = ttest_ind(df_legendary[col], df_nonlegendary[col], equal_var=False, alternative = 'two-sided')\n", + " results[col] = (t_stat, p_value)\n", + " print(col)\n", + " print(p_value<0.05)\n", + "results\n", + "\n", + "#p-values are all less than alpha (0.05), reject the null hypothesis that stats are the same, \n", + "# accept alternative that nonlegendary Pokemon have different stats" ] }, { @@ -337,7 +404,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 56, "metadata": {}, "outputs": [ { @@ -453,7 +520,7 @@ "4 624.0 262.0 1.9250 65500.0 " ] }, - "execution_count": 5, + "execution_count": 56, "metadata": {}, "output_type": "execute_result" } @@ -483,22 +550,43 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, + "execution_count": 75, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "from scipy import stats\n", + "\n", + "school_x=-118\n", + "school_y=34\n", + "hospital_x=-122\n", + "hospital_y=37\n", + "\n", + "df['dist_school'] = np.sqrt((df['longitude']-school_x)**2 + (df['latitude']-school_y)**2)\n", + "df['dist_hospital'] = np.sqrt((df['longitude']-hospital_x)**2 + (df['latitude']-hospital_y)**2)\n", + "\n", + "s_h_close=df[(df.dist_school<0.5) | (df.dist_hospital<0.5) ]\n", + "s_h_far=df[(df.dist_school>=0.5) & (df.dist_hospital>=0.5) ]\n", + "\n", + "# # Two-sample t-test\n", + "t_stat, p_value = stats.ttest_ind(s_h_close['median_house_value'], s_h_far['median_house_value'], alternative='greater')\n", + "print(p_value < 0.05)\n", + "\n", + "#p-value is less than alpha (0.05), reject the null hypothesis median house values are the same, \n", + "#accept alternative that houses closer to schools or hospitals (Euclidean distance lower than 0.5) have a higher median value" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -512,9 +600,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.13.9" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }