PCA和PLS特征选择案例
时间: 2023-12-16 10:15:57 浏览: 117
好的,下面是一个示例代码,实现了对鸢尾花数据集进行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
from sklearn.feature_selection import SelectKBest, f_regression
# 加载数据集
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)
# 特征选择
selector = SelectKBest(f_regression, k=2)
X_train_fs = selector.fit_transform(X_train, y_train)
X_test_fs = selector.transform(X_test)
# PLS回归
pls = PLSRegression(n_components=2)
pls.fit(X_train_fs, y_train)
y_pred_pls = pls.predict(X_test_fs)
# 计算模型得分
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
```
在这个示例中,我们使用了 `SelectKBest` 和 `f_regression` 进行了特征选择,只选择了两个最相关的特征进行建模。可以看到,经过特征选择后的PLS模型得分略高于PCA降维的模型得分。
阅读全文