File size: 5,050 Bytes
1018eda |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
import os
import pickle
import pandas as pd
class SVM_Regressor:
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", "svr_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
Xmean_file = os.path.join("data", "SVR_train_mean.csv")
self.df_mean = pd.read_csv(Xmean_file, index_col=0)
Xstd_file = os.path.join("data", "SVR_train_std.csv")
self.df_std = pd.read_csv(Xstd_file, index_col=0)
ynorm_file = os.path.join("data", "svr_y_norms.csv")
df_ynorm = pd.read_csv(ynorm_file, index_col=False)
self.y_mean = df_ynorm['y_mean'].iloc[0]
self.y_std = df_ynorm['y_std'].iloc[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 f"""It is recommended to take {(prediction[0]*self.y_std + self.y_mean):.2f} litres of water for this session."""
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
|