使用PCA、Isomap对乳腺癌数据集进行降维,使用KNN对数据集进行分类,并显示所有结果
时间: 2024-02-11 20:04:22 浏览: 86
好的,我可以为您展示如何使用PCA、Isomap进行降维,并使用KNN对数据集进行分类,并显示所有结果。
首先,我们加载乳腺癌数据集,并对数据进行预处理:
```python
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.manifold import Isomap
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import numpy as np
data = load_breast_cancer()
X, y = data.data, data.target
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
```
接下来,我们使用PCA和Isomap对数据进行降维,并使用KNN进行分类。
使用PCA进行降维和分类:
```python
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)
X_train, X_test, y_train, y_test = train_test_split(X_pca, y, test_size=0.2, random_state=42)
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
accuracy_pca = np.mean(y_pred == y_test)
print("PCA + KNN accuracy: {:.2f}%".format(accuracy_pca * 100))
```
使用Isomap进行降维和分类:
```python
isomap = Isomap(n_components=2)
X_isomap = isomap.fit_transform(X_scaled)
X_train, X_test, y_train, y_test = train_test_split(X_isomap, y, test_size=0.2, random_state=42)
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
accuracy_isomap = np.mean(y_pred == y_test)
print("Isomap + KNN accuracy: {:.2f}%".format(accuracy_isomap * 100))
```
最后,我们将所有结果进行显示:
```python
print("PCA + KNN accuracy: {:.2f}%".format(accuracy_pca * 100))
print("Isomap + KNN accuracy: {:.2f}%".format(accuracy_isomap * 100))
```
完整代码如下:
```python
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.manifold import Isomap
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import numpy as np
data = load_breast_cancer()
X, y = data.data, data.target
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)
X_train, X_test, y_train, y_test = train_test_split(X_pca, y, test_size=0.2, random_state=42)
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
accuracy_pca = np.mean(y_pred == y_test)
print("PCA + KNN accuracy: {:.2f}%".format(accuracy_pca * 100))
isomap = Isomap(n_components=2)
X_isomap = isomap.fit_transform(X_scaled)
X_train, X_test, y_train, y_test = train_test_split(X_isomap, y, test_size=0.2, random_state=42)
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
accuracy_isomap = np.mean(y_pred == y_test)
print("Isomap + KNN accuracy: {:.2f}%".format(accuracy_isomap * 100))
print("PCA + KNN accuracy: {:.2f}%".format(accuracy_pca * 100))
print("Isomap + KNN accuracy: {:.2f}%".format(accuracy_isomap * 100))
```
阅读全文