Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,6 +37,15 @@ class RSM_BoxBehnken:
|
|
| 37 |
Inicializa la clase con los datos del diseño Box-Behnken.
|
| 38 |
"""
|
| 39 |
self.data = data.copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
self.model = None
|
| 41 |
self.model_simplified = None
|
| 42 |
self.optimized_results = None
|
|
@@ -94,8 +103,8 @@ class RSM_BoxBehnken:
|
|
| 94 |
Ajusta el modelo de segundo orden completo a los datos.
|
| 95 |
"""
|
| 96 |
formula = f'{self.y_name} ~ {self.x1_name} + {self.x2_name} + {self.x3_name} + ' \
|
| 97 |
-
f'
|
| 98 |
-
f'{self.x1_name}
|
| 99 |
self.model = smf.ols(formula, data=self.data).fit()
|
| 100 |
print("Modelo Completo:")
|
| 101 |
print(self.model.summary())
|
|
@@ -106,7 +115,7 @@ class RSM_BoxBehnken:
|
|
| 106 |
Ajusta el modelo de segundo orden a los datos, eliminando términos no significativos.
|
| 107 |
"""
|
| 108 |
formula = f'{self.y_name} ~ {self.x1_name} + {self.x2_name} + ' \
|
| 109 |
-
f'
|
| 110 |
self.model_simplified = smf.ols(formula, data=self.data).fit()
|
| 111 |
print("\nModelo Simplificado:")
|
| 112 |
print(self.model_simplified.summary())
|
|
@@ -117,9 +126,9 @@ class RSM_BoxBehnken:
|
|
| 117 |
Ajusta un modelo Ridge Regression.
|
| 118 |
"""
|
| 119 |
# Preparar datos
|
| 120 |
-
#
|
| 121 |
X = self.data[[self.x1_name, self.x2_name, self.x3_name,
|
| 122 |
-
f'
|
| 123 |
y = self.data[self.y_name]
|
| 124 |
|
| 125 |
# Ajustar modelo Ridge
|
|
@@ -142,9 +151,9 @@ class RSM_BoxBehnken:
|
|
| 142 |
Ajusta un modelo LASSO Regression.
|
| 143 |
"""
|
| 144 |
# Preparar datos
|
| 145 |
-
#
|
| 146 |
X = self.data[[self.x1_name, self.x2_name, self.x3_name,
|
| 147 |
-
f'
|
| 148 |
y = self.data[self.y_name]
|
| 149 |
|
| 150 |
# Ajustar modelo LASSO
|
|
@@ -167,9 +176,9 @@ class RSM_BoxBehnken:
|
|
| 167 |
Ajusta un modelo ElasticNet Regression.
|
| 168 |
"""
|
| 169 |
# Preparar datos
|
| 170 |
-
#
|
| 171 |
X = self.data[[self.x1_name, self.x2_name, self.x3_name,
|
| 172 |
-
f'
|
| 173 |
y = self.data[self.y_name]
|
| 174 |
|
| 175 |
# Ajustar modelo ElasticNet
|
|
@@ -274,7 +283,10 @@ class RSM_BoxBehnken:
|
|
| 274 |
return -self.model_simplified.predict(pd.DataFrame({
|
| 275 |
self.x1_name: [x[0]],
|
| 276 |
self.x2_name: [x[1]],
|
| 277 |
-
self.x3_name: [x[2]]
|
|
|
|
|
|
|
|
|
|
| 278 |
})).values[0]
|
| 279 |
|
| 280 |
bounds = [(-1, 1), (-1, 1), (-1, 1)]
|
|
@@ -319,7 +331,10 @@ class RSM_BoxBehnken:
|
|
| 319 |
return -self.model_simplified.predict(pd.DataFrame({
|
| 320 |
self.x1_name: [x[0]],
|
| 321 |
self.x2_name: [x[1]],
|
| 322 |
-
self.x3_name: [x[2]]
|
|
|
|
|
|
|
|
|
|
| 323 |
})).values[0]
|
| 324 |
|
| 325 |
# Ejecutar optimización bayesiana
|
|
@@ -363,7 +378,10 @@ class RSM_BoxBehnken:
|
|
| 363 |
return -self.model_simplified.predict(pd.DataFrame({
|
| 364 |
self.x1_name: [x[0]],
|
| 365 |
self.x2_name: [x[1]],
|
| 366 |
-
self.x3_name: [x[2]]
|
|
|
|
|
|
|
|
|
|
| 367 |
})).values[0]
|
| 368 |
|
| 369 |
# Parámetros PSO
|
|
@@ -493,7 +511,7 @@ class RSM_BoxBehnken:
|
|
| 493 |
|
| 494 |
# Variables predictoras
|
| 495 |
X = self.data[[self.x1_name, self.x2_name, self.x3_name,
|
| 496 |
-
f'
|
| 497 |
|
| 498 |
# Calcular VIF para cada variable
|
| 499 |
vif_data = pd.DataFrame()
|
|
@@ -629,7 +647,7 @@ class RSM_BoxBehnken:
|
|
| 629 |
|
| 630 |
# Preparar datos
|
| 631 |
X = self.data[[self.x1_name, self.x2_name, self.x3_name,
|
| 632 |
-
f'
|
| 633 |
y = self.data[self.y_name]
|
| 634 |
|
| 635 |
# Calcular R² de validación cruzada
|
|
@@ -684,6 +702,10 @@ class RSM_BoxBehnken:
|
|
| 684 |
varying_variables[1]: y_grid_coded.flatten(),
|
| 685 |
})
|
| 686 |
prediction_data[fixed_variable] = self.natural_to_coded(fixed_level, fixed_variable)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 687 |
|
| 688 |
# Calcular los valores predichos
|
| 689 |
z_pred = self.model_simplified.predict(prediction_data).values.reshape(x_grid_coded.shape)
|
|
@@ -777,6 +799,10 @@ class RSM_BoxBehnken:
|
|
| 777 |
varying_variables[1]: y_grid_coded.flatten(),
|
| 778 |
})
|
| 779 |
prediction_data[fixed_variable] = self.natural_to_coded(fixed_level, fixed_variable)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 780 |
|
| 781 |
# Calcular los valores predichos
|
| 782 |
z_pred = self.model_simplified.predict(prediction_data).values.reshape(x_grid_coded.shape)
|
|
@@ -964,11 +990,11 @@ class RSM_BoxBehnken:
|
|
| 964 |
equation += f" + {coef:.3f}*{self.x2_name}"
|
| 965 |
elif term == f'{self.x3_name}':
|
| 966 |
equation += f" + {coef:.3f}*{self.x3_name}"
|
| 967 |
-
elif term == f'
|
| 968 |
equation += f" + {coef:.3f}*{self.x1_name}^2"
|
| 969 |
-
elif term == f'
|
| 970 |
equation += f" + {coef:.3f}*{self.x2_name}^2"
|
| 971 |
-
elif term == f'
|
| 972 |
equation += f" + {coef:.3f}*{self.x3_name}^2"
|
| 973 |
|
| 974 |
return equation
|
|
@@ -1052,11 +1078,11 @@ class RSM_BoxBehnken:
|
|
| 1052 |
for index, row in anova_table.iterrows():
|
| 1053 |
if index != 'Residual':
|
| 1054 |
factor_name = index
|
| 1055 |
-
if factor_name == f'
|
| 1056 |
factor_name = f'{self.x1_name}^2'
|
| 1057 |
-
elif factor_name == f'
|
| 1058 |
factor_name = f'{self.x2_name}^2'
|
| 1059 |
-
elif factor_name == f'
|
| 1060 |
factor_name = f'{self.x3_name}^2'
|
| 1061 |
ss_factor = row['sum_sq']
|
| 1062 |
df_factor = row['df']
|
|
@@ -1100,7 +1126,7 @@ class RSM_BoxBehnken:
|
|
| 1100 |
# --- ANOVA detallada ---
|
| 1101 |
# 1. Ajustar un modelo solo con los términos de primer orden y cuadráticos
|
| 1102 |
formula_reduced = f'{self.y_name} ~ {self.x1_name} + {self.x2_name} + {self.x3_name} + ' \
|
| 1103 |
-
f'
|
| 1104 |
model_reduced = smf.ols(formula_reduced, data=self.data).fit()
|
| 1105 |
|
| 1106 |
# 2. ANOVA del modelo reducido
|
|
@@ -1158,9 +1184,9 @@ class RSM_BoxBehnken:
|
|
| 1158 |
})
|
| 1159 |
|
| 1160 |
# Calcular la suma de cuadrados y estadísticos F para la curvatura
|
| 1161 |
-
ss_curvature = anova_reduced['sum_sq'][f'
|
| 1162 |
-
anova_reduced['sum_sq'][f'
|
| 1163 |
-
anova_reduced['sum_sq'][f'
|
| 1164 |
df_curvature = 3
|
| 1165 |
ms_curvature = ss_curvature / df_curvature
|
| 1166 |
f_curvature = ms_curvature / ms_residual
|
|
|
|
| 37 |
Inicializa la clase con los datos del diseño Box-Behnken.
|
| 38 |
"""
|
| 39 |
self.data = data.copy()
|
| 40 |
+
# Crear columnas cuadráticas con nombres consistentes
|
| 41 |
+
self.data[f'{x1_name}_sq'] = self.data[x1_name] ** 2
|
| 42 |
+
self.data[f'{x2_name}_sq'] = self.data[x2_name] ** 2
|
| 43 |
+
self.data[f'{x3_name}_sq'] = self.data[x3_name] ** 2
|
| 44 |
+
# Crear columnas de interacción
|
| 45 |
+
self.data[f'{x1_name}_{x2_name}'] = self.data[x1_name] * self.data[x2_name]
|
| 46 |
+
self.data[f'{x1_name}_{x3_name}'] = self.data[x1_name] * self.data[x3_name]
|
| 47 |
+
self.data[f'{x2_name}_{x3_name}'] = self.data[x2_name] * self.data[x3_name]
|
| 48 |
+
|
| 49 |
self.model = None
|
| 50 |
self.model_simplified = None
|
| 51 |
self.optimized_results = None
|
|
|
|
| 103 |
Ajusta el modelo de segundo orden completo a los datos.
|
| 104 |
"""
|
| 105 |
formula = f'{self.y_name} ~ {self.x1_name} + {self.x2_name} + {self.x3_name} + ' \
|
| 106 |
+
f'{self.x1_name}_sq + {self.x2_name}_sq + {self.x3_name}_sq + ' \
|
| 107 |
+
f'{self.x1_name}_{self.x2_name} + {self.x1_name}_{self.x3_name} + {self.x2_name}_{self.x3_name}'
|
| 108 |
self.model = smf.ols(formula, data=self.data).fit()
|
| 109 |
print("Modelo Completo:")
|
| 110 |
print(self.model.summary())
|
|
|
|
| 115 |
Ajusta el modelo de segundo orden a los datos, eliminando términos no significativos.
|
| 116 |
"""
|
| 117 |
formula = f'{self.y_name} ~ {self.x1_name} + {self.x2_name} + ' \
|
| 118 |
+
f'{self.x1_name}_sq + {self.x2_name}_sq + {self.x3_name}_sq'
|
| 119 |
self.model_simplified = smf.ols(formula, data=self.data).fit()
|
| 120 |
print("\nModelo Simplificado:")
|
| 121 |
print(self.model_simplified.summary())
|
|
|
|
| 126 |
Ajusta un modelo Ridge Regression.
|
| 127 |
"""
|
| 128 |
# Preparar datos
|
| 129 |
+
# Usar los nombres consistentes de las columnas cuadráticas
|
| 130 |
X = self.data[[self.x1_name, self.x2_name, self.x3_name,
|
| 131 |
+
f'{self.x1_name}_sq', f'{self.x2_name}_sq', f'{self.x3_name}_sq']]
|
| 132 |
y = self.data[self.y_name]
|
| 133 |
|
| 134 |
# Ajustar modelo Ridge
|
|
|
|
| 151 |
Ajusta un modelo LASSO Regression.
|
| 152 |
"""
|
| 153 |
# Preparar datos
|
| 154 |
+
# Usar los nombres consistentes de las columnas cuadráticas
|
| 155 |
X = self.data[[self.x1_name, self.x2_name, self.x3_name,
|
| 156 |
+
f'{self.x1_name}_sq', f'{self.x2_name}_sq', f'{self.x3_name}_sq']]
|
| 157 |
y = self.data[self.y_name]
|
| 158 |
|
| 159 |
# Ajustar modelo LASSO
|
|
|
|
| 176 |
Ajusta un modelo ElasticNet Regression.
|
| 177 |
"""
|
| 178 |
# Preparar datos
|
| 179 |
+
# Usar los nombres consistentes de las columnas cuadráticas
|
| 180 |
X = self.data[[self.x1_name, self.x2_name, self.x3_name,
|
| 181 |
+
f'{self.x1_name}_sq', f'{self.x2_name}_sq', f'{self.x3_name}_sq']]
|
| 182 |
y = self.data[self.y_name]
|
| 183 |
|
| 184 |
# Ajustar modelo ElasticNet
|
|
|
|
| 283 |
return -self.model_simplified.predict(pd.DataFrame({
|
| 284 |
self.x1_name: [x[0]],
|
| 285 |
self.x2_name: [x[1]],
|
| 286 |
+
self.x3_name: [x[2]],
|
| 287 |
+
f'{self.x1_name}_sq': [x[0]**2],
|
| 288 |
+
f'{self.x2_name}_sq': [x[1]**2],
|
| 289 |
+
f'{self.x3_name}_sq': [x[2]**2]
|
| 290 |
})).values[0]
|
| 291 |
|
| 292 |
bounds = [(-1, 1), (-1, 1), (-1, 1)]
|
|
|
|
| 331 |
return -self.model_simplified.predict(pd.DataFrame({
|
| 332 |
self.x1_name: [x[0]],
|
| 333 |
self.x2_name: [x[1]],
|
| 334 |
+
self.x3_name: [x[2]],
|
| 335 |
+
f'{self.x1_name}_sq': [x[0]**2],
|
| 336 |
+
f'{self.x2_name}_sq': [x[1]**2],
|
| 337 |
+
f'{self.x3_name}_sq': [x[2]**2]
|
| 338 |
})).values[0]
|
| 339 |
|
| 340 |
# Ejecutar optimización bayesiana
|
|
|
|
| 378 |
return -self.model_simplified.predict(pd.DataFrame({
|
| 379 |
self.x1_name: [x[0]],
|
| 380 |
self.x2_name: [x[1]],
|
| 381 |
+
self.x3_name: [x[2]],
|
| 382 |
+
f'{self.x1_name}_sq': [x[0]**2],
|
| 383 |
+
f'{self.x2_name}_sq': [x[1]**2],
|
| 384 |
+
f'{self.x3_name}_sq': [x[2]**2]
|
| 385 |
})).values[0]
|
| 386 |
|
| 387 |
# Parámetros PSO
|
|
|
|
| 511 |
|
| 512 |
# Variables predictoras
|
| 513 |
X = self.data[[self.x1_name, self.x2_name, self.x3_name,
|
| 514 |
+
f'{self.x1_name}_sq', f'{self.x2_name}_sq', f'{self.x3_name}_sq']]
|
| 515 |
|
| 516 |
# Calcular VIF para cada variable
|
| 517 |
vif_data = pd.DataFrame()
|
|
|
|
| 647 |
|
| 648 |
# Preparar datos
|
| 649 |
X = self.data[[self.x1_name, self.x2_name, self.x3_name,
|
| 650 |
+
f'{self.x1_name}_sq', f'{self.x2_name}_sq', f'{self.x3_name}_sq']]
|
| 651 |
y = self.data[self.y_name]
|
| 652 |
|
| 653 |
# Calcular R² de validación cruzada
|
|
|
|
| 702 |
varying_variables[1]: y_grid_coded.flatten(),
|
| 703 |
})
|
| 704 |
prediction_data[fixed_variable] = self.natural_to_coded(fixed_level, fixed_variable)
|
| 705 |
+
# Agregar términos cuadráticos
|
| 706 |
+
prediction_data[f'{varying_variables[0]}_sq'] = prediction_data[varying_variables[0]] ** 2
|
| 707 |
+
prediction_data[f'{varying_variables[1]}_sq'] = prediction_data[varying_variables[1]] ** 2
|
| 708 |
+
prediction_data[f'{fixed_variable}_sq'] = prediction_data[fixed_variable] ** 2
|
| 709 |
|
| 710 |
# Calcular los valores predichos
|
| 711 |
z_pred = self.model_simplified.predict(prediction_data).values.reshape(x_grid_coded.shape)
|
|
|
|
| 799 |
varying_variables[1]: y_grid_coded.flatten(),
|
| 800 |
})
|
| 801 |
prediction_data[fixed_variable] = self.natural_to_coded(fixed_level, fixed_variable)
|
| 802 |
+
# Agregar términos cuadráticos
|
| 803 |
+
prediction_data[f'{varying_variables[0]}_sq'] = prediction_data[varying_variables[0]] ** 2
|
| 804 |
+
prediction_data[f'{varying_variables[1]}_sq'] = prediction_data[varying_variables[1]] ** 2
|
| 805 |
+
prediction_data[f'{fixed_variable}_sq'] = prediction_data[fixed_variable] ** 2
|
| 806 |
|
| 807 |
# Calcular los valores predichos
|
| 808 |
z_pred = self.model_simplified.predict(prediction_data).values.reshape(x_grid_coded.shape)
|
|
|
|
| 990 |
equation += f" + {coef:.3f}*{self.x2_name}"
|
| 991 |
elif term == f'{self.x3_name}':
|
| 992 |
equation += f" + {coef:.3f}*{self.x3_name}"
|
| 993 |
+
elif term == f'{self.x1_name}_sq':
|
| 994 |
equation += f" + {coef:.3f}*{self.x1_name}^2"
|
| 995 |
+
elif term == f'{self.x2_name}_sq':
|
| 996 |
equation += f" + {coef:.3f}*{self.x2_name}^2"
|
| 997 |
+
elif term == f'{self.x3_name}_sq':
|
| 998 |
equation += f" + {coef:.3f}*{self.x3_name}^2"
|
| 999 |
|
| 1000 |
return equation
|
|
|
|
| 1078 |
for index, row in anova_table.iterrows():
|
| 1079 |
if index != 'Residual':
|
| 1080 |
factor_name = index
|
| 1081 |
+
if factor_name == f'{self.x1_name}_sq':
|
| 1082 |
factor_name = f'{self.x1_name}^2'
|
| 1083 |
+
elif factor_name == f'{self.x2_name}_sq':
|
| 1084 |
factor_name = f'{self.x2_name}^2'
|
| 1085 |
+
elif factor_name == f'{self.x3_name}_sq':
|
| 1086 |
factor_name = f'{self.x3_name}^2'
|
| 1087 |
ss_factor = row['sum_sq']
|
| 1088 |
df_factor = row['df']
|
|
|
|
| 1126 |
# --- ANOVA detallada ---
|
| 1127 |
# 1. Ajustar un modelo solo con los términos de primer orden y cuadráticos
|
| 1128 |
formula_reduced = f'{self.y_name} ~ {self.x1_name} + {self.x2_name} + {self.x3_name} + ' \
|
| 1129 |
+
f'{self.x1_name}_sq + {self.x2_name}_sq + {self.x3_name}_sq'
|
| 1130 |
model_reduced = smf.ols(formula_reduced, data=self.data).fit()
|
| 1131 |
|
| 1132 |
# 2. ANOVA del modelo reducido
|
|
|
|
| 1184 |
})
|
| 1185 |
|
| 1186 |
# Calcular la suma de cuadrados y estadísticos F para la curvatura
|
| 1187 |
+
ss_curvature = anova_reduced['sum_sq'][f'{self.x1_name}_sq'] + \
|
| 1188 |
+
anova_reduced['sum_sq'][f'{self.x2_name}_sq'] + \
|
| 1189 |
+
anova_reduced['sum_sq'][f'{self.x3_name}_sq']
|
| 1190 |
df_curvature = 3
|
| 1191 |
ms_curvature = ss_curvature / df_curvature
|
| 1192 |
f_curvature = ms_curvature / ms_residual
|