Error: K-Mean Clustering Algorithm data plots is not visible in Python
Error: K-Mean Clustering Algorithm data plots is not visible in Python
您好,我想实现 K-Means 聚类算法。
为此,我从 sample.csv 文件中获取数据并对其应用 K-Means 聚类。这是我的源代码
## K-Means.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()
我在终端中运行上面的代码后,输出如下:
上图没有显示任何聚类数据图,所以我想直观地查看我的聚类数据图。我怎样才能解决这个问题。
我是这个领域的新手。
谢谢
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 个点生成的散点是:
对于使用 kmeans 聚类模型的代码,您正在为模型中的每个标签绘图,这将生成 10 个图。只需更改限制即可发挥作用。
您好,我想实现 K-Means 聚类算法。
为此,我从 sample.csv 文件中获取数据并对其应用 K-Means 聚类。这是我的源代码
## K-Means.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()
我在终端中运行上面的代码后,输出如下:
上图没有显示任何聚类数据图,所以我想直观地查看我的聚类数据图。我怎样才能解决这个问题。 我是这个领域的新手。 谢谢
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 个点生成的散点是:
对于使用 kmeans 聚类模型的代码,您正在为模型中的每个标签绘图,这将生成 10 个图。只需更改限制即可发挥作用。