"""实验 A：相同数据划分下比较基线、直线、森林，并观察 PCA。"""
import json
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, GridSearchCV, KFold
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.dummy import DummyRegressor
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error
from sklearn.decomposition import PCA
from common import materials, OUT

X, y = materials()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.25, random_state=42)
print('训练／测试形状:', X_train.shape, X_test.shape)
cv = KFold(n_splits=5, shuffle=True, random_state=42)
forest = GridSearchCV(RandomForestRegressor(n_estimators=100, random_state=42),
                      {'max_depth': [3, 6, None]}, cv=cv, scoring='neg_mean_absolute_error')
models = {'mean': DummyRegressor(), 'linear': make_pipeline(StandardScaler(), LinearRegression()), 'forest': forest}
scores = {}
fig, axes = plt.subplots(1, 3, figsize=(12, 4), constrained_layout=True)
for ax, (name, model) in zip(axes, models.items()):
    model.fit(X_train, y_train)
    prediction = model.predict(X_test)
    scores[name] = float(mean_absolute_error(y_test, prediction))
    ax.scatter(y_test, prediction, s=16)
    lo, hi = min(y_test), max(y_test)
    ax.plot([lo, hi], [lo, hi], '--', color='gray')
    ax.set(xlabel='Measured teaching response', ylabel='Prediction', title=f'{name}: MAE={scores[name]:.3f}')
fig.savefig(OUT / 'a_predictions.png', dpi=150)
plt.close(fig)
# PCA 只在训练集拟合；颜色只是帮助观察，与 PCA 拟合无关。
projection = make_pipeline(StandardScaler(), PCA(n_components=2))
z = projection.fit_transform(X_train)
ratio = projection[-1].explained_variance_ratio_
fig, ax = plt.subplots(figsize=(6, 4), constrained_layout=True)
points = ax.scatter(z[:, 0], z[:, 1], c=y_train, s=18)
fig.colorbar(points, ax=ax, label='Teaching response')
ax.set(xlabel='PC1', ylabel='PC2', title=f'PCA: variance retained={sum(ratio):.1%}')
fig.savefig(OUT / 'a_pca.png', dpi=150)
plt.close(fig)
result = {'test_mae': scores, 'forest_best_parameters': forest.best_params_, 'pca_variance_ratio': ratio.tolist()}
(OUT / 'a_results.json').write_text(json.dumps(result, indent=2), encoding='utf-8')
print(json.dumps(result, indent=2))
