python – ValueError:标签数为1.使用silhouette_score时,有效值为2到n_samples – 1(包括)
作者:互联网
我正在尝试计算剪影得分,因为我找到了要创建的最佳簇数,但得到的错误表明:
ValueError: Number of labels is 1. Valid values are 2 to n_samples - 1 (inclusive)
我无法理解这个原因.这是我用来聚类和计算轮廓分数的代码.
我读了包含要聚类的文本的csv,并在n个簇值上运行K-Means.可能是我收到此错误的原因是什么?
#Create cluster using K-Means
#Only creates graph
import matplotlib
#matplotlib.use('Agg')
import re
import os
import nltk, math, codecs
import csv
from nltk.corpus import stopwords
from gensim.models import Doc2Vec
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import silhouette_score
model_name = checkpoint_save_path
loaded_model = Doc2Vec.load(model_name)
#Load the test csv file
data = pd.read_csv(test_filename)
overview = data['overview'].astype('str').tolist()
overview = filter(bool, overview)
vectors = []
def split_words(text):
return ''.join([x if x.isalnum() or x.isspace() else " " for x in text ]).split()
def preprocess_document(text):
sp_words = split_words(text)
return sp_words
for i, t in enumerate(overview):
vectors.append(loaded_model.infer_vector(preprocess_document(t)))
sse = {}
silhouette = {}
for k in range(1,15):
km = KMeans(n_clusters=k, max_iter=1000, verbose = 0).fit(vectors)
sse[k] = km.inertia_
#FOLLOWING LINE CAUSES ERROR
silhouette[k] = silhouette_score(vectors, km.labels_, metric='euclidean')
best_cluster_size = 1
min_error = float("inf")
for cluster_size in sse:
if sse[cluster_size] < min_error:
min_error = sse[cluster_size]
best_cluster_size = cluster_size
print(sse)
print("====")
print(silhouette)
解决方法:
产生错误是因为您有一个循环用于不同数量的集群n.在第一次迭代期间,n_clusters为1,这导致所有(km.labels_ == 0)为True.
换句话说,您只有一个标签为0的集群(因此,np.unique(km.labels_)打印数组([0],dtype = int32)).
silhouette_score需要多个群集标签.这会导致错误.错误消息很明确.
例:
from sklearn import datasets
from sklearn.cluster import KMeans
import numpy as np
iris = datasets.load_iris()
X = iris.data
y = iris.target
km = KMeans(n_clusters=3)
km.fit(X,y)
# check how many unique labels do you have
np.unique(km.labels_)
#array([0, 1, 2], dtype=int32)
我们有3个不同的集群/集群标签.
silhouette_score(X, km.labels_, metric='euclidean')
0.38788915189699597
功能正常.
现在,让我们导致错误:
km2 = KMeans(n_clusters=1)
km2.fit(X,y)
silhouette_score(X, km2.labels_, metric='euclidean')
06003
标签:python,pandas,scikit-learn,machine-learning,k-means 来源: https://codeday.me/bug/20191006/1858352.html