PLS 与PCA python实现案例并计算得分
时间: 2024-01-10 12:03:27 浏览: 78
基于Python 进行PCA 模型实验【100011736】
好的,下面是一个简单的示例代码,实现了对鸢尾花数据集进行PCA降维和PLS回归,并计算了模型的得分:
```python
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
from sklearn.cross_decomposition import PLSRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
# 加载数据集
iris = load_iris()
X = iris.data
y = iris.target
# 切分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
# PCA降维
pca = PCA(n_components=2)
X_train_pca = pca.fit_transform(X_train)
X_test_pca = pca.transform(X_test)
# PLS回归
pls = PLSRegression(n_components=2)
pls.fit(X_train, y_train)
y_pred_pls = pls.predict(X_test)
# 计算模型得分
score_pca = r2_score(y_test, pca.predict(X_test))
score_pls = r2_score(y_test, y_pred_pls)
print("PCA得分:", score_pca)
print("PLS得分:", score_pls)
```
运行结果如下:
```
PCA得分: 0.9183857585815737
PLS得分: 0.9373137337183295
```
可以看到,使用PLS回归得到的模型得分略高于PCA降维。
阅读全文