• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python neighbors.NearestCentroid类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中sklearn.neighbors.NearestCentroid的典型用法代码示例。如果您正苦于以下问题:Python NearestCentroid类的具体用法?Python NearestCentroid怎么用?Python NearestCentroid使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了NearestCentroid类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: train_with

    def train_with(self, training_data_list, answers):
        #put data in right format
        training_data = self.get_sparse_matrix(training_data_list)

        if training_data is not False:

        #make model
            if self.model_name == "random_forest":
                forest = RandomForestClassifier(n_estimators=100)
                self.model = forest.fit(training_data.todense(), answers)
            elif self.model_name == "centroid_prediction":
                clf = NearestCentroid()
                self.model = clf.fit(training_data, answers)
            elif self.model_name == "linearSVC":
                SVC = LinearSVC()
                self.model = SVC.fit(training_data.todense(), answers)
            elif self.model_name == "nearest_neighbor":
                near = KNeighborsClassifier()
                self.model = near.fit(training_data.todense(), answers)
            elif self.model_name == "decision_tree":
                clf = tree.DecisionTreeClassifier()
                self.model = clf.fit(training_data.todense(), answers)
            elif self.model_name == "svc":
                clf = svm.SVC()
                self.model = clf.fit(training_data, answers)
开发者ID:wongstein,项目名称:thesis,代码行数:25,代码来源:classification.py


示例2: NC

def NC(data_train, data_train_vectors, data_test_vectors, **kwargs):
    # Implementing classification model- using NearestCentroid
    clf_nc =  NearestCentroid()
    clf_nc.fit(data_train_vectors, data_train.target)
    y_pred = clf_nc.predict(data_test_vectors)
    
    return y_pred
开发者ID:RaoUmer,项目名称:docs_classification,代码行数:7,代码来源:ml_docs_classification_2.py


示例3: nearest_centroid_classifier

def nearest_centroid_classifier(X_train, categories, X_test, test_categories):
    from sklearn.neighbors import NearestCentroid
    clf = NearestCentroid().fit(X_train, categories)
    y_roccio_predicted = clf.predict(X_test)
    print "\n Here is the classification report for NearestCentroid classifier:"
    print metrics.classification_report(test_categories, y_roccio_predicted)
    to_latex(test_categories, y_roccio_predicted)  
开发者ID:LewkowskiArkadiusz,项目名称:magistrerka_app,代码行数:7,代码来源:main.py


示例4: test_shrinkage_threshold_decoded_y

def test_shrinkage_threshold_decoded_y():
    clf = NearestCentroid(shrink_threshold=0.01)
    y_ind = np.asarray(y)
    y_ind[y_ind == -1] = 0
    clf.fit(X, y_ind)
    centroid_encoded = clf.centroids_
    clf.fit(X, y)
    assert_array_equal(centroid_encoded, clf.centroids_)
开发者ID:1TTT9,项目名称:scikit-learn,代码行数:8,代码来源:test_nearest_centroid.py


示例5: test_manhattan_metric

def test_manhattan_metric():
    # Test the manhattan metric.

    clf = NearestCentroid(metric='manhattan')
    clf.fit(X, y)
    dense_centroid = clf.centroids_
    clf.fit(X_csr, y)
    assert_array_equal(clf.centroids_, dense_centroid)
    assert_array_equal(dense_centroid, [[-1, -1], [1, 1]])
开发者ID:1TTT9,项目名称:scikit-learn,代码行数:9,代码来源:test_nearest_centroid.py


示例6: test_iris_shrinkage

def test_iris_shrinkage():
    # Check consistency on dataset iris, when using shrinkage.
    for metric in ('euclidean', 'cosine'):
        for shrink_threshold in [None, 0.1, 0.5]:
            clf = NearestCentroid(metric=metric,
                                  shrink_threshold=shrink_threshold)
            clf = clf.fit(iris.data, iris.target)
            score = np.mean(clf.predict(iris.data) == iris.target)
            assert score > 0.8, "Failed with score = " + str(score)
开发者ID:1TTT9,项目名称:scikit-learn,代码行数:9,代码来源:test_nearest_centroid.py


示例7: nearest_centroid_classifier

def nearest_centroid_classifier(X_train, X_test, y_train, y_test):
    from sklearn.neighbors import NearestCentroid
    clf = NearestCentroid().fit(X_train, y_train)

    evaluate_cross_validation(clf,X_train, y_train, 5)


    y_roccio_predicted = clf.predict(X_test)
    print "\n Here is the classification report for NearestCentroid classifier:"
    print metrics.classification_report(y_test, y_roccio_predicted)
开发者ID:LewkowskiArkadiusz,项目名称:artykul,代码行数:10,代码来源:main.py


示例8: test_shrinkage_correct

def test_shrinkage_correct():
    # Ensure that the shrinking is correct.
    # The expected result is calculated by R (pamr),
    # which is implemented by the author of the original paper.
    # (One need to modify the code to output the new centroid in pamr.predict)

    X = np.array([[0, 1], [1, 0], [1, 1], [2, 0], [6, 8]])
    y = np.array([1, 1, 2, 2, 2])
    clf = NearestCentroid(shrink_threshold=0.1)
    clf.fit(X, y)
    expected_result = np.array([[0.7787310, 0.8545292], [2.814179, 2.763647]])
    np.testing.assert_array_almost_equal(clf.centroids_, expected_result)
开发者ID:Lavanya-Basavaraju,项目名称:scikit-learn,代码行数:12,代码来源:test_nearest_centroid.py


示例9: test_pickle

def test_pickle():
    import pickle

    # classification
    obj = NearestCentroid()
    obj.fit(iris.data, iris.target)
    score = obj.score(iris.data, iris.target)
    s = pickle.dumps(obj)

    obj2 = pickle.loads(s)
    assert_equal(type(obj2), obj.__class__)
    score2 = obj2.score(iris.data, iris.target)
    assert_array_equal(score, score2,
                       "Failed to generate same score"
                       " after pickling (classification).")
开发者ID:1TTT9,项目名称:scikit-learn,代码行数:15,代码来源:test_nearest_centroid.py


示例10: NCClassifier

class NCClassifier(Classifier):
    """Rocchio classifier"""
    def __init__(self, shrink=None):
        self.cl = NearestCentroid(shrink_threshold=shrink)
        self.shrink = shrink

    def retrain(self, vectorFeature, vectorTarget):
        if self.shrink != None:
            self.cl.fit([v.toarray()[0] for v in vectorFeature], vectorTarget)
        else:
            super(NCClassifier, self).retrain(vectorFeature, vectorTarget)

    def classify(self, vectorizedTest):
        if self.shrink != None:
            return self.cl.predict(vectorizedTest.toarray()[0])[0]
        else:
            return super(NCClassifier, self).classify(vectorizedTest)
开发者ID:giacbrd,项目名称:CipCipPy,代码行数:17,代码来源:__init__.py


示例11: nearestNeighbour

def nearestNeighbour():
	import numpy as np
	import pylab as pl
	from matplotlib.colors import ListedColormap
	from sklearn import datasets
	from sklearn.neighbors import NearestCentroid

	n_neighbors = 15

	# import some data to play with
	iris = datasets.load_iris()
	X = iris.data[:, :2]  # we only take the first two features. We could
	                      # avoid this ugly slicing by using a two-dim dataset
	y = iris.target

	h = .02  # step size in the mesh

	# Create color maps
	cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF'])
	cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])

	for shrinkage in [None, 0.1]:
	    # we create an instance of Neighbours Classifier and fit the data.
	    clf = NearestCentroid(shrink_threshold=shrinkage)
	    clf.fit(X, y)
	    y_pred = clf.predict(X)
	    print shrinkage, np.mean(y == y_pred)
	    # Plot the decision boundary. For that, we will asign a color to each
	    # point in the mesh [x_min, m_max]x[y_min, y_max].
	    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
	    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
	    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
	                         np.arange(y_min, y_max, h))
	    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

	    # Put the result into a color plot
	    Z = Z.reshape(xx.shape)
	    pl.figure()
	    pl.pcolormesh(xx, yy, Z, cmap=cmap_light)

	    # Plot also the training points
	    pl.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold)
	    pl.title("3-Class classification (shrink_threshold=%r)"
	             % shrinkage)
	    pl.axis('tight')
开发者ID:anirudhvenkats,项目名称:clowdflows,代码行数:45,代码来源:test.py


示例12: create_and_train_model

def create_and_train_model(engine):
    cmd = "SELECT review_rating, review_text FROM bf_reviews"
    bfdf = pd.read_sql_query(cmd, engine)
    bfdfl = bfdf[bfdf['review_text'].str.len() > 300].copy()
    train_data = bfdfl['review_text'].values[:1000]
    y_train = bfdfl['review_rating'].values[:1000]

    t0 = time()
    vectorizer = TfidfVectorizer(sublinear_tf=True, max_df=0.5,
                                 stop_words='english')
    X_train = vectorizer.fit_transform(train_data)
    duration = time() - t0
    print('vectorized in {:.2f} seconds.'.format(duration))
    print(X_train.shape)

    clf = NearestCentroid()
    clf.fit(X_train, y_train)
    return clf, vectorizer
开发者ID:mattgiguere,项目名称:doglodge,代码行数:18,代码来源:retrieve_best_hotels2.py


示例13: BinBasedCluster

class BinBasedCluster(BaseEstimator):
    def __init__(self, bins=[0, 0.5, 1] + range(5, 36)):
        self.bins = bins

    def fit(self, X, y):

        biny = self.bin_data(y)

        self.pred = NearestCentroid().fit(X, biny)
        return self

    def predict(self, X):
        return self.pred.predict(X)

    def score(self, X, y, is_raw=True):
        clusters = self.pred.predict(X)
        if is_raw:
            return adjusted_rand_score(self.bin_data(y), clusters)
        else:
            return adjusted_rand_score(y, clusters)

    def bin_data(self, y):
        return np.digitize(y, self.bins)

    def make_vern_points(self, X, y):

        sel = SelectKBest(score_func=normalized_mutual_info_score_scorefunc)
        sdata = sel.fit_transform(X, y)
        print X.shape, sdata.shape

        pca = PCA(n_components=2)
        pca_trans = pca.fit_transform(sdata)

        biny = self.bin_data(y)

        pred = NearestCentroid().fit(pca_trans, biny)

        x_min, x_max = pca_trans[:, 0].min() - 1, pca_trans[:, 0].max() + 1
        y_min, y_max = pca_trans[:, 1].min() - 1, pca_trans[:, 1].max() + 1
        xx, yy = np.meshgrid(np.linspace(x_min, x_max, 50), np.linspace(y_min, y_max, 50))
        Z = pred.predict(np.c_[xx.ravel(), yy.ravel()])
        Z = Z.reshape(xx.shape)

        return pca_trans, biny, xx, yy, Z
开发者ID:JudoWill,项目名称:PySeqUtils,代码行数:44,代码来源:SeqSklearn.py


示例14: make_vern_points

    def make_vern_points(self, X, y):

        sel = SelectKBest(score_func=normalized_mutual_info_score_scorefunc)
        sdata = sel.fit_transform(X, y)
        print X.shape, sdata.shape

        pca = PCA(n_components=2)
        pca_trans = pca.fit_transform(sdata)

        biny = self.bin_data(y)

        pred = NearestCentroid().fit(pca_trans, biny)

        x_min, x_max = pca_trans[:, 0].min() - 1, pca_trans[:, 0].max() + 1
        y_min, y_max = pca_trans[:, 1].min() - 1, pca_trans[:, 1].max() + 1
        xx, yy = np.meshgrid(np.linspace(x_min, x_max, 50), np.linspace(y_min, y_max, 50))
        Z = pred.predict(np.c_[xx.ravel(), yy.ravel()])
        Z = Z.reshape(xx.shape)

        return pca_trans, biny, xx, yy, Z
开发者ID:JudoWill,项目名称:PySeqUtils,代码行数:20,代码来源:SeqSklearn.py


示例15: test_predict_translated_data

def test_predict_translated_data():
    # Test that NearestCentroid gives same results on translated data

    rng = np.random.RandomState(0)
    X = rng.rand(50, 50)
    y = rng.randint(0, 3, 50)
    noise = rng.rand(50)
    clf = NearestCentroid(shrink_threshold=0.1)
    clf.fit(X, y)
    y_init = clf.predict(X)
    clf = NearestCentroid(shrink_threshold=0.1)
    X_noise = X + noise
    clf.fit(X_noise, y)
    y_translate = clf.predict(X_noise)
    assert_array_equal(y_init, y_translate)
开发者ID:1TTT9,项目名称:scikit-learn,代码行数:15,代码来源:test_nearest_centroid.py


示例16: test_precomputed

def test_precomputed():
    clf = NearestCentroid(metric='precomputed')
    with assert_raises(ValueError) as context:
        clf.fit(X, y)
    assert_equal(ValueError, type(context.exception))
开发者ID:Lavanya-Basavaraju,项目名称:scikit-learn,代码行数:5,代码来源:test_nearest_centroid.py


示例17: get_results


#.........这里部分代码省略.........
	#if 'area' in feature_names: 
	print (feature_names)

	print ("\n")
	#-------

	nmf = decomposition.NMF(n_components=3, init='random',random_state=0).fit(tfidf.todense())
	topic_list=[]
	l= int(len(feature_names)/5)
	#print l
	for topic_idx, topic in enumerate(nmf.components_):
		topic_list.append(topic.argsort()[:-l-1:-1])
    
    #print("Topic #%d:" % topic_idx)
    #print (topic)
	#print "Hello----"
	#print topic_list


	train_target=[]	
	for arr in v.toarray():
		train_target.append(calculate_Topic(arr,topic_list))
	#print train_target
	#clf = MultinomialNB()
	#clf2= LinearSVC()
	#clf1=NearestCentroid()
	#clf.fit(tfidf.todense(),train_target)
	#clf1.fit(tfidf.todense(),train_target)
	#clf2.fit(tfidf.todense(),train_target)
	#print (clf.predict(X_test))
	#print (clf1.predict(X_test))
	#print (clf2.predict(X_test))
	#print "Hello"
	ch2 = SelectKBest(chi2, k=l*2)
	X_train = ch2.fit_transform(tfidf.todense(), train_target)

	cs= ch2.scores_.argsort()[::-1]
	cs_featurenames=[]
	cs=cs[:l*2]
	for x in cs:
		cs_featurenames.append(feature_names[x])

	print (cs_featurenames)
	print "\n"

	nmf1 = decomposition.NMF(n_components=3, init='random',random_state=0).fit(X_train)
	topic_list=[]
	l= int(len(feature_names)/5)
	#print l
	for topic_idx, topic in enumerate(nmf1.components_):
		z=topic.argsort()[:-l-1:-1]
		topic_list.append(z)
		print("Topic #%d:---------------------------------------" % topic_idx)
		for y in z:
			print cs_featurenames[y]
    #print (topic)
	#print "Hello----"    
	#print topic_list
	train_target=[]	
	for arr in X_train:
		train_target.append(calculate_Topic(arr,topic_list))

	#---------
	#print "hello"
	#print train_target
	#print ch2.get_feature_names()
	#print X_train
	#print train_target
	#print "=--------------"
	#print ta
	#print X_test
	train_count=[0]*4
	#print train_target
	for x in train_target:
		train_count[x]=train_count[x]+1
	#print "hello"
	#print train_count	


	clf = MultinomialNB()
	clf2= LinearSVC()
	clf1=NearestCentroid()
	clf.fit(X_train,train_target)
	clf1.fit(X_train,train_target)
	clf2.fit(X_train,train_target)	
	dic={}
	hotels=read_hotels(city,dic)
	temp=[]
	for each in hotels:
		temp.append(calculate(vectorizer,transformer,train_count,ch2,each,clf,clf1,clf2,positive,negative,train_set))
	res=[]	
	temp1=numpy.array(temp).argsort()[::-1]
	#print temp1
	print "Top %d recommendations are as follows[in the FORMAT Index,(Hotel name,Location),Score]:\n" %no

	for g in temp1[:no]:
		print g,dic[g],temp[g]
		res.append(dic[g])

	return res	
开发者ID:Sandy4321,项目名称:hotel_recommender_system,代码行数:101,代码来源:tfidf.py


示例18: test_iris

def test_iris():
    # Check consistency on dataset iris.
    for metric in ('euclidean', 'cosine'):
        clf = NearestCentroid(metric=metric).fit(iris.data, iris.target)
        score = np.mean(clf.predict(iris.data) == iris.target)
        assert score > 0.9, "Failed with score = " + str(score)
开发者ID:1TTT9,项目名称:scikit-learn,代码行数:6,代码来源:test_nearest_centroid.py


示例19: test_precomputed

def test_precomputed():
    clf = NearestCentroid(metric="precomputed")
    clf.fit(X, y)
    S = pairwise_distances(T, clf.centroids_)
    assert_array_equal(clf.predict(S), true_result)
开发者ID:1TTT9,项目名称:scikit-learn,代码行数:5,代码来源:test_nearest_centroid.py


示例20: test_classification_toy

def test_classification_toy():
    # Check classification on a toy dataset, including sparse versions.
    clf = NearestCentroid()
    clf.fit(X, y)
    assert_array_equal(clf.predict(T), true_result)

    # Same test, but with a sparse matrix to fit and test.
    clf = NearestCentroid()
    clf.fit(X_csr, y)
    assert_array_equal(clf.predict(T_csr), true_result)

    # Fit with sparse, test with non-sparse
    clf = NearestCentroid()
    clf.fit(X_csr, y)
    assert_array_equal(clf.predict(T), true_result)

    # Fit with non-sparse, test with sparse
    clf = NearestCentroid()
    clf.fit(X, y)
    assert_array_equal(clf.predict(T_csr), true_result)

    # Fit and predict with non-CSR sparse matrices
    clf = NearestCentroid()
    clf.fit(X_csr.tocoo(), y)
    assert_array_equal(clf.predict(T_csr.tolil()), true_result)
开发者ID:1TTT9,项目名称:scikit-learn,代码行数:25,代码来源:test_nearest_centroid.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python neighbors.NearestNeighbors类代码示例发布时间:2022-05-27
下一篇:
Python neighbors.LSHForest类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap