import matplotlib.pyplot as plt from sklearn.preprocessing import StandardScaler # Separate features and labels X = balanced_data.drop(columns=['Label']) y = balanced_data['Label'] # Normalize the features scaler = StandardScaler() X_scaled = scaler.fit_transform(X) # Perform LDA lda = LDA_fs(m=1) # Instanciate LDA object with 1 axis X_lda = lda.fit_transform(X_scaled, y) # Fit the model and project the data # Plot the LDA projection plt.figure(figsize=(10, 6)) plt.hist(X_lda[y == 'Good'], bins=20, alpha=0.7, label='Good', color='green') plt.hist(X_lda[y == 'Bad'], bins=20, alpha=0.7, label='Bad', color='red') plt.title("LDA Projection of Good and Bad Parts") plt.xlabel("LDA Component") plt.ylabel("Frequency") plt.legend() plt.show() # Examine feature contributions to the LDA component feature_importance = pd.DataFrame({'Feature': X.columns, 'LDA Coefficient': lda.W[0]}) feature_importance = feature_importance.sort_values(by='LDA Coefficient', ascending=False) # Display feature importance print("Feature Contributions to LDA Component:") print(feature_importance)