KMeans Clustering - 在 SVM 中使用创建的集群
KMeans Clustering - use created clusters in SVM
我正在使用 KMeans 聚类将数据分成 2 个集群,如下所示。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import cluster
##################################################
# load data
clm = np.genfromtxt('data.csv',delimiter=',', skiprows=1)
X = clm[:,(1,12)].astype(float)
# define kmeans learner, then fit with data
kmeans = cluster.KMeans(n_clusters=2)
kmeans.fit(X)
# define centroids
centroids = kmeans.cluster_centers_
labels = kmeans.labels_
#print(centroids)
#print(labels)
#set color
colors = ["g.", "r."]
for i in range(len(X)):
print("coordinate:",X[i], "label:", labels[i])
plt.plot(X[i][0], X[i][1], colors[labels[i]], markersize= 10)
# plot
plt.scatter(centroids[:,0], centroids[:,1], marker = "x", s=150, linewidth = 5, zorder=10)
plt.xlabel('Read proportion')
plt.ylabel('Memory_used_Read')
plt.xlim(0,100)
plt.ylim(ymin=0)
plt.show()
在 KMeans 聚类中,集合 S=(x,y) 没有标记数据,而在 SVM 中,集合 S 输入 x 和目标数据 y。我要做的是在 SVM 中单独使用创建的集群。我不确定如何为每个集群获取 X,y。如果您有任何建议,请告诉我。
谢谢。
您不是 100% 清楚您要做什么。如果你想使用集群分配作为 class 标签,你可以从 y = kmeans.predict(X)
获得 y。特征 X 是相同的。像这样在 SVM 中使用它们:
y = kmeans.predict(X)
svm = SVM()
svm.fit(X, y)
我正在使用 KMeans 聚类将数据分成 2 个集群,如下所示。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import cluster
##################################################
# load data
clm = np.genfromtxt('data.csv',delimiter=',', skiprows=1)
X = clm[:,(1,12)].astype(float)
# define kmeans learner, then fit with data
kmeans = cluster.KMeans(n_clusters=2)
kmeans.fit(X)
# define centroids
centroids = kmeans.cluster_centers_
labels = kmeans.labels_
#print(centroids)
#print(labels)
#set color
colors = ["g.", "r."]
for i in range(len(X)):
print("coordinate:",X[i], "label:", labels[i])
plt.plot(X[i][0], X[i][1], colors[labels[i]], markersize= 10)
# plot
plt.scatter(centroids[:,0], centroids[:,1], marker = "x", s=150, linewidth = 5, zorder=10)
plt.xlabel('Read proportion')
plt.ylabel('Memory_used_Read')
plt.xlim(0,100)
plt.ylim(ymin=0)
plt.show()
在 KMeans 聚类中,集合 S=(x,y) 没有标记数据,而在 SVM 中,集合 S 输入 x 和目标数据 y。我要做的是在 SVM 中单独使用创建的集群。我不确定如何为每个集群获取 X,y。如果您有任何建议,请告诉我。
谢谢。
您不是 100% 清楚您要做什么。如果你想使用集群分配作为 class 标签,你可以从 y = kmeans.predict(X)
获得 y。特征 X 是相同的。像这样在 SVM 中使用它们:
y = kmeans.predict(X)
svm = SVM()
svm.fit(X, y)