DSA_SoSe_24/Cleaning.ipynb

291 lines
7.5 KiB
Plaintext
Raw Permalink Normal View History

2024-06-07 10:59:33 +02:00
{
"cells": [
{
"cell_type": "markdown",
"id": "c95fbd16-09ed-497b-892a-473496150996",
"metadata": {},
"source": [
"<h1>Cleaning</h1>\n",
"<p>Import dataset using the ucirepo package</p>"
]
},
{
"cell_type": "code",
2024-06-11 17:22:38 +02:00
"execution_count": 1,
2024-06-07 10:59:33 +02:00
"id": "3eb339fa-ef85-4544-9ad0-bc22d4de9f1a",
"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>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>goal</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>0</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>2</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>1</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>0</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>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" age sex cp trestbps chol fbs restecg thalach exang oldpeak slope \\\n",
"0 63 1 1 145 233 1 2 150 0 2.3 3 \n",
"1 67 1 4 160 286 0 2 108 1 1.5 2 \n",
"2 67 1 4 120 229 0 2 129 1 2.6 2 \n",
"3 37 1 3 130 250 0 0 187 0 3.5 3 \n",
"4 41 0 2 130 204 0 2 172 0 1.4 1 \n",
"\n",
" ca thal goal \n",
"0 0.0 6.0 0 \n",
"1 3.0 3.0 2 \n",
"2 2.0 7.0 1 \n",
"3 0.0 3.0 0 \n",
"4 0.0 3.0 0 "
]
},
2024-06-11 17:22:38 +02:00
"execution_count": 1,
2024-06-07 10:59:33 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from ucimlrepo import fetch_ucirepo\n",
"import pandas as pd\n",
"\n",
"# fetch dataset \n",
"heart_disease = fetch_ucirepo(id=45) \n",
" \n",
"# data (as pandas dataframes) \n",
"X = heart_disease.data.features \n",
"y = heart_disease.data.targets \n",
"\n",
"df = pd.concat([X, y], axis=1)\n",
"df = df.rename(columns={'num':'goal'})\n",
"\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"id": "8c5ab8b9-e46a-4968-b0c8-fe393f093f73",
"metadata": {},
"source": [
"<p>Get overview of all missing values. As there are only a few, those rows can be dropped.</p>"
]
},
{
"cell_type": "code",
2024-06-11 17:22:38 +02:00
"execution_count": 2,
2024-06-07 10:59:33 +02:00
"id": "6f7e6a3a-63cb-40e2-8746-937c24b184ef",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"age 0\n",
"sex 0\n",
"cp 0\n",
"trestbps 0\n",
"chol 0\n",
"fbs 0\n",
"restecg 0\n",
"thalach 0\n",
"exang 0\n",
"oldpeak 0\n",
"slope 0\n",
"ca 4\n",
"thal 2\n",
"goal 0\n",
"dtype: int64"
]
},
2024-06-11 17:22:38 +02:00
"execution_count": 2,
2024-06-07 10:59:33 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.isna().sum()"
]
},
{
"cell_type": "code",
2024-06-11 17:22:38 +02:00
"execution_count": 3,
2024-06-07 10:59:33 +02:00
"id": "d1639e92-d401-49fb-a1f1-67250ffa2c81",
"metadata": {},
"outputs": [],
"source": [
"df.dropna(inplace=True)"
]
},
2024-06-07 15:16:29 +02:00
{
"cell_type": "markdown",
"id": "192da26d-0fb1-4b06-a046-a41b66576ed0",
"metadata": {},
"source": [
"<h1>Preprocessing</h1>\n",
"<p>Split</p>"
]
},
2024-06-07 10:59:33 +02:00
{
"cell_type": "code",
2024-06-11 17:22:38 +02:00
"execution_count": 4,
2024-06-07 15:16:29 +02:00
"id": "24675f41-d48f-4e27-a3d8-e303556ee7d1",
"metadata": {},
"outputs": [],
"source": [
"df['goal'] = df['goal'].replace({0: 0, 1: 1, 2: 1, 3: 1, 4: 1})"
]
},
{
"cell_type": "code",
2024-06-11 17:22:38 +02:00
"execution_count": 5,
2024-06-07 10:59:33 +02:00
"id": "d7bf2c46-7885-4dfe-a4e7-8b8439cf0434",
"metadata": {},
"outputs": [],
"source": [
2024-06-07 13:38:26 +02:00
"# save 'cleaned' dataset as csv file for further processing\n",
2024-06-07 15:16:29 +02:00
"import os\n",
"os.makedirs('data', exist_ok=True)\n",
2024-06-07 10:59:33 +02:00
"df.to_csv('./data/dataset_cleaned.csv', index=False)"
]
}
],
"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",
2024-06-11 17:22:38 +02:00
"version": "3.11.7"
2024-06-07 10:59:33 +02:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}