from__future__importannotationsimportmatplotlib.pyplotaspltimportnumpyasnpfromsklearn.cross_decompositionimportPLSRegressionfromsklearn.datasetsimportload_linnerudfromsklearn.model_selectionimportKFold,cross_val_scorefromsklearn.pipelineimportPipelinefromsklearn.preprocessingimportStandardScalerdefevaluate_pls_latent_factors(cv_splits:int=5,xlabel:str="Number of latent factors",ylabel:str="CV MSE (lower is better)",label_best:str="best={k}",title:str|None=None,)->dict[str,object]:"""Cross-validate PLS regression for different latent factor counts.
Args:
cv_splits: Number of folds for cross-validation.
xlabel: Label for the number-of-factors axis.
ylabel: Label for the cross-validation error axis.
label_best: Format string for the best-factor annotation.
title: Optional plot title.
Returns:
Dictionary with the selected factor count, CV score, and loadings.
"""data=load_linnerud()X=data["data"]y=data["target"][:,0]max_components=min(X.shape[1],6)components=np.arange(1,max_components+1)cv=KFold(n_splits=cv_splits,shuffle=True,random_state=0)scores=[]pipelines=[]forkincomponents:model=Pipeline([("scale",StandardScaler()),("pls",PLSRegression(n_components=int(k))),])cv_score=cross_val_score(model,X,y,cv=cv,scoring="neg_mean_squared_error",).mean()scores.append(cv_score)pipelines.append(model)scores_arr=np.array(scores)best_idx=int(np.argmax(scores_arr))best_k=int(components[best_idx])best_mse=float(-scores_arr[best_idx])best_model=pipelines[best_idx].fit(X,y)x_loadings=best_model["pls"].x_loadings_y_loadings=best_model["pls"].y_loadings_fig,ax=plt.subplots(figsize=(8,4))ax.plot(components,-scores_arr,marker="o")ax.axvline(best_k,color="red",linestyle="--",label=label_best.format(k=best_k))ax.set_xlabel(xlabel)ax.set_ylabel(ylabel)iftitle:ax.set_title(title)ax.legend()fig.tight_layout()plt.show()return{"best_k":best_k,"best_mse":best_mse,"x_loadings":x_loadings,"y_loadings":y_loadings,}metrics=evaluate_pls_latent_factors(xlabel="潜在因子の数",ylabel="CV MSE (小さいほど良い)",label_best="最適k={k}",title="PLS における潜在因子数の選択",)print(f"最適な潜在因子数: {metrics['best_k']}")print(f"最適CV MSE: {metrics['best_mse']:.3f}")print("Xのローディング:", metrics['x_loadings'])print("Yのローディング:", metrics['y_loadings'])
Wold, H. (1975). Soft Modelling by Latent Variables: The Non-Linear Iterative Partial Least Squares (NIPALS) Approach. In Perspectives in Probability and Statistics. Academic Press.
Geladi, P., & Kowalski, B. R. (1986). Partial Least-Squares Regression: A Tutorial. Analytica Chimica Acta, 185, 1–17.