kmeans的肘部法

我正在进行一个聚类任务,并使用了肘部法来确定最佳的聚类数量(k),但我得到的是一条线性图,我无法从图中确定k值。[enter image description here][2]

谢谢

enter image description here


回答:

There are many ways to do this kind of thing.  For one thing, you can use Yellowbrick to do the work.import pandas as pdimport matplotlib as mpl import matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dfrom sklearn.cluster import KMeansfrom sklearn.datasets import make_blobsfrom sklearn import datasetsfrom yellowbrick.cluster import KElbowVisualizer, SilhouetteVisualizermpl.rcParams["figure.figsize"] = (9,6)# Load iris flower datasetiris = datasets.load_iris()X = iris.data #clustering is unsupervised learning hence we load only X(i.e.iris.data) and not Y(i.e. iris.target)# Converting the data into dataframefeature_names = iris.feature_namesiris_dataframe = pd.DataFrame(X, columns=feature_names)iris_dataframe.head(10)# Fitting the model with a dummy model, with 3 clusters (we already know there are 3 classes in the Iris dataset)k_means = KMeans(n_clusters=3)k_means.fit(X)# Plotting a 3d plot using matplotlib to visualize the data pointsfig = plt.figure(figsize=(7,7))ax = fig.add_subplot(111, projection='3d')# Setting the colors to match cluster resultscolors = ['red' if label == 0 else 'purple' if label==1 else 'green' for label in k_means.labels_]ax.scatter(X[:,3], X[:,0], X[:,2], c=colors)

# Instantiate the clustering model and visualizermodel = KMeans()visualizer = KElbowVisualizer(model, k=(2,11))visualizer.fit(X)    # Fit the data to the visualizervisualizer.show()    # Draw/show/show the data

enter image description here

请查看下面的链接以获取更多信息。

https://notebook.community/DistrictDataLabs/yellowbrick/examples/gokriznastic/Iris%20-%20clustering%20example

https://github.com/ASH-WICUS/Notebooks/blob/master/Clustering%20-%20Historical%20Stock%20Prices.ipynb

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注