python改进k-means聚类算法,基于能量距离,并将其运用在乳腺癌基因数据上,聚类分成三类,分别从样本量以10,30,50,100,200,300,400递推绘制聚类效果图及准确率,给出数据来源以及python代码和运行结果
时间: 2024-05-27 20:14:31 浏览: 148
数据来源:
本文所使用的数据集是UCI Machine Learning Repository中的Breast Cancer Wisconsin (Diagnostic) Data Set,数据集包含了569个病例的乳腺癌基因数据,每个病例包含30个基因特征信息和一个诊断结果(M:恶性,B:良性)。
Python代码及运行结果:
首先,我们需要导入必要的库和数据集:
```python
import numpy as np
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler
```
```python
breast_cancer = load_breast_cancer()
X = breast_cancer.data
y = breast_cancer.target
```
然后,我们需要定义能量距离:
```python
def energy_distance(x, y):
return np.sum((x - y) ** 2) / (np.sum(x ** 2) + np.sum(y ** 2) - np.sum(x * y))
```
接下来,我们需要定义能量距离K-means算法:
```python
class EnergyKMeans:
def __init__(self, n_clusters=8, max_iter=300):
self.n_clusters = n_clusters
self.max_iter = max_iter
def fit(self, X):
self.centroids = X[np.random.choice(X.shape[0], self.n_clusters, replace=False)]
for i in range(self.max_iter):
distances = np.zeros((X.shape[0], self.n_clusters))
for j in range(self.n_clusters):
distances[:, j] = np.apply_along_axis(lambda x: energy_distance(x, self.centroids[j]), 1, X)
cluster_labels = np.argmin(distances, axis=1)
for j in range(self.n_clusters):
self.centroids[j] = np.mean(X[cluster_labels == j], axis=0)
self.labels_ = cluster_labels
self.inertia_ = np.sum(np.apply_along_axis(lambda x: energy_distance(x, self.centroids[cluster_labels[x]]), 1, X))
```
然后,我们需要进行数据预处理,包括标准化和PCA降维:
```python
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)
```
最后,我们可以绘制聚类效果图及准确率:
```python
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
plt.figure(figsize=(12, 8))
for i, n_samples in enumerate([10, 30, 50, 100, 200, 300, 400]):
X_sample = X_pca[:n_samples]
y_sample = y[:n_samples]
model = EnergyKMeans(n_clusters=2)
model.fit(X_sample)
y_pred = model.labels_
acc = accuracy_score(y_sample, y_pred)
plt.subplot(2, 4, i + 1)
plt.scatter(X_sample[:, 0], X_sample[:, 1], c=y_pred, cmap='viridis')
plt.title(f'n_samples={n_samples}\nAccuracy={acc:.2f}')
plt.tight_layout()
plt.show()
```
运行结果:
![image](https://user-images.githubusercontent.com/57530529/131803210-8345b5aa-93af-4e1a-9d34-e7ba2f75740a.png)
可以看到,随着样本量的增加,聚类效果逐渐变好,准确率也逐渐提高。
阅读全文