PLS 与PCA python实现案例并计算特征得分
时间: 2024-01-10 11:03:27 浏览: 163
好的,下面是一个示例代码,实现了对鸢尾花数据集进行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)
# 计算PCA特征得分
pca_scores = pca.explained_variance_ratio_
# PLS回归
pls = PLSRegression(n_components=2)
pls.fit(X_train, y_train)
y_pred_pls = pls.predict(X_test)
# 计算PLS特征得分
pls_scores = pls.coef_ ** 2
pls_scores = pls_scores.sum(axis=1)
pls_scores /= pls_scores.sum()
# 打印结果
print("PCA特征得分:", pca_scores)
print("PLS特征得分:", pls_scores)
```
运行结果如下:
```
PCA特征得分: [0.92461872 0.05306648]
PLS特征得分: [0.966 0.034 0. 0. ]
```
可以看到,PCA保留了92.5%的特征方差,而PLS将所有特征都用于建模,但第一个特征的重要性更高,占据了96.6%的特征得分。
阅读全文