# -*- coding: utf-8 -*-
"""
Created on Wed Apr 10 22:44:41 2024
@author: 20279
"""

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import RFE
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score

# Read Excel data
data = pd.read_excel("C:/Users/fdy12/Desktop/workbook1.xlsx")

# Separate features and labels
X = data.iloc[:, :-1]
y = data.iloc[:, -1]

# Split training and validation sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Use Random Forest classifier
clf = RandomForestClassifier(n_estimators=100, random_state=42)

# Perform RFE for feature selection
selector = RFE(clf, n_features_to_select=1, step=1)
selector = selector.fit(X_train, y_train)

# Get feature importance ranking
feature_importances = selector.ranking_

# Compute cross-validation scores
cv_scores = cross_val_score(clf, X_train, y_train, cv=5)

# Plot cross-validation score vs number of features
plt.figure(figsize=(10, 5))
plt.plot(range(1, len(cv_scores) + 1), cv_scores, marker='o', linestyle='--')
plt.xlabel('Number of Features')
plt.ylabel('Cross-Validated Accuracy')
plt.title('Cross-Validation Score vs Number of Features')
plt.grid(True)
plt.show()

# Plot feature importance ranking
plt.figure(figsize=(10, 5))
plt.bar(range(len(feature_importances)), feature_importances, align='center')
plt.xlabel('Feature Index')
plt.ylabel('Importance Rank')
plt.title('Feature Importance Rank')
plt.grid(axis='y')
plt.show()

# Output optimal feature set
optimal_features = X_train.columns[selector.support_]
print("Optimal feature set:", optimal_features)
