81 lines
3.1 KiB
Python
81 lines
3.1 KiB
Python
# %% [1] IMPORT LIBRARIES & LOAD DATA
|
|
import pandas as pd
|
|
import seaborn as sns
|
|
import matplotlib.pyplot as plt
|
|
from sklearn.preprocessing import LabelEncoder
|
|
import joblib
|
|
|
|
# Mengatur tema visualisasi agar terlihat profesional untuk laporan TA
|
|
plt.style.use('seaborn-v0_8-whitegrid')
|
|
|
|
print("--- [STEP 2] EDA, Visualisasi, & Preprocessing (Integrasi RIASEC) ---")
|
|
df = pd.read_csv('dataset_15mapel_bersih.csv')
|
|
|
|
# %% [2] VISUALISASI 1: DISTRIBUSI KELAS (SEBELUM ENCODING)
|
|
print("\nMenampilkan Grafik Distribusi Jurusan...")
|
|
plt.figure(figsize=(12, 10))
|
|
# Membuat grafik batang horizontal agar 30 nama jurusan terbaca jelas
|
|
sns.countplot(y='rekomendasi_jurusan', data=df,
|
|
order=df['rekomendasi_jurusan'].value_counts().index,
|
|
palette='mako')
|
|
plt.title('Distribusi Jumlah Siswa per Jurusan (Memastikan Data Seimbang)', fontsize=16, fontweight='bold')
|
|
plt.xlabel('Jumlah Siswa', fontsize=12)
|
|
plt.ylabel('Jurusan', fontsize=12)
|
|
plt.tight_layout()
|
|
plt.show()
|
|
|
|
# %% [3] PREPROCESSING TAHAP 1: HAPUS KOLOM TEKS (IDENTITAS)
|
|
print("\nMelakukan Preprocessing Dasar...")
|
|
|
|
# Kolom kategori teks yang akan kita pertahankan untuk di-encode
|
|
kolom_kategori_penting = ['ekonomi_orang_tua', 'rekomendasi_jurusan']
|
|
|
|
# Hapus kolom id_siswa dan nama_lengkap karena mesin tidak belajar dari nama/ID
|
|
for col in df.select_dtypes(include=['object']).columns:
|
|
if col not in kolom_kategori_penting:
|
|
df = df.drop(columns=[col])
|
|
print(f"✅ Kolom '{col}' berhasil dibuang.")
|
|
|
|
df = df.dropna()
|
|
|
|
# %% [4] PREPROCESSING TAHAP 2: LABEL ENCODING
|
|
print("\nMelakukan Encoding (Mengubah Teks menjadi Angka)...")
|
|
encoders = {}
|
|
|
|
for col in kolom_kategori_penting:
|
|
if col in df.columns:
|
|
le = LabelEncoder()
|
|
df[col] = le.fit_transform(df[col])
|
|
encoders[col] = le
|
|
print(f"✅ Kamus '{col}' tersimpan. Jumlah kelas: {len(le.classes_)}")
|
|
|
|
# Simpan encoder untuk digunakan di web/aplikasi (sangat penting untuk decode di PHP Laravel)
|
|
joblib.dump(encoders, 'encoders_15mapel.pkl')
|
|
|
|
# %% [5] VISUALISASI 2: HEATMAP KORELASI (SETELAH ENCODING)
|
|
print("\nMenampilkan Heatmap Korelasi Antar Fitur...")
|
|
plt.figure(figsize=(22, 18)) # Ukuran diperbesar karena fiturnya banyak (21 fitur)
|
|
|
|
# Membuat korelasi pearson
|
|
corr_matrix = df.corr()
|
|
|
|
# Menampilkan Heatmap
|
|
sns.heatmap(corr_matrix, annot=True, fmt='.2f', cmap='coolwarm',
|
|
vmin=-1, vmax=1, square=True, linewidths=.5, annot_kws={"size": 8})
|
|
plt.title('Heatmap Korelasi: 15 Mapel, 6 Skor RIASEC, Ekonomi, dan Target Jurusan', fontsize=18, fontweight='bold')
|
|
plt.xticks(rotation=45, ha='right', fontsize=10)
|
|
plt.yticks(fontsize=10)
|
|
plt.tight_layout()
|
|
plt.show()
|
|
|
|
# %% [6] MENYIMPAN DAFTAR FITUR & DATASET BERSIH
|
|
print("\nMenyimpan urutan kolom (features) agar saat testing di Laravel tidak tertukar posisinya...")
|
|
feature_columns = [col for col in df.columns if col != 'rekomendasi_jurusan']
|
|
joblib.dump(feature_columns, 'feature_columns_15mapel.pkl')
|
|
print(f"✅ Urutan fitur tersimpan: {feature_columns}")
|
|
|
|
# Simpan data yang sudah berupa angka semua (termasuk 6 kolom riasec)
|
|
df.to_csv('dataset_siswa_siap_model.csv', index=False)
|
|
print("\nData siap latih berhasil disimpan sebagai 'dataset_siswa_siap_model.csv'!")
|
|
# %%
|