|
import os |
|
import pickle |
|
import pandas as pd |
|
|
|
class SVM_Classifier: |
|
def __init__(self): |
|
|
|
self.weight = 70 |
|
self.height = 1.75 |
|
self.gender = "Male" |
|
self.duration = 1.0 |
|
self.fat = 25 |
|
self.freq = 3 |
|
self.experience = 1 |
|
self.workout = "Cardio" |
|
|
|
|
|
self.debug_info = {} |
|
|
|
|
|
try: |
|
|
|
model_file = os.path.join("data", "svm_model.pkl") |
|
with open(model_file, 'rb') as f: |
|
self.svm_model = pickle.load(f) |
|
|
|
|
|
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] |
|
|
|
|
|
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]' |
|
|
|
|
|
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" |
|
|
|
|
|
debug_text += "Original Values:\n" |
|
for key, value in self.debug_info['original_values'].items(): |
|
debug_text += f"{key}: {value}\n" |
|
|
|
|
|
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" |
|
|
|
|
|
debug_text += "\nNormalized Values:\n" |
|
for key, value in self.debug_info['normalized_values'].items(): |
|
debug_text += f"{key}: {value:.4f}\n" |
|
|
|
|
|
debug_text += f"\nGender Encoding: {self.debug_info['gender_encoding']}\n" |
|
debug_text += f"Workout Encoding: {self.debug_info['workout_encoding']}\n" |
|
|
|
|
|
debug_text += f"\nVector: {self.debug_info['final_feature_vector']}\n" |
|
|
|
|
|
debug_text += f"\nFinal Prediction: {self.debug_info['prediction']}\n" |
|
|
|
return debug_text |
|
|