diff --git a/lab-hypothesis-testing.ipynb b/lab-hypothesis-testing.ipynb
index 0cc26d5..7bea771 100644
--- a/lab-hypothesis-testing.ipynb
+++ b/lab-hypothesis-testing.ipynb
@@ -51,7 +51,7 @@
},
{
"cell_type": "code",
- "execution_count": 3,
+ "execution_count": 2,
"metadata": {},
"outputs": [
{
@@ -278,7 +278,7 @@
"[800 rows x 11 columns]"
]
},
- "execution_count": 3,
+ "execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
@@ -297,11 +297,57 @@
},
{
"cell_type": "code",
- "execution_count": 1,
+ "execution_count": 14,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([False, True])"
+ ]
+ },
+ "execution_count": 14,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
- "#code here"
+ "df[\"Legendary\"].unique()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "We reject H0\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Assumption: dragon mean(HP) > Grass mean(HP)\n",
+ "# Hypotheses: H0: Dragon mean(HP) <= Grass mean(HP)\n",
+ "# H1: Dragon mean(HP) > Grass mean(HP)\n",
+ "# Statisticql testdf: TWO SAMPLE T-TEST: compare 2 groups means\n",
+ "dragon_hp = df[df['Type 1'] == \"Dragon\"][\"HP\"]\n",
+ "grass_hp = df[df['Type 1'] == \"Grass\"][\"HP\"]\n",
+ "statistic, p_value = st.ttest_ind(dragon_hp, grass_hp, equal_var = False, alternative = \"greater\")\n",
+ "if p_value < 0.05:\n",
+ " print(\"We reject H0\")\n",
+ "else:\n",
+ " print(\"fail to reject H0\")\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## H0 is rejected, there is evidence that Dragon mean(HP) is greater than Grass mean(HP)."
]
},
{
@@ -313,11 +359,113 @@
},
{
"cell_type": "code",
- "execution_count": 18,
+ "execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
- "#code here"
+ "# Assumption: Legendary Pokemons have different stats than Non_legendary pokemon. We compare the means of different stats for 2 groups. We use 2-sample T-test\n",
+ "# H0: number of stats1 = number os stats2\n",
+ "# H1: number of stats1 != number os stats2\n",
+ "#Conclusion: After running the hypothesis tests, we conlcude that there is insufficient evidence thet the stats are the same for the 2 groups of Pokemons"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "For HP stats, reject H0: \n",
+ " --> there is insufficient evidence that the HP means of the 2 groups are the same\n",
+ "-----------------------------------------------------------------------------------------------------------------\n",
+ "For Attack stats, reject H0: --> there is insufficient evidence that the HP means of the 2 groups are the same\n",
+ "-----------------------------------------------------------------------------------------------------------------\n",
+ "For Defense stats, reject H0: --> there is insufficient evidence that the HP means of the 2 groups are the same\n",
+ "-----------------------------------------------------------------------------------------------------------------\n",
+ "For Sp. Atk\t stats, reject H0: --> there is insufficient evidence that the HP means of the 2 groups are the same\n",
+ "-----------------------------------------------------------------------------------------------------------------\n",
+ "For Sp. Def stats, reject H0: --> there is insufficient evidence that the HP means of the 2 groups are the same\n",
+ "-----------------------------------------------------------------------------------------------------------------\n",
+ "For Speed stats, reject H0: --> there is insufficient evidence that the HP means of the 2 groups are the same\n",
+ "-----------------------------------------------------------------------------------------------------------------\n",
+ "For Generation stats, reject H0: --> there is insufficient evidence that the HP means of the 2 groups are the same\n"
+ ]
+ }
+ ],
+ "source": [
+ "legendary_hp = df[df[\"Legendary\"] == True]['HP']\n",
+ "non_legendary_hp = df[df[\"Legendary\"] == False]['HP']\n",
+ "legendary_Attack = df[df[\"Legendary\"] == True]['Attack']\n",
+ "non_legendary_Attack = df[df[\"Legendary\"] == False]['Attack']\n",
+ "legendary_Defense = df[df[\"Legendary\"] == True]['Defense']\n",
+ "non_legendary_Defense = df[df[\"Legendary\"] == False]['Defense']\n",
+ "legendary_sp_atk = df[df[\"Legendary\"] == True]['Sp. Atk']\n",
+ "non_legendary_sp_atk = df[df[\"Legendary\"] == False]['Sp. Atk']\n",
+ "legendary_sp_def = df[df[\"Legendary\"] == True]['Sp. Def']\n",
+ "non_legendary_sp_def = df[df[\"Legendary\"] == False]['Sp. Def']\n",
+ "legendary_Speed = df[df[\"Legendary\"] == True]['Speed']\n",
+ "non_legendary_Speed = df[df[\"Legendary\"] == False]['Speed']\n",
+ "legendary_gen= df[df[\"Legendary\"] == True]['Generation']\n",
+ "non_legendary_gen = df[df[\"Legendary\"] == False]['Generation']\n",
+ "\n",
+ "#HP stats\n",
+ "statistic_hp, p_value_hp = st.ttest_ind(legendary_hp, non_legendary_hp, equal_var = False, alternative = \"two-sided\")\n",
+ "if p_value_hp < 0.05:\n",
+ " print(\"For HP stats, reject H0: \")\n",
+ " print(\" --> there is insufficient evidence that the HP means of the 2 groups are the same\")\n",
+ "else:\n",
+ " print(\"Fail to reject H0 for stat HP: -->there is enough evidence that the means of the 2 groups are different\")\n",
+ "print('-----------------------------------------------------------------------------------------------------------------')\n",
+ "#----------------------------------------------------------------------------------------------------------------------#\n",
+ "# 2. Attack stats\n",
+ "statistic_attack, p_value_attack = st.ttest_ind(legendary_Attack, non_legendary_Attack, equal_var = False, alternative = \"two-sided\")\n",
+ "if p_value_attack < 0.05:\n",
+ " print(\"For Attack stats, reject H0: --> there is insufficient evidence that the HP means of the 2 groups are the same\")\n",
+ "else:\n",
+ " print(\"Fail to reject H0 for stat Attack: -->there is enough evidence that the means of the 2 groups are different\")\n",
+ "print('-----------------------------------------------------------------------------------------------------------------')\n",
+ "#----------------------------------------------------------------------------------------------------------------------#\n",
+ "#3. Defense\n",
+ "statistic_Defense, p_value_Defense = st.ttest_ind(legendary_Defense, non_legendary_Defense, equal_var = False, alternative = \"two-sided\")\n",
+ "if p_value_attack < 0.05:\n",
+ " print(\"For Defense stats, reject H0: --> there is insufficient evidence that the HP means of the 2 groups are the same\")\n",
+ "else:\n",
+ " print(\"Fail to reject H0 for stat Defense: -->there is enough evidence that the means of the 2 groups are different\")\n",
+ "print('-----------------------------------------------------------------------------------------------------------------')\n",
+ "#----------------------------------------------------------------------------------------------------------------------#\n",
+ "#4. Sp. Atk\t\n",
+ "statistic__sp_atk, p_value__sp_atk = st.ttest_ind(legendary_sp_atk, non_legendary_sp_atk, equal_var = False, alternative = \"two-sided\")\n",
+ "if p_value_attack < 0.05:\n",
+ " print(\"For Sp. Atk\t stats, reject H0: --> there is insufficient evidence that the HP means of the 2 groups are the same\")\n",
+ "else:\n",
+ " print(\"Fail to reject H0 for stat Sp. Atk\t: -->there is enough evidence that the means of the 2 groups are different\")\n",
+ "print('-----------------------------------------------------------------------------------------------------------------')\n",
+ "#----------------------------------------------------------------------------------------------------------------------#\n",
+ "#5. Sp. Def\n",
+ "statistic_sp_def, p_value_sp_def = st.ttest_ind(legendary_sp_def, non_legendary_sp_def, equal_var = False, alternative = \"two-sided\")\n",
+ "if p_value_attack < 0.05:\n",
+ " print(\"For Sp. Def stats, reject H0: --> there is insufficient evidence that the HP means of the 2 groups are the same\")\n",
+ "else:\n",
+ " print(\"Fail to reject H0 for stat Sp. Def\t: -->there is enough evidence that the means of the 2 groups are different\")\n",
+ "print('-----------------------------------------------------------------------------------------------------------------')\n",
+ "#----------------------------------------------------------------------------------------------------------------------#\n",
+ "#6. Speed\n",
+ "statistic_Speed, p_value_Speed = st.ttest_ind(legendary_Speed, non_legendary_Speed, equal_var = False, alternative = \"two-sided\")\n",
+ "if p_value_attack < 0.05:\n",
+ " print(\"For Speed stats, reject H0: --> there is insufficient evidence that the HP means of the 2 groups are the same\")\n",
+ "else:\n",
+ " print(\"Fail to reject H0 for stat Speed\t: -->there is enough evidence that the means of the 2 groups are different\")\n",
+ "print('-----------------------------------------------------------------------------------------------------------------')\n",
+ "#----------------------------------------------------------------------------------------------------------------------#\n",
+ "#6. Generation\n",
+ "statistic_gen, p_value_gen= st.ttest_ind(legendary_gen, non_legendary_gen, equal_var = False, alternative = \"two-sided\")\n",
+ "if p_value_attack < 0.05:\n",
+ " print(\"For Generation stats, reject H0: --> there is insufficient evidence that the HP means of the 2 groups are the same\")\n",
+ "else:\n",
+ " print(\"Fail to reject H0 for stat Generation\t: -->there is enough evidence that the means of the 2 groups are different\")"
]
},
{
@@ -337,9 +485,16 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 29,
"metadata": {},
"outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "(17000, 9)\n"
+ ]
+ },
{
"data": {
"text/html": [
@@ -453,13 +608,17 @@
"4 624.0 262.0 1.9250 65500.0 "
]
},
- "execution_count": 5,
+ "execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
+ "import pandas as pd\n",
+ "import numpy as np\n",
+ "\n",
"df = pd.read_csv(\"https://raw.githubusercontent.com/data-bootcamp-v4/data/main/california_housing.csv\")\n",
+ "print(df.shape)\n",
"df.head()"
]
},
@@ -483,24 +642,545 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 24,
"metadata": {},
"outputs": [],
- "source": []
+ "source": [
+ "#Write a function to calculate euclidean distance from each house (neighborhood) to the school and to the hospital.\n",
+ "# we cosnider a point p1(x1, y1) and point p2(x2, y2): Euclidean: d = sqrt((x2-x1)^2 + (y2-y1)^2)\n",
+ "\n",
+ "\n",
+ "def dist_to_school(lon, lat):\n",
+ " \"\"\"\n",
+ " dx=(Δlon)×111.32×cos(mean latitude)\n",
+ " dy=(Δlat)×111.32\n",
+ " \n",
+ " 1 degree of latitude ≈ 111.32 km\n",
+ " 1 degree of longitude ≈ 111.32 × cos(latitude) km\n",
+ " \"\"\"\n",
+ " lat_school = 34\n",
+ " lon_school = -118\n",
+ " \n",
+ " #calculate the cartisian coordinates of the school and hospital:\n",
+ " dx = (lon - lon_school) * 111.32 * np.cos (np.radians((lat+lat_school)/2))\n",
+ " dy = (lat - lat_school) * 111.32\n",
+ " \n",
+ "\n",
+ " dist_school = np.sqrt(\n",
+ " dx**2 + dy**2\n",
+ " )\n",
+ " return round(dist_school, 2)\n",
+ " \n",
+ "def dist_to_hsptl(lon, lat):\n",
+ " lat_hsptl = 37 \n",
+ " lon_hsptl = -122\n",
+ " #calculate the cartisian coordinates of the school and hospital:\n",
+ " dx = (lon - lon_hsptl) * 111.32 * np.cos (np.radians((lat+lat_hsptl)/2))\n",
+ " dy = (lat - lat_hsptl) * 111.32\n",
+ " \n",
+ "\n",
+ " dist_hsptl = np.sqrt(\n",
+ " dx**2 + dy**2\n",
+ " )\n",
+ " return round( dist_hsptl , 2)\n",
+ "\n",
+ "\n"
+ ]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 25,
"metadata": {},
- "outputs": [],
- "source": []
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "np.float64(340.82)"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ },
+ {
+ "data": {
+ "text/plain": [
+ "np.float64(763.15)"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "dist_school = dist_to_school(-114.31, 34.19)\n",
+ "dist_hsptl = dist_to_hsptl(-114.31, 34.19)\n",
+ "display(dist_school)\n",
+ "display(dist_hsptl)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 35,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " longitude | \n",
+ " latitude | \n",
+ " housing_median_age | \n",
+ " total_rooms | \n",
+ " total_bedrooms | \n",
+ " population | \n",
+ " households | \n",
+ " median_income | \n",
+ " median_house_value | \n",
+ " dist_from_school_km | \n",
+ " dist_from_hospital_km | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | count | \n",
+ " 17000.000000 | \n",
+ " 17000.000000 | \n",
+ " 17000.000000 | \n",
+ " 17000.000000 | \n",
+ " 17000.000000 | \n",
+ " 17000.000000 | \n",
+ " 17000.000000 | \n",
+ " 17000.000000 | \n",
+ " 17000.000000 | \n",
+ " 17000.000000 | \n",
+ " 17000.000000 | \n",
+ "
\n",
+ " \n",
+ " | mean | \n",
+ " -119.562108 | \n",
+ " 35.625225 | \n",
+ " 28.589353 | \n",
+ " 2643.664412 | \n",
+ " 539.410824 | \n",
+ " 1429.573941 | \n",
+ " 501.221941 | \n",
+ " 3.883578 | \n",
+ " 207300.912353 | \n",
+ " 276.042827 | \n",
+ " 350.161103 | \n",
+ "
\n",
+ " \n",
+ " | std | \n",
+ " 2.005166 | \n",
+ " 2.137340 | \n",
+ " 12.586937 | \n",
+ " 2179.947071 | \n",
+ " 421.499452 | \n",
+ " 1147.852959 | \n",
+ " 384.520841 | \n",
+ " 1.908157 | \n",
+ " 115983.764387 | \n",
+ " 256.103069 | \n",
+ " 198.285776 | \n",
+ "
\n",
+ " \n",
+ " | min | \n",
+ " -124.350000 | \n",
+ " 32.540000 | \n",
+ " 1.000000 | \n",
+ " 2.000000 | \n",
+ " 1.000000 | \n",
+ " 3.000000 | \n",
+ " 1.000000 | \n",
+ " 0.499900 | \n",
+ " 14999.000000 | \n",
+ " 0.920000 | \n",
+ " 0.000000 | \n",
+ "
\n",
+ " \n",
+ " | 25% | \n",
+ " -121.790000 | \n",
+ " 33.930000 | \n",
+ " 18.000000 | \n",
+ " 1462.000000 | \n",
+ " 297.000000 | \n",
+ " 790.000000 | \n",
+ " 282.000000 | \n",
+ " 2.566375 | \n",
+ " 119400.000000 | \n",
+ " 35.060000 | \n",
+ " 141.990000 | \n",
+ "
\n",
+ " \n",
+ " | 50% | \n",
+ " -118.490000 | \n",
+ " 34.250000 | \n",
+ " 29.000000 | \n",
+ " 2127.000000 | \n",
+ " 434.000000 | \n",
+ " 1167.000000 | \n",
+ " 409.000000 | \n",
+ " 3.544600 | \n",
+ " 180400.000000 | \n",
+ " 163.030000 | \n",
+ " 443.630000 | \n",
+ "
\n",
+ " \n",
+ " | 75% | \n",
+ " -118.000000 | \n",
+ " 37.720000 | \n",
+ " 37.000000 | \n",
+ " 3151.250000 | \n",
+ " 648.250000 | \n",
+ " 1721.000000 | \n",
+ " 605.250000 | \n",
+ " 4.767000 | \n",
+ " 265000.000000 | \n",
+ " 544.935000 | \n",
+ " 499.120000 | \n",
+ "
\n",
+ " \n",
+ " | max | \n",
+ " -114.310000 | \n",
+ " 41.950000 | \n",
+ " 52.000000 | \n",
+ " 37937.000000 | \n",
+ " 6445.000000 | \n",
+ " 35682.000000 | \n",
+ " 6082.000000 | \n",
+ " 15.000100 | \n",
+ " 500001.000000 | \n",
+ " 1036.100000 | \n",
+ " 822.050000 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " longitude latitude housing_median_age total_rooms \\\n",
+ "count 17000.000000 17000.000000 17000.000000 17000.000000 \n",
+ "mean -119.562108 35.625225 28.589353 2643.664412 \n",
+ "std 2.005166 2.137340 12.586937 2179.947071 \n",
+ "min -124.350000 32.540000 1.000000 2.000000 \n",
+ "25% -121.790000 33.930000 18.000000 1462.000000 \n",
+ "50% -118.490000 34.250000 29.000000 2127.000000 \n",
+ "75% -118.000000 37.720000 37.000000 3151.250000 \n",
+ "max -114.310000 41.950000 52.000000 37937.000000 \n",
+ "\n",
+ " total_bedrooms population households median_income \\\n",
+ "count 17000.000000 17000.000000 17000.000000 17000.000000 \n",
+ "mean 539.410824 1429.573941 501.221941 3.883578 \n",
+ "std 421.499452 1147.852959 384.520841 1.908157 \n",
+ "min 1.000000 3.000000 1.000000 0.499900 \n",
+ "25% 297.000000 790.000000 282.000000 2.566375 \n",
+ "50% 434.000000 1167.000000 409.000000 3.544600 \n",
+ "75% 648.250000 1721.000000 605.250000 4.767000 \n",
+ "max 6445.000000 35682.000000 6082.000000 15.000100 \n",
+ "\n",
+ " median_house_value dist_from_school_km dist_from_hospital_km \n",
+ "count 17000.000000 17000.000000 17000.000000 \n",
+ "mean 207300.912353 276.042827 350.161103 \n",
+ "std 115983.764387 256.103069 198.285776 \n",
+ "min 14999.000000 0.920000 0.000000 \n",
+ "25% 119400.000000 35.060000 141.990000 \n",
+ "50% 180400.000000 163.030000 443.630000 \n",
+ "75% 265000.000000 544.935000 499.120000 \n",
+ "max 500001.000000 1036.100000 822.050000 "
+ ]
+ },
+ "execution_count": 35,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "#Apply the 2 functions on the df\n",
+ "df['dist_from_school_km'] = df.apply(lambda row: dist_to_school(row['longitude'], row['latitude']), axis = 1)\n",
+ "df['dist_from_hospital_km'] = df.apply(lambda row: dist_to_hsptl(row['longitude'], row['latitude']), axis = 1)\n",
+ "df.head()\n",
+ "df.describe()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 34,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ "RangeIndex: 17000 entries, 0 to 16999\n",
+ "Data columns (total 11 columns):\n",
+ " # Column Non-Null Count Dtype \n",
+ "--- ------ -------------- ----- \n",
+ " 0 longitude 17000 non-null float64\n",
+ " 1 latitude 17000 non-null float64\n",
+ " 2 housing_median_age 17000 non-null float64\n",
+ " 3 total_rooms 17000 non-null float64\n",
+ " 4 total_bedrooms 17000 non-null float64\n",
+ " 5 population 17000 non-null float64\n",
+ " 6 households 17000 non-null float64\n",
+ " 7 median_income 17000 non-null float64\n",
+ " 8 median_house_value 17000 non-null float64\n",
+ " 9 dist_from_school_km 17000 non-null float64\n",
+ " 10 dist_from_hospital_km 17000 non-null float64\n",
+ "dtypes: float64(11)\n",
+ "memory usage: 1.4 MB\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "2053892291536"
+ ]
+ },
+ "execution_count": 34,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Divide your dataset into houses close and far from either a hospital or school."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 49,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " longitude | \n",
+ " latitude | \n",
+ " housing_median_age | \n",
+ " total_rooms | \n",
+ " total_bedrooms | \n",
+ " population | \n",
+ " households | \n",
+ " median_income | \n",
+ " median_house_value | \n",
+ " dist_from_school_km | \n",
+ " dist_from_hospital_km | \n",
+ " close_to_school | \n",
+ " close_to_hospital | \n",
+ " close_to_facility | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " -114.31 | \n",
+ " 34.19 | \n",
+ " 15.0 | \n",
+ " 5612.0 | \n",
+ " 1283.0 | \n",
+ " 1015.0 | \n",
+ " 472.0 | \n",
+ " 1.4936 | \n",
+ " 66900.0 | \n",
+ " 340.82 | \n",
+ " 763.15 | \n",
+ " far | \n",
+ " far | \n",
+ " far | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " -114.47 | \n",
+ " 34.40 | \n",
+ " 19.0 | \n",
+ " 7650.0 | \n",
+ " 1901.0 | \n",
+ " 1129.0 | \n",
+ " 463.0 | \n",
+ " 1.8200 | \n",
+ " 80100.0 | \n",
+ " 328.05 | \n",
+ " 739.70 | \n",
+ " far | \n",
+ " far | \n",
+ " far | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " -114.56 | \n",
+ " 33.69 | \n",
+ " 17.0 | \n",
+ " 720.0 | \n",
+ " 174.0 | \n",
+ " 333.0 | \n",
+ " 117.0 | \n",
+ " 1.6509 | \n",
+ " 85700.0 | \n",
+ " 319.92 | \n",
+ " 769.52 | \n",
+ " far | \n",
+ " far | \n",
+ " far | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " -114.57 | \n",
+ " 33.64 | \n",
+ " 14.0 | \n",
+ " 1501.0 | \n",
+ " 337.0 | \n",
+ " 515.0 | \n",
+ " 226.0 | \n",
+ " 3.1917 | \n",
+ " 73400.0 | \n",
+ " 319.74 | \n",
+ " 771.59 | \n",
+ " far | \n",
+ " far | \n",
+ " far | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " -114.57 | \n",
+ " 33.57 | \n",
+ " 20.0 | \n",
+ " 1454.0 | \n",
+ " 326.0 | \n",
+ " 624.0 | \n",
+ " 262.0 | \n",
+ " 1.9250 | \n",
+ " 65500.0 | \n",
+ " 320.94 | \n",
+ " 775.65 | \n",
+ " far | \n",
+ " far | \n",
+ " far | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " longitude latitude housing_median_age total_rooms total_bedrooms \\\n",
+ "0 -114.31 34.19 15.0 5612.0 1283.0 \n",
+ "1 -114.47 34.40 19.0 7650.0 1901.0 \n",
+ "2 -114.56 33.69 17.0 720.0 174.0 \n",
+ "3 -114.57 33.64 14.0 1501.0 337.0 \n",
+ "4 -114.57 33.57 20.0 1454.0 326.0 \n",
+ "\n",
+ " population households median_income median_house_value \\\n",
+ "0 1015.0 472.0 1.4936 66900.0 \n",
+ "1 1129.0 463.0 1.8200 80100.0 \n",
+ "2 333.0 117.0 1.6509 85700.0 \n",
+ "3 515.0 226.0 3.1917 73400.0 \n",
+ "4 624.0 262.0 1.9250 65500.0 \n",
+ "\n",
+ " dist_from_school_km dist_from_hospital_km close_to_school \\\n",
+ "0 340.82 763.15 far \n",
+ "1 328.05 739.70 far \n",
+ "2 319.92 769.52 far \n",
+ "3 319.74 771.59 far \n",
+ "4 320.94 775.65 far \n",
+ "\n",
+ " close_to_hospital close_to_facility \n",
+ "0 far far \n",
+ "1 far far \n",
+ "2 far far \n",
+ "3 far far \n",
+ "4 far far "
+ ]
+ },
+ "execution_count": 49,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df['close_to_facility'] = np.where(\n",
+ " (df['dist_from_school_km'] < 0.5) |\n",
+ " (df['dist_from_hospital_km'] < 0.5),\n",
+ " \"close\", \"far\"\n",
+ ")\n",
+ "df_close = df[df['close_to_facility'] == \"close\"]\n",
+ "df_far = df[df['close_to_facility'] == \"far\"]\n",
+ "\n",
+ "\n",
+ "df.head()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Hypothesis\n",
+ "## Null Hypothesis: price_mean near facility <= price_mean far from facility\n",
+ "## H1: price_mean near facility > price_mean far from facility\n",
+ "we use Two-sample independent T-test\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 51,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Fail to reject 0.7263498868187508 >= 0.05.: there is insufficient evidence that the mean price of near houses is greater than far houses\n"
+ ]
+ }
+ ],
+ "source": [
+ "from scipy import stats as st\n",
+ "stat, p_value = st.ttest_ind(df_close['median_house_value'], df_far['median_house_value'], alternative = \"greater\")\n",
+ "if p_value <0.05:\n",
+ " print(f\"Hypothesis rejected: {p_value} < 0.05. There is sufficient evidence that the price of near houses is greater than far houses\")\n",
+ "else:\n",
+ " print(f\"Fail to reject {p_value} >= 0.05.: there is insufficient evidence that the mean price of near houses is greater than far houses\")"
+ ]
}
],
"metadata": {
"kernelspec": {
- "display_name": "Python 3",
+ "display_name": "Python [conda env:base] *",
"language": "python",
- "name": "python3"
+ "name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
@@ -512,9 +1192,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
}