1 line
15 KiB
Plaintext
1 line
15 KiB
Plaintext
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyNE/AVfPCndZRo3VrKym5D7"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","execution_count":2,"metadata":{"id":"Uy-HrhzJJ3Nn","executionInfo":{"status":"ok","timestamp":1793973707520,"user_tz":-420,"elapsed":39,"user":{"displayName":"Elfan Cris","userId":"15481999268029990356"}}},"outputs":[],"source":["import pandas as pd\n","\n","import numpy as np\n","\n","import matplotlib.pyplot as plt\n","\n","import seaborn as sns\n","\n","\n","from sklearn.model_selection import train_test_split\n","\n","from sklearn.tree import DecisionTreeClassifier\n","\n","from sklearn.ensemble import RandomForestClassifier\n","\n","from sklearn.metrics import accuracy_score, classification_report, confusion_matrix"]},{"cell_type":"code","source":["# Membaca file CSV\n","df = pd.read_csv('DataSet-Tugas-1.csv')\n","\n","# Menampilkan 5 baris pertama untuk memastikan data termuat dengan benar\n","df.head()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":206},"id":"nUVSP27_KRsM","executionInfo":{"status":"ok","timestamp":1793973715866,"user_tz":-420,"elapsed":103,"user":{"displayName":"Elfan Cris","userId":"15481999268029990356"}},"outputId":"979a4fcf-71f2-448f-e248-d2c239e3d480"},"execution_count":3,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" Hematokrit Hemoglobin Jumlah Eritrosit Jumlah Lekosit Jumlah Trombosit \\\n","0 45.0 15.2 5.81 5.9 219 \n","1 45.0 15.3 5.49 6.2 312 \n","2 47.5 16.6 5.84 7.4 244 \n","3 45.4 15.0 5.33 6.7 342 \n","4 44.9 15.8 5.19 7.0 328 \n","\n"," MCH MCHC MCV SOURCE \n","0 26.2 33.8 77.5 rwj \n","1 27.9 34.0 82.0 rwj \n","2 28.4 34.9 81.3 rwj \n","3 28.1 33.0 85.2 rwj \n","4 30.4 35.2 86.5 rwj "],"text/html":["\n"," <div id=\"df-673bfa32-5e2f-4f6b-8bc4-0ec7a512e06b\" class=\"colab-df-container\">\n"," <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>Hematokrit</th>\n"," <th>Hemoglobin</th>\n"," <th>Jumlah Eritrosit</th>\n"," <th>Jumlah Lekosit</th>\n"," <th>Jumlah Trombosit</th>\n"," <th>MCH</th>\n"," <th>MCHC</th>\n"," <th>MCV</th>\n"," <th>SOURCE</th>\n"," </tr>\n"," </thead>\n"," <tbody>\n"," <tr>\n"," <th>0</th>\n"," <td>45.0</td>\n"," <td>15.2</td>\n"," <td>5.81</td>\n"," <td>5.9</td>\n"," <td>219</td>\n"," <td>26.2</td>\n"," <td>33.8</td>\n"," <td>77.5</td>\n"," <td>rwj</td>\n"," </tr>\n"," <tr>\n"," <th>1</th>\n"," <td>45.0</td>\n"," <td>15.3</td>\n"," <td>5.49</td>\n"," <td>6.2</td>\n"," <td>312</td>\n"," <td>27.9</td>\n"," <td>34.0</td>\n"," <td>82.0</td>\n"," <td>rwj</td>\n"," </tr>\n"," <tr>\n"," <th>2</th>\n"," <td>47.5</td>\n"," <td>16.6</td>\n"," <td>5.84</td>\n"," <td>7.4</td>\n"," <td>244</td>\n"," <td>28.4</td>\n"," <td>34.9</td>\n"," <td>81.3</td>\n"," <td>rwj</td>\n"," </tr>\n"," <tr>\n"," <th>3</th>\n"," <td>45.4</td>\n"," <td>15.0</td>\n"," <td>5.33</td>\n"," <td>6.7</td>\n"," <td>342</td>\n"," <td>28.1</td>\n"," <td>33.0</td>\n"," <td>85.2</td>\n"," <td>rwj</td>\n"," </tr>\n"," <tr>\n"," <th>4</th>\n"," <td>44.9</td>\n"," <td>15.8</td>\n"," <td>5.19</td>\n"," <td>7.0</td>\n"," <td>328</td>\n"," <td>30.4</td>\n"," <td>35.2</td>\n"," <td>86.5</td>\n"," <td>rwj</td>\n"," </tr>\n"," </tbody>\n","</table>\n","</div>\n"," <div class=\"colab-df-buttons\">\n","\n"," <div class=\"colab-df-container\">\n"," <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-673bfa32-5e2f-4f6b-8bc4-0ec7a512e06b')\"\n"," title=\"Convert this dataframe to an interactive table.\"\n"," style=\"display:none;\">\n","\n"," <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n"," <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n"," </svg>\n"," </button>\n","\n"," <style>\n"," .colab-df-container {\n"," display:flex;\n"," gap: 12px;\n"," }\n","\n"," .colab-df-convert {\n"," background-color: #E8F0FE;\n"," border: none;\n"," border-radius: 50%;\n"," cursor: pointer;\n"," display: none;\n"," fill: #1967D2;\n"," height: 32px;\n"," padding: 0 0 0 0;\n"," width: 32px;\n"," }\n","\n"," .colab-df-convert:hover {\n"," background-color: #E2EBFA;\n"," box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n"," fill: #174EA6;\n"," }\n","\n"," .colab-df-buttons div {\n"," margin-bottom: 4px;\n"," }\n","\n"," [theme=dark] .colab-df-convert {\n"," background-color: #3B4455;\n"," fill: #D2E3FC;\n"," }\n","\n"," [theme=dark] .colab-df-convert:hover {\n"," background-color: #434B5C;\n"," box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n"," filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n"," fill: #FFFFFF;\n"," }\n"," </style>\n","\n"," <script>\n"," const buttonEl =\n"," document.querySelector('#df-673bfa32-5e2f-4f6b-8bc4-0ec7a512e06b button.colab-df-convert');\n"," buttonEl.style.display =\n"," google.colab.kernel.accessAllowed ? 'block' : 'none';\n","\n"," async function convertToInteractive(key) {\n"," const element = document.querySelector('#df-673bfa32-5e2f-4f6b-8bc4-0ec7a512e06b');\n"," const dataTable =\n"," await google.colab.kernel.invokeFunction('convertToInteractive',\n"," [key], {});\n"," if (!dataTable) return;\n","\n"," const docLinkHtml = 'Like what you see? Visit the ' +\n"," '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n"," + ' to learn more about interactive tables.';\n"," element.innerHTML = '';\n"," dataTable['output_type'] = 'display_data';\n"," await google.colab.output.renderOutput(dataTable, element);\n"," const docLink = document.createElement('div');\n"," docLink.innerHTML = docLinkHtml;\n"," element.appendChild(docLink);\n"," }\n"," </script>\n"," </div>\n","\n","\n"," </div>\n"," </div>\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"df","summary":"{\n \"name\": \"df\",\n \"rows\": 4517,\n \"fields\": [\n {\n \"column\": \"Hematokrit\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 6.2491782216471,\n \"min\": 13.7,\n \"max\": 71.3,\n \"num_unique_values\": 357,\n \"samples\": [\n 50.9,\n 46.7,\n 58.1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Hemoglobin\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2.2005436286635716,\n \"min\": 3.8,\n \"max\": 24.9,\n \"num_unique_values\": 153,\n \"samples\": [\n 9.0,\n 8.4,\n 17.5\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Jumlah Eritrosit\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.7885816164402666,\n \"min\": 1.48,\n \"max\": 7.86,\n \"num_unique_values\": 438,\n \"samples\": [\n 6.47,\n 4.94,\n 6.54\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Jumlah Lekosit\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 5.37338679582883,\n \"min\": 1.1,\n \"max\": 76.6,\n \"num_unique_values\": 296,\n \"samples\": [\n 18.7,\n 13.0,\n 9.5\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Jumlah Trombosit\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 113,\n \"min\": 8,\n \"max\": 1183,\n \"num_unique_values\": 556,\n \"samples\": [\n 355,\n 421,\n 305\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"MCH\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2.796720521352356,\n \"min\": 14.9,\n \"max\": 40.8,\n \"num_unique_values\": 210,\n \"samples\": [\n 31.0,\n 17.5,\n 23.4\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"MCHC\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1.238716289689131,\n \"min\": 26.0,\n \"max\": 39.0,\n \"num_unique_values\": 106,\n \"samples\": [\n 36.2,\n 33.5,\n 35.2\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"MCV\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 7.140812899585524,\n \"min\": 54.0,\n \"max\": 115.6,\n \"num_unique_values\": 432,\n \"samples\": [\n 115.3,\n 94.9,\n 99.7\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"SOURCE\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"rwi\",\n \"rwj\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":3}]},{"cell_type":"code","source":["# Memeriksa informasi umum struktur data & tipe datanya\n","df.info()\n","\n","# Memisahkan Fitur (X) dan Target (y) secara benar:\n","# X adalah semua kolom dari awal sampai kolom ke-7 (kolom fitur laboratorium)\n","X = df.iloc[:, :-1] # atau df.drop(columns=['SOURCE'])\n","\n","# y adalah kolom terakhir yaitu 'SOURCE' (target kelas: 'rwj' / 'rwi')\n","y = df.iloc[:, -1] # atau df['SOURCE']\n","\n","# Membagi data menjadi Data Latih (80%) dan Data Uji (20%)\n","X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"raBgTpzjKSrX","executionInfo":{"status":"ok","timestamp":1793973741239,"user_tz":-420,"elapsed":1514,"user":{"displayName":"Elfan Cris","userId":"15481999268029990356"}},"outputId":"d6294b7f-6443-4551-cc4d-39fa6fcea60f"},"execution_count":4,"outputs":[{"output_type":"stream","name":"stdout","text":["<class 'pandas.core.frame.DataFrame'>\n","RangeIndex: 4517 entries, 0 to 4516\n","Data columns (total 9 columns):\n"," # Column Non-Null Count Dtype \n","--- ------ -------------- ----- \n"," 0 Hematokrit 4517 non-null float64\n"," 1 Hemoglobin 4517 non-null float64\n"," 2 Jumlah Eritrosit 4517 non-null float64\n"," 3 Jumlah Lekosit 4517 non-null float64\n"," 4 Jumlah Trombosit 4517 non-null int64 \n"," 5 MCH 4517 non-null float64\n"," 6 MCHC 4517 non-null float64\n"," 7 MCV 4517 non-null float64\n"," 8 SOURCE 4517 non-null object \n","dtypes: float64(7), int64(1), object(1)\n","memory usage: 317.7+ KB\n"]}]},{"cell_type":"code","source":["# Inisialisasi dan melatih model Decision Tree\n","model_1 = DecisionTreeClassifier(random_state=42)\n","model_1.fit(X_train, y_train)\n","\n","print(\"Model 1 (Decision Tree) berhasil dilatih!\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"YjBvt2pHKVug","executionInfo":{"status":"ok","timestamp":1793973763963,"user_tz":-420,"elapsed":36,"user":{"displayName":"Elfan Cris","userId":"15481999268029990356"}},"outputId":"1f984531-8551-451d-e94b-971d2a3da22d"},"execution_count":5,"outputs":[{"output_type":"stream","name":"stdout","text":["Model 1 (Decision Tree) berhasil dilatih!\n"]}]},{"cell_type":"code","source":["# Inisialisasi dan melatih model Random Forest (sebagai pembanding)[cite: 2]\n","model_2 = RandomForestClassifier(random_state=42)\n","model_2.fit(X_train, y_train)\n","\n","print(\"Model 2 (Random Forest) berhasil dilatih!\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"EIaNh-9tKY7Y","executionInfo":{"status":"ok","timestamp":1793973775285,"user_tz":-420,"elapsed":2669,"user":{"displayName":"Elfan Cris","userId":"15481999268029990356"}},"outputId":"7b25f28d-0c4b-4c43-f427-7acdf91068bd"},"execution_count":6,"outputs":[{"output_type":"stream","name":"stdout","text":["Model 2 (Random Forest) berhasil dilatih!\n"]}]},{"cell_type":"code","source":["# Evaluasi Model 1 (Decision Tree)\n","y_pred_1 = model_1.predict(X_test)\n","acc_1 = accuracy_score(y_test, y_pred_1)\n","print(f\"Akurasi Model 1 (Decision Tree): {acc_1 * 100:.2f}%\")\n","print(classification_report(y_test, y_pred_1))\n","print(\"-\" * 60)\n","\n","# Evaluasi Model 2 (Random Forest)\n","y_pred_2 = model_2.predict(X_test)\n","acc_2 = accuracy_score(y_test, y_pred_2)\n","print(f\"Akurasi Model 2 (Random Forest): {acc_2 * 100:.2f}%\")\n","print(classification_report(y_test, y_pred_2))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"hDiRPgx1KdY4","executionInfo":{"status":"ok","timestamp":1793973789556,"user_tz":-420,"elapsed":42,"user":{"displayName":"Elfan Cris","userId":"15481999268029990356"}},"outputId":"70ac9cfe-a29e-4d65-ff9c-4ff40d020d8a"},"execution_count":7,"outputs":[{"output_type":"stream","name":"stdout","text":["Akurasi Model 1 (Decision Tree): 65.49%\n"," precision recall f1-score support\n","\n"," rwi 0.59 0.60 0.59 381\n"," rwj 0.70 0.69 0.70 523\n","\n"," accuracy 0.65 904\n"," macro avg 0.65 0.65 0.65 904\n","weighted avg 0.66 0.65 0.66 904\n","\n","------------------------------------------------------------\n","Akurasi Model 2 (Random Forest): 74.12%\n"," precision recall f1-score support\n","\n"," rwi 0.71 0.64 0.68 381\n"," rwj 0.76 0.81 0.78 523\n","\n"," accuracy 0.74 904\n"," macro avg 0.74 0.73 0.73 904\n","weighted avg 0.74 0.74 0.74 904\n","\n"]}]}]} |