错误:KMean聚类算法数据图在Python中不可见

2024-04-27 03:13:16 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我想实现K-均值聚类算法。在

为此,我从示例.csv在文件上应用K-K方法。这是我的源代码

克-平均值.py

# clustering dataset
import pandas

from sklearn.cluster import KMeans
from sklearn import metrics
import numpy as np
import matplotlib.pyplot as plt

variables = pandas.read_csv("/Users/srikanth/Desktop/sample1.csv")
print(variables)
x1 = variables[['X']]
x2 = variables[['Y']]
print(x1)
print(x2)

plt.plot()
plt.xlim([0, 10])
plt.ylim([0, 10])
plt.title('Dataset')
plt.xlabel('X - Values')
plt.ylabel('Y - Values')
plt.scatter(x1, x2)
plt.show()

# create new plot and data
plt.plot()
X = np.array(list(zip(x1, x2))).reshape(len(x1), 2)
colors = ['b', 'g', 'r']
markers = ['o', 'v', 's']

# KMeans algorithm
K = 3
kmeans_model = KMeans(n_clusters=K).fit(X)

plt.plot()
for i, l in enumerate(kmeans_model.labels_):
    plt.plot(x1[i], x2[i], color=colors[l], marker=markers[l],ls='None')
    plt.xlim([0, 10])
    plt.ylim([0, 10])
    plt.show()

在终端中运行上述代码后,输出如下:

Console Output

plots visualization

我不想看到任何聚集的数据,所以我不想看到上面的数据。我该怎么解决这个问题呢。 我对这个地区不熟悉。 谢谢你


Tags: csvfromimportpandasplotasnpplt
1条回答
网友
1楼 · 发布于 2024-04-27 03:13:16
from sklearn.cluster import KMeans
from sklearn import metrics
import numpy as np
import matplotlib.pyplot as plt

variables = pandas.read_csv("/Users/srikanth/Desktop/sample1.csv")
print(variables)
x1 = variables[['X']]
x2 = variables[['Y']]
plt.plot()
plt.xlim([150, 190])
plt.ylim([40, 90])
plt.title('Dataset')
plt.xlabel('X - Values')
plt.ylabel('Y - Values')
plt.scatter(x1, x2)
plt.show()

它对10个点产生的散射是:

enter image description here

对于使用kmeans集群模型的代码,您正在为模型中的每个标签绘制,这将生成10个图。只要改变限制就行了。在

相关问题 更多 >