Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
299 changes: 280 additions & 19 deletions lab-hypothesis-testing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,21 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"#libraries\n",
"#Libraries\n",
"import pandas as pd\n",
"import scipy.stats as st\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import scipy.stats as st\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -278,7 +279,7 @@
"[800 rows x 11 columns]"
]
},
"execution_count": 3,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -288,6 +289,28 @@
"df"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(['Grass', 'Fire', 'Water', 'Bug', 'Normal', 'Poison', 'Electric',\n",
" 'Ground', 'Fairy', 'Fighting', 'Psychic', 'Rock', 'Ghost', 'Ice',\n",
" 'Dragon', 'Dark', 'Steel', 'Flying'], dtype=object)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['Type 1'].unique()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -297,11 +320,34 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 15,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"statistic = 3.33 and p_value = 0.001.\n",
"Because p_value is lower than significance level 0.05, \n",
"we reject the null hypothesis, this means that Pokemons type Dragon HP average is greater on average than Pokemons of type Grass\n"
]
}
],
"source": [
"#code here"
"#defining hypothesis\n",
"#H0: Pokemons of type Dragon HP average stats = Grass HP average \n",
"#H1: Pokemons of type Dragon HP average stats > Grass HP average -> right one-side P(D)> P(G)\n",
"# significance level\n",
"alpha=0.05\n",
"#collecting data\n",
"D_hp=df[df['Type 1']=='Dragon']['HP']\n",
"G_hp=df[df['Type 1']=='Grass']['HP']\n",
"\n",
"statistic = round(st.ttest_ind(D_hp,G_hp, equal_var=False, alternative=\"greater\")[0],2)\n",
"pvalue = round(st.ttest_ind(D_hp,G_hp, equal_var=False, alternative=\"greater\")[1],3)\n",
"\n",
"print(f'statistic = {statistic} and p_value = {pvalue}.')\n",
"print(f'Because p_value is lower than significance level {alpha}, \\nwe reject the null hypothesis, this means that Pokemons type Dragon HP average is greater on average than Pokemons of type Grass')"
]
},
{
Expand All @@ -313,11 +359,76 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 20,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<pandas.core.groupby.generic.DataFrameGroupBy object at 0x31f10ee00>"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#code here"
"cols=['HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed']\n",
"L_pok =df.groupby('Legendary')[cols]"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"For HP: statistic=8.98, p-value=0.0\n",
"→ Reject H0: Legendary and Non-Legendary Pokémon differ significantly in HP\n",
"\n",
"For Attack: statistic=10.44, p-value=0.0\n",
"→ Reject H0: Legendary and Non-Legendary Pokémon differ significantly in Attack\n",
"\n",
"For Defense: statistic=7.64, p-value=0.0\n",
"→ Reject H0: Legendary and Non-Legendary Pokémon differ significantly in Defense\n",
"\n",
"For Sp. Atk: statistic=13.42, p-value=0.0\n",
"→ Reject H0: Legendary and Non-Legendary Pokémon differ significantly in Sp. Atk\n",
"\n",
"For Sp. Def: statistic=10.02, p-value=0.0\n",
"→ Reject H0: Legendary and Non-Legendary Pokémon differ significantly in Sp. Def\n",
"\n",
"For Speed: statistic=11.48, p-value=0.0\n",
"→ Reject H0: Legendary and Non-Legendary Pokémon differ significantly in Speed\n",
"\n"
]
}
],
"source": [
"#defining hypothesis\n",
"#H0: Legendary Pokemons stats = to Non-Legendary ones \n",
"#H1: Legendary Pokemons stats != to Non-Legendary ones -> two-side either > or < \n",
"# significance level\n",
"alpha=0.05\n",
"#collecting data\n",
"cols=['HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed']\n",
"\n",
"for i in cols: \n",
" L_pok =df.groupby('Legendary')[cols].get_group(True) #-> Legendary ones\n",
" nL_pok =df.groupby('Legendary')[cols].get_group(False) # Non-legendary ones\n",
" statistic = round(st.ttest_ind(L_pok[i],nL_pok[i], equal_var=False, alternative='two-sided')[0],2)\n",
" pvalue = round(st.ttest_ind(L_pok[i],nL_pok[i],equal_var=False, alternative='two-sided')[1],3)\n",
" \n",
" if pvalue < alpha:\n",
" print(f'For {i}: statistic={statistic}, p-value={pvalue}')\n",
" print(f'→ Reject H0: Legendary and Non-Legendary Pokémon differ significantly in {i}\\n')\n",
" else:\n",
" print(f'For {i}: statistic={statistic}, p-value={pvalue}')\n",
" print(f'→ Fail to reject H0: No significant difference in {i}\\n')"
]
},
{
Expand All @@ -337,7 +448,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 27,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -453,7 +564,7 @@
"4 624.0 262.0 1.9250 65500.0 "
]
},
"execution_count": 5,
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -483,10 +594,160 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>longitude</th>\n",
" <th>latitude</th>\n",
" <th>housing_median_age</th>\n",
" <th>total_rooms</th>\n",
" <th>total_bedrooms</th>\n",
" <th>population</th>\n",
" <th>households</th>\n",
" <th>median_income</th>\n",
" <th>median_house_value</th>\n",
" <th>dist_school</th>\n",
" <th>dist_hospital</th>\n",
" <th>close</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>-114.31</td>\n",
" <td>34.19</td>\n",
" <td>15.0</td>\n",
" <td>5612.0</td>\n",
" <td>1283.0</td>\n",
" <td>1015.0</td>\n",
" <td>472.0</td>\n",
" <td>1.4936</td>\n",
" <td>66900.0</td>\n",
" <td>3.694888</td>\n",
" <td>8.187319</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>-114.47</td>\n",
" <td>34.40</td>\n",
" <td>19.0</td>\n",
" <td>7650.0</td>\n",
" <td>1901.0</td>\n",
" <td>1129.0</td>\n",
" <td>463.0</td>\n",
" <td>1.8200</td>\n",
" <td>80100.0</td>\n",
" <td>3.552591</td>\n",
" <td>7.966235</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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",
"\n",
" population households median_income median_house_value dist_school \\\n",
"0 1015.0 472.0 1.4936 66900.0 3.694888 \n",
"1 1129.0 463.0 1.8200 80100.0 3.552591 \n",
"\n",
" dist_hospital close \n",
"0 8.187319 False \n",
"1 7.966235 False "
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#hypothesis:\n",
"#H0: houses close to either a school or a hospital have in average = median_house_value than any other houses\n",
"#H1: houses close to either a school or a hospital have in average > median_house_value than any other houses\n",
"\n",
"#significance level:\n",
"alpha = 0.05\n",
"school = (-118, 34)\n",
"hospital = (-122, 37)\n",
"\n",
"# Calculate distances\n",
"def euclidean_distance(x1,y1,x2,y2):\n",
" return np.sqrt((x1-x2)**2+(y1-y2)**2)\n",
" \n",
"df['dist_school'] = df.apply(lambda row: euclidean_distance(row['longitude'], row['latitude'], school[0], school[1]), axis=1)\n",
"df['dist_hospital'] = df.apply(lambda row: euclidean_distance(row['longitude'], row['latitude'], hospital[0], hospital[1]), axis=1)\n",
"\n",
"# Define close/far\n",
"df['close'] = (df['dist_school'] < 0.50) | (df['dist_hospital'] < 0.50)\n",
"df.head(2)\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": []
"source": [
"#collecting the data\n",
"close =df.groupby('close')['median_house_value'].get_group(True) #-> Houses close to school or hospital\n",
"far =df.groupby('close')['median_house_value'].get_group(False) #-> Houses far to school or hospital\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"As: statistic=37.99, p-value=0.0\n",
"→ Reject H0: Houses close to school or hospital Pokémon are more expensive than other houses\n"
]
}
],
"source": [
" \n",
"\n",
"statistic = round(st.ttest_ind(close,far, equal_var=False, alternative='greater')[0],2)\n",
"pvalue = round(st.ttest_ind(close,far,equal_var=False, alternative='greater')[1],3)\n",
"\n",
"if pvalue < alpha:\n",
" print(f'As: statistic={statistic}, p-value={pvalue}')\n",
" print(f'→ Reject H0: Houses close to school or hospital are in average more expensive than other houses')\n",
"else:\n",
" print(f'As: statistic={statistic}, p-value={pvalue}')\n",
" print(f'→ Fail to reject H0: Houses close to school or hospital have in average a median_house_value alike to other houses')"
]
},
{
"cell_type": "code",
Expand All @@ -498,9 +759,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -512,9 +773,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
}