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


Python NearestCentroid.fit方法代码示例

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


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

示例1: train_with

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
    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,代码行数:27,代码来源:classification.py

示例2: test_classification_toy

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
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,代码行数:27,代码来源:test_nearest_centroid.py

示例3: NC

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
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,代码行数:9,代码来源:ml_docs_classification_2.py

示例4: test_shrinkage_threshold_decoded_y

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
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,代码行数:10,代码来源:test_nearest_centroid.py

示例5: test_manhattan_metric

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
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,代码行数:11,代码来源:test_nearest_centroid.py

示例6: test_shrinkage_correct

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
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,代码行数:14,代码来源:test_nearest_centroid.py

示例7: test_predict_translated_data

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
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,代码行数:17,代码来源:test_nearest_centroid.py

示例8: test_pickle

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
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,代码行数:17,代码来源:test_nearest_centroid.py

示例9: NCClassifier

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
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,代码行数:19,代码来源:__init__.py

示例10: nearestNeighbour

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
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,代码行数:47,代码来源:test.py

示例11: test_iris_shrinkage

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
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,代码行数:11,代码来源:test_nearest_centroid.py

示例12: create_and_train_model

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
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,代码行数:20,代码来源:retrieve_best_hotels2.py

示例13: range

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
        X_test = sample2
        y_test = labels[272:,i]
    else:
        X_train = training
        y_train = labels[:172,i]
        X_test = sampletest
        y_test = labels[172:,i]

    posterior = np.empty([100,72,6])
    box = np.zeros([6,6])
    for j in range(4,5):
        for k in range(1,2):
            accuracy = np.zeros(100)
            for m in range(0,100):
                ncc = NearestCentroid()
                ncc.fit(X_train, y_train)
                y_pred = ncc.predict(X_test)
                
                n=0
                for i in range(0,len(y_pred)):
                    if y_pred[i] == y_test[i]:
                #print i, y_pred[i], y_test[i]
                        n = n+1
                        accuracy[m] = accuracy[m]+1
                    box[y_test[i]-1,y_pred[i]-1] = box[y_test[i]-1,y_pred[i]-1] + 1
                #posterior[m] =  knc.predict_proba(X_test)
            print j, k, np.mean(accuracy)/0.72, np.std(accuracy)/0.72
            #print 30, 20, sum(accuracy[0:8])/8.0, sum(accuracy[8:18])/10.0, sum(accuracy[18:30])/12.0, sum(accuracy[56:72])/16.0, sum(accuracy[30:43])/13.0, sum(accuracy[43:56])/13.0, sum(accuracy)/72.0
        '''
    means = np.empty([72,6])
    stds = np.empty([72,6])
开发者ID:d-giles,项目名称:KeplerML,代码行数:33,代码来源:nearestcentroid.py

示例14: test_precomputed

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
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,代码行数:7,代码来源:test_nearest_centroid.py

示例15: test_precomputed

# 需要导入模块: from sklearn.neighbors import NearestCentroid [as 别名]
# 或者: from sklearn.neighbors.NearestCentroid import fit [as 别名]
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,代码行数:7,代码来源:test_nearest_centroid.py


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