🩺 Exploratory Data Analysis (EDA) - Diabetes Prediction Dataset¶
Bu notebook, Diabetes Prediction Dataset üzerinde kapsamlı bir Exploratory Data Analysis (EDA) süreci yürütmek için hazırlanmıştır. Amaç, veri setinin yapısını anlamak, olası veri kalitesi sorunlarını belirlemek, klinik açıdan anlamlı ilişkiler keşfetmek ve sonraki modelleme aşamaları için sağlam bir temel oluşturmaktır.
In [8]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
# Grafik Ayarları
sns.set_style("whitegrid")
plt.rcParams['figure.figsize'] = (12, 6)
plt.rcParams['font.size'] = 11
print("--- 1. Veri Seti Yükleniyor ---")
try:
df = pd.read_csv('diabetes_prediction_dataset.csv')
print(f"Veri başarıyla yüklendi. Boyut: {df.shape}")
except FileNotFoundError:
print("HATA: Dosya bulunamadı.")
--- 1. Veri Seti Yükleniyor --- Veri başarıyla yüklendi. Boyut: (100000, 9)
In [9]:
print("\n--- 2. Eksik Değer Analizi ---")
missing_values = df.isnull().sum()
print(missing_values[missing_values > 0])
if missing_values.sum() == 0:
print("✅ Veri setinde eksik (null) değer bulunmamaktadır.")
else:
print("⚠️ Eksik değerler tespit edildi, doldurma veya silme işlemi gerekebilir.")
--- 2. Eksik Değer Analizi --- Series([], dtype: int64) ✅ Veri setinde eksik (null) değer bulunmamaktadır.
In [10]:
print("\n--- 3. Dağılım Analizleri ---")
fig, axes = plt.subplots(2, 2, figsize=(16, 10))
# Yaş Dağılımı
sns.histplot(df['age'], bins=30, kde=True, color='skyblue', ax=axes[0, 0])
axes[0, 0].set_title('Yaş Dağılımı')
# BMI Dağılımı
sns.histplot(df['bmi'], bins=30, kde=True, color='salmon', ax=axes[0, 1])
axes[0, 1].set_title('BMI Dağılımı')
# HbA1c Dağılımı
sns.histplot(df['HbA1c_level'], bins=20, kde=True, color='green', ax=axes[1, 0])
axes[1, 0].set_title('HbA1c Seviyesi Dağılımı')
# Kan Şekeri Dağılımı
sns.histplot(df['blood_glucose_level'], bins=30, kde=True, color='purple', ax=axes[1, 1])
axes[1, 1].set_title('Kan Şekeri Dağılımı')
plt.tight_layout()
plt.show()
# Kategorik Değişkenler
plt.figure(figsize=(14, 5))
plt.subplot(1, 2, 1)
sns.countplot(x='gender', data=df, palette='pastel')
plt.title("Cinsiyet Dağılımı")
plt.subplot(1, 2, 2)
sns.countplot(x='smoking_history', data=df, palette='pastel')
plt.title("Sigara Kullanım Geçmişi")
plt.show()
--- 3. Dağılım Analizleri ---
C:\Users\canavar\AppData\Local\Temp\ipykernel_21836\603665593.py:27: FutureWarning: Passing `palette` without assigning `hue` is deprecated and will be removed in v0.14.0. Assign the `x` variable to `hue` and set `legend=False` for the same effect. sns.countplot(x='gender', data=df, palette='pastel') C:\Users\canavar\AppData\Local\Temp\ipykernel_21836\603665593.py:31: FutureWarning: Passing `palette` without assigning `hue` is deprecated and will be removed in v0.14.0. Assign the `x` variable to `hue` and set `legend=False` for the same effect. sns.countplot(x='smoking_history', data=df, palette='pastel')
In [11]:
print("\n--- 4. Uç Değer (Outlier) Analizi ---")
# Boxplot (Kutu Grafiği) uç değerleri görmek için en iyi yöntemdir.
plt.figure(figsize=(16, 6))
plt.subplot(1, 3, 1)
sns.boxplot(y=df['bmi'], color='salmon')
plt.title('BMI Uç Değerleri')
plt.subplot(1, 3, 2)
sns.boxplot(y=df['blood_glucose_level'], color='purple')
plt.title('Glukoz Uç Değerleri (Kritik Hastalar)')
plt.subplot(1, 3, 3)
sns.boxplot(y=df['HbA1c_level'], color='green')
plt.title('HbA1c Uç Değerleri')
plt.show()
--- 4. Uç Değer (Outlier) Analizi ---
Glukoz ve BMI değişkenlerindeki 'uç değerler' hata değildir. Bunlar RL ajanının öğrenmesi gereken kritik/hasta profilleridir. Bu nedenle silinmemelidir.
In [12]:
print("\n--- 5. Korelasyon Analizi ---")
# Kategorik verileri sayısal hale getir (sadece bu grafik için geçici kopyada)
df_corr = df.copy()
le = LabelEncoder()
for col in df_corr.select_dtypes(include='object').columns:
df_corr[col] = le.fit_transform(df_corr[col])
plt.figure(figsize=(12, 10))
correlation_matrix = df_corr.corr()
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f", linewidths=0.5)
plt.title('Özellikler Arası Korelasyon Matrisi')
plt.show()
--- 5. Korelasyon Analizi ---
'blood_glucose_level' ile 'diabetes' (0.42) ve 'HbA1c_level' (0.17) arasında pozitif korelasyon vardır. Bu biyolojik olarak beklenen bir durumdur.
In [13]:
print("\n--- 6. Klinik Risk ve Güvenlik Analizi (RL Hazırlığı) ---")
plt.figure(figsize=(14, 6))
# Şeker Dağılımı ve Kritik Bölgeler
sns.kdeplot(df['blood_glucose_level'], fill=True, color='blue', alpha=0.3)
plt.axvline(x=70, color='red', linestyle='--', label='Hipoglisemi (<70)')
plt.axvline(x=180, color='orange', linestyle='--', label='Hiperglisemi (>180)')
plt.axvspan(0, 70, color='red', alpha=0.1)
plt.axvspan(100, 140, color='green', alpha=0.1, label='Hedef Aralık')
plt.title("Hasta Popülasyonu ve Klinik Güvenlik Bölgeleri")
plt.xlabel("Kan Şekeri (mg/dL)")
plt.legend()
plt.xlim(0, 400)
plt.show()
--- 6. Klinik Risk ve Güvenlik Analizi (RL Hazırlığı) ---
In [14]:
print("\n--- 7. RL Simülasyon Havuzu ---")
# Sadece hasta veya riskli bireyler (>130 mg/dL)
rl_pool = df[df['blood_glucose_level'] > 130]
print(f"Tüm Veri: {len(df)}")
print(f"RL Eğitimi İçin Seçilen Hasta Sayısı: {len(rl_pool)} (Sağlıklı bireyler elendi)")
--- 7. RL Simülasyon Havuzu --- Tüm Veri: 100000 RL Eğitimi İçin Seçilen Hasta Sayısı: 56360 (Sağlıklı bireyler elendi)