使用SciKit Learn将K-Means输出到CSV-提供群集名称

时间:2019-02-03 15:52:15

标签: python python-3.x scikit-learn

我有下面的scikit学习脚本,该脚本可以在每个集群中输出一个漂亮的图表(如下)。

我有几个问题: -如何使用群集名称或ID将其导出到CSV? -如何命名群集? -如何确保群集始终具有相同的名称?例如,我想将右上角的细分受众群称为“高消费群体”,以便在哪里总是正确的?

谢谢!

enter image description here

#import the required libraries
# - matplotlib is a charting library
# - Seaborn builds on top of Matplotlib and introduces additional plot types. It also makes your traditional Matplotlib plots look a bit prettier.
# - Numpy is numerical Python
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn.datasets.samples_generator import make_blobs
from sklearn.cluster import KMeans
#Generate sample data, with distinct clusters for testing
#n_samples = the number of datapoints, equally split across each clusters
#centers = The number of centers to generate (number of clusters) - a center is the arithmetic mean of all the points belonging to the cluster.
#cluster_std = the standard deviation of the clusters - a quantity expressing by how much the members of a group differ from the mean value for the group (how tight is the cluster going to be)
#random_state = controls the random number generator being used.  If you don't mention the random_state in the code, then whenever you execute your code a new random value is generated and the train and test datasets would have different values each time. However, if you use a particular value for random_state(random_state = 1 or any other value) everytime the result will be same,i.e, same values in train and test datasets.
#make_blobs generates "isotropic Gaussian blobs" - X is a numpy array with two columns which contain the (x, y) Gaussian coordinates of these points, whereas y contains the list of categories for each.
#X, y = simply means that the output of make_blobs() has two elements, that are assigned to X and y.
X, y = make_blobs(n_samples=300, centers=4,
                       cluster_std=0.50, random_state=0)
#X now looks like this - column zero becomes the X axis, column1 becomes the Y axis
array([[ 1.85219907,  1.10411295],
       [-1.27582283,  7.76448722],
       [ 1.0060939 ,  4.43642592],
       [-1.20998253,  7.83203579],
       [ 1.92461484,  1.06347673],
       [ 2.28565919,  0.79166208],
       [-1.57379043,  2.69773813],
       [ 1.04917913,  4.31668562],
       [-1.07436851,  7.93489945],
       [-1.15872975,  7.97295642]
#The below statement, will enable us to visualise matplotlib charts, even in ipython
#Using matplotlib backend: MacOSX
#Populating the interactive namespace from numpy and matplotlib
%pylab
#plot the chart
#s = the sizer of the points.
#X[:, 0] is the numpy coordinates way of selecting every row entry for column 0 - i.e. a single column from the numpy array.
#X[:, 1] is the numpy coordinates way of selecting every row entry for column 1 - i.e. a single column from the numpy array.
plt.scatter(X[:, 0], X[:, 1], s=50);
#now, I am definining that I want to find 4 clusters within the data. The general rule I follow is, I will have 7 times less clusters than datapoints.
kmeans = KMeans(n_clusters=4)
#build the model, based on X with the number of clusters defined above
kmeans.fit(X)
#now we're going to find clusters in the randomly generated dataset
predict = kmeans.predict(X)
#now we can plot the prediction
#c = colour, which is based on the predict variable we defined above
#s = the size of the plots
#X[:, 0] is the numpy coordinates way of selecting every row entry for column 0 - i.e. a single column from the numpy array.
#X[:, 1] is the numpy coordinates way of selecting every row entry for column 1 - i.e. a single column from the numpy array.
plt.scatter(X[:, 0], X[:, 1], c=predict, s=50)

1 个答案:

答案 0 :(得分:1)

根据您的代码,以下内容对我有用。您当然可以使用numpy来存储CSV,但我只是更喜欢熊猫。每次运行代码时,排序行应该都会为您提供相同的结果。但是,由于集群的初始化可能会产生影响,因此我也会在您的代码中设置一个种子,例如np.random.seed(42)并使用random_state参数调用kmeans函数,例如kmeans = KMeans(n_clusters=4, random_state=42)

# transform to dataframe
import pandas as pd
import seaborn as sns
df = pd.DataFrame(X)
df.columns = ["var1", "var2"]
df["cluster"] = predict
colors = sns.color_palette()[0:4]
df = df.sort_values("cluster")

# check plot
sns.scatterplot(df["var1"], df["var2"], hue=df["cluster"], palette=colors)
plt.show()

# define rename schema
mynames = {"0": "center_left", "1": "top_left", "2": "bot_right", "3": "center"}
df["cluster_name"] = [mynames[str(i)] for i in df.cluster]

# plot again to verify order
sns.scatterplot(df["var1"], df["var2"], hue=df["cluster_name"], 
                palette=colors)
sns.despine()
plt.show()

# save dataframe as CSV
df.to_csv("myoutput.csv")

第一个情节如下:

enter image description here

第二个情节如下:

enter image description here

CSV将如下所示:

enter image description here