Code Diamond
Code Diamond
import numpy as np
from sklearn.model_selection import train_test_split, GridSearchCV,
RandomizedSearchCV
from sklearn.preprocessing import OneHotEncoder, MinMaxScaler
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.neural_network import MLPRegressor
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.metrics import mean_squared_error,
mean_absolute_percentage_error
from sklearn.cluster import KMeans
from deap import base, creator, tools, algorithms
import random, warnings
warnings.filterwarnings("ignore")
Daimonds
# === 讀取資料 ===
df = pd.read_excel("daimonds.xlsx")
categorical_cols = ['color', 'clarity']
numerical_cols = ['carat', 'y']
target_col = 'price'
X_train_fuzzy = generate_gaussian_mf_features(X_train_scaled)
X_val_fuzzy = generate_gaussian_mf_features(X_val_scaled)
y_pred_train_scaled = model.predict(X_train_fuzzy).reshape(-1, 1)
y_pred_val_scaled = model.predict(X_val_fuzzy).reshape(-1, 1)
y_pred_train = scaler_y.inverse_transform(y_pred_train_scaled)
y_pred_val = scaler_y.inverse_transform(y_pred_val_scaled)
y_train_orig = scaler_y.inverse_transform(y_train_scaled)
y_val_orig = scaler_y.inverse_transform(y_val_scaled)
results = []
📊 GA - Train 評估結果:
- MAPE : 25112.7541
- MSE : 18622354043964312.00
- RMSE : 136463746.26
- NRMSE: 5561.8192
📊 GA - Validation 評估結果:
- MAPE : 25141.8347
- MSE : 18524009626157444.00
- RMSE : 136102937.61
- NRMSE: 7275.1719
📊 所有方法比較結果:
Method MAPE MSE RMSE \
0 Baseline - Train 0.362919 9.844944e+05 9.922169e+02
1 Baseline - Validation 0.361365 9.835521e+05 9.917420e+02
2 GA - Train 25112.754142 1.862235e+16 1.364637e+08
3 GA - Validation 25141.834680 1.852401e+16 1.361029e+08
4 PSO - Train 25058.935955 1.848362e+16 1.359545e+08
5 PSO - Validation 25056.638548 1.841209e+16 1.356911e+08
NRMSE
0 0.040440
1 0.053012
2 5561.819207
3 7275.171899
4 5541.062947
5 7253.160388