当前位置: 首页>>代码示例>>Python>>正文


Python KMeans.score方法代码示例

本文整理汇总了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)
开发者ID:UC3MSocialRobots,项目名称:novelty-detection-in-hri,代码行数:51,代码来源:helper.py

示例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
开发者ID:lwoloszy,项目名称:albumpitch,代码行数:27,代码来源:genres.py

示例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)
开发者ID:michalt25,项目名称:Courses,代码行数:9,代码来源:kmeans.py

示例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))
开发者ID:lukaselmer,项目名称:ethz-data-mining,代码行数:9,代码来源:evaluate.py

示例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=',')
开发者ID:yusuk-e,项目名称:Sports_Analysis002,代码行数:47,代码来源:new_input.py

示例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)
开发者ID:shikharbahl,项目名称:acai,代码行数:20,代码来源:cluster.py

示例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;
开发者ID:bigfatnoob,项目名称:Apriori-Confidence,代码行数:11,代码来源:K-Means.py

示例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)
开发者ID:BIDS-collaborative,项目名称:EDAM,代码行数:11,代码来源:predict_lh.py

示例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
开发者ID:ashvant,项目名称:Machine-Learning-Techniques-NTU,代码行数:11,代码来源:hw4q19_sklearn.py

示例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()
开发者ID:jeka3230,项目名称:Pattern-recognition,代码行数:12,代码来源:Clustering.py

示例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
开发者ID:banjopickin,项目名称:women_workforce,代码行数:13,代码来源:model_vis.py

示例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
开发者ID:lujonathanh,项目名称:coffdrop,代码行数:55,代码来源:partition_3_11_16.py

示例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()
开发者ID:jwchennlp,项目名称:study,代码行数:40,代码来源:cluster.py

示例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()
开发者ID:freephys,项目名称:mylab,代码行数:19,代码来源:ex7.py

示例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()
开发者ID:xwtt8,项目名称:Hospital-Readmission-,代码行数:21,代码来源:Clustering.py


注:本文中的sklearn.cluster.KMeans.score方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。