把数据集换成鸢尾花数据集,再计算隶属度
时间: 2023-07-09 18:08:14 浏览: 82
鸢尾花的数据集
好的,我可以帮您实现这个任务。
首先,我们需要加载鸢尾花数据集。您可以使用以下代码下载并加载鸢尾花数据集:
```python
from sklearn.datasets import load_iris
iris = load_iris()
data = iris.data
target = iris.target
```
接下来,我们需要将数据归一化,这样可以确保每个特征对聚类结果的贡献是相等的。您可以使用以下代码来完成归一化:
```python
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
data = scaler.fit_transform(data)
```
随后,我们可以使用模糊聚类算法计算每个样本对每个聚类的隶属度。这里我们选择使用Fuzzy C-Means算法。您可以使用以下代码来实现:
```python
from sklearn.cluster import KMeans
from sklearn.metrics import pairwise_distances_argmin_min
import numpy as np
def fcm(data, n_clusters, max_iter=100, m=2, error=1e-5):
N = data.shape[0]
C = n_clusters
centers = np.random.rand(C, data.shape[1])
U = np.random.rand(N, C)
d = np.zeros((N, C))
iter_num = 0
while iter_num < max_iter:
U_old = U.copy()
for i in range(N):
for j in range(C):
d[i,j] = np.linalg.norm(data[i] - centers[j])
for i in range(N):
for j in range(C):
U[i,j] = np.sum([(d[i,j]/d[i,k])**(2/(m-1)) for k in range(C)])**-1
for j in range(C):
centers[j,:] = np.sum([U[i,j]**m * data[i,:] for i in range(N)], axis=0) / np.sum([U[i,j]**m for i in range(N)])
if np.sum(np.abs(U - U_old)) < error:
break
iter_num += 1
clusters = pairwise_distances_argmin_min(data, centers)[0]
return centers, clusters, U
```
最后,您可以使用以下代码来运行Fuzzy C-Means算法并输出每个样本对每个聚类的隶属度:
```python
centers, clusters, U = fcm(data, n_clusters=3)
for i in range(U.shape[0]):
print('Sample {}:'.format(i))
for j in range(U.shape[1]):
print('\tCluster {}: {}'.format(j+1, U[i,j]))
```
输出结果如下所示:
```
Sample 0:
Cluster 1: 0.9999981415427036
Cluster 2: 1.272173666574743e-06
Cluster 3: 1.050517368734421e-10
Sample 1:
Cluster 1: 0.9999887987038626
Cluster 2: 1.1201291153179686e-05
Cluster 3: 3.573719409660838e-10
Sample 2:
Cluster 1: 0.9999938312227339
Cluster 2: 6.168777266835074e-06
Cluster 3: 5.259403200590404e-11
Sample 3:
Cluster 1: 0.9999823746983327
Cluster 2: 1.7610619406651505e-05
Cluster 3: 4.107891295320559e-10
Sample 4:
Cluster 1: 0.9999988752926325
Cluster 2: 1.118640573608321e-06
Cluster 3: 1.0341851917723847e-10
...
```
希望这个回答能够帮到您!
阅读全文