From 0ea8df9399201fe210c460985b775274684fd393 Mon Sep 17 00:00:00 2001 From: Anuj Saha <153378181+AnujSaha0111@users.noreply.github.com> Date: Tue, 1 Oct 2024 19:07:05 +0530 Subject: [PATCH 1/7] Improve Console Output Readability Improve console output readability with enhanced print formatting using colorama --- improve_console_output.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 improve_console_output.py diff --git a/improve_console_output.py b/improve_console_output.py new file mode 100644 index 0000000..bef58ff --- /dev/null +++ b/improve_console_output.py @@ -0,0 +1,22 @@ +from colorama import Fore, Style + +def analyze_model(accuracy): + print(f"{Fore.BLUE}Analyzing model...{Style.RESET_ALL}") + print(f"{Fore.GREEN}Accuracy: {accuracy:.3f}{Style.RESET_ALL}") + +def display_error(message): + print(f"{Fore.RED}Error: {message}{Style.RESET_ALL}") + +def display_warning(message): + print(f"{Fore.YELLOW}Warning: {message}{Style.RESET_ALL}") + +def main(): + accuracy = 0.91234 # Example accuracy value + analyze_model(accuracy) + + # Example usage of other print functions + display_error("An error occurred while loading the model.") + display_warning("This model may take a long time to train.") + +if __name__ == "__main__": + main() From 44351d5fbf0198682a6b58824955c67d1e93b800 Mon Sep 17 00:00:00 2001 From: Anuj Saha <153378181+AnujSaha0111@users.noreply.github.com> Date: Tue, 1 Oct 2024 19:42:24 +0530 Subject: [PATCH 2/7] Custom Logger custom logger that formats log messages with colors using colorama Example usage of it in Core Functions: # model_analysis.py from custom_logger import setup_logger # Initialize logger logger = setup_logger('ModelAnalysis') def analyze_model(accuracy): logger.info("Analyzing model...") logger.info(f"Accuracy: {accuracy:.3f}") def display_error(message): logger.error(f"Error: {message}") def display_warning(message): logger.warning(f"Warning: {message}") def main(): accuracy = 0.91234 # Example accuracy value analyze_model(accuracy) # Example usage of other log functions display_error("An error occurred while loading the model.") display_warning("This model may take a long time to train.") if __name__ == "__main__": main() --- custom_logger.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 custom_logger.py diff --git a/custom_logger.py b/custom_logger.py new file mode 100644 index 0000000..a38f5dd --- /dev/null +++ b/custom_logger.py @@ -0,0 +1,35 @@ +import logging +from colorama import Fore, Style + +class CustomFormatter(logging.Formatter): + """Custom formatter to add color to log messages based on their severity.""" + + COLORS = { + 'DEBUG': Fore.BLUE, + 'INFO': Fore.GREEN, + 'WARNING': Fore.YELLOW, + 'ERROR': Fore.RED, + 'CRITICAL': Fore.MAGENTA, + } + + def format(self, record): + log_color = self.COLORS.get(record.levelname, Style.RESET_ALL) + message = super().format(record) + return f"{log_color}{message}{Style.RESET_ALL}" + +def setup_logger(name): + """Sets up the custom logger.""" + logger = logging.getLogger(name) + logger.setLevel(logging.DEBUG) + + # Create console handler + ch = logging.StreamHandler() + ch.setLevel(logging.DEBUG) + + # Create formatter + formatter = CustomFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + ch.setFormatter(formatter) + + # Adding handler to logger + logger.addHandler(ch) + return logger From bc2a2be2950998b0a4c73946b6b0720642883d29 Mon Sep 17 00:00:00 2001 From: Anuj Saha <153378181+AnujSaha0111@users.noreply.github.com> Date: Tue, 1 Oct 2024 19:43:41 +0530 Subject: [PATCH 3/7] Improving Console Output Integrate enhanced print formatting into core package with custom logger --- improve_console_output.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/improve_console_output.py b/improve_console_output.py index bef58ff..60496cf 100644 --- a/improve_console_output.py +++ b/improve_console_output.py @@ -1,20 +1,27 @@ -from colorama import Fore, Style +import colorama +from custom_logger import setup_logger + +# Initialize colorama +colorama.init() + +# Initialize logger +logger = setup_logger('ModelAnalysis') def analyze_model(accuracy): - print(f"{Fore.BLUE}Analyzing model...{Style.RESET_ALL}") - print(f"{Fore.GREEN}Accuracy: {accuracy:.3f}{Style.RESET_ALL}") + logger.info("Analyzing model...") + logger.info(f"Accuracy: {accuracy:.3f}") def display_error(message): - print(f"{Fore.RED}Error: {message}{Style.RESET_ALL}") + logger.error(f"Error: {message}") def display_warning(message): - print(f"{Fore.YELLOW}Warning: {message}{Style.RESET_ALL}") + logger.warning(f"Warning: {message}") def main(): accuracy = 0.91234 # Example accuracy value analyze_model(accuracy) - # Example usage of other print functions + # Example usage of other log functions display_error("An error occurred while loading the model.") display_warning("This model may take a long time to train.") From bd2fa1712b5629a38f37ec6cf773cf6acd37de23 Mon Sep 17 00:00:00 2001 From: Anuj Saha <153378181+AnujSaha0111@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:46:43 +0530 Subject: [PATCH 4/7] Updated _console_output Enhanced console output readability with colorama --- improve_console_output.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/improve_console_output.py b/improve_console_output.py index 60496cf..b52f577 100644 --- a/improve_console_output.py +++ b/improve_console_output.py @@ -1,27 +1,27 @@ import colorama -from custom_logger import setup_logger +from colorama import Fore, Style # Initialize colorama -colorama.init() - -# Initialize logger -logger = setup_logger('ModelAnalysis') +colorama.init(autoreset=True) # Ensures colors are reset automatically after each print def analyze_model(accuracy): - logger.info("Analyzing model...") - logger.info(f"Accuracy: {accuracy:.3f}") + # Enhanced print formatting for analysis messages + print(f"{Fore.BLUE}Analyzing model...{Style.RESET_ALL}") + print(f"{Fore.GREEN}Accuracy: {accuracy:.3f}") def display_error(message): - logger.error(f"Error: {message}") + # Use red for error messages + print(f"{Fore.RED}Error: {message}") def display_warning(message): - logger.warning(f"Warning: {message}") + # Use yellow for warning messages + print(f"{Fore.YELLOW}Warning: {message}") def main(): accuracy = 0.91234 # Example accuracy value analyze_model(accuracy) - # Example usage of other log functions + # Example usage of enhanced error and warning messages display_error("An error occurred while loading the model.") display_warning("This model may take a long time to train.") From fcb49f1ce141669d25442e34948abfed7c85f512 Mon Sep 17 00:00:00 2001 From: Anuj Saha <153378181+AnujSaha0111@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:08:48 +0530 Subject: [PATCH 5/7] Delete custom_logger.py --- custom_logger.py | 35 ----------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 custom_logger.py diff --git a/custom_logger.py b/custom_logger.py deleted file mode 100644 index a38f5dd..0000000 --- a/custom_logger.py +++ /dev/null @@ -1,35 +0,0 @@ -import logging -from colorama import Fore, Style - -class CustomFormatter(logging.Formatter): - """Custom formatter to add color to log messages based on their severity.""" - - COLORS = { - 'DEBUG': Fore.BLUE, - 'INFO': Fore.GREEN, - 'WARNING': Fore.YELLOW, - 'ERROR': Fore.RED, - 'CRITICAL': Fore.MAGENTA, - } - - def format(self, record): - log_color = self.COLORS.get(record.levelname, Style.RESET_ALL) - message = super().format(record) - return f"{log_color}{message}{Style.RESET_ALL}" - -def setup_logger(name): - """Sets up the custom logger.""" - logger = logging.getLogger(name) - logger.setLevel(logging.DEBUG) - - # Create console handler - ch = logging.StreamHandler() - ch.setLevel(logging.DEBUG) - - # Create formatter - formatter = CustomFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') - ch.setFormatter(formatter) - - # Adding handler to logger - logger.addHandler(ch) - return logger From 84d386511348ca729b357ee6393597fa12a3fd5a Mon Sep 17 00:00:00 2001 From: Anuj Saha <153378181+AnujSaha0111@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:09:29 +0530 Subject: [PATCH 6/7] Update improve_console_output.py From 789b037885ea7bd0dae31c917ca396ffa010e78d Mon Sep 17 00:00:00 2001 From: Anuj Saha <153378181+AnujSaha0111@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:15:26 +0530 Subject: [PATCH 7/7] Update core.py --- explainableai/core.py | 53 +++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/explainableai/core.py b/explainableai/core.py index 50fd1ed..f0f10fb 100644 --- a/explainableai/core.py +++ b/explainableai/core.py @@ -1,4 +1,8 @@ -# explainableai/core.py +import colorama +from colorama import Fore, Style + +# Initialize colorama +colorama.init(autoreset=True) import pandas as pd import numpy as np @@ -21,7 +25,6 @@ from reportlab.platypus import PageBreak - class XAIWrapper: def __init__(self): self.model = None @@ -47,11 +50,11 @@ def fit(self, models, X, y, feature_names=None): self.feature_names = feature_names if feature_names is not None else X.columns.tolist() self.is_classifier = all(hasattr(model, "predict_proba") for model in self.models.values()) - print("Preprocessing data...") + print(f"{Fore.BLUE}Preprocessing data...{Style.RESET_ALL}") self._preprocess_data() - print("Fitting models and analyzing...") - self.model_comparison_results = self._compare_models() + print(f"{Fore.BLUE}Fitting models and analyzing...{Style.RESET_ALL}") + self._analyze_models() # Select the best model based on cv_score best_model_name = max(self.model_comparison_results, key=lambda x: self.model_comparison_results[x]['cv_score']) @@ -266,44 +269,44 @@ def _print_results(self, results): @staticmethod def perform_eda(df): - print("\nExploratory Data Analysis:") - print(f"Dataset shape: {df.shape}") - print("\nDataset info:") + print(f"{Fore.CYAN}Exploratory Data Analysis:{Style.RESET_ALL}") + print(f"{Fore.GREEN}Dataset shape: {df.shape}{Style.RESET_ALL}") + print(f"{Fore.CYAN}Dataset info:{Style.RESET_ALL}") df.info() - print("\nSummary statistics:") + print(f"{Fore.CYAN}Summary statistics:{Style.RESET_ALL}") print(df.describe()) - print("\nMissing values:") + print(f"{Fore.CYAN}Missing values:{Style.RESET_ALL}") print(df.isnull().sum()) - print("\nData types:") + print(f"{Fore.CYAN}Data types:{Style.RESET_ALL}") print(df.dtypes) - print("\nUnique values in each column:") + print(f"{Fore.CYAN}Unique values in each column:{Style.RESET_ALL}") for col in df.columns: - print(f"{col}: {df[col].nunique()}") - + print(f"{Fore.GREEN}{col}: {df[col].nunique()}{Style.RESET_ALL}") + # Additional EDA steps - print("\nCorrelation matrix:") + print(f"{Fore.CYAN}Correlation matrix:{Style.RESET_ALL}") corr_matrix = df.select_dtypes(include=[np.number]).corr() print(corr_matrix) - + # Identify highly correlated features high_corr = np.where(np.abs(corr_matrix) > 0.8) high_corr_list = [(corr_matrix.index[x], corr_matrix.columns[y]) for x, y in zip(*high_corr) if x != y and x < y] if high_corr_list: - print("\nHighly correlated features:") + print(f"{Fore.YELLOW}Highly correlated features:{Style.RESET_ALL}") for feat1, feat2 in high_corr_list: - print(f"{feat1} - {feat2}: {corr_matrix.loc[feat1, feat2]:.2f}") - + print(f"{Fore.GREEN}{feat1} - {feat2}: {corr_matrix.loc[feat1, feat2]:.2f}{Style.RESET_ALL}") + # Identify potential outliers - print("\nPotential outliers (values beyond 3 standard deviations):") + print(f"{Fore.CYAN}Potential outliers (values beyond 3 standard deviations):{Style.RESET_ALL}") numeric_cols = df.select_dtypes(include=[np.number]).columns for col in numeric_cols: mean = df[col].mean() std = df[col].std() - outliers = df[(df[col] < mean - 3*std) | (df[col] > mean + 3*std)] + outliers = df[(df[col] < mean - 3 * std) | (df[col] > mean + 3 * std)] if not outliers.empty: - print(f"{col}: {len(outliers)} potential outliers") - + print(f"{Fore.GREEN}{col}: {len(outliers)} potential outliers{Style.RESET_ALL}") + # Class distribution for the target variable (assuming last column is target) target_col = df.columns[-1] - print(f"\nClass distribution for target variable '{target_col}':") - print(df[target_col].value_counts(normalize=True)) \ No newline at end of file + print(f"{Fore.CYAN}Class distribution for target variable '{target_col}':{Style.RESET_ALL}") + print(df[target_col].value_counts(normalize=True))