import os import pickle import pandas as pd class SVM_Classifier: def __init__(self): self.weight = 70 # default weight in kg self.height = 1.75 # default height in m self.gender = "Male" # default gender self.duration = 1.0 # default duration in hours self.fat = 25 # default fat percentage self.freq = 3 # default workouts per week self.experience = 1 # default experience level self.workout = "Cardio" # default workout type # Add debug info dictionary self.debug_info = {} # Load the model and required data try: # Load the SVM model model_file = os.path.join("data", "svm_model.pkl") with open(model_file, 'rb') as f: self.svm_model = pickle.load(f) # Load the column names cols_file = os.path.join("data", "column_names.csv") with open(cols_file, 'r') as f: self.column_names = [line.strip() for line in f] # Load normalization parameters mean_file = os.path.join("data", "SVM_train_mean.csv") self.df_mean = pd.read_csv(mean_file, index_col=0) std_file = os.path.join("data", "SVM_train_std.csv") self.df_std = pd.read_csv(std_file, index_col=0) except Exception as e: print(f"Error loading model files: {str(e)}") raise def make_prediction(self): try: num = [self.weight, self.height, self.duration, self.fat, self.freq, self.experience] self.debug_info['original_values'] = dict(zip( ['weight', 'height', 'duration', 'fat', 'freq', 'experience'], num )) m_norm = self.df_mean.values.flatten().tolist() s_norm = self.df_std.values.flatten().tolist() self.debug_info['normalization'] = { 'means': m_norm, 'stds': s_norm } norm = [(x-y)/z for x, y, z in zip(num, m_norm, s_norm)] self.debug_info['normalized_values'] = dict(zip( ['weight', 'height', 'duration', 'fat', 'freq', 'experience'], norm )) if self.gender == 'Female': norm.extend([1,0]) self.debug_info['gender_encoding'] = 'Female: [1, 0]' else: norm.extend([0,1]) self.debug_info['gender_encoding'] = 'Male: [0, 1]' # Add one-hot encoded workout type workout_encoding = { 'Cardio': [1, 0, 0, 0], 'HIIT': [0, 1, 0, 0], 'Strength': [0, 0, 1, 0], 'Yoga': [0, 0, 0, 1] } norm.extend(workout_encoding[self.workout]) self.debug_info['workout_encoding'] = f'{self.workout}: {workout_encoding[self.workout]}' X = pd.DataFrame([norm],columns=self.column_names) self.debug_info['final_feature_vector'] = X.to_dict('records')[0] prediction = self.svm_model.predict(X) self.debug_info['prediction'] = prediction[0] return prediction[0] except Exception as e: self.debug_info['error'] = str(e) return f"Error: {str(e)}" def get_debug_info(self): """Returns formatted debug information""" debug_text = "=== DEBUG INFORMATION ===\n\n" # Original values debug_text += "Original Values:\n" for key, value in self.debug_info['original_values'].items(): debug_text += f"{key}: {value}\n" # Normalization parameters debug_text += "\nNormalization Parameters:\n" for i, (mean, std) in enumerate(zip( self.debug_info['normalization']['means'], self.debug_info['normalization']['stds'] )): debug_text += f"Feature {i}: mean={mean:.4f}, std={std:.4f}\n" # Normalized values debug_text += "\nNormalized Values:\n" for key, value in self.debug_info['normalized_values'].items(): debug_text += f"{key}: {value:.4f}\n" # Encodings debug_text += f"\nGender Encoding: {self.debug_info['gender_encoding']}\n" debug_text += f"Workout Encoding: {self.debug_info['workout_encoding']}\n" # Final vector debug_text += f"\nVector: {self.debug_info['final_feature_vector']}\n" # Final prediction debug_text += f"\nFinal Prediction: {self.debug_info['prediction']}\n" return debug_text