本文整理汇总了Python中sklearn.cluster.KMeans.score方法的典型用法代码示例。如果您正苦于以下问题:Python KMeans.score方法的具体用法?Python KMeans.score怎么用?Python KMeans.score使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.cluster.KMeans
的用法示例。
在下文中一共展示了KMeans.score方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compute_print_scores
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def compute_print_scores(normal_users, queue):
K_GMM_n, K_KMeans_n, K_GMM_s, K_KMeans_s = Ks
print 'novelty score GMM'
B = GMM(covariance_type='full', n_components = 1)
B.fit(queue)
x = [B.score([i]).mean() for i in queue]
print get_score_last_item(x, K_GMM_n)
print 'novelty score OneClassSVM'
x = anom_one_class(queue, [queue[-1]])
print x[-1]
print 'novelty score LSA'
anomalymodel = lsanomaly.LSAnomaly()
X = np.array(queue)
anomalymodel.fit(X)
print anomalymodel.predict(np.array([queue[-1]]))
print 'novelty score degree K_means'
K = KMeans(n_clusters=1)
K.fit(queue)
x = [K.score([i]) for i in queue]
print get_score_last_item(x, K_KMeans_n)
normal_and_new = normal_users + [queue[-1]]
print 'degree of belonging to known class GMM'
B = GMM(covariance_type='full', n_components = 1)
B.fit(normal_users)
x = [B.score([i]).mean() for i in normal_and_new]
print get_score_last_item(x, K_GMM_s)
print 'degree of belonging to known class OneClassSVM'
x = anom_one_class(normal_users, [queue[-1]])
print x[-1]
print 'degree of belonging to known class LSA'
anomalymodel = lsanomaly.LSAnomaly()
X = np.array(normal_users)
anomalymodel.fit(X)
print anomalymodel.predict(np.array([queue[-1]]))
print 'degree of belonging to known class K_means'
K = KMeans(n_clusters=1)
K.fit(normal_users)
x = [K.score([i]) for i in normal_and_new]
print get_score_last_item(x, K_KMeans_s)
示例2: explore_k
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def explore_k(svd_trans, k_range):
'''
Explores various values of k in KMeans
Args:
svd_trans: dense array with lsi transformed data
k_range: the range of k-values to explore
Returns:
scores: list of intertia scores for each k value
'''
scores = []
# spherical kmeans, so normalize
normalizer = Normalizer()
norm_data = normalizer.fit_transform(svd_trans)
for k in np.arange:
km = KMeans(n_clusters=k, init='k-means++', max_iter=100, n_init=1,
verbose=2)
km.fit(norm_data)
scores.append(-1*km.score(norm_data))
plt.plot(k_range, scores)
plt.xlabel('# of clusters')
plt.ylabel('Inertia')
sns.despine(offset=5, trim=True)
return scores
示例3: explore
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def explore(data):
errList=[];
for i in range(2,int(len(data)**0.5)):
km = KMeans(n_clusters=i)
km.fit(data)
err = abs(km.score(data))
errList.append(err)
示例4: run
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def run():
cluster_centers = load_prediction()
test_data = load_test_data()
k = KMeans(n_clusters=200)
k.cluster_centers_ = cluster_centers
score = k.score(test_data)
print("Score: %f" % (score / len(test_data) * -1))
示例5: Clustering
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def Clustering():
PCA_threshold = 0.8
for team_id in team_dic.itervalues():
BoF_Team = BoF[team_id]
dim = np.shape(BoF_Team)[0]
threshold_dim = 0
for i in range(dim):
pca = PCA(n_components = i)
pca.fit(BoF_Team)
X = pca.transform(BoF_Team)
E = pca.explained_variance_ratio_
if np.sum(E) > PCA_threshold:
thereshold_dim = len(E)
print 'Team' + str(team_id)+ ' dim:%d' % thereshold_dim
break
pca = PCA(n_components = thereshold_dim)
pca.fit(BoF_Team)
X = pca.transform(BoF_Team)
min_score = 10000
for i in range(100):
model = KMeans(n_clusters=K, init='k-means++', max_iter=300, tol=0.0001).fit(X)
if min_score > model.score(X):
min_score = model.score(X)
labels = model.labels_
print min_score
pca = PCA(n_components = 2)
pca.fit(BoF_Team)
X = pca.transform(BoF_Team)
for k in range(K):
labels_ind = np.where(labels == k)[0]
plt.scatter(X[labels_ind,0], X[labels_ind,1], color=C[k])
plt.legend(['C0','C1','C2','C3','C4'], loc=4)
plt.title('Team' + str(team_id) + '1_PCA_kmeans')
plt.savefig('Seq_Team' + str(team_id)+ '/Team' + str(team_id) + '_PCA_kmeans.png')
plt.show()
plt.close()
np.savetxt('Seq_Team' + str(team_id) + '/labels_Team' + str(team_id) + '.csv', \
labels, delimiter=',')
示例6: cluster
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def cluster(train_latents, train_labels, test_latents, test_labels):
num_classes = np.shape(train_labels)[-1]
labels_hot = np.argmax(test_labels, axis=-1)
train_latents = np.reshape(train_latents,
newshape=[train_latents.shape[0], -1])
test_latents = np.reshape(test_latents,
newshape=[test_latents.shape[0], -1])
kmeans = KMeans(init='random', n_clusters=num_classes,
random_state=0, max_iter=1000, n_init=FLAGS.n_init,
n_jobs=FLAGS.n_jobs)
kmeans.fit(train_latents)
print(kmeans.cluster_centers_)
print('Train/Test k-means objective = %.4f / %.4f' %
(-kmeans.score(train_latents), -kmeans.score(test_latents)))
print('Train/Test accuracy %.4f / %.3f' %
(error(np.argmax(train_labels, axis=-1), kmeans.predict(train_latents), k=num_classes),
error(np.argmax(test_labels, axis=-1), kmeans.predict(test_latents), k=num_classes)))
return error(labels_hot, kmeans.predict(test_latents), k=num_classes)
示例7: findKForKMeans
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def findKForKMeans(X):
graph=[];
for i in range(2,200):
km = KMeans(n_clusters=i);
km.fit(X);
y = km.score(X);
graph.append(y);
print i, y
print graph;
示例8: predictKMeans
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def predictKMeans(X, y):
col_mean = np.nanmean(X,axis=0)
inds = np.where(np.isnan(X))
X[inds]=np.take(col_mean,inds[1])
km = KMeans(n_clusters=2)
X_train, X_test, y_train, y_test = chooseRandom(X, y)
km.fit(X_train, y_train)
return km.score(X_test, y_test)
示例9: experiment
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def experiment(nexperiments, nclusters):
E_ins = []
for _ in range(nexperiments):
kmeans = KMeans(nclusters, max_iter=300, n_init=1, init='random')
kmeans.fit(X_train)
score = kmeans.score(X_train)
E_in = -score / nsamples
E_ins.append(E_in)
return E_ins
示例10: showKMeans
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def showKMeans(X, N):
scores = []
for number in xrange(N / 6, N / 2):
clustering = KMeans(n_clusters=number, max_iter=MAX_ITER, n_init=N_INIT, n_jobs=N_JOBS )
clustering.fit_predict(X)
scores.append(clustering.score(X))
plt.plot(scores)
plt.xlabel(XLABEL)
plt.ylabel(YLABEL)
plt.show()
示例11: kmean_score
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def kmean_score(X,nclust):
'''
calculate kmeans score
:param X:numpy array, data set to cluster
:param nclust: int, number of cluster
:return: float
'''
km = KMeans(nclust)
km.fit(X)
rss = -km.score(X)
return rss
示例12: partition_gene_kmeans
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def partition_gene_kmeans(geneToCases, patientToGenes, gene_list, num_components, num_bins, title=None, do_plot=True):
# get gene index mapping
giv = getgiv(geneToCases.keys(), gene_list)
# convert patients into vectors
patientToVector = getpatientToVector(patientToGenes, giv)
vectors = patientToVector.values()
print vectors[0]
print "Length of vectors is ", len(vectors[0])
km = KMeans(num_components)
km.fit(vectors)
clusterToPatient = {}
for patient in patientToVector:
cluster = km.predict(patientToVector[patient])[0]
if cluster not in clusterToPatient:
clusterToPatient[cluster] = set()
clusterToPatient[cluster].add(patient)
# plot patients in each cluster
if do_plot:
bins = range(0, max([len(p_gene) for p_gene in patientToGenes.values()]), max([len(p_gene) for p_gene in patientToGenes.values()])/num_bins)
plt.figure()
for cluster in clusterToPatient:
plt.hist([len(patientToGenes[p]) for p in clusterToPatient[cluster]], bins=bins, label=str(cluster), alpha = 1.0/num_components)
plt.xlabel('# Somatic Mutations In Tumor', fontsize=20)
plt.ylabel('Number of Samples', fontsize=20)
plt.legend()
plt.title("Kmeans size " + str(num_components), fontsize=20)
plt.show()
data = {}
data['Score'] = km.score(vectors)
data['Number'] = num_components
data['% Explained'] = np.round([100 * len(clusterToPatient[cluster]) * 1.0 / len(patientToGenes) for cluster in clusterToPatient], 2)
data['Vector size'] = len(vectors[0])
# data['Covariates'] = np.round(g.covars_,2)
# data["Total log probability"] = sum(g.score(obs))
# data["AIC"] = g.aic(obs)
# data["BIC"] = g.bic(obs)
# data['Explained'] = [np.round([len([in_w for in_w in respon if in_w[i] == max(in_w)]) * 1.0 /len(respon) for i in range(num_components)], 2)]
return data
示例13: cluster
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def cluster():
f=open("./forum/flu.txt")
flu =[]
for line in f.readlines():
flu.append(line)
f.close()
vectorizer = TfidfVectorizer(sublinear_tf= True,min_df=0,max_df=1.0,ngram_range=(1,1),smooth_idf=True,use_idf=1,strip_accents=None)
x=vectorizer.fit_transform(flu)
n_samples,n_features=x.shape
print n_samples,n_features
kmeans =KMeans(n_clusters=4, init='k-means++', n_init=10, max_iter=300, tol=0.0001, precompute_distances=True, verbose=0, random_state=None, copy_x=True, n_jobs=1)
kmeans.fit(x)
print kmeans.score(x)
c=kmeans.predict(x)
f1=open('./forum/1.txt','w')
f2=open('./forum/2.txt','w')
f3=open('./forum/3.txt','w')
f4=open('./forum4.txt','w')
for i in range(0,len(c)):
if c[i]== 0:
f1.write('%s'%(flu[i]))
elif c[i]==1:
f2.write('%s'%(flu[i]))
elif c[i]==2:
f3.write('%s'%(flu[i]))
else:
f4.write('%s'%(flu[i]))
f1.close()
f2.close()
f3.close()
f4.close()
示例14: test_kmeans
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def test_kmeans():
X = loadmat('ex7/ex7data2.mat')['X']
n_init = int(X.shape[0] ** 0.5)
Js = []
Ks = range(1, 11)
for k in Ks:
km = KMeans(n_clusters=k, n_init=n_init, n_jobs=-1)
km.fit(X)
Js.append(km.score(X))
# plotJ(Ks,Js)
bestK = best_accelation(Js)
km = KMeans(n_clusters=bestK, n_init=n_init, n_jobs=-1)
km.fit(X)
plt.clf()
plt.scatter(*np.split(X, 2, axis=1), c='g')
plt.scatter(*np.split(km.cluster_centers_, 2, axis=1), c='b', marker='D')
plt.show()
示例15: _runKmeans
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import score [as 别名]
def _runKmeans(train_data):
#print ('Running Kmeans Clustering...')
num_clusters = 5
model = KMeans(init='k-means++', n_clusters=num_clusters, n_init=10)
max_score = 0
iteration = 20
best_classification = []
for i in range(1,iteration):
# print "Iteration number "+str(i)
model.fit(train_data)
score = model.score(train_data)
if i == 1 or score > max_score:
max_score = score
best_classification = model.predict(train_data)
#print ("Done!")
return best_classification.tolist()