990 lines
155 KiB
Plaintext
990 lines
155 KiB
Plaintext
|
{
|
|||
|
"cells": [
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 1,
|
|||
|
"id": "ffe86c63-5718-4c71-a3a3-94672d34e676",
|
|||
|
"metadata": {
|
|||
|
"editable": true,
|
|||
|
"slideshow": {
|
|||
|
"slide_type": ""
|
|||
|
},
|
|||
|
"tags": []
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"# import libraries\n",
|
|||
|
"from ucimlrepo import fetch_ucirepo\n",
|
|||
|
"import pandas as pd\n",
|
|||
|
"import matplotlib.pyplot as plt\n",
|
|||
|
"import numpy as np\n",
|
|||
|
"import seaborn as sns"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "d1b8dc0f-8e8b-485a-9147-cf694f832357",
|
|||
|
"metadata": {
|
|||
|
"editable": true,
|
|||
|
"slideshow": {
|
|||
|
"slide_type": ""
|
|||
|
},
|
|||
|
"tags": []
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Explorative Datenanalyse\n",
|
|||
|
"Deskriptive Statistik: Berechnung von Mittelwerten, Mediane, Standardabweichungen und anderen Kenngrößen für die verschiedenen Merkmale. \n",
|
|||
|
"Visualisierung: Nutzung von Histogrammen, Boxplots und Scatterplots, um die Verteilung und Beziehungen der Merkmale zu analysieren. \n",
|
|||
|
"Korrelation: Untersuchung der Korrelation zwischen den Merkmalen und dem Outcome."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "282a1c63-e28c-4d81-b1dc-1d971e9a4c12",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"### Dataset preparation"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 85,
|
|||
|
"id": "130b7fb5-7f85-4393-a741-05246920ce63",
|
|||
|
"metadata": {
|
|||
|
"editable": true,
|
|||
|
"slideshow": {
|
|||
|
"slide_type": ""
|
|||
|
},
|
|||
|
"tags": []
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"<class 'pandas.core.frame.DataFrame'>\n",
|
|||
|
"RangeIndex: 303 entries, 0 to 302\n",
|
|||
|
"Data columns (total 14 columns):\n",
|
|||
|
" # Column Non-Null Count Dtype \n",
|
|||
|
"--- ------ -------------- ----- \n",
|
|||
|
" 0 age 303 non-null int64 \n",
|
|||
|
" 1 sex 303 non-null int64 \n",
|
|||
|
" 2 cp 303 non-null int64 \n",
|
|||
|
" 3 trestbps 303 non-null int64 \n",
|
|||
|
" 4 chol 303 non-null int64 \n",
|
|||
|
" 5 fbs 303 non-null int64 \n",
|
|||
|
" 6 restecg 303 non-null int64 \n",
|
|||
|
" 7 thalach 303 non-null int64 \n",
|
|||
|
" 8 exang 303 non-null int64 \n",
|
|||
|
" 9 oldpeak 303 non-null float64\n",
|
|||
|
" 10 slope 303 non-null int64 \n",
|
|||
|
" 11 ca 299 non-null float64\n",
|
|||
|
" 12 thal 301 non-null float64\n",
|
|||
|
" 13 healthy 303 non-null int64 \n",
|
|||
|
"dtypes: float64(3), int64(11)\n",
|
|||
|
"memory usage: 33.3 KB\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# fetch dataset \n",
|
|||
|
"heart_disease = fetch_ucirepo(id=45)\n",
|
|||
|
"\n",
|
|||
|
"# features\n",
|
|||
|
"x = heart_disease.data.features \n",
|
|||
|
"\n",
|
|||
|
"# target variable\n",
|
|||
|
"y = heart_disease.data.targets \n",
|
|||
|
"\n",
|
|||
|
"# complete dataframe\n",
|
|||
|
"heart_df = pd.concat([x, y], axis=1).rename(columns={'num':'healthy'})\n",
|
|||
|
"\n",
|
|||
|
"# replace values for target variable: 0=sick, 1=healthy\n",
|
|||
|
"heart_df['healthy'] = heart_df['healthy'].replace({0: 1, 1: 0, 2: 0, 3: 0, 4: 0})\n",
|
|||
|
"\n",
|
|||
|
"# view summary of dataset\n",
|
|||
|
"heart_df.info()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 4,
|
|||
|
"id": "0780f01d-771b-4443-86b2-0e2aa8ef464f",
|
|||
|
"metadata": {
|
|||
|
"editable": true,
|
|||
|
"slideshow": {
|
|||
|
"slide_type": ""
|
|||
|
},
|
|||
|
"tags": []
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"Number of rows with NaN values to be removed: 6\n",
|
|||
|
"Total number of rows: 303\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"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>age</th>\n",
|
|||
|
" <th>sex</th>\n",
|
|||
|
" <th>cp</th>\n",
|
|||
|
" <th>trestbps</th>\n",
|
|||
|
" <th>chol</th>\n",
|
|||
|
" <th>fbs</th>\n",
|
|||
|
" <th>restecg</th>\n",
|
|||
|
" <th>thalach</th>\n",
|
|||
|
" <th>exang</th>\n",
|
|||
|
" <th>oldpeak</th>\n",
|
|||
|
" <th>slope</th>\n",
|
|||
|
" <th>ca</th>\n",
|
|||
|
" <th>thal</th>\n",
|
|||
|
" <th>healthy</th>\n",
|
|||
|
" </tr>\n",
|
|||
|
" </thead>\n",
|
|||
|
" <tbody>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>0</th>\n",
|
|||
|
" <td>63</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>145</td>\n",
|
|||
|
" <td>233</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>2</td>\n",
|
|||
|
" <td>150</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>2.3</td>\n",
|
|||
|
" <td>3</td>\n",
|
|||
|
" <td>0.0</td>\n",
|
|||
|
" <td>6.0</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>1</th>\n",
|
|||
|
" <td>67</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>4</td>\n",
|
|||
|
" <td>160</td>\n",
|
|||
|
" <td>286</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>2</td>\n",
|
|||
|
" <td>108</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>1.5</td>\n",
|
|||
|
" <td>2</td>\n",
|
|||
|
" <td>3.0</td>\n",
|
|||
|
" <td>3.0</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>2</th>\n",
|
|||
|
" <td>67</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>4</td>\n",
|
|||
|
" <td>120</td>\n",
|
|||
|
" <td>229</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>2</td>\n",
|
|||
|
" <td>129</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>2.6</td>\n",
|
|||
|
" <td>2</td>\n",
|
|||
|
" <td>2.0</td>\n",
|
|||
|
" <td>7.0</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>3</th>\n",
|
|||
|
" <td>37</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>3</td>\n",
|
|||
|
" <td>130</td>\n",
|
|||
|
" <td>250</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>187</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>3.5</td>\n",
|
|||
|
" <td>3</td>\n",
|
|||
|
" <td>0.0</td>\n",
|
|||
|
" <td>3.0</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>4</th>\n",
|
|||
|
" <td>41</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>2</td>\n",
|
|||
|
" <td>130</td>\n",
|
|||
|
" <td>204</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>2</td>\n",
|
|||
|
" <td>172</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>1.4</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>0.0</td>\n",
|
|||
|
" <td>3.0</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>...</th>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>297</th>\n",
|
|||
|
" <td>57</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>4</td>\n",
|
|||
|
" <td>140</td>\n",
|
|||
|
" <td>241</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>123</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>0.2</td>\n",
|
|||
|
" <td>2</td>\n",
|
|||
|
" <td>0.0</td>\n",
|
|||
|
" <td>7.0</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>298</th>\n",
|
|||
|
" <td>45</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>110</td>\n",
|
|||
|
" <td>264</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>132</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>1.2</td>\n",
|
|||
|
" <td>2</td>\n",
|
|||
|
" <td>0.0</td>\n",
|
|||
|
" <td>7.0</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>299</th>\n",
|
|||
|
" <td>68</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>4</td>\n",
|
|||
|
" <td>144</td>\n",
|
|||
|
" <td>193</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>141</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>3.4</td>\n",
|
|||
|
" <td>2</td>\n",
|
|||
|
" <td>2.0</td>\n",
|
|||
|
" <td>7.0</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>300</th>\n",
|
|||
|
" <td>57</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>4</td>\n",
|
|||
|
" <td>130</td>\n",
|
|||
|
" <td>131</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>115</td>\n",
|
|||
|
" <td>1</td>\n",
|
|||
|
" <td>1.2</td>\n",
|
|||
|
" <td>2</td>\n",
|
|||
|
" <td>1.0</td>\n",
|
|||
|
" <td>7.0</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>301</th>\n",
|
|||
|
" <td>57</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>2</td>\n",
|
|||
|
" <td>130</td>\n",
|
|||
|
" <td>236</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>2</td>\n",
|
|||
|
" <td>174</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" <td>0.0</td>\n",
|
|||
|
" <td>2</td>\n",
|
|||
|
" <td>1.0</td>\n",
|
|||
|
" <td>3.0</td>\n",
|
|||
|
" <td>0</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" </tbody>\n",
|
|||
|
"</table>\n",
|
|||
|
"<p>297 rows × 14 columns</p>\n",
|
|||
|
"</div>"
|
|||
|
],
|
|||
|
"text/plain": [
|
|||
|
" age sex cp trestbps chol fbs restecg thalach exang oldpeak \\\n",
|
|||
|
"0 63 1 1 145 233 1 2 150 0 2.3 \n",
|
|||
|
"1 67 1 4 160 286 0 2 108 1 1.5 \n",
|
|||
|
"2 67 1 4 120 229 0 2 129 1 2.6 \n",
|
|||
|
"3 37 1 3 130 250 0 0 187 0 3.5 \n",
|
|||
|
"4 41 0 2 130 204 0 2 172 0 1.4 \n",
|
|||
|
".. ... ... .. ... ... ... ... ... ... ... \n",
|
|||
|
"297 57 0 4 140 241 0 0 123 1 0.2 \n",
|
|||
|
"298 45 1 1 110 264 0 0 132 0 1.2 \n",
|
|||
|
"299 68 1 4 144 193 1 0 141 0 3.4 \n",
|
|||
|
"300 57 1 4 130 131 0 0 115 1 1.2 \n",
|
|||
|
"301 57 0 2 130 236 0 2 174 0 0.0 \n",
|
|||
|
"\n",
|
|||
|
" slope ca thal healthy \n",
|
|||
|
"0 3 0.0 6.0 1 \n",
|
|||
|
"1 2 3.0 3.0 0 \n",
|
|||
|
"2 2 2.0 7.0 0 \n",
|
|||
|
"3 3 0.0 3.0 1 \n",
|
|||
|
"4 1 0.0 3.0 1 \n",
|
|||
|
".. ... ... ... ... \n",
|
|||
|
"297 2 0.0 7.0 0 \n",
|
|||
|
"298 2 0.0 7.0 0 \n",
|
|||
|
"299 2 2.0 7.0 0 \n",
|
|||
|
"300 2 1.0 7.0 0 \n",
|
|||
|
"301 2 1.0 3.0 0 \n",
|
|||
|
"\n",
|
|||
|
"[297 rows x 14 columns]"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 4,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# data cleaning\n",
|
|||
|
"rows_with_nan = heart_df.isna().any(axis=1).sum()\n",
|
|||
|
"print(f\"Number of rows with NaN values to be removed: {rows_with_nan}\")\n",
|
|||
|
"total_rows = heart_df.shape[0]\n",
|
|||
|
"print(f\"Total number of rows: {total_rows}\")\n",
|
|||
|
"\n",
|
|||
|
"# remove rows with NaN values\n",
|
|||
|
"heart_df = heart_df.dropna()\n",
|
|||
|
"\n",
|
|||
|
"heart_df"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "15d04b9c-2803-43e8-98a0-118dda07536f",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"### Clustering and PCA\n",
|
|||
|
"Um zu analysieren, ob ähnliche Merkmale auch zur gleichen Diagnose führen, wird zuerst ein k-Means Clustering angewandt."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 6,
|
|||
|
"id": "76dc9ce9-8e81-4a50-ad4d-557576a58a92",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"# prepare data for clustering\n",
|
|||
|
"from sklearn.decomposition import PCA\n",
|
|||
|
"from sklearn.preprocessing import StandardScaler\n",
|
|||
|
"\n",
|
|||
|
"# split data into featrues (x) and targets (y)\n",
|
|||
|
"X = heart_df.iloc[:, :-1]\n",
|
|||
|
"y = heart_df.iloc[:, -1]\n",
|
|||
|
"\n",
|
|||
|
"# scale data (MinMax probieren)\n",
|
|||
|
"scaler = StandardScaler() \n",
|
|||
|
"X_scaled = scaler.fit_transform (X)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 7,
|
|||
|
"id": "b56af950-7a58-43b5-8a83-1fd95b0c7220",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/html": [
|
|||
|
"<style>#sk-container-id-1 {color: black;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-r
|
|||
|
],
|
|||
|
"text/plain": [
|
|||
|
"KMeans(n_clusters=2, n_init='auto', random_state=42)"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 7,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# k-means clustering\n",
|
|||
|
"from sklearn.cluster import KMeans\n",
|
|||
|
"from sklearn.metrics import confusion_matrix\n",
|
|||
|
"\n",
|
|||
|
"# prepare model\n",
|
|||
|
"kmeans = KMeans(n_clusters=2, random_state=42, n_init='auto')\n",
|
|||
|
"kmeans.fit(X_scaled)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 14,
|
|||
|
"id": "4dfc00f7-a2b6-4408-b48d-c24a9c32f74d",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"79.12% der Datensätze wurden mithilfe von KMeans richtig einem Cluster zugeordnet\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# calculate percentage of data points correctly assigned to each cluster\n",
|
|||
|
"cluster1 = kmeans.labels_ == 0\n",
|
|||
|
"cluster2 = kmeans.labels_ == 1\n",
|
|||
|
"\n",
|
|||
|
"perc_cluster1 = np.round(np.mean(cluster1 == y) * 100, decimals=2)\n",
|
|||
|
"perc_cluster2 = np.round(np.mean(cluster2 == y) * 100, decimals=2)\n",
|
|||
|
"\n",
|
|||
|
"# choose cluster with higher correspondence\n",
|
|||
|
"if perc_cluster1 > perc_cluster2:\n",
|
|||
|
" km_healthy = cluster1\n",
|
|||
|
" max_perc = perc_cluster1\n",
|
|||
|
"else:\n",
|
|||
|
" km_healthy = cluster2\n",
|
|||
|
" max_perc = perc_cluster2\n",
|
|||
|
"\n",
|
|||
|
"print(f\"{max_perc}% der Datensätze wurden mithilfe von KMeans richtig einem Cluster zugeordnet\")\n",
|
|||
|
"\n",
|
|||
|
"# hier vlt noch irgendwie diskutieren ob das ein smart way ist um das auszuwerten, anscheinend gibt's dafür andere Metriken"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 50,
|
|||
|
"id": "d5edbc93-22e2-4086-aa77-ec7a76765048",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"Proportion of variance explained by each principal component:\n",
|
|||
|
"[0.23695056 0.12349486 0.0960383 0.0851614 0.07638123 0.06720242\n",
|
|||
|
" 0.06493755 0.05994447 0.05268635 0.04368496 0.03486402 0.03140469\n",
|
|||
|
" 0.02724919]\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAArwAAAIhCAYAAACsQmneAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAABqSUlEQVR4nO3deXiMV//H8c9kj5CQiESEiKUktS9tUdRe1VZX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lSopICBA1apV0zPPPKMzZ87Y9WmxWPTII4/o7bff1lVXXaXAwEAlJiZq/vz5eR5/27Zt6tq1q8qVK6egoCA1aNBA77//vl2b3I8L58yZo8cff1yVKlVSYGCg/v3vf+vuu++WdP4NMPcjz1mzZtnGcvFZ36ysLI0cOVLx8fEKCAhQpUqV9PDDD+vYsWN27apWraqbb75ZX3zxhRo1aqTg4GDVrl1b7733XqG266W21Z49e2SxWLRy5Urt2LHjkh+xd+rUSbGxsZo5c2ae23bs2KENGzaod+/e8vPzU3Jysrp27arY2FgFBQWpRo0a+te//qXDhw/b3S932sLmzZt11113qVy5crbCwNGUhgULFqhjx46qWLGigoODlZCQoBEjRujkyZN27XKfQ7///rtuuukmlS5dWpUrV9bjjz+e57ly5swZjRs3TgkJCQoKClJERITatGmjdevW2doYYzR16lQ1aNBAwcHBKleunO666y7t2rWrUPuiMObPny+LxaI333zTLp6UlCRfX18lJydL+me/vfjiixo/fryqVKmioKAgNWnSRKtWrbrk4xR23zia0nDDDTeoTp06+vHHH9WyZUuVKlVK1apV08SJE2W1Wu3un5mZqSeeeMLueT5kyJA8+yozM1MPPPCAIiIiVLp0ad1444369ddfndl0ysrK0rBhwxQdHa3g4GC1bt1aKSkpttvnzJkji8Wi9evX57nvuHHj5O/vr/379zv1mJJzxy5Hx4Lc49ScOXOUkJCgUqVKqX79+lq6dGmex9q5c6fuueceRUVFKTAwUFWqVFHv3r1tz2dHUxpy89m+fbvatWunkJAQRUZG6pFHHtGpU6fs+n/rrbfUqlUrVahQQSEhIapbt65efPHFIk07OHfunCZNmqTatWtr9OjRDttER0fr+uuvt1139tg+c+ZM1apVS8HBwWrSpIm+//57GWP00ksvKT4+XqVLl1bbtm3zFNW5z+Fvv/1W1113nYKDg1WpUiWNHj1aOTk5dm2dzakw+/G3335Tz549VaFCBQUGBiohIUFvvfWWXZvcfTlv3jw988wziomJUWhoqNq3b69ffvnFbizLli3Tn3/+aTflJte0adNUv359lS5dWmXKlFHt2rX19NNPO9wfKAQDFNLMmTONJPPjjz/axV977TUjycyYMcMYY0yfPn2Mv7+/qVq1qpkwYYJZtWqV+fLLL83p06dNvXr1TEhIiHn55ZfNihUrzOjRo42fn5+56aab7PqUZCpXrmwSExPNvHnzzJIlS8yNN95oJJmPP/7Y1m7nzp2mTJkypnr16mb27Nlm2bJl5p577jGSzKRJk2ztvv76ayPJVKpUydx1111myZIlZunSpSYtLc288MILRpJ56623zPr168369evNwYMHbWOJi4uz9WO1Wk2nTp2Mn5+fGT16tFmxYoV5+eWXTUhIiGnYsKHJysqytY2LizOxsbEmMTHRzJ4923z55Zfm7rvvNpLM6tWrC9zWhdlWWVlZZv369aZhw4amWrVqttwzMjLy7XfUqFFGkklNTbWLP/nkk0aS2bFjhzHGmGnTppkJEyaYJUuWmNWrV5v333/f1K9f39SqVcucPXvWdr+kpCQjycTFxZnhw4eb5ORk8+mnn9rddqHnnnvOvPrqq2bZsmXmm2++MdOnTzfx8fGmTZs2du369OljAgICTEJCgnn55ZfNypUrzbPPPmssFosZO3asrd25c+dMmzZtjJ+fn3niiSfM8uXLzZIlS8zTTz9t5s2bZ2v3wAMPGH9/f/P444+bL774wnz44Yemdu3aJioqyqSlpRW4L3Kf999//705d+6c3SU7O9uu7cCBA01AQIDtNbJq1Srj4+NjRo0aZWuze/du2/P7+uuvNwsXLjQff/yxadq0qfH39zfr1q3L89i7d++2xQq7bxzdt3Xr1iYiIsLUrFnTTJ8+3SQnJ5tBgwYZSeb999+3tTt58qRp0KCBKV++vJk8ebJZuXKlee2110xYWJhp27atsVqtxpjzr4c2bdqYwMBAM378eLNixQqTlJRkqlWrZiSZpKSkArdt7uuycuXKpmvXruazzz4zH3zwgalRo4YJDQ01f/zxhzHGmDNnzpjo6Ghz77332t3/3LlzJiYmxtx9990FPk5xj125t114LDDm/HGqatWq5pprrjEfffSRWb58ubnhhhuMn5+fLXdjjElNTTWlS5c2VatWNdOnTzerVq0yH3zwgenWrZvJzMy02xZff/217X65r4MqVarYtu+YMWOMn5+fufnmm+1yGTp0qJk2bZr54osvzFdffWVeffVVU758eXP//ffbtXM0joutW7fOSDLDhw8vsF0uZ4/tcXFxpnnz5mbRokXmk08+MVdddZUJDw83Q4cONV27djVLly41c+fONVFRUaZevXq255sx/zyHY2JizOuvv26+/PJL89hjjxlJ5uGHHy5yToXZj9u3bzdhYWGmbt26Zvbs2WbFihXm8ccfNz4+PmbMmDG2drn7smrVqubee+81y5YtM/PmzTNVqlQxNWvWtB03tm/fblq0aGGio6Ntx+/169cbY4yZN2+ekWQeffRRs2LFCrNy5Uozffp089hjjxVqnyAvCl4U2sVv/MePHzdLly41kZGRpkyZMrbCoU+fPkaSee+99+zuP336dCPJfPTRR3bxSZMmGUlmxYoVtpgkExwcbFeMZGdnm9q1a5saNWrYYj169DCBgYFm7969dn127tzZlCpVyhw7dswY888BqFW
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 800x600 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# apply PCA\n",
|
|||
|
"pca = PCA()\n",
|
|||
|
"pca.fit(X_scaled)\n",
|
|||
|
"\n",
|
|||
|
"print(f\"Proportion of variance explained by each principal component:\\n{pca.explained_variance_ratio_}\")\n",
|
|||
|
"\n",
|
|||
|
"# Plot the proportion of variance explained\n",
|
|||
|
"plt.figure(figsize=(8, 6))\n",
|
|||
|
"plt.bar(range(1, len(pca.explained_variance_ratio_) + 1), pca.explained_variance_ratio_, color='skyblue')\n",
|
|||
|
"plt.xlabel('Principal Component')\n",
|
|||
|
"plt.ylabel('Proportion of Variance Explained')\n",
|
|||
|
"plt.title('Proportion of Variance Explained by Principal Components')\n",
|
|||
|
"plt.xticks(range(1, len(pca.explained_variance_ratio_) + 1))\n",
|
|||
|
"plt.grid(axis='y', linestyle='--', alpha=0.7)\n",
|
|||
|
"plt.show()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 48,
|
|||
|
"id": "c3d40f19-8299-44ab-bb8a-326fdd2008c1",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"Contributions of features to the first principal component:\n",
|
|||
|
" Feature Contribution\n",
|
|||
|
"9 oldpeak 0.397006\n",
|
|||
|
"10 slope 0.352024\n",
|
|||
|
"12 thal 0.346208\n",
|
|||
|
"8 exang 0.333140\n",
|
|||
|
"11 ca 0.306420\n",
|
|||
|
"2 cp 0.286212\n",
|
|||
|
"0 age 0.285868\n",
|
|||
|
"3 trestbps 0.167841\n",
|
|||
|
"6 restecg 0.145938\n",
|
|||
|
"1 sex 0.116802\n",
|
|||
|
"4 chol 0.083504\n",
|
|||
|
"5 fbs 0.076094\n",
|
|||
|
"7 thalach -0.392706\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# get the loadings or weights of features in the first principal component\n",
|
|||
|
"first_pc_loadings = pca.components_[0]\n",
|
|||
|
"\n",
|
|||
|
"# create a DataFrame to display the contributions of features to the first principal component\n",
|
|||
|
"pc_loadings_df = pd.DataFrame({\"Feature\": X.columns, \"Contribution\": first_pc_loadings})\n",
|
|||
|
"pc_loadings_df = pc_loadings_df.sort_values(by=\"Contribution\", ascending=False)\n",
|
|||
|
"\n",
|
|||
|
"print(\"Contributions of features to the first principal component:\")\n",
|
|||
|
"print(pc_loadings_df)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "b68452ef-aa60-4b2f-aec4-4762f9136360",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"#### Interpretation:\n",
|
|||
|
"Die \"Proportion of Variance Explained by Principal Components\" gibt an, wie viel der gesamten Varianz in den Daten durch jede einzelne Hauptkomponente erklärt wird.\n",
|
|||
|
"Hauptkomponenten mit einer höheren Proportion der erklärten Varianz tragen mehr zur Erklärung der Variation in den Daten bei und sind daher wichtiger.\n",
|
|||
|
"Eine höhere Proportion der erklärten Varianz in einer bestimmten Hauptkomponente kann darauf hinweisen, dass diese Hauptkomponente wichtige Informationen über die Variation in den Merkmalen oder Beobachtungen enthält, die mit den zugrunde liegenden Mustern oder Gruppierungen in den Herzdaten zusammenhängen könnten. Dabei ist wichtig zu erwähnen, dass die Ergebnisse nicht in direktem Zusammenhang mit der goal-Variable stehen, sondern lediglich auf Muster in den Daten hinweisen.\n",
|
|||
|
"\n",
|
|||
|
"Merkmale, die hohe Contributions zu der ersten Hauptkomponente haben, tragen wesentlich zur Variation in den Daten bei. Diese Merkmale haben möglicherweise eine starke Korrelation mit der ersten Hauptkomponente und könnten daher wichtigere Informationen über die zugrunde liegenden Muster oder Strukturen in den Daten liefern, als jene mit einer geringen Contribution."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "e8dcc395-b548-4dde-bd46-67a962af6f03",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"### Cholesterinwerte im Vergleich Frauen/Männer"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 97,
|
|||
|
"id": "ed81d87f-a0b0-4dbb-88f6-471423ce3151",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stderr",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"/Users/nicole/anaconda3/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead\n",
|
|||
|
" if pd.api.types.is_categorical_dtype(vector):\n",
|
|||
|
"/Users/nicole/anaconda3/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead\n",
|
|||
|
" if pd.api.types.is_categorical_dtype(vector):\n",
|
|||
|
"/Users/nicole/anaconda3/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead\n",
|
|||
|
" if pd.api.types.is_categorical_dtype(vector):\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAArcAAAIhCAYAAABUopIpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAABGzklEQVR4nO3deXQUVf7+8afI1tkTAiSEfZd9FQZEAwYIqywqI7jAwHdkDDAgIIooiwsoM4MwoDgisiOjIyiDBgirgwGNLIqgqOwoIcoWliRAcn9/8KOPbVi6s9BQvF/n9DnpW7erPlWdNA+3b1VZxhgjAAAAwAaKebsAAAAAoLAQbgEAAGAbhFsAAADYBuEWAAAAtkG4BQAAgG0QbgEAAGAbhFsAAADYBuEWAAAAtkG4BQAAgG0QboFbyNdff60//elPqlSpkhwOh0JCQtSoUSNNmjRJx48fd/arWLGiOnfuXGjb3b9/vyzL0pw5cwptnb+VkpKicePG6eTJk0Wy/r59+6pixYpFsu6itGjRIk2ZMsXbZRSaVq1aqU6dOgVaR3Z2tl5//XXFxcUpKipKfn5+ioqKUqtWrfSvf/1Lp0+fLqRq88eyLA0aNOi6/ebMmSPLsrR///4iqePy+r/88svr9n3jjTeK7G8b8AbCLXCLmDlzpho3bqzU1FQ99dRTWrFihZYuXaoHH3xQb775pvr37+/tEvMtJSVF48ePL7Jw+/zzz2vp0qVFsu6iZLdwW1C//PKLWrRooWHDhqlGjRp66623tHbtWs2aNUv16tXTyJEjlZiY6O0ybzmEW9iNr7cLAHB9mzZt0hNPPKG2bdvqww8/VEBAgHNZ27ZtNXz4cK1YscKLFd6czp07p6CgIFWpUsXbpXjkct1w9cgjj2jHjh1avXq17rnnHpdl3bp109ixY5WUlOSl6gDcLBi5BW4BEyZMkGVZeuutt1yC7WX+/v6677778rSvWLFCjRo1UmBgoO644w698847efp888036tq1qyIjI+VwONSgQQPNnTvXrbp++OEH9e7dW6VKlVJAQIBq1qyp119/3aVPbm6uXnrpJdWoUUOBgYGKiIhQvXr1NHXqVEnSuHHj9NRTT0mSKlWqJMuyZFmW1q9f71zHv//9bzVv3lzBwcEKCQlRQkKCtm3b5rKdvn37KiQkRDt27FC7du0UGhqq+Ph457LfT0u4/PXx/PnzVbNmTQUFBal+/fpavny5s8/OnTtlWZbef/99Z9uWLVtkWZZq167tsr777rtPjRs3dmkrSN2tWrXSxx9/rAMHDjiPiWVZztedP39eL730ku644w4FBASoZMmS+tOf/qRffvnlqu/X77f5448/qmPHjgoJCVG5cuU0fPhwZWdnu/QdP368mjVrpuLFiyssLEyNGjXSrFmzZIzJs95FixapefPmCgkJUUhIiBo0aKBZs2bl6Zeamqq7775bQUFBqly5sl555RXl5uZes+bU1FStWrVKjz/+eJ5ge1lUVJQeeeQRlzZ3j9PatWvVqlUrRUVFKTAwUOXLl9f999+vc+fOOftkZ2frhRdeUM2aNeVwOBQVFaXWrVsrJSUlTy3X+r26ltWrVys+Pl5hYWEKCgrSXXfdpTVr1uTp991336lXr16Kjo5WQECAypcvr8ceeyzP+3f69Gk98cQTKlGihKKiotSjRw/9/PPPzuUVK1bUzp07tWHDBufv2K04hQdwYQDc1C5evGiCgoJMs2bN3H5NhQoVTNmyZU2tWrXMvHnzzMqVK82DDz5oJJkNGzY4+3333XcmNDTUVKlSxcybN898/PHHplevXkaSefXVV5399u3bZySZ2bNnO9t27txpwsPDTd26dc28efPMqlWrzPDhw02xYsXMuHHjnP0mTpxofHx8zNixY82aNWvMihUrzJQpU5x9Dh06ZAYPHmwkmSVLlphNmzaZTZs2mVOnThljjHn55ZeNZVmmX79+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c3PNhQsXnI+LFy86l7l7nP7zn/8YSWb79u1X3f68efOMJDNz5sxr1unu79Xvw+3Zs2dN8eLFTZcuXVzWl5OTY+rXr2+aNm3qbLv33ntNRESESU9Pv2odl9efmJjo0j5p0iQjyRw5csTZVrt2bRMXF3fN/QJuJUxLAGyqQYMGKl++vPO5w+FQ9erVdeDAAWfb2rVrFR8fr3Llyrm8tm/fvjp37pw2bdp0xXVnZWVpzZo16t69u4KCgnTx4kXno2PHjsrKytLmzZslSU2bNtVXX32lxMRErVy5UhkZGW7vw8qVK3Xx4kU99thjLttwOByKi4tzmbpw2f333+/2+lu3bq3Q0FDn8+joaJUqVcrlGMXHx2vv3r3at2+fsrKytHHjRrVv316tW7dWcnKypEtfJQcEBKhly5Y3pO7ly5crIiJCXbp0cVl/gwYNFBMTc8X1/55lWerSpYtLW7169Vz2Xbr0O9KmTRuFh4fLx8dHfn5+GjNmjI4dO6b09HRJUnJysnJycjRw4MDrbjcmJkZNmza97nbd9dFHH8nPz8/5CA8Pdy5z9zg1aNBA/v7+evzxxzV37lzt3bs3z3aSkpLkcDjUr1+/69bkzu/V76WkpOj48ePq06ePS625ublq3769UlNTdfbsWZ07d04bNmxQz549VbJkyevW8vvpSvXq1ZOkfB9v4FbACWXATa5EiRIKCgrSvn37PHpdVFRUnraAgABlZmY6nx87dkylS5fO0y82Nta5/EqOHTumixcvatq0aZo2bdoV+/z666+SpFGjRik4OFgLFizQm2++KR8fH91zzz169dVX1aRJk2vuw9GjRyVJd9555xWXFyvm+v/zoKAghYWFXXOdv+XOMWrTpo2kSwG2UqVKunDhgu69914dPXpUL774onPZXXfdpcDAwBtS99GjR3Xy5En5+/tfcfnlY38tQUFBcjgcLm0BAQHKyspyPv/iiy/Url07tWrVSjN
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 800x600 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"heart_df['sex'] = heart_df['sex'].replace({0: 'female', 1: 'male'})\n",
|
|||
|
"plt.figure(figsize=(8, 6))\n",
|
|||
|
"sns.boxplot(x='sex', y='chol', data=heart_df)\n",
|
|||
|
"plt.title('Cholesterinwerte nach Geschlecht')\n",
|
|||
|
"plt.xlabel('Geschlecht')\n",
|
|||
|
"plt.ylabel('Cholesterin in mg/dl')\n",
|
|||
|
"plt.show()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 65,
|
|||
|
"id": "3617a148-3ae4-4eb9-99d1-c7a6aa78bd17",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
" Geschlecht Untere_Grenze Obere_Grenze\n",
|
|||
|
"0 Männer 234.288517 246.199046\n",
|
|||
|
"1 Frauen 249.044612 275.413721\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# Konfidenzintervall (95%) für Cholesterin Level jeweils für Männer und Frauen\n",
|
|||
|
"from scipy import stats\n",
|
|||
|
"\n",
|
|||
|
"# Filtern nach Geschlecht und Berechnen des Konfidenzintervalls\n",
|
|||
|
"conf_level = 0.95\n",
|
|||
|
"chol_men = heart_df.loc[heart_df['sex'] == 'male', 'chol']\n",
|
|||
|
"chol_women = heart_df.loc[heart_df['sex'] == 'female', 'chol']\n",
|
|||
|
"conf_int_men = stats.t.interval(conf_level, len(chol_men) - 1, loc=chol_men.mean(), scale=stats.sem(chol_men))\n",
|
|||
|
"conf_int_women = stats.t.interval(conf_level, len(chol_women) - 1, loc=chol_women.mean(), scale=stats.sem(chol_women))\n",
|
|||
|
"\n",
|
|||
|
"result_table_men_vs_women = pd.DataFrame({\n",
|
|||
|
" 'Geschlecht': ['Männer', 'Frauen'],\n",
|
|||
|
" 'Untere_Grenze': [conf_int_men[0], conf_int_women[0]],\n",
|
|||
|
" 'Obere_Grenze': [conf_int_men[1], conf_int_women[1]]\n",
|
|||
|
"})\n",
|
|||
|
"\n",
|
|||
|
"print(result_table_men_vs_women)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "1b4ad40f-dd3b-4309-a5f3-a085d042c1eb",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"### Cholesterin im Vergleich zur Erkrankung"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 87,
|
|||
|
"id": "60f42093-3616-4e2b-8557-56794af3f25b",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stderr",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"/Users/nicole/anaconda3/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead\n",
|
|||
|
" if pd.api.types.is_categorical_dtype(vector):\n",
|
|||
|
"/Users/nicole/anaconda3/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead\n",
|
|||
|
" if pd.api.types.is_categorical_dtype(vector):\n",
|
|||
|
"/Users/nicole/anaconda3/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead\n",
|
|||
|
" if pd.api.types.is_categorical_dtype(vector):\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA1IAAAIhCAYAAABE54vcAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrA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nHj166IsvvrCHtYIsWrRIDzzwgDp27KiFCxeqevXq+vHHH/X999879Dt37pz69++v0aNHKyYmRl9//bWee+45+fv76+mnny50/xenE15q06ZNmjRpkho2bFik4yvI4MGDdc899+jBBx/UmTNnrto/Ly+vwPfk5SNFO3bs0N69ezVt2jTVqVNHlSpVyvecnJwcjRw5Up999pk++eSTfN+nKVOmqF27dlq4cKEqVKig6tWr688//5QkzZgxQ8HBwTp9+rQSExPVqVMnrVmzxh4qL3r99dd1yy236JVXXpF0YYps7969deDAAfn7+xd4jB988IGGDx+uUaNG6bXXXitwSurVFOVn165du9StWzfVq1dPS5Yskbe3txYuXKhly5ZZfj0ApcQEgFKWmppqSjLvueeeIj+nVq1aps1mM3/77Td7W1ZWllmlShVzzJgx9rZ77rnH9PT0NA8ePOjw/F69epne3t7mqVOnTNM0zQMHDpiSzMWLF9v79OjRwwwNDTXT09Mdnvvwww+bNpvNPHHihGmaptm3b1+zWbNmV6z3pZdeMiWZBw4ccGg/ePCg6e7ubo4fP96hPTMz0wwODjaHDBlibxsxYoQpyXznnXfy7X/EiBFmrVq1HNokmUFBQWZGRoa9LTU11axQoYI5Z86cK9Zb0PmYMWOGKcl8+eWXHfo2a9bMlGSuXLnS3nbu3DmzWrVq5uDBg6/4OqZpmuvWrTMlmXFxcfa2tLQ009PT0xw6dKhpmsVznj777DNTkrlgwQKH9jlz5piSzBkzZtjbFi9e7PD9OnPmjFmlShWzX79+Ds/Nzc01mzZtarZp06bQ52ZmZpp+fn5m+/btzby8vELPw8W6P/jgA4f23r17m/Xr1y/0eQX573//awYGBpqdO3c2c3JyTNMs+Ht60eXHf/F7/fTTTxfp9S5+Dwt7HDp0yN63Vq1appubm7lv374C6xg3bpyZlpZmtm/f3qxZs6b57bffFvhaHTp0uGpd58+fN8+dO2d26dLFHDRokL394rlo3Lixef78eXv7li1bTEnme++9Z28bMWKEWalSJdM0TfOFF14w3dzczBdffNHhdS6er8td/l64ePxF+dn1t7/9zaxUqZL5559/2ttyc3PNyMjIAn+WAHA+pvYBKDOaNWum8PBw+9c2m0316tXTb7/9Zm9bu3atunTpkm+ka+TIkfrrr7/y/RX/ouzsbK1Zs0aDBg2St7e3zp8/b3/07t1b2dnZ9ilAbdq00XfffaexY8fqiy++UEZGRpGP4YsvvtD58+c1fPhwh9ew2Wzq2LGjw/S/i+68884i779z587y9fW1fx0UFKTq1as7nCOrLr+WqUGDBjIMQ7169bK3ubu7q27dukV6nY4dO+rmm292mN63fPly5eTk2Kf1Fcd5Wr9+vaQLI2qXuvfee69aY0pKik6cOKERI0Y4vH5eXp569uyprVu3Fjpik5KSooyMDI0dO7bA6V+XMgxD/fr1c2hr0qSJpe9XamqqevbsqRo1aigxMVEVK1Ys8nMvZ+W9Jkkvvviitm7dmu8RFBTk0K9JkyaqV69egfs4cOCA2rVrp4yMDG3evLnQ6+MKq23hwoVq0aKFbDab3N3d5eHhoTVr1mjv3r35+vbp08dhRKlJkyaSlO98m6apMWPGaMaMGUpISLjiSHZRFOVn1/r163XHHXeoatW
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 1000x600 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# Boxplot gruppiert nach Diagnose\n",
|
|||
|
"plt.figure(figsize=(10, 6))\n",
|
|||
|
"sns.boxplot(x='healthy', y='chol', data=heart_df)\n",
|
|||
|
"plt.title('Cholesterin im Vergleich zur Erkrankung')\n",
|
|||
|
"plt.xlabel('Diagnose')\n",
|
|||
|
"plt.ylabel('Cholesterin in mg/dl')\n",
|
|||
|
"plt.show()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 91,
|
|||
|
"id": "7bef4e62-968e-4a2f-9948-063fac1e7a88",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
" Diagnose Untere_Grenze Obere_Grenze\n",
|
|||
|
"0 Gesund 234.397652 250.882836\n",
|
|||
|
"1 Krank 243.175250 259.774391\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# Filtern nach healthy und Berechnen des Konfidenzintervalls\n",
|
|||
|
"conf_level = 0.95\n",
|
|||
|
"chol_healthy = heart_df.loc[heart_df['healthy'] == 1, 'chol']\n",
|
|||
|
"chol_sick = heart_df.loc[heart_df['healthy'] == 0, 'chol']\n",
|
|||
|
"conf_int_healthy = stats.t.interval(conf_level, len(chol_healthy) - 1, loc=chol_healthy.mean(), scale=stats.sem(chol_healthy))\n",
|
|||
|
"conf_int_sick = stats.t.interval(conf_level, len(chol_sick) - 1, loc=chol_sick.mean(), scale=stats.sem(chol_sick))\n",
|
|||
|
"\n",
|
|||
|
"result_table_healthy_vs_sick = pd.DataFrame({\n",
|
|||
|
" 'Diagnose': ['Gesund', 'Krank'],\n",
|
|||
|
" 'Untere_Grenze': [conf_int_healthy[0], conf_int_sick[0]],\n",
|
|||
|
" 'Obere_Grenze': [conf_int_healthy[1], conf_int_sick[1]]\n",
|
|||
|
"})\n",
|
|||
|
"\n",
|
|||
|
"print(result_table_healthy_vs_sick)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 93,
|
|||
|
"id": "2aa2b03a-1da6-4846-be5d-1070fa1ae43e",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"t-Statistik: 1.4829209163941466\n",
|
|||
|
"p-Wert: 0.06957083510218263\n",
|
|||
|
"Der p-Wert ist größer als oder gleich 0.05, daher wird die Nullhypothese nicht abgelehnt.\n",
|
|||
|
"Es gibt keine signifikanten Hinweise darauf, dass der Cholesterinwert bei kranken Personen höher ist als bei gesunden Personen.\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# t-Test\n",
|
|||
|
"# Nullhypothese (HO) = Cholesterinwert bei Kranken ist gleich oder kleiner wie bei Gesunden\n",
|
|||
|
"# Alternativhypothese (H1) Cholesterinwert bei Kranken ist höher als bei Gesunden\n",
|
|||
|
"\n",
|
|||
|
"from scipy.stats import ttest_ind\n",
|
|||
|
"\n",
|
|||
|
"# Daten für gesunde und kranke Personen\n",
|
|||
|
"chol_healthy = heart_df.loc[heart_df['healthy'] == 1, 'chol']\n",
|
|||
|
"chol_sick = heart_df.loc[heart_df['healthy'] == 0, 'chol']\n",
|
|||
|
"\n",
|
|||
|
"# Durchführung des t-Tests\n",
|
|||
|
"t_statistic, p_value = ttest_ind(chol_sick, chol_healthy, alternative='greater')\n",
|
|||
|
"\n",
|
|||
|
"# Ausgabe der Ergebnisse\n",
|
|||
|
"print(\"t-Statistik:\", t_statistic)\n",
|
|||
|
"print(\"p-Wert:\", p_value)\n",
|
|||
|
"\n",
|
|||
|
"# Überprüfung der Nullhypothese\n",
|
|||
|
"if p_value < 0.05:\n",
|
|||
|
" print(\"Der p-Wert ist kleiner als 0.05, daher wird die Nullhypothese abgelehnt.\")\n",
|
|||
|
" print(\"Es gibt signifikante Hinweise darauf, dass der Cholesterinwert bei kranken Personen höher ist als bei gesunden Personen.\")\n",
|
|||
|
"else:\n",
|
|||
|
" print(\"Der p-Wert ist größer als oder gleich 0.05, daher wird die Nullhypothese nicht abgelehnt.\")\n",
|
|||
|
" print(\"Es gibt keine signifikanten Hinweise darauf, dass der Cholesterinwert bei kranken Personen höher ist als bei gesunden Personen.\")"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "c3852db8-3787-4ddf-aed7-80396f7a89ac",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"### Systolischer Ruheblutdruck"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 99,
|
|||
|
"id": "f063878c-5866-4a19-b065-a41d3e26e290",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stderr",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"/Users/nicole/anaconda3/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead\n",
|
|||
|
" if pd.api.types.is_categorical_dtype(vector):\n",
|
|||
|
"/Users/nicole/anaconda3/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead\n",
|
|||
|
" if pd.api.types.is_categorical_dtype(vector):\n",
|
|||
|
"/Users/nicole/anaconda3/lib/python3.11/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead\n",
|
|||
|
" if pd.api.types.is_categorical_dtype(vector):\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAArcAAAIhCAYAAABUopIpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAABk+klEQVR4nO3dd3hU1f7+/XsS0oAkkEJCIASlCRKKBFBEem8Kfg0alOo5KopUUVCaegQ5ShHOEQsQiogNsNBBqhQpIiKdAwQwMQIhgZACmf38wcP8HBNwdjLDhPB+Xddczl57z557Amw/WbP2WhbDMAwBAAAARYCHuwMAAAAAzkJxCwAAgCKD4hYAAABFBsUtAAAAigyKWwAAABQZFLcAnObq1au6PgHLn58DAHCrUNwCcIqdO3fKy8tLc+bM0YkTJ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Z5/Vr7/+qlGjRmn//v3avn373xZgf3bmzBnVr19fV65c0ciRI1WrVi2dO3dOK1euVEpKSp6zQ5w+fVodOnRQdna2tm7davt8zpCUlKTHH39cr7zyil5//XUtXbpUb775plJSUjR9+nRJktVq1cMPP6xNmzZp+PDhatSokU6ePKkxY8aoWbNm2rlzp13P5e7du3XgwAG99tpruuuuu1SiRIm/zXH16lXbf48ePaqXXnpJpUuXVseOHQv8GcuWLasVK1aoXbt26tevn55++mlJshW8a9eu1cMPP6wHHnhACxcuVE5OjiZOnHjDXzaWLFmiTZs2afTo0QoPD1eZMmUc+kXnupUrV6pz586qXr26Jk2apAoVKujEiRNatWrVDV+zYcMGde3aVU2aNNGCBQtUvHhxEz8BAGZQ3AJwi9DQUC1evNh2w0/jxo1VpUoVjR8/Xh999JEkqX///rr33nv1/fff28actm3bVmfPntXIkSPVs2dPeXj8vy+gWrZsqQ8++CDP9+vWrZsmTpwoSWrTpo3CwsLUo0cPff755+rRo4fDuUePHq2zZ8/q559/VvXq1W3tsbGxeR6/Z88edezYUZUqVdKSJUsUFBTk8Hs54ty5c/r666/VpUsXSdc+W0ZGht5//30NHz5cFSpU0Oeff64VK1boq6++Urdu3WyvrV27turXr6/4+Hg999xztvbk5GTt379fVatWdSjDX3+RkK4VpN9++63KlClT4M/o4+OjevXqSZLKly+fa6jIq6++qrCwMK1evdo2FKVt27a2nt+/unTpkn755ReVLl3a1mbmRrXnn39eFSpU0Pbt2+2GvvTp0yfP4+fPn69+/frp2Wef1eTJk+3+zgJwPv6FAXCLuLg4uzvZo6Ki1KhRI61bt06SdPToUR08eNBWeF69etX26NChgxITE3Xo0CG7cz766KM3fL+/FrCxsbEqVqyY7f0ctXz5cjVv3tyusL2RlStX6qGHHlKTJk20evVqpxe2kuTv728rbK+Li4uT1WrVxo0bJUnfffedSpUqpc6dO9v9HOvUqaPw8PBchV2tWrUcLmylazeU7dixQzt27ND27du1aNEiVa1aVR06dNDWrVsL/BlvJj09XTt27FC3bt3sCk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UFxeX63XJycnq2rWr/vGPfyg1NVVjxoyRr6+vRowYYTvmgw8+UPv27dW2bVv17t1b5cqV0/nz53XgwAHt3r1bX3zxhcM5Fy1apGLFiql169a22RJq1659wxvBbuT111/X8uXL1aRJE40cOVLR0dG6cOGCVqxYoSFDhuiee+6xO97Ly0uffvqpnn76af3f//2f5s6dqyeeeOKG569fv76qVaumYcOG6erVqypdurQWL16szZs353l8cHCwnnvuOSUkJKhq1apatmyZPvroIz333HOqUKGCJOn
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 800x600 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"plt.figure(figsize=(8, 6))\n",
|
|||
|
"sns.boxplot(x='sex', y='trestbps', data=heart_df)\n",
|
|||
|
"plt.title('Überblick über Blutdruck')\n",
|
|||
|
"plt.xlabel('Geschlecht')\n",
|
|||
|
"plt.ylabel('Systolischer Ruheblutdruck (in mmHg bei Aufnahme ins Krankenhaus)')\n",
|
|||
|
"plt.show()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 101,
|
|||
|
"id": "f20c6b80-bac5-489e-98e1-b5f2d9d90615",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
" Diagnose Untere_Grenze Obere_Grenze\n",
|
|||
|
"0 Gesund 126.751354 131.748646\n",
|
|||
|
"1 Krank 131.420542 137.716148\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# Filtern nach goal und Berechnen des Konfidenzintervalls\n",
|
|||
|
"conf_level = 0.95\n",
|
|||
|
"blutdruck_gesund = heart_df.loc[heart_df['healthy'] == 1, 'trestbps']\n",
|
|||
|
"blutdruck_krank = heart_df.loc[heart_df['healthy'] == 0, 'trestbps']\n",
|
|||
|
"conf_int_gesund = stats.t.interval(conf_level, len(blutdruck_gesund) - 1, loc=blutdruck_gesund.mean(), scale=stats.sem(blutdruck_gesund))\n",
|
|||
|
"conf_int_krank = stats.t.interval(conf_level, len(blutdruck_krank) - 1, loc=blutdruck_krank.mean(), scale=stats.sem(blutdruck_krank))\n",
|
|||
|
"\n",
|
|||
|
"# Erstellen der Tabelle\n",
|
|||
|
"result_table_blutdruck = pd.DataFrame({\n",
|
|||
|
" 'Diagnose': ['Gesund', 'Krank'],\n",
|
|||
|
" 'Untere_Grenze': [conf_int_gesund[0], conf_int_krank[0]],\n",
|
|||
|
" 'Obere_Grenze': [conf_int_gesund[1], conf_int_krank[1]]\n",
|
|||
|
"})\n",
|
|||
|
"\n",
|
|||
|
"# Anzeige der Tabelle\n",
|
|||
|
"print(result_table_blutdruck)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 111,
|
|||
|
"id": "bc919e7f-2b7b-41c1-a5b3-a97d61baa6ca",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"t-Statistik: 2.647004396805282\n",
|
|||
|
"p-Wert: 0.004274134464297464\n",
|
|||
|
"Der p-Wert ist kleiner als 0.05, daher wird die Nullhypothese abgelehnt.\n",
|
|||
|
"Es gibt signifikante Hinweise darauf, dass der Blutdruck bei kranken Personen höher ist als bei gesunden Personen.\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# t-Test\n",
|
|||
|
"# H0 Kranke haben einen niedrigeren oder gleichen Blutdruck wie Gesunde\n",
|
|||
|
"# H1 Kranke haben einen höheren BLutdruck als Gesunde\n",
|
|||
|
"\n",
|
|||
|
"# Daten für gesunde und kranke Personen\n",
|
|||
|
"blutdruck_healthy = heart_df.loc[heart_df['healthy'] == 1, 'trestbps']\n",
|
|||
|
"blutdruck_sick = heart_df.loc[heart_df['healthy'] == 0, 'trestbps']\n",
|
|||
|
"\n",
|
|||
|
"# Durchführung des t-Tests\n",
|
|||
|
"t_statistic, p_value = ttest_ind(blutdruck_sick, blutdruck_healthy, alternative='greater')\n",
|
|||
|
"\n",
|
|||
|
"print(\"t-Statistik:\", t_statistic)\n",
|
|||
|
"print(\"p-Wert:\", p_value)\n",
|
|||
|
"\n",
|
|||
|
"# Überprüfung Nullhypothese\n",
|
|||
|
"if p_value < 0.05:\n",
|
|||
|
" print(\"Der p-Wert ist kleiner als 0.05, daher wird die Nullhypothese abgelehnt.\")\n",
|
|||
|
" print(\"Es gibt signifikante Hinweise darauf, dass der Blutdruck bei kranken Personen höher ist als bei gesunden Personen.\")\n",
|
|||
|
"else:\n",
|
|||
|
" print(\"Der p-Wert ist größer als oder gleich 0.05, daher wird die Nullhypothese nicht abgelehnt.\")\n",
|
|||
|
" print(\"Es gibt keine signifikanten Hinweise darauf, dass der Blutdruck bei kranken Personen höher ist als bei gesunden Personen.\")"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "4a055d25-1c55-4a3f-a201-4ea252cac4dd",
|
|||
|
"metadata": {
|
|||
|
"editable": true,
|
|||
|
"slideshow": {
|
|||
|
"slide_type": ""
|
|||
|
},
|
|||
|
"tags": []
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Weitere Dinge die wir machen könnten\n",
|
|||
|
"- Korrelationsanalyse\n",
|
|||
|
"- logistische Regression, KNN, Entscheidungsbäume\n",
|
|||
|
"- ROC/AUC Kurve plotten für Modelle\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
"- Classification:\n",
|
|||
|
"- SVMs\n",
|
|||
|
"- Decision Trees\n",
|
|||
|
"- K-Nearest Neighbor\n",
|
|||
|
"- Random Forest\n",
|
|||
|
"\n",
|
|||
|
"Dimensionality Reduction\n",
|
|||
|
"- PCA\n",
|
|||
|
"\n",
|
|||
|
"Regression:\n",
|
|||
|
"- lineare \n",
|
|||
|
"- logistische\n",
|
|||
|
"\n",
|
|||
|
"Clustering:\n",
|
|||
|
"- hierarchisch -> bäume\n",
|
|||
|
"- K-means clustering\n",
|
|||
|
"\n",
|
|||
|
"Statistik:\n",
|
|||
|
"- Hypothesen"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"metadata": {
|
|||
|
"kernelspec": {
|
|||
|
"display_name": "Python 3 (ipykernel)",
|
|||
|
"language": "python",
|
|||
|
"name": "python3"
|
|||
|
},
|
|||
|
"language_info": {
|
|||
|
"codemirror_mode": {
|
|||
|
"name": "ipython",
|
|||
|
"version": 3
|
|||
|
},
|
|||
|
"file_extension": ".py",
|
|||
|
"mimetype": "text/x-python",
|
|||
|
"name": "python",
|
|||
|
"nbconvert_exporter": "python",
|
|||
|
"pygments_lexer": "ipython3",
|
|||
|
"version": "3.11.5"
|
|||
|
}
|
|||
|
},
|
|||
|
"nbformat": 4,
|
|||
|
"nbformat_minor": 5
|
|||
|
}
|