feat: 将数据集从国外员工缺勤数据替换为中国企业缺勤模拟数据

- 新增中国企业员工缺勤模拟数据集生成脚本(generate_dataset.py),覆盖7个行业、180家企业、2600名员工
  - 重构 config.py,更新特征字段为中文名称,调整目标列、员工ID、行业类型等配置
  - 重构 clustering.py,简化聚类逻辑,更新聚类特征和群体命名(高压通勤型、健康波动型等)
  - 重构 feature_mining.py,更新相关性分析和群体比较维度(按行业、班次、婚姻状态等)
  - 新增 model_features.py 定义模型训练特征
  - 更新 preprocessing.py 和 train_model.py 适配新数据结构
  - 更新各 API 路由默认参数(model: random_forest, dimension: industry)
  - 前端更新主题样式和各视图组件适配中文字段
  - 更新系统名称为 China Enterprise Absence Analysis System
This commit is contained in:
2026-03-11 10:46:58 +08:00
parent a39d8b2fd2
commit e63267cef6
39 changed files with 15731 additions and 5648 deletions

View File

@@ -1,9 +1,6 @@
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from sklearn.preprocessing import MinMaxScaler
import joblib
import os
import config
from core.preprocessing import get_clean_data
@@ -14,216 +11,123 @@ class KMeansAnalyzer:
self.n_clusters = n_clusters
self.model = None
self.scaler = MinMaxScaler()
self.data = None
self.data_scaled = None
self.labels = None
def _get_feature_columns(self, df):
df.columns = [col.strip() for col in df.columns]
feature_map = {
'Age': None,
'Service time': None,
'Work load Average/day': None,
'Body mass index': None,
'Absenteeism time in hours': None
}
for key in feature_map:
if key in df.columns:
feature_map[key] = key
else:
for col in df.columns:
if key.replace(' ', '').lower() == col.replace(' ', '').lower():
feature_map[key] = col
break
actual_features = [v for v in feature_map.values() if v is not None]
return actual_features
self.feature_cols = [
'年龄',
'司龄年数',
'月均加班时长',
'通勤时长分钟',
'BMI',
'缺勤时长(小时)',
]
def fit(self, n_clusters=None):
if n_clusters:
self.n_clusters = n_clusters
df = get_clean_data()
df = df.reset_index(drop=True)
feature_cols = self._get_feature_columns(df)
if not feature_cols:
feature_cols = ['Age', 'Service time', 'Body mass index', 'Absenteeism time in hours']
feature_cols = [c for c in feature_cols if c in df.columns]
self.data = df[feature_cols].values
self.scaler = MinMaxScaler()
self.data_scaled = self.scaler.fit_transform(self.data)
self.model = KMeans(
n_clusters=self.n_clusters,
random_state=config.RANDOM_STATE,
n_init=10
)
self.labels = self.model.fit_predict(self.data_scaled)
df = get_clean_data().reset_index(drop=True)
data = df[self.feature_cols].values
data_scaled = self.scaler.fit_transform(data)
self.model = KMeans(n_clusters=self.n_clusters, random_state=config.RANDOM_STATE, n_init=10)
self.labels = self.model.fit_predict(data_scaled)
return self.model
def get_cluster_results(self, n_clusters=3):
if self.model is None or self.n_clusters != n_clusters:
self.fit(n_clusters)
centers = self.scaler.inverse_transform(self.model.cluster_centers_)
unique, counts = np.unique(self.labels, return_counts=True)
total = len(self.labels)
cluster_names = self._generate_cluster_names(centers)
feature_cols = self._get_feature_columns(get_clean_data())
names = self._generate_cluster_names(centers)
clusters = []
for i, (cluster_id, count) in enumerate(zip(unique, counts)):
center_dict = {}
for j, fname in enumerate(feature_cols):
if j < len(centers[i]):
center_dict[fname] = round(centers[i][j], 2)
for cluster_id, count in zip(unique, counts):
center = centers[int(cluster_id)]
clusters.append({
'id': int(cluster_id),
'name': cluster_names.get(cluster_id, f'群体{cluster_id+1}'),
'name': names.get(int(cluster_id), f'群体{int(cluster_id) + 1}'),
'member_count': int(count),
'percentage': round(count / total * 100, 1),
'center': center_dict,
'description': self._generate_description(cluster_names.get(cluster_id, ''))
'center': {
feature: round(float(value), 2)
for feature, value in zip(self.feature_cols, center)
},
'description': self._generate_description(names.get(int(cluster_id), '')),
})
return {
'n_clusters': self.n_clusters,
'clusters': clusters
}
return {'n_clusters': self.n_clusters, 'clusters': clusters}
def get_cluster_profile(self, n_clusters=3):
if self.model is None or self.n_clusters != n_clusters:
self.fit(n_clusters)
centers_scaled = self.model.cluster_centers_
df = get_clean_data()
df.columns = [col.strip() for col in df.columns]
feature_cols = self._get_feature_columns(df)
dimensions = ['年龄', '工龄', '工作负荷', 'BMI', '缺勤倾向'][:len(feature_cols)]
cluster_names = self._generate_cluster_names(
self.scaler.inverse_transform(centers_scaled)
)
clusters = []
for i in range(self.n_clusters):
clusters.append({
'id': i,
'name': cluster_names.get(i, f'群体{i+1}'),
'values': [round(v, 2) for v in centers_scaled[i]]
})
names = self._generate_cluster_names(self.scaler.inverse_transform(centers_scaled))
return {
'dimensions': dimensions,
'dimension_keys': feature_cols,
'clusters': clusters
'dimensions': ['年龄', '司龄', '加班', '通勤', 'BMI', '缺勤'],
'dimension_keys': self.feature_cols,
'clusters': [
{
'id': idx,
'name': names.get(idx, f'群体{idx + 1}'),
'values': [round(float(v), 2) for v in centers_scaled[idx]],
}
for idx in range(self.n_clusters)
],
}
def get_scatter_data(self, n_clusters=3, x_axis='Age', y_axis='Absenteeism time in hours'):
def get_scatter_data(self, n_clusters=3, x_axis='月均加班时长', y_axis='缺勤时长(小时)'):
if self.model is None or self.n_clusters != n_clusters:
self.fit(n_clusters)
df = get_clean_data()
df = df.reset_index(drop=True)
df.columns = [col.strip() for col in df.columns]
x_col = None
y_col = None
for col in df.columns:
if x_axis.replace(' ', '').lower() in col.replace(' ', '').lower():
x_col = col
if y_axis.replace(' ', '').lower() in col.replace(' ', '').lower():
y_col = col
if x_col is None:
x_col = df.columns[0]
if y_col is None:
y_col = df.columns[-1]
df = get_clean_data().reset_index(drop=True)
if x_axis not in df.columns:
x_axis = '月均加班时长'
if y_axis not in df.columns:
y_axis = config.TARGET_COLUMN
points = []
for idx in range(min(len(df), len(self.labels))):
row = df.iloc[idx]
points.append({
'employee_id': int(row['ID']),
'x': float(row[x_col]),
'y': float(row[y_col]),
'cluster_id': int(self.labels[idx])
'employee_id': str(row[config.EMPLOYEE_ID_COLUMN]),
'x': float(row[x_axis]),
'y': float(row[y_axis]),
'cluster_id': int(self.labels[idx]),
})
cluster_colors = {
'0': '#67C23A',
'1': '#E6A23C',
'2': '#F56C6C',
'3': '#909399',
'4': '#409EFF'
}
return {
'x_axis': x_col,
'x_axis_name': config.FEATURE_NAME_CN.get(x_col, x_col),
'y_axis': y_col,
'y_axis_name': config.FEATURE_NAME_CN.get(y_col, y_col),
'x_axis': x_axis,
'x_axis_name': config.FEATURE_NAME_CN.get(x_axis, x_axis),
'y_axis': y_axis,
'y_axis_name': config.FEATURE_NAME_CN.get(y_axis, y_axis),
'points': points[:500],
'cluster_colors': cluster_colors
'cluster_colors': {
'0': '#5B8FF9',
'1': '#61DDAA',
'2': '#F6BD16',
'3': '#E8684A',
'4': '#6DC8EC',
},
}
def _generate_cluster_names(self, centers):
names = {}
for i, center in enumerate(centers):
if len(center) >= 5:
service_time = center[1]
work_load = center[2]
bmi = center[3]
absent = center[4]
for idx, center in enumerate(centers):
_, tenure, overtime, commute, bmi, absence = center
if overtime > 38 and commute > 55 and absence > 8:
names[idx] = '高压通勤型'
elif bmi > 27 and absence > 8:
names[idx] = '健康波动型'
elif tenure > 8 and absence < 6:
names[idx] = '稳定低风险型'
elif overtime > 28 and absence > 7:
names[idx] = '轮班负荷型'
else:
service_time = center[1] if len(center) > 1 else 0
work_load = 0
bmi = center[2] if len(center) > 2 else 0
absent = center[3] if len(center) > 3 else 0
if service_time > 15 and absent < 3:
names[i] = '模范型员工'
elif work_load > 260 and absent > 5:
names[i] = '压力型员工'
elif bmi > 28:
names[i] = '生活习惯型员工'
else:
names[i] = f'群体{i+1}'
names[idx] = f'群体{idx + 1}'
return names
def _generate_description(self, name):
descriptions = {
'模范型员工': '工龄长、工作稳定、缺勤率低',
'压力型员工': '工作负荷大、缺勤较多',
'生活习惯型员工': 'BMI偏高、需关注健康'
'高压通勤型': '加班和通勤压力都高,缺勤时长偏长。',
'健康波动型': '健康相关风险更高,需要重点关注。',
'稳定低风险型': '司龄较长,缺勤水平稳定且偏低。',
'轮班负荷型': '排班和工作负荷较重,缺勤风险较高。',
}
return descriptions.get(name, '常规员工群体')
def save_model(self):
os.makedirs(config.MODELS_DIR, exist_ok=True)
joblib.dump(self.model, config.KMEANS_MODEL_PATH)
def load_model(self):
if os.path.exists(config.KMEANS_MODEL_PATH):
self.model = joblib.load(config.KMEANS_MODEL_PATH)
self.n_clusters = self.model.n_clusters
return descriptions.get(name, '常规员工群体')
kmeans_analyzer = KMeansAnalyzer()

View File

@@ -1,4 +1,3 @@
import pandas as pd
import numpy as np
import config
@@ -7,145 +6,67 @@ from core.preprocessing import get_clean_data
def calculate_correlation():
df = get_clean_data()
numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
if 'ID' in numeric_cols:
numeric_cols.remove('ID')
corr_matrix = df[numeric_cols].corr()
return corr_matrix
for candidate in [config.EMPLOYEE_ID_COLUMN]:
if candidate in numeric_cols:
numeric_cols.remove(candidate)
return df[numeric_cols].corr()
def get_correlation_for_heatmap():
corr_matrix = calculate_correlation()
key_features = [
'Age',
'Service time',
'Distance from Residence to Work',
'Work load Average/day ',
'Body mass index',
'Absenteeism time in hours'
'月均加班时长',
'通勤时长分钟',
'近90天缺勤次数',
'BMI',
'近30天睡眠时长均值',
'缺勤时长(小时)',
]
key_features = [f for f in key_features if f in corr_matrix.columns]
sub_matrix = corr_matrix.loc[key_features, key_features]
result = {
return {
'features': [config.FEATURE_NAME_CN.get(f, f) for f in key_features],
'matrix': sub_matrix.values.round(2).tolist()
'matrix': sub_matrix.values.round(2).tolist(),
}
return result
def calculate_feature_importance(model, feature_names):
if hasattr(model, 'feature_importances_'):
importance = model.feature_importances_
else:
raise ValueError("Model does not have feature_importances_ attribute")
importance_dict = dict(zip(feature_names, importance))
sorted_importance = sorted(importance_dict.items(), key=lambda x: x[1], reverse=True)
return sorted_importance
def get_feature_importance_from_model(model_path, feature_names):
import joblib
model = joblib.load(model_path)
return calculate_feature_importance(model, feature_names)
def group_comparison(dimension):
df = get_clean_data()
dimension_map = {
'drinker': ('Social drinker', {0: '不饮酒', 1: '饮酒'}),
'smoker': ('Social smoker', {0: '不吸烟', 1: '吸烟'}),
'education': ('Education', {1: '高中', 2: '本科', 3: '研究生', 4: '博士'}),
'children': ('Son', {0: '无子女'}, lambda x: x > 0, '有子女'),
'pet': ('Pet', {0: '宠物'}, lambda x: x > 0, '有宠物')
'industry': ('所属行业', None, '所属行业'),
'shift_type': ('班次类型', None, '班次类型'),
'job_family': ('岗位序列', None, '岗位序列'),
'marital_status': ('婚姻状态', None, '婚姻状态'),
'chronic_disease': ('是否慢性病史', {0: '慢性病史', 1: '有慢性病史'}, '慢性病史'),
}
if dimension not in dimension_map:
raise ValueError(f"Invalid dimension: {dimension}")
col, value_map = dimension_map[dimension][0], dimension_map[dimension][1]
if dimension in ['children', 'pet']:
threshold_fn = dimension_map[dimension][2]
other_label = dimension_map[dimension][3]
groups = []
for val in [0]:
group_df = df[df[col] == val]
if len(group_df) > 0:
groups.append({
'name': value_map.get(val, str(val)),
'value': val,
'avg_hours': round(group_df['Absenteeism time in hours'].mean(), 2),
'count': len(group_df),
'percentage': round(len(group_df) / len(df) * 100, 1)
})
group_df = df[df[col].apply(threshold_fn)]
if len(group_df) > 0:
groups.append({
'name': other_label,
'value': 1,
'avg_hours': round(group_df['Absenteeism time in hours'].mean(), 2),
'count': len(group_df),
'percentage': round(len(group_df) / len(df) * 100, 1)
})
else:
groups = []
for val in sorted(df[col].unique()):
group_df = df[df[col] == val]
if len(group_df) > 0:
groups.append({
'name': value_map.get(val, str(val)),
'value': int(val),
'avg_hours': round(group_df['Absenteeism time in hours'].mean(), 2),
'count': len(group_df),
'percentage': round(len(group_df) / len(df) * 100, 1)
})
if len(groups) >= 2:
diff_value = abs(groups[0]['avg_hours'] - groups[1]['avg_hours'])
base = min(groups[0]['avg_hours'], groups[1]['avg_hours'])
diff_percentage = round(diff_value / base * 100, 1) if base > 0 else 0
else:
diff_value = 0
diff_percentage = 0
column, value_map, dimension_name = dimension_map[dimension]
groups = []
for value in sorted(df[column].unique()):
group_df = df[df[column] == value]
groups.append({
'name': value_map.get(value, value) if value_map else str(value),
'value': int(value) if isinstance(value, (int, np.integer)) else str(value),
'avg_hours': round(group_df[config.TARGET_COLUMN].mean(), 2),
'count': int(len(group_df)),
'percentage': round(len(group_df) / len(df) * 100, 1),
})
groups.sort(key=lambda item: item['avg_hours'], reverse=True)
top = groups[0]['avg_hours'] if groups else 0
bottom = groups[-1]['avg_hours'] if len(groups) > 1 else 0
diff_value = round(top - bottom, 2)
diff_percentage = round(diff_value / bottom * 100, 1) if bottom else 0
return {
'dimension': dimension,
'dimension_name': {
'drinker': '饮酒习惯',
'smoker': '吸烟习惯',
'education': '学历',
'children': '子女',
'pet': '宠物'
}.get(dimension, dimension),
'dimension_name': dimension_name,
'groups': groups,
'difference': {
'value': diff_value,
'percentage': diff_percentage
}
'percentage': diff_percentage,
},
}
if __name__ == '__main__':
print("Correlation matrix:")
corr = get_correlation_for_heatmap()
print(corr)
print("\nGroup comparison (drinker):")
comp = group_comparison('drinker')
print(comp)

View File

@@ -0,0 +1,336 @@
import os
import sys
import numpy as np
import pandas as pd
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import config
INDUSTRIES = {
'制造业': {'shift_bias': 0.9, 'overtime_bias': 0.8, 'night_bias': 0.8},
'互联网': {'shift_bias': 0.2, 'overtime_bias': 1.0, 'night_bias': 0.2},
'零售连锁': {'shift_bias': 0.7, 'overtime_bias': 0.5, 'night_bias': 0.3},
'物流运输': {'shift_bias': 0.9, 'overtime_bias': 0.7, 'night_bias': 0.9},
'金融服务': {'shift_bias': 0.1, 'overtime_bias': 0.7, 'night_bias': 0.1},
'医药健康': {'shift_bias': 0.6, 'overtime_bias': 0.6, 'night_bias': 0.5},
'建筑工程': {'shift_bias': 0.5, 'overtime_bias': 0.8, 'night_bias': 0.3},
}
def season_from_month(month):
if month in [12, 1, 2]:
return 1
if month in [3, 4, 5]:
return 2
if month in [6, 7, 8]:
return 3
return 4
def weighted_choice(rng, items, probs):
probs = np.array(probs, dtype=float)
probs = probs / probs.sum()
return rng.choice(items, p=probs)
def build_company_pool(rng, company_count=180):
industries = list(INDUSTRIES.keys())
scales = ['100人以下', '100-499人', '500-999人', '1000-4999人', '5000人及以上']
city_tiers = ['一线', '新一线', '二线', '三线及以下']
companies = []
for idx in range(company_count):
industry = weighted_choice(rng, industries, [0.22, 0.14, 0.14, 0.14, 0.1, 0.12, 0.14])
companies.append({
'企业编号': f'C{idx + 1:03d}',
'所属行业': industry,
'企业规模': weighted_choice(rng, scales, [0.15, 0.28, 0.2, 0.24, 0.13]),
'所在城市等级': weighted_choice(rng, city_tiers, [0.18, 0.34, 0.3, 0.18]),
})
return companies
def build_employee_pool(rng, companies, employee_count=2600):
genders = ['', '']
employment_types = ['正式员工', '劳务派遣', '外包驻场', '实习生']
departments = ['生产', '研发', '销售', '客服', '职能', '仓储物流', '门店运营']
job_families = ['管理', '专业技术', '销售业务', '生产操作', '行政支持', '客服坐席']
job_levels = ['初级', '中级', '高级', '主管', '经理及以上']
educations = ['中专及以下', '大专', '本科', '硕士', '博士']
marital = ['未婚', '已婚', '离异/其他']
housing = ['自有住房', '租房', '宿舍']
shifts = ['标准白班', '两班倒', '三班倒', '弹性班']
performance = ['A', 'B', 'C', 'D']
stress = ['', '', '']
employees = []
for idx in range(employee_count):
company = companies[rng.integers(0, len(companies))]
industry = company['所属行业']
age = int(np.clip(rng.normal(33, 7), 20, 55))
tenure = round(float(np.clip(age - 21 + rng.normal(0, 2), 0.2, 32)), 1)
family_bias = 0.6 if age >= 30 else 0.25
married = weighted_choice(rng, marital, [0.45, 0.48, 0.07] if age < 30 else [0.18, 0.72, 0.1])
children = int(np.clip(rng.poisson(0.4 if married == '未婚' else family_bias), 0, 3))
industry_profile = INDUSTRIES[industry]
shift = weighted_choice(
rng,
shifts,
[
max(0.1, 1 - industry_profile['shift_bias']),
0.35 * industry_profile['shift_bias'],
0.25 * industry_profile['shift_bias'],
0.2,
],
)
night_flag = int(shift == '三班倒' or (shift == '两班倒' and rng.random() < industry_profile['night_bias']))
overtime = float(np.clip(rng.normal(22 + 18 * industry_profile['overtime_bias'], 10), 0, 90))
commute_minutes = float(np.clip(rng.normal(42, 18), 8, 130))
commute_km = float(np.clip(commute_minutes * rng.uniform(0.35, 0.75), 2, 65))
performance_level = weighted_choice(rng, performance, [0.18, 0.46, 0.26, 0.1])
chronic_flag = int(rng.random() < max(0.05, (age - 26) * 0.01))
check_abnormal = int(chronic_flag == 1 or rng.random() < 0.14)
sleep_hours = round(float(np.clip(rng.normal(6.9 - 0.35 * night_flag, 0.8), 4.5, 9.0)), 1)
exercise = int(np.clip(rng.poisson(2.2), 0, 7))
smoking = int(rng.random() < (0.22 if rng.random() < 0.55 else 0.08))
drinking = int(rng.random() < 0.27)
stress_level = weighted_choice(
rng,
stress,
[0.22, 0.52, 0.26 + min(0.15, overtime / 120)],
)
bmi = round(float(np.clip(rng.normal(24.2, 3.2), 17.5, 36.5)), 1)
history_count = int(np.clip(rng.poisson(1.2 + chronic_flag * 0.6 + children * 0.15), 0, 8))
history_hours = float(np.clip(rng.normal(18 + chronic_flag * 10 + history_count * 3, 10), 0, 120))
discipline = int(np.clip(rng.poisson(0.2), 0, 4))
team_size = int(np.clip(rng.normal(11, 5), 3, 40))
manager_span = int(np.clip(team_size + rng.normal(3, 2), 4, 60))
local_hukou = int(rng.random() < 0.58)
cross_city = int(commute_minutes > 65 or (local_hukou == 0 and rng.random() < 0.35))
sedentary = int(weighted_choice(rng, [0, 1], [0.45, 0.55]) if company['所属行业'] in ['互联网', '金融服务'] else rng.random() < 0.3)
employees.append({
'企业编号': company['企业编号'],
'所属行业': industry,
'企业规模': company['企业规模'],
'所在城市等级': company['所在城市等级'],
'用工类型': weighted_choice(rng, employment_types, [0.74, 0.12, 0.1, 0.04]),
'部门条线': weighted_choice(rng, departments, [0.18, 0.16, 0.14, 0.11, 0.12, 0.14, 0.15]),
'岗位序列': weighted_choice(rng, job_families, [0.08, 0.24, 0.16, 0.2, 0.12, 0.2]),
'岗位级别': weighted_choice(rng, job_levels, [0.34, 0.32, 0.18, 0.11, 0.05]),
'员工编号': f'E{idx + 1:05d}',
'性别': weighted_choice(rng, genders, [0.56, 0.44]),
'年龄': age,
'司龄年数': tenure,
'最高学历': weighted_choice(rng, educations, [0.14, 0.28, 0.4, 0.15, 0.03]),
'婚姻状态': married,
'是否本地户籍': local_hukou,
'子女数量': children,
'是否独生子女家庭负担': int(children >= 2 or (married == '已婚' and rng.random() < 0.18)),
'居住类型': weighted_choice(rng, housing, [0.38, 0.48, 0.14]),
'班次类型': shift,
'是否夜班岗位': night_flag,
'月均加班时长': round(overtime, 1),
'近30天出勤天数': int(np.clip(rng.normal(21.5, 2.2), 14, 27)),
'近90天缺勤次数': history_count,
'近180天请假总时长': round(history_hours, 1),
'通勤时长分钟': round(commute_minutes, 1),
'通勤距离公里': round(commute_km, 1),
'是否跨城通勤': cross_city,
'绩效等级': performance_level,
'近12月违纪次数': discipline,
'团队人数': team_size,
'直属上级管理跨度': manager_span,
'BMI': bmi,
'是否慢性病史': chronic_flag,
'年度体检异常标记': check_abnormal,
'近30天睡眠时长均值': sleep_hours,
'每周运动频次': exercise,
'是否吸烟': smoking,
'是否饮酒': drinking,
'心理压力等级': stress_level,
'是否长期久坐岗位': sedentary,
})
return employees
def sample_event(rng, employee):
month = int(rng.integers(1, 13))
weekday = int(rng.integers(1, 8))
near_holiday = int(rng.random() < (0.3 if month in [1, 2, 4, 5, 9, 10] else 0.16))
leave_type_items = ['病假', '事假', '年假', '调休', '婚假', '丧假', '产检育儿假', '工伤假', '其他']
leave_type = weighted_choice(rng, leave_type_items, [0.3, 0.22, 0.12, 0.14, 0.03, 0.02, 0.06, 0.02, 0.09])
if employee['子女数量'] > 0 and rng.random() < 0.14:
reason_category = '子女照护'
else:
reason_category = weighted_choice(
rng,
['身体不适', '家庭事务', '交通受阻', '突发事件', '职业疲劳', '就医复查'],
[0.28, 0.19, 0.09, 0.11, 0.2, 0.13],
)
medical_certificate = int(leave_type in ['病假', '工伤假'] or reason_category in ['身体不适', '就医复查'])
urgent_leave = int(rng.random() < (0.45 if leave_type in ['病假', '事假', '工伤假'] else 0.18))
continuous_absence = int(rng.random() < (0.2 if leave_type in ['病假', '产检育儿假', '工伤假'] else 0.08))
previous_overtime = int(rng.random() < min(0.85, employee['月均加班时长'] / 65))
season = season_from_month(month)
channel = weighted_choice(rng, ['系统申请', '主管代提', '临时电话报备'], [0.68, 0.18, 0.14])
base = 0.95
base += min(employee['月均加班时长'] / 28, 1.8)
base += min(employee['通勤时长分钟'] / 65, 1.2)
base += employee['是否夜班岗位'] * 0.9
base += employee['是否慢性病史'] * 1.25
base += employee['年度体检异常标记'] * 0.6
base += 0.35 * employee['子女数量']
base += 0.5 if employee['心理压力等级'] == '' else (0.2 if employee['心理压力等级'] == '' else -0.1)
base += 0.4 if employee['是否跨城通勤'] else 0
base += 0.35 if previous_overtime else 0
base += 0.35 if near_holiday else 0
base += 0.3 if continuous_absence else 0
base += 0.3 if employee['近90天缺勤次数'] >= 3 else 0
base -= 0.35 if employee['绩效等级'] == 'A' else (0.15 if employee['绩效等级'] == 'B' else 0)
base -= min(employee['司龄年数'] / 40, 0.5)
base -= min(employee['每周运动频次'] * 0.08, 0.3)
base -= 0.2 if employee['近30天睡眠时长均值'] >= 7.5 else 0
leave_bonus = {
'病假': 2.0,
'事假': 0.8,
'年假': 0.1,
'调休': 0.1,
'婚假': 3.0,
'丧假': 2.8,
'产检育儿假': 2.4,
'工伤假': 3.8,
'其他': 0.5,
}
reason_bonus = {
'身体不适': 1.0,
'家庭事务': 0.5,
'子女照护': 0.8,
'交通受阻': 0.2,
'突发事件': 0.6,
'职业疲劳': 0.7,
'就医复查': 1.2,
}
industry_bonus = {
'制造业': 0.35,
'互联网': 0.2,
'零售连锁': 0.25,
'物流运输': 0.4,
'金融服务': 0.1,
'医药健康': 0.2,
'建筑工程': 0.35,
}
season_bonus = {1: 0.35, 2: 0.0, 3: 0.15, 4: 0.05}
weekday_bonus = {1: 0.05, 2: 0.0, 3: 0.0, 4: 0.05, 5: 0.15, 6: 0.25, 7: 0.3}
duration = base
duration += leave_bonus[leave_type]
duration += reason_bonus[reason_category]
duration += industry_bonus[employee['所属行业']]
duration += season_bonus[season]
duration += weekday_bonus[weekday]
duration += 0.55 if medical_certificate else 0
duration += 0.4 if urgent_leave else -0.05
duration += rng.normal(0, 0.9)
if leave_type in ['婚假', '丧假', '工伤假'] and rng.random() < 0.5:
duration += rng.uniform(1.5, 5)
if leave_type == '病假' and employee['是否慢性病史'] == 1 and rng.random() < 0.35:
duration += rng.uniform(1, 4)
if leave_type in ['年假', '调休']:
duration *= rng.uniform(0.7, 0.95)
duration = round(float(np.clip(duration, 0.5, 24.0)), 1)
event = employee.copy()
event.update({
'缺勤月份': month,
'星期几': weekday,
'是否节假日前后': near_holiday,
'季节': season,
'请假申请渠道': channel,
'请假类型': leave_type,
'请假原因大类': reason_category,
'是否提供医院证明': medical_certificate,
'是否临时请假': urgent_leave,
'是否连续缺勤': continuous_absence,
'前一工作日是否加班': previous_overtime,
'缺勤时长(小时)': duration,
})
return event
def validate_dataset(df):
required_columns = [
'员工编号',
'所属行业',
'岗位序列',
'月均加班时长',
'通勤时长分钟',
'是否慢性病史',
'请假类型',
'缺勤时长(小时)',
]
for column in required_columns:
if column not in df.columns:
raise ValueError(f'Missing required column: {column}')
if len(df) < 10000:
raise ValueError('Synthetic dataset is smaller than expected')
if df['员工编号'].nunique() < 2000:
raise ValueError('Employee coverage is too small')
high_risk_ratio = (df['缺勤时长(小时)'] > 8).mean()
if not 0.15 <= high_risk_ratio <= 0.4:
raise ValueError(f'High risk ratio out of range: {high_risk_ratio:.3f}')
medical_mean = df[df['是否提供医院证明'] == 1]['缺勤时长(小时)'].mean()
no_medical_mean = df[df['是否提供医院证明'] == 0]['缺勤时长(小时)'].mean()
if medical_mean <= no_medical_mean:
raise ValueError('Medical certificate signal is not effective')
night_mean = df[df['是否夜班岗位'] == 1]['缺勤时长(小时)'].mean()
day_mean = df[df['是否夜班岗位'] == 0]['缺勤时长(小时)'].mean()
if night_mean <= day_mean:
raise ValueError('Night shift signal is not effective')
def generate_dataset(output_path=None, sample_count=12000, random_state=None):
rng = np.random.default_rng(config.RANDOM_STATE if random_state is None else random_state)
companies = build_company_pool(rng)
employees = build_employee_pool(rng, companies)
events = []
employee_idx = rng.integers(0, len(employees), size=sample_count)
for idx in employee_idx:
events.append(sample_event(rng, employees[int(idx)]))
df = pd.DataFrame(events)
validate_dataset(df)
if output_path:
os.makedirs(os.path.dirname(output_path), exist_ok=True)
df.to_csv(output_path, index=False, encoding='utf-8-sig')
return df
def ensure_dataset():
if not os.path.exists(config.RAW_DATA_PATH):
generate_dataset(config.RAW_DATA_PATH)
return
try:
df = pd.read_csv(config.RAW_DATA_PATH)
validate_dataset(df)
except Exception:
generate_dataset(config.RAW_DATA_PATH)
if __name__ == '__main__':
dataset = generate_dataset(config.RAW_DATA_PATH)
print(f'Generated dataset: {config.RAW_DATA_PATH}')
print(dataset.head())

View File

@@ -0,0 +1,326 @@
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
import config
TARGET_COLUMN = config.TARGET_COLUMN
ID_COLUMN = config.EMPLOYEE_ID_COLUMN
COMPANY_COLUMN = config.COMPANY_ID_COLUMN
LEAKY_COLUMNS = [ID_COLUMN, COMPANY_COLUMN]
ORDINAL_COLUMNS = [
'企业规模',
'所在城市等级',
'岗位级别',
'最高学历',
'绩效等级',
'心理压力等级',
'工龄分层',
'年龄分层',
'通勤分层',
'加班分层',
]
NUMERICAL_OUTLIER_COLUMNS = [
'年龄',
'司龄年数',
'月均加班时长',
'近30天出勤天数',
'近90天缺勤次数',
'近180天请假总时长',
'通勤时长分钟',
'通勤距离公里',
'团队人数',
'直属上级管理跨度',
'BMI',
'近30天睡眠时长均值',
'每周运动频次',
]
DEFAULT_PREDICTION_INPUT = {
'industry': '制造业',
'company_size': '1000-4999人',
'city_tier': '新一线',
'age': 31,
'tenure_years': 4.5,
'education_level': '本科',
'marital_status': '已婚',
'job_family': '专业技术',
'job_level': '中级',
'employment_type': '正式员工',
'shift_type': '标准白班',
'is_night_shift': 0,
'monthly_overtime_hours': 26,
'attendance_days_30d': 22,
'absence_count_90d': 1,
'leave_hours_180d': 18,
'commute_minutes': 42,
'commute_km': 18,
'cross_city_commute': 0,
'performance_level': 'B',
'disciplinary_count_12m': 0,
'team_size': 10,
'manager_span': 14,
'bmi': 24.5,
'chronic_disease_flag': 0,
'annual_check_abnormal_flag': 0,
'sleep_hours': 7.1,
'exercise_frequency': 2,
'smoking_flag': 0,
'drinking_flag': 0,
'stress_level': '',
'sedentary_job_flag': 1,
'local_hukou_flag': 1,
'children_count': 1,
'single_child_burden_flag': 0,
'absence_month': 5,
'weekday': 2,
'near_holiday_flag': 0,
'leave_channel': '系统申请',
'leave_type': '病假',
'leave_reason_category': '身体不适',
'medical_certificate_flag': 1,
'urgent_leave_flag': 1,
'continuous_absence_flag': 0,
'previous_day_overtime_flag': 1,
}
def make_target_bins(y):
y_series = pd.Series(y)
bins = pd.cut(
y_series,
bins=[0, 4, 8, 12, np.inf],
labels=['low', 'medium', 'high', 'extreme'],
include_lowest=True,
)
return bins.astype(str)
def normalize_columns(df):
df = df.copy()
df.columns = [col.strip() for col in df.columns]
return df
def prepare_modeling_dataframe(df):
df = normalize_columns(df)
drop_cols = [col for col in LEAKY_COLUMNS if col in df.columns]
if drop_cols:
df = df.drop(columns=drop_cols)
return df
def fit_outlier_bounds(df, columns, lower_pct=1, upper_pct=99):
bounds = {}
for col in columns:
if col in df.columns and pd.api.types.is_numeric_dtype(df[col]):
bounds[col] = (
float(df[col].quantile(lower_pct / 100)),
float(df[col].quantile(upper_pct / 100)),
)
return bounds
def apply_outlier_bounds(df, bounds):
df = df.copy()
for col, (lower, upper) in bounds.items():
if col in df.columns:
df[col] = df[col].clip(lower, upper)
return df
def engineer_features(df):
df = df.copy()
df['加班通勤压力指数'] = (
df['月均加班时长'] * 0.45
+ df['通勤时长分钟'] * 0.35
+ df['是否夜班岗位'] * 12
+ df['前一工作日是否加班'] * 6
) / 10
df['家庭负担指数'] = (
df['子女数量'] * 1.2
+ df['是否独生子女家庭负担'] * 1.5
+ (df['婚姻状态'] == '已婚').astype(int) * 0.6
)
df['健康风险指数'] = (
df['是否慢性病史'] * 2
+ df['年度体检异常标记'] * 1.2
+ (df['BMI'] >= 28).astype(int) * 1.1
+ df['是否吸烟'] * 0.8
+ df['是否饮酒'] * 0.4
+ (df['近30天睡眠时长均值'] < 6.5).astype(int) * 1.2
)
df['岗位稳定性指数'] = (
df['司龄年数'] * 0.3
+ (df['绩效等级'] == 'A').astype(int) * 1.2
+ (df['绩效等级'] == 'B').astype(int) * 0.8
- df['近12月违纪次数'] * 0.7
)
df['节假日风险标记'] = (
(df['是否节假日前后'] == 1) | (df['请假类型'].isin(['事假', '年假', '调休']))
).astype(int)
df['排班压力标记'] = (
(df['班次类型'].isin(['两班倒', '三班倒'])) | (df['是否夜班岗位'] == 1)
).astype(int)
df['缺勤历史强度'] = df['近90天缺勤次数'] * 1.5 + df['近180天请假总时长'] / 12
df['生活规律指数'] = (
df['近30天睡眠时长均值'] * 0.6
+ df['每周运动频次'] * 0.7
- df['是否吸烟'] * 1.1
- df['是否饮酒'] * 0.5
)
df['管理负荷指数'] = df['团队人数'] * 0.4 + df['直属上级管理跨度'] * 0.25
df['工龄分层'] = pd.cut(df['司龄年数'], bins=[0, 2, 5, 10, 40], labels=['1', '2', '3', '4'])
df['年龄分层'] = pd.cut(df['年龄'], bins=[18, 25, 32, 40, 60], labels=['1', '2', '3', '4'])
df['通勤分层'] = pd.cut(df['通勤时长分钟'], bins=[0, 25, 45, 70, 180], labels=['1', '2', '3', '4'])
df['加班分层'] = pd.cut(df['月均加班时长'], bins=[-1, 10, 25, 45, 120], labels=['1', '2', '3', '4'])
return df
def fit_label_encoders(df, ordinal_columns=None):
ordinal_columns = ordinal_columns or ORDINAL_COLUMNS
df = df.copy()
encoders = {}
object_columns = df.select_dtypes(include=['object', 'category']).columns.tolist()
encode_columns = sorted(set(object_columns + [col for col in ordinal_columns if col in df.columns]))
for col in encode_columns:
encoder = LabelEncoder()
df[col] = encoder.fit_transform(df[col].astype(str))
encoders[col] = encoder
return df, encoders
def apply_label_encoders(df, encoders):
df = df.copy()
for col, encoder in encoders.items():
if col not in df.columns:
continue
value_map = {cls: idx for idx, cls in enumerate(encoder.classes_)}
df[col] = df[col].astype(str).map(lambda value: value_map.get(value, 0))
return df
def extract_xy(df):
y = df[TARGET_COLUMN].values if TARGET_COLUMN in df.columns else None
X_df = df.drop(columns=[TARGET_COLUMN]) if TARGET_COLUMN in df.columns else df.copy()
return X_df, y
def build_prediction_dataframe(data):
feature_row = {
'企业编号': 'PREDICT_COMPANY',
'所属行业': data.get('industry', DEFAULT_PREDICTION_INPUT['industry']),
'企业规模': data.get('company_size', DEFAULT_PREDICTION_INPUT['company_size']),
'所在城市等级': data.get('city_tier', DEFAULT_PREDICTION_INPUT['city_tier']),
'用工类型': data.get('employment_type', DEFAULT_PREDICTION_INPUT['employment_type']),
'部门条线': data.get('department_line', '研发'),
'岗位序列': data.get('job_family', DEFAULT_PREDICTION_INPUT['job_family']),
'岗位级别': data.get('job_level', DEFAULT_PREDICTION_INPUT['job_level']),
'员工编号': 'PREDICT_EMPLOYEE',
'性别': data.get('gender', ''),
'年龄': data.get('age', DEFAULT_PREDICTION_INPUT['age']),
'司龄年数': data.get('tenure_years', DEFAULT_PREDICTION_INPUT['tenure_years']),
'最高学历': data.get('education_level', DEFAULT_PREDICTION_INPUT['education_level']),
'婚姻状态': data.get('marital_status', DEFAULT_PREDICTION_INPUT['marital_status']),
'是否本地户籍': data.get('local_hukou_flag', DEFAULT_PREDICTION_INPUT['local_hukou_flag']),
'子女数量': data.get('children_count', DEFAULT_PREDICTION_INPUT['children_count']),
'是否独生子女家庭负担': data.get(
'single_child_burden_flag',
DEFAULT_PREDICTION_INPUT['single_child_burden_flag'],
),
'居住类型': data.get('housing_type', '租房'),
'班次类型': data.get('shift_type', DEFAULT_PREDICTION_INPUT['shift_type']),
'是否夜班岗位': data.get('is_night_shift', DEFAULT_PREDICTION_INPUT['is_night_shift']),
'月均加班时长': data.get(
'monthly_overtime_hours',
DEFAULT_PREDICTION_INPUT['monthly_overtime_hours'],
),
'近30天出勤天数': data.get(
'attendance_days_30d',
DEFAULT_PREDICTION_INPUT['attendance_days_30d'],
),
'近90天缺勤次数': data.get('absence_count_90d', DEFAULT_PREDICTION_INPUT['absence_count_90d']),
'近180天请假总时长': data.get('leave_hours_180d', DEFAULT_PREDICTION_INPUT['leave_hours_180d']),
'通勤时长分钟': data.get('commute_minutes', DEFAULT_PREDICTION_INPUT['commute_minutes']),
'通勤距离公里': data.get('commute_km', DEFAULT_PREDICTION_INPUT['commute_km']),
'是否跨城通勤': data.get(
'cross_city_commute',
DEFAULT_PREDICTION_INPUT['cross_city_commute'],
),
'绩效等级': data.get('performance_level', DEFAULT_PREDICTION_INPUT['performance_level']),
'近12月违纪次数': data.get(
'disciplinary_count_12m',
DEFAULT_PREDICTION_INPUT['disciplinary_count_12m'],
),
'团队人数': data.get('team_size', DEFAULT_PREDICTION_INPUT['team_size']),
'直属上级管理跨度': data.get('manager_span', DEFAULT_PREDICTION_INPUT['manager_span']),
'BMI': data.get('bmi', DEFAULT_PREDICTION_INPUT['bmi']),
'是否慢性病史': data.get(
'chronic_disease_flag',
DEFAULT_PREDICTION_INPUT['chronic_disease_flag'],
),
'年度体检异常标记': data.get(
'annual_check_abnormal_flag',
DEFAULT_PREDICTION_INPUT['annual_check_abnormal_flag'],
),
'近30天睡眠时长均值': data.get('sleep_hours', DEFAULT_PREDICTION_INPUT['sleep_hours']),
'每周运动频次': data.get(
'exercise_frequency',
DEFAULT_PREDICTION_INPUT['exercise_frequency'],
),
'是否吸烟': data.get('smoking_flag', DEFAULT_PREDICTION_INPUT['smoking_flag']),
'是否饮酒': data.get('drinking_flag', DEFAULT_PREDICTION_INPUT['drinking_flag']),
'心理压力等级': data.get('stress_level', DEFAULT_PREDICTION_INPUT['stress_level']),
'是否长期久坐岗位': data.get(
'sedentary_job_flag',
DEFAULT_PREDICTION_INPUT['sedentary_job_flag'],
),
'缺勤月份': data.get('absence_month', DEFAULT_PREDICTION_INPUT['absence_month']),
'星期几': data.get('weekday', DEFAULT_PREDICTION_INPUT['weekday']),
'是否节假日前后': data.get('near_holiday_flag', DEFAULT_PREDICTION_INPUT['near_holiday_flag']),
'季节': _season_from_month(data.get('absence_month', DEFAULT_PREDICTION_INPUT['absence_month'])),
'请假申请渠道': data.get('leave_channel', DEFAULT_PREDICTION_INPUT['leave_channel']),
'请假类型': data.get('leave_type', DEFAULT_PREDICTION_INPUT['leave_type']),
'请假原因大类': data.get(
'leave_reason_category',
DEFAULT_PREDICTION_INPUT['leave_reason_category'],
),
'是否提供医院证明': data.get(
'medical_certificate_flag',
DEFAULT_PREDICTION_INPUT['medical_certificate_flag'],
),
'是否临时请假': data.get('urgent_leave_flag', DEFAULT_PREDICTION_INPUT['urgent_leave_flag']),
'是否连续缺勤': data.get(
'continuous_absence_flag',
DEFAULT_PREDICTION_INPUT['continuous_absence_flag'],
),
'前一工作日是否加班': data.get(
'previous_day_overtime_flag',
DEFAULT_PREDICTION_INPUT['previous_day_overtime_flag'],
),
}
return pd.DataFrame([feature_row])
def _season_from_month(month):
month = int(month)
if month in [12, 1, 2]:
return 1
if month in [3, 4, 5]:
return 2
if month in [6, 7, 8]:
return 3
return 4
def align_feature_frame(df, feature_names):
aligned = df.copy()
for feature in feature_names:
if feature not in aligned.columns:
aligned[feature] = 0
return aligned[feature_names]
def to_float_array(df):
return df.values.astype(float)

View File

@@ -1,10 +1,11 @@
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
import joblib
import os
import joblib
import pandas as pd
from sklearn.preprocessing import StandardScaler
import config
from core.generate_dataset import ensure_dataset
class DataPreprocessor:
@@ -12,67 +13,57 @@ class DataPreprocessor:
self.scaler = StandardScaler()
self.is_fitted = False
self.feature_names = None
def load_raw_data(self):
ensure_dataset()
df = pd.read_csv(config.RAW_DATA_PATH, sep=config.CSV_SEPARATOR)
df.columns = df.columns.str.strip()
return df
def clean_data(self, df):
df = df.copy()
df = df.drop_duplicates()
for col in df.columns:
if df[col].isnull().sum() > 0:
if df[col].dtype in ['int64', 'float64']:
df[col].fillna(df[col].median(), inplace=True)
else:
df[col].fillna(df[col].mode()[0], inplace=True)
if df[col].isnull().sum() == 0:
continue
if pd.api.types.is_numeric_dtype(df[col]):
df[col] = df[col].fillna(df[col].median())
else:
df[col] = df[col].fillna(df[col].mode()[0])
return df
def fit_transform(self, df):
df = self.clean_data(df)
if 'Absenteeism time in hours' in df.columns:
y = df['Absenteeism time in hours'].values
feature_df = df.drop(columns=['Absenteeism time in hours'])
if config.TARGET_COLUMN in df.columns:
y = df[config.TARGET_COLUMN].values
feature_df = df.drop(columns=[config.TARGET_COLUMN])
else:
y = None
feature_df = df
self.feature_names = list(feature_df.columns)
X = feature_df.values
X = self.scaler.fit_transform(X)
X = self.scaler.fit_transform(feature_df.values)
self.is_fitted = True
return X, y
def transform(self, df):
if not self.is_fitted:
raise ValueError("Preprocessor has not been fitted yet.")
df = self.clean_data(df)
if 'Absenteeism time in hours' in df.columns:
feature_df = df.drop(columns=['Absenteeism time in hours'])
if config.TARGET_COLUMN in df.columns:
feature_df = df.drop(columns=[config.TARGET_COLUMN])
else:
feature_df = df
X = feature_df.values
X = self.scaler.transform(X)
return X
return self.scaler.transform(feature_df.values)
def save_preprocessor(self):
os.makedirs(config.MODELS_DIR, exist_ok=True)
joblib.dump(self.scaler, config.SCALER_PATH)
joblib.dump(self.feature_names, os.path.join(config.MODELS_DIR, 'feature_names.pkl'))
def load_preprocessor(self):
self.scaler = joblib.load(config.SCALER_PATH)
feature_names_path = os.path.join(config.MODELS_DIR, 'feature_names.pkl')
@@ -84,22 +75,18 @@ class DataPreprocessor:
def get_clean_data():
preprocessor = DataPreprocessor()
df = preprocessor.load_raw_data()
df = preprocessor.clean_data(df)
return df
return preprocessor.clean_data(df)
def save_clean_data():
preprocessor = DataPreprocessor()
df = preprocessor.load_raw_data()
df = preprocessor.clean_data(df)
os.makedirs(config.PROCESSED_DATA_DIR, exist_ok=True)
df.to_csv(config.CLEAN_DATA_PATH, index=False, sep=',')
return df
if __name__ == '__main__':
df = save_clean_data()
print(f"Clean data saved. Shape: {df.shape}")
print(df.head())
data = save_clean_data()
print(f"Clean data saved. Shape: {data.shape}")

View File

@@ -1,123 +1,57 @@
import sys
import os
import sys
import time
from datetime import datetime
import joblib
import numpy as np
from sklearn.ensemble import ExtraTreesRegressor, GradientBoostingRegressor, RandomForestRegressor
from sklearn.feature_selection import SelectKBest, f_regression
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.model_selection import RandomizedSearchCV, train_test_split
from sklearn.preprocessing import RobustScaler
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import pandas as pd
import numpy as np
import time
from sklearn.ensemble import (
RandomForestRegressor,
GradientBoostingRegressor,
ExtraTreesRegressor,
StackingRegressor
)
from sklearn.linear_model import Ridge
from sklearn.model_selection import train_test_split, RandomizedSearchCV
from sklearn.preprocessing import RobustScaler, LabelEncoder
from sklearn.feature_selection import SelectKBest, f_regression
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
import xgboost as xgb
import lightgbm as lgb
import joblib
import warnings
warnings.filterwarnings('ignore')
import config
from core.model_features import (
NUMERICAL_OUTLIER_COLUMNS,
ORDINAL_COLUMNS,
TARGET_COLUMN,
align_feature_frame,
apply_label_encoders,
apply_outlier_bounds,
engineer_features,
extract_xy,
fit_label_encoders,
fit_outlier_bounds,
make_target_bins,
normalize_columns,
prepare_modeling_dataframe,
to_float_array,
)
from core.preprocessing import get_clean_data
try:
import lightgbm as lgb
except ImportError:
lgb = None
try:
import xgboost as xgb
except ImportError:
xgb = None
def print_training_log(model_name, start_time, best_score, best_params, n_iter, cv_folds):
elapsed = time.time() - start_time
print(f" {''*50}")
print(f" Model: {model_name}")
print(f" Time: {elapsed:.1f}s")
print(f" Best CV R2: {best_score:.4f}")
print(f" Best params:")
for k, v in best_params.items():
print(f" - {k}: {v}")
print(f" Iterations: {n_iter}, CV folds: {cv_folds}")
print(f" {''*50}")
class DataAugmenter:
def __init__(self, noise_level=0.02, n_augment=2):
self.noise_level = noise_level
self.n_augment = n_augment
def augment(self, df, target_col='Absenteeism time in hours'):
print(f"\nData Augmentation...")
print(f" Original size: {len(df)}")
augmented_dfs = [df]
numerical_cols = df.select_dtypes(include=[np.number]).columns.tolist()
if target_col in numerical_cols:
numerical_cols.remove(target_col)
for i in range(self.n_augment):
df_aug = df.copy()
for col in numerical_cols:
if col in df_aug.columns:
std_val = df_aug[col].std()
if std_val > 0:
noise = np.random.normal(0, self.noise_level * std_val, len(df_aug))
df_aug[col] = df_aug[col] + noise
augmented_dfs.append(df_aug)
df_result = pd.concat(augmented_dfs, ignore_index=True)
print(f" Augmented size: {len(df_result)}")
return df_result
def smote_regression(self, df, target_col='Absenteeism time in hours'):
df = df.copy()
y = df[target_col].values
bins = [0, 1, 4, 8, 100]
labels = ['zero', 'low', 'medium', 'high']
df['_target_bin'] = pd.cut(y, bins=bins, labels=labels, include_lowest=True)
bin_counts = df['_target_bin'].value_counts()
max_count = bin_counts.max()
numerical_cols = df.select_dtypes(include=[np.number]).columns.tolist()
if target_col in numerical_cols:
numerical_cols.remove(target_col)
if '_target_bin' in numerical_cols:
numerical_cols.remove('_target_bin')
augmented_rows = []
for bin_label in labels:
bin_df = df[df['_target_bin'] == bin_label].drop(columns=['_target_bin'])
bin_size = len(bin_df)
if bin_size < max_count and bin_size > 0:
n_samples_to_add = max_count - bin_size
for _ in range(n_samples_to_add):
idx = np.random.choice(bin_df.index)
sample = bin_df.loc[idx].copy()
for col in numerical_cols:
if col in sample.index:
std_val = bin_df[col].std()
if std_val > 0:
noise = np.random.normal(0, 0.02 * std_val)
sample[col] = sample[col] + noise
augmented_rows.append(sample)
if augmented_rows:
df_aug = pd.DataFrame(augmented_rows)
df_result = pd.concat([df.drop(columns=['_target_bin']), df_aug], ignore_index=True)
else:
df_result = df.drop(columns=['_target_bin'])
print(f" After SMOTE-like augmentation: {len(df_result)}")
return df_result
print(f' {"-" * 50}')
print(f' Model: {model_name}')
print(f' Time: {elapsed:.1f}s')
print(f' Best CV R2: {best_score:.4f}')
for key, value in best_params.items():
print(f' - {key}: {value}')
print(f' Iterations: {n_iter}, CV folds: {cv_folds}')
class OptimizedModelTrainer:
@@ -128,461 +62,237 @@ class OptimizedModelTrainer:
self.selected_features = None
self.label_encoders = {}
self.model_metrics = {}
self.augmenter = DataAugmenter(noise_level=0.02, n_augment=2)
self.training_metadata = {}
self.feature_selector = None
self.outlier_bounds = {}
self.feature_k = 22
self.target_transform = 'log1p'
self.enabled_models = ['random_forest', 'gradient_boosting', 'extra_trees', 'lightgbm', 'xgboost']
def analyze_data(self, df):
print("\n" + "="*60)
print("Data Analysis")
print("="*60)
y = df['Absenteeism time in hours']
print(f"\nTarget variable statistics:")
print(f" Min: {y.min()}")
print(f" Max: {y.max()}")
print(f" Mean: {y.mean():.2f}")
print(f" Median: {y.median():.2f}")
print(f" Std: {y.std():.2f}")
print(f" Skewness: {y.skew():.2f}")
print(f"\nTarget distribution:")
print(f" Zero values: {(y == 0).sum()} ({(y == 0).sum() / len(y) * 100:.1f}%)")
print(f" 1-8 hours: {((y > 0) & (y <= 8)).sum()} ({((y > 0) & (y <= 8)).sum() / len(y) * 100:.1f}%)")
print(f" >8 hours: {(y > 8).sum()} ({(y > 8).sum() / len(y) * 100:.1f}%)")
return y
def clip_outliers(self, df, columns, lower_pct=1, upper_pct=99):
df_clean = df.copy()
for col in columns:
if col in df_clean.columns and df_clean[col].dtype in ['int64', 'float64']:
if col == 'Absenteeism time in hours':
continue
lower = df_clean[col].quantile(lower_pct / 100)
upper = df_clean[col].quantile(upper_pct / 100)
df_clean[col] = df_clean[col].clip(lower, upper)
return df_clean
def feature_engineering(self, df):
df = df.copy()
df['workload_per_age'] = df['Work load Average/day'] / (df['Age'] + 1)
df['expense_per_distance'] = df['Transportation expense'] / (df['Distance from Residence to Work'] + 1)
df['age_service_ratio'] = df['Age'] / (df['Service time'] + 1)
df['has_children'] = (df['Son'] > 0).astype(int)
df['has_pet'] = (df['Pet'] > 0).astype(int)
df['family_responsibility'] = df['Son'] + df['Pet']
df['health_risk'] = ((df['Social drinker'] == 1) | (df['Social smoker'] == 1) | (df['Body mass index'] > 30)).astype(int)
df['lifestyle_risk'] = df['Social drinker'].astype(int) + df['Social smoker'].astype(int)
df['age_group'] = pd.cut(df['Age'], bins=[0, 30, 40, 50, 100], labels=[1, 2, 3, 4])
df['service_group'] = pd.cut(df['Service time'], bins=[0, 5, 10, 20, 100], labels=[1, 2, 3, 4])
df['bmi_category'] = pd.cut(df['Body mass index'], bins=[0, 18.5, 25, 30, 100], labels=[1, 2, 3, 4])
df['workload_category'] = pd.cut(df['Work load Average/day'], bins=[0, 200, 250, 300, 500], labels=[1, 2, 3, 4])
df['commute_category'] = pd.cut(df['Distance from Residence to Work'], bins=[0, 10, 20, 50, 100], labels=[1, 2, 3, 4])
df['seasonal_risk'] = df['Seasons'].apply(lambda x: 1 if x in [1, 3] else 0)
df['weekday_risk'] = df['Day of the week'].apply(lambda x: 1 if x in [2, 6] else 0)
df['hit_target_ratio'] = df['Hit target'] / 100
df['experience_level'] = pd.cut(df['Service time'], bins=[0, 5, 10, 15, 100], labels=[1, 2, 3, 4])
df['age_workload_interaction'] = df['Age'] * df['Work load Average/day'] / 10000
df['service_bmi_interaction'] = df['Service time'] * df['Body mass index'] / 100
return df
y = df[TARGET_COLUMN]
print('\nData Analysis')
print(f' Samples: {len(df)}')
print(f' Mean: {y.mean():.2f}, Median: {y.median():.2f}, Std: {y.std():.2f}')
print(f' High risk ratio (>8h): {(y > 8).mean() * 100:.1f}%')
def select_features(self, X, y, k=20):
print("\nFeature Selection...")
selector = SelectKBest(score_func=f_regression, k=min(k, X.shape[1]))
selector.fit(X, y)
scores = selector.scores_
feature_scores = list(zip(self.feature_names, scores))
feature_scores.sort(key=lambda x: x[1], reverse=True)
print(f"\nTop {min(k, len(feature_scores))} features by F-score:")
for i, (name, score) in enumerate(feature_scores[:min(k, len(feature_scores))]):
cn = config.FEATURE_NAME_CN.get(name, name)
print(f" {i+1}. {cn}: {score:.2f}")
selected_mask = selector.get_support()
self.selected_features = [f for f, s in zip(self.feature_names, selected_mask) if s]
self.feature_selector = selector
mask = selector.get_support()
self.selected_features = [name for name, keep in zip(self.feature_names, mask) if keep]
return selector.transform(X)
def transform_target(self, y):
return np.log1p(np.clip(y, a_min=0, a_max=None)) if self.target_transform == 'log1p' else y
def inverse_transform_target(self, y_pred):
return np.expm1(y_pred) if self.target_transform == 'log1p' else y_pred
def transform_features(self, X_df):
X_df = align_feature_frame(X_df, self.feature_names)
X = self.scaler.transform(to_float_array(X_df))
return self.feature_selector.transform(X) if self.feature_selector else X
def prepare_data(self):
df = get_clean_data()
df.columns = [col.strip() for col in df.columns]
df = df.drop(columns=['ID'])
cols_to_drop = ['Weight', 'Height', 'Reason for absence']
for col in cols_to_drop:
if col in df.columns:
df = df.drop(columns=[col])
print(" Removed features: Weight, Height, Reason for absence (data leakage risk)")
df = normalize_columns(get_clean_data())
df = prepare_modeling_dataframe(df)
self.analyze_data(df)
print("\n" + "="*60)
print("Data Preprocessing")
print("="*60)
numerical_cols = ['Age', 'Service time', 'Work load Average/day',
'Transportation expense', 'Distance from Residence to Work',
'Hit target', 'Body mass index']
df = self.clip_outliers(df, numerical_cols)
print(" Outliers clipped (1st-99th percentile)")
print("\n" + "="*60)
print("Data Augmentation")
print("="*60)
df = self.augmenter.smote_regression(df)
df = self.augmenter.augment(df)
print("\n" + "="*60)
print("Feature Engineering")
print("="*60)
df = self.feature_engineering(df)
y = df['Absenteeism time in hours'].values
X_df = df.drop(columns=['Absenteeism time in hours'])
ordinal_cols = ['Month of absence', 'Day of the week', 'Seasons',
'Disciplinary failure', 'Education', 'Social drinker',
'Social smoker', 'age_group', 'service_group',
'bmi_category', 'workload_category', 'commute_category',
'experience_level']
for col in ordinal_cols:
if col in X_df.columns:
le = LabelEncoder()
X_df[col] = le.fit_transform(X_df[col].astype(str))
self.label_encoders[col] = le
self.feature_names = list(X_df.columns)
X = X_df.values.astype(float)
X = self.scaler.fit_transform(X)
X = self.select_features(X, y, k=20)
print(f"\nFinal feature count: {X.shape[1]}")
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
target_bins = make_target_bins(df[TARGET_COLUMN].values)
train_df, test_df = train_test_split(
df,
test_size=config.TEST_SIZE,
random_state=config.RANDOM_STATE,
stratify=target_bins,
)
train_df = train_df.reset_index(drop=True)
test_df = test_df.reset_index(drop=True)
self.outlier_bounds = fit_outlier_bounds(train_df, NUMERICAL_OUTLIER_COLUMNS)
train_df = apply_outlier_bounds(train_df, self.outlier_bounds)
test_df = apply_outlier_bounds(test_df, self.outlier_bounds)
train_df = engineer_features(train_df)
test_df = engineer_features(test_df)
X_train_df, y_train = extract_xy(train_df)
X_test_df, y_test = extract_xy(test_df)
X_train_df, self.label_encoders = fit_label_encoders(X_train_df, ORDINAL_COLUMNS)
X_test_df = apply_label_encoders(X_test_df, self.label_encoders)
self.feature_names = list(X_train_df.columns)
X_test_df = align_feature_frame(X_test_df, self.feature_names)
X_train = self.scaler.fit_transform(to_float_array(X_train_df))
X_test = self.scaler.transform(to_float_array(X_test_df))
transformed_target = self.transform_target(y_train)
X_train = self.select_features(X_train, transformed_target, k=self.feature_k)
X_test = self.transform_features(X_test_df)
self.training_metadata = {
'train_samples': int(len(train_df)),
'test_samples': int(len(test_df)),
'feature_count_before_selection': int(len(self.feature_names)),
'feature_count_after_selection': int(X_train.shape[1]),
'training_date': datetime.now().strftime('%Y-%m-%d'),
'target_transform': self.target_transform,
'available_models': list(self.enabled_models),
}
return X_train, X_test, y_train, y_test
def _run_search(self, name, estimator, params, X_train, y_train, n_iter=12):
start_time = time.time()
search = RandomizedSearchCV(
estimator,
param_distributions=params,
n_iter=n_iter,
cv=4,
scoring='r2',
n_jobs=-1,
random_state=config.RANDOM_STATE,
)
search.fit(X_train, y_train)
self.models[name] = search.best_estimator_
print_training_log(name, start_time, search.best_score_, search.best_params_, n_iter, 4)
def train_random_forest(self, X_train, y_train):
print("\n" + "="*60)
print("Training Random Forest")
print("="*60)
start_time = time.time()
rf = RandomForestRegressor(random_state=42, n_jobs=-1)
param_distributions = {
'n_estimators': [200, 300, 400],
'max_depth': [10, 15, 20, 25],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4],
'max_features': ['sqrt', 0.7]
}
print(f" Searching {20*5} parameter combinations...")
random_search = RandomizedSearchCV(
rf, param_distributions, n_iter=20, cv=5,
scoring='r2', n_jobs=-1, random_state=42
self._run_search(
'random_forest',
RandomForestRegressor(random_state=config.RANDOM_STATE, n_jobs=-1),
{
'n_estimators': [200, 300, 400],
'max_depth': [10, 14, 18, None],
'min_samples_split': [2, 4, 8],
'min_samples_leaf': [1, 2, 3],
'max_features': ['sqrt', 0.7],
},
X_train,
y_train,
)
random_search.fit(X_train, y_train)
self.models['random_forest'] = random_search.best_estimator_
print_training_log("Random Forest", start_time, random_search.best_score_,
random_search.best_params_, 20, 5)
return random_search.best_estimator_
def train_xgboost(self, X_train, y_train):
print("\n" + "="*60)
print("Training XGBoost")
print("="*60)
start_time = time.time()
xgb_model = xgb.XGBRegressor(random_state=42, n_jobs=-1)
param_distributions = {
'n_estimators': [200, 300, 400],
'max_depth': [5, 7, 9],
'learning_rate': [0.05, 0.1],
'subsample': [0.7, 0.8],
'colsample_bytree': [0.7, 0.8],
'min_child_weight': [1, 3],
'reg_alpha': [0, 0.1],
'reg_lambda': [1, 1.5]
}
print(f" Searching {20*5} parameter combinations...")
random_search = RandomizedSearchCV(
xgb_model, param_distributions, n_iter=20, cv=5,
scoring='r2', n_jobs=-1, random_state=42
)
random_search.fit(X_train, y_train)
self.models['xgboost'] = random_search.best_estimator_
print_training_log("XGBoost", start_time, random_search.best_score_,
random_search.best_params_, 20, 5)
return random_search.best_estimator_
def train_lightgbm(self, X_train, y_train):
print("\n" + "="*60)
print("Training LightGBM")
print("="*60)
start_time = time.time()
lgb_model = lgb.LGBMRegressor(random_state=42, n_jobs=-1, verbose=-1)
param_distributions = {
'n_estimators': [200, 300, 400],
'max_depth': [7, 9, 11, -1],
'learning_rate': [0.05, 0.1],
'subsample': [0.7, 0.8],
'colsample_bytree': [0.7, 0.8],
'min_child_samples': [5, 10, 20],
'reg_alpha': [0, 0.1],
'reg_lambda': [1, 1.5],
'num_leaves': [31, 50, 70]
}
print(f" Searching {20*5} parameter combinations...")
random_search = RandomizedSearchCV(
lgb_model, param_distributions, n_iter=20, cv=5,
scoring='r2', n_jobs=-1, random_state=42
)
random_search.fit(X_train, y_train)
self.models['lightgbm'] = random_search.best_estimator_
print_training_log("LightGBM", start_time, random_search.best_score_,
random_search.best_params_, 20, 5)
return random_search.best_estimator_
def train_gradient_boosting(self, X_train, y_train):
print("\n" + "="*60)
print("Training Gradient Boosting")
print("="*60)
start_time = time.time()
gb = GradientBoostingRegressor(random_state=42)
param_distributions = {
'n_estimators': [200, 300],
'max_depth': [5, 7, 9],
'learning_rate': [0.05, 0.1],
'subsample': [0.7, 0.8],
'min_samples_split': [2, 5],
'min_samples_leaf': [1, 2]
}
print(f" Searching {15*5} parameter combinations...")
random_search = RandomizedSearchCV(
gb, param_distributions, n_iter=15, cv=5,
scoring='r2', n_jobs=-1, random_state=42
self._run_search(
'gradient_boosting',
GradientBoostingRegressor(random_state=config.RANDOM_STATE),
{
'n_estimators': [160, 220, 300],
'max_depth': [3, 4, 5],
'learning_rate': [0.03, 0.05, 0.08],
'subsample': [0.7, 0.85, 1.0],
'min_samples_split': [2, 4, 6],
'min_samples_leaf': [1, 2, 3],
},
X_train,
y_train,
)
random_search.fit(X_train, y_train)
self.models['gradient_boosting'] = random_search.best_estimator_
print_training_log("Gradient Boosting", start_time, random_search.best_score_,
random_search.best_params_, 15, 5)
return random_search.best_estimator_
def train_extra_trees(self, X_train, y_train):
print("\n" + "="*60)
print("Training Extra Trees")
print("="*60)
start_time = time.time()
et = ExtraTreesRegressor(random_state=42, n_jobs=-1)
param_distributions = {
'n_estimators': [200, 300, 400],
'max_depth': [10, 15, 20],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4],
'max_features': ['sqrt', 0.7]
}
print(f" Searching {20*5} parameter combinations...")
random_search = RandomizedSearchCV(
et, param_distributions, n_iter=20, cv=5,
scoring='r2', n_jobs=-1, random_state=42
self._run_search(
'extra_trees',
ExtraTreesRegressor(random_state=config.RANDOM_STATE, n_jobs=-1),
{
'n_estimators': [220, 320, 420],
'max_depth': [10, 15, 20, None],
'min_samples_split': [2, 4, 8],
'min_samples_leaf': [1, 2, 3],
'max_features': ['sqrt', 0.7],
},
X_train,
y_train,
)
random_search.fit(X_train, y_train)
self.models['extra_trees'] = random_search.best_estimator_
print_training_log("Extra Trees", start_time, random_search.best_score_,
random_search.best_params_, 20, 5)
return random_search.best_estimator_
def train_stacking(self, X_train, y_train):
print("\n" + "="*60)
print("Training Stacking Ensemble")
print("="*60)
start_time = time.time()
base_estimators = []
if 'random_forest' in self.models:
base_estimators.append(('rf', self.models['random_forest']))
if 'xgboost' in self.models:
base_estimators.append(('xgb', self.models['xgboost']))
if 'lightgbm' in self.models:
base_estimators.append(('lgb', self.models['lightgbm']))
if 'gradient_boosting' in self.models:
base_estimators.append(('gb', self.models['gradient_boosting']))
if len(base_estimators) < 2:
print(" Not enough base models for stacking")
return None
print(f" Base estimators: {[name for name, _ in base_estimators]}")
print(f" Meta learner: Ridge")
print(f" CV folds: 5")
stacking = StackingRegressor(
estimators=base_estimators,
final_estimator=Ridge(alpha=1.0),
cv=5,
n_jobs=-1
def train_lightgbm(self, X_train, y_train):
if lgb is None:
return
self._run_search(
'lightgbm',
lgb.LGBMRegressor(random_state=config.RANDOM_STATE, n_jobs=-1, verbose=-1),
{
'n_estimators': [180, 260, 340],
'max_depth': [7, 9, -1],
'learning_rate': [0.03, 0.05, 0.08],
'subsample': [0.7, 0.85, 1.0],
'colsample_bytree': [0.7, 0.85, 1.0],
'num_leaves': [31, 50, 70],
},
X_train,
y_train,
)
stacking.fit(X_train, y_train)
self.models['stacking'] = stacking
elapsed = time.time() - start_time
print(f" {''*50}")
print(f" Stacking ensemble created in {elapsed:.1f}s")
print(f" {''*50}")
return stacking
def train_xgboost(self, X_train, y_train):
if xgb is None:
return
self._run_search(
'xgboost',
xgb.XGBRegressor(random_state=config.RANDOM_STATE, n_jobs=-1),
{
'n_estimators': [180, 260, 340],
'max_depth': [4, 6, 8],
'learning_rate': [0.03, 0.05, 0.08],
'subsample': [0.7, 0.85, 1.0],
'colsample_bytree': [0.7, 0.85, 1.0],
'min_child_weight': [1, 3, 5],
},
X_train,
y_train,
)
def evaluate_model(self, model, X_test, y_test):
y_pred = model.predict(X_test)
r2 = r2_score(y_test, y_pred)
y_pred = self.inverse_transform_target(model.predict(X_test))
y_pred = np.clip(y_pred, a_min=0, a_max=None)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
mae = mean_absolute_error(y_test, y_pred)
return {
'r2': round(r2, 4),
'r2': round(r2_score(y_test, y_pred), 4),
'mse': round(mse, 4),
'rmse': round(rmse, 4),
'mae': round(mae, 4)
'rmse': round(np.sqrt(mse), 4),
'mae': round(mean_absolute_error(y_test, y_pred), 4),
}
def save_models(self):
os.makedirs(config.MODELS_DIR, exist_ok=True)
for name, model in self.models.items():
if model is not None:
model_path = os.path.join(config.MODELS_DIR, f'{name}_model.pkl')
joblib.dump(model, model_path)
print(f" {name} saved")
joblib.dump(model, os.path.join(config.MODELS_DIR, f'{name}_model.pkl'))
joblib.dump(self.scaler, config.SCALER_PATH)
joblib.dump(self.feature_names, os.path.join(config.MODELS_DIR, 'feature_names.pkl'))
joblib.dump(self.selected_features, os.path.join(config.MODELS_DIR, 'selected_features.pkl'))
joblib.dump(self.label_encoders, os.path.join(config.MODELS_DIR, 'label_encoders.pkl'))
joblib.dump(self.model_metrics, os.path.join(config.MODELS_DIR, 'model_metrics.pkl'))
print(" Scaler and feature info saved")
joblib.dump(self.training_metadata, os.path.join(config.MODELS_DIR, 'training_metadata.pkl'))
def train_all(self):
total_start = time.time()
print("\n" + "="*60)
print("Optimized Model Training Started")
print("="*60)
print(f"Start time: {time.strftime('%Y-%m-%d %H:%M:%S')}")
print('\nOptimized Model Training Started')
X_train, X_test, y_train, y_test = self.prepare_data()
print(f"\nTrain size: {len(X_train)}, Test size: {len(X_test)}")
print("\n" + "="*60)
print("Training Models with Hyperparameter Optimization")
print("="*60)
self.train_random_forest(X_train, y_train)
self.train_extra_trees(X_train, y_train)
self.train_xgboost(X_train, y_train)
self.train_lightgbm(X_train, y_train)
self.train_gradient_boosting(X_train, y_train)
self.train_stacking(X_train, y_train)
print("\n" + "="*60)
print("Evaluating Models on Test Set")
print("="*60)
best_r2 = -float('inf')
best_model = None
y_train_transformed = self.transform_target(y_train)
if 'random_forest' in self.enabled_models:
self.train_random_forest(X_train, y_train_transformed)
if 'gradient_boosting' in self.enabled_models:
self.train_gradient_boosting(X_train, y_train_transformed)
if 'extra_trees' in self.enabled_models:
self.train_extra_trees(X_train, y_train_transformed)
if 'lightgbm' in self.enabled_models:
self.train_lightgbm(X_train, y_train_transformed)
if 'xgboost' in self.enabled_models:
self.train_xgboost(X_train, y_train_transformed)
for name, model in self.models.items():
if model is not None:
metrics = self.evaluate_model(model, X_test, y_test)
self.model_metrics[name] = metrics
status = "Good" if metrics['r2'] > 0.5 else ("OK" if metrics['r2'] > 0.3 else "Poor")
status_icon = "" if status == "Good" else ("" if status == "OK" else "")
print(f" {status_icon} {name:20s} - R2: {metrics['r2']:.4f}, RMSE: {metrics['rmse']:.4f}, MAE: {metrics['mae']:.4f}")
if metrics['r2'] > best_r2:
best_r2 = metrics['r2']
best_model = name
print(f"\n ★ Best Model: {best_model} (R2 = {best_r2:.4f})")
print("\n" + "="*60)
print("Saving Models")
print("="*60)
metrics = self.evaluate_model(model, X_test, y_test)
self.model_metrics[name] = metrics
print(f' {name:20s} R2={metrics["r2"]:.4f} RMSE={metrics["rmse"]:.4f} MAE={metrics["mae"]:.4f}')
self.save_models()
return self.model_metrics
def train_and_save_models():
total_start = time.time()
start = time.time()
trainer = OptimizedModelTrainer()
metrics = trainer.train_all()
total_elapsed = time.time() - total_start
print("\n" + "="*60)
print("Training Complete!")
print("="*60)
print(f"Total training time: {total_elapsed:.1f}s ({total_elapsed/60:.1f} min)")
print(f"End time: {time.strftime('%Y-%m-%d %H:%M:%S')}")
print("\n" + "-"*60)
print("Final Model Ranking (by R2)")
print("-"*60)
sorted_metrics = sorted(metrics.items(), key=lambda x: x[1]['r2'], reverse=True)
for i, (name, m) in enumerate(sorted_metrics, 1):
medal = "🥇" if i == 1 else ("🥈" if i == 2 else ("🥉" if i == 3 else " "))
print(f" {medal} {i}. {name:20s} - R2: {m['r2']:.4f}, RMSE: {m['rmse']:.4f}")
print(f'\nTraining Complete in {time.time() - start:.1f}s')
for idx, (name, metric) in enumerate(sorted(metrics.items(), key=lambda item: item[1]['r2'], reverse=True), start=1):
print(f'{idx}. {name} - R2={metric["r2"]:.4f}')
return metrics