import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder
import joblib

# Memuat data dari file Excel
data = pd.read_excel('D:/Skripsi/ekstraksi fitur/data_fitur.xlsx')

# Pisahkan fitur dan label
X = data[['Jumlah Objek', 'Eccentricity Rata-Rata', 'Metric Rata-Rata', 'Area Total', 'Perimeter Total']]
y = data['Label']

# Encode label menjadi numerik
le = LabelEncoder()
y = le.fit_transform(y)

# Bagi data menjadi subset pelatihan dan pengujian
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=9)

# Inisialisasi dan latih model Random Forest
rf_model = RandomForestClassifier(n_estimators=13, random_state=9)
rf_model.fit(X_train, y_train)

# Simpan model yang telah dilatih
model_path = 'D:/Skripsi/ekstraksi fitur/rf_model.joblib'
joblib.dump(rf_model, model_path)
print(f"Model disimpan di {model_path}")

# Prediksi kelas pada subset pengujian
y_pred = rf_model.predict(X_test)

# Evaluasi kinerja model
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

# Simpan LabelEncoder
label_encoder_path = 'D:/Skripsi/ekstraksi fitur/label_encoder.joblib'
joblib.dump(le, label_encoder_path)
print(f"Label encoder disimpan di {label_encoder_path}")
