def predict(model, X_test, y_test): X_train_encoded, y_train, X_test_encoded, y_test, encoder = load_encoded_data( ) y_hat = model.predict(X_test_encoded) #print("Accuracy Score: ", "\n") print("Accuracy Score: ", accuracy_score(y_test, y_hat))
def get(self): X_train_encoded, y_train, X_test_encoded, y_test, encoder = data_loading.load_encoded_data( ) print("possible instances: ", X_test_encoded.shape[0]) possible_instance = [] for i in range(0, X_test_encoded.shape[0]): possible_instance.append(i) return possible_instance
def post(self): reqparse_args = reqparse.RequestParser() reqparse_args.add_argument("chosen_model", type=str) args = reqparse_args.parse_args() X_train_encoded, y_train, X_test_encoded, y_test, encoder = data_loading.load_encoded_data( ) # Choose a model and train it model, model_name = model_training.model_selection( args["chosen_model"]) model = model_training.train_model(X_train_encoded, y_train, model) model_training.save_trained_model(model, model_name) return "model successfully trained"
def get(self): reqparse_args = reqparse.RequestParser() reqparse_args.add_argument("chosen_model", type=str) args = reqparse_args.parse_args() model = model_training.load_trained_model(args["chosen_model"]) X_train_encoded, y_train, X_test_encoded, y_test, encoder = data_loading.load_encoded_data( ) y_hat = model.predict(X_test_encoded) y_score = model.predict.proba(X_test_encoded[:, 1]) print("ChosenModel: Accuracy score", accuracy_score(y_test, y_hat)) print("ChosenModel: AUC", roc_auc_score(y_test, y_score)) # need proba? return "run successfully"
def main_global(model_name): """Main function for the global explanations.""" X_train_encoded, y_train, X_test_encoded, y_test, encoder = data_loading.load_encoded_data() print(X_train_encoded) print(type(X_train_encoded)) X_test, y_test = data_loading.load_data(type="testing") model = model_training.load_trained_model(model_name) # categorical_features_indices = dataset["german_credit_dataset"]["categorical_features_indices"] categorical_features_names, categorical_features_indices, feature_names = data_loading.analyze_dataset() df_credit = data_loading.load_data(type="raw") df_credit = data_loading.data_preparation(df_credit) df_credit, categorical_encoding, categorical_encoding_label = data_loading.encoding(df_credit) explain_with_shap_global(X_test_encoded , model, model_name)
def main_init_xai(model_name, chosen_instance=4): """Prepare plots and data for the xai-dashboard""" X_train_encoded, y_train, X_test_encoded, y_test, encoder = data_loading.load_encoded_data() X_test, y_test = data_loading.load_data(type="testing") model = model_training.load_trained_model(model_name) # categorical_features_indices = dataset["german_credit_dataset"]["categorical_features_indices"] categorical_features_names, categorical_features_indices, feature_names = data_loading.analyze_dataset() df_credit = data_loading.load_data(type="raw") df_credit = data_loading.data_preparation(df_credit) df_credit, categorical_encoding, categorical_encoding_label = data_loading.encoding(df_credit) #explain_with_lime(X_test, model, model_name, encoder, categorical_features_indices, # categorical_encoding, categorical_encoding_label, # feature_names, test_instance=chosen_instance) explain_with_shap_global(X_train_encoded, model, model_name)
def main_local(model_name, chosen_instance=1): """Main function for local explanations""" X_train_encoded, y_train, X_test_encoded, y_test, encoder = data_loading.load_encoded_data() X_test, y_test = data_loading.load_data(type="testing") model = model_training.load_trained_model(model_name) categorical_features_names, categorical_features_indices, feature_names = data_loading.analyze_dataset() df_credit = data_loading.load_data(type="raw") df_credit = data_loading.data_preparation(df_credit) df_credit, categorical_encoding, categorical_encoding_label = data_loading.encoding(df_credit) explain_with_lime(X_test, model, model_name, encoder, categorical_features_indices, categorical_encoding, categorical_encoding_label, feature_names, test_instance=chosen_instance) explain_with_shap_local(X_test_encoded, model, model_name, feature_names=X_test_encoded.columns.tolist() , test_instance=chosen_instance)
from xgboost import XGBClassifier import matplotlib.pyplot as plt from backend.xai_cockpit.model_explaining import * from backend.inference.predict import * import dataframe_image as dfi if __name__ == '__main__': s = (input("Type 'start' to initialize: ")) initialize() df = load_data(type="raw") s # explore_data(df) data_exploration(df) X_train_encoded, y_train, X_test_encoded, y_test, encoder = data_loading.load_encoded_data( ) # Choose a model and train it model, model_name = model_training.model_selection(input("Select Model: ")) print("Trained model: ", model_name) X_test = data_loading.train_test_split(df) model = model_training.train_model(X_train_encoded, y_train, model) model_training.save_trained_model(model, model_name) print("Training score data: ") print(model.score(X_train_encoded, y_train)) #good: 20, bad:35 i = int(input("Enter instance for prediction (0-100):")) print("Chosen Instance: ", i) print(X_test_encoded.iloc[i]) #print(y_test.iloc[i]) #print(X_test)