本文整理汇总了Python中sklearn.neighbors.NearestCentroid方法的典型用法代码示例。如果您正苦于以下问题:Python neighbors.NearestCentroid方法的具体用法?Python neighbors.NearestCentroid怎么用?Python neighbors.NearestCentroid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.neighbors
的用法示例。
在下文中一共展示了neighbors.NearestCentroid方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_classification_toy
# 需要导入模块: from sklearn import neighbors [as 别名]
# 或者: from sklearn.neighbors import NearestCentroid [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)
示例2: test_objectmapper
# 需要导入模块: from sklearn import neighbors [as 别名]
# 或者: from sklearn.neighbors import NearestCentroid [as 别名]
def test_objectmapper(self):
df = pdml.ModelFrame([])
self.assertIs(df.neighbors.NearestNeighbors,
neighbors.NearestNeighbors)
self.assertIs(df.neighbors.KNeighborsClassifier,
neighbors.KNeighborsClassifier)
self.assertIs(df.neighbors.RadiusNeighborsClassifier,
neighbors.RadiusNeighborsClassifier)
self.assertIs(df.neighbors.KNeighborsRegressor,
neighbors.KNeighborsRegressor)
self.assertIs(df.neighbors.RadiusNeighborsRegressor,
neighbors.RadiusNeighborsRegressor)
self.assertIs(df.neighbors.NearestCentroid, neighbors.NearestCentroid)
self.assertIs(df.neighbors.BallTree, neighbors.BallTree)
self.assertIs(df.neighbors.KDTree, neighbors.KDTree)
self.assertIs(df.neighbors.DistanceMetric, neighbors.DistanceMetric)
self.assertIs(df.neighbors.KernelDensity, neighbors.KernelDensity)
示例3: test_precomputed
# 需要导入模块: from sklearn import neighbors [as 别名]
# 或者: from sklearn.neighbors import NearestCentroid [as 别名]
def test_precomputed():
clf = NearestCentroid(metric='precomputed')
with assert_raises(ValueError):
clf.fit(X, y)
示例4: test_iris
# 需要导入模块: from sklearn import neighbors [as 别名]
# 或者: from sklearn.neighbors import NearestCentroid [as 别名]
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)
示例5: test_iris_shrinkage
# 需要导入模块: from sklearn import neighbors [as 别名]
# 或者: from sklearn.neighbors import NearestCentroid [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)
示例6: test_pickle
# 需要导入模块: from sklearn import neighbors [as 别名]
# 或者: from sklearn.neighbors import NearestCentroid [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).")
示例7: test_shrinkage_threshold_decoded_y
# 需要导入模块: from sklearn import neighbors [as 别名]
# 或者: from sklearn.neighbors import NearestCentroid [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_)
示例8: test_predict_translated_data
# 需要导入模块: from sklearn import neighbors [as 别名]
# 或者: from sklearn.neighbors import NearestCentroid [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)
示例9: test_manhattan_metric
# 需要导入模块: from sklearn import neighbors [as 别名]
# 或者: from sklearn.neighbors import NearestCentroid [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]])
示例10: test_plot13
# 需要导入模块: from sklearn import neighbors [as 别名]
# 或者: from sklearn.neighbors import NearestCentroid [as 别名]
def test_plot13(self):
np.random.seed(seed)
X, y = iris_data()
X = X[:, [0, 2]]
dml = NCMML()
clf = NearestCentroid()
dml_plot(X, y, clf, cmap="gist_rainbow", figsize=(15, 8))
self.newsave()
dml_plot(X, y, dml=dml, clf=clf, cmap="gist_rainbow", figsize=(15, 8))
self.newsave()
dml_pairplots(X, y, dml=dml, clf=clf, cmap="gist_rainbow", figsize=(15, 8))
self.newsave()
plt.close()
示例11: test_plot16
# 需要导入模块: from sklearn import neighbors [as 别名]
# 或者: from sklearn.neighbors import NearestCentroid [as 别名]
def test_plot16(self):
np.random.seed(seed)
X, y = toy_datasets.balls_toy_dataset(centers=[[-1.0, 0.0], [0.0, 0.0], [1.0, 0.0]],
rads=[0.3, 0.3, 0.3], samples=[50, 50, 50],
noise=[0.1, 0.1, 0.1])
y[y == 2] = 0
y = y.astype(int)
ncm = NearestCentroid()
ncmc = NCMC_Classifier(centroids_num=[2, 1])
dml_multiplot(X, y, nrow=1, ncol=2, clfs=[ncm, ncmc], cmap='rainbow',
subtitles=['NCM', 'NCMC'], figsize=(6, 3))
self.newsave()
plt.close()
示例12: test_precomputed
# 需要导入模块: from sklearn import neighbors [as 别名]
# 或者: from sklearn.neighbors import NearestCentroid [as 别名]
def test_precomputed():
clf = NearestCentroid(metric='precomputed')
with assert_raises(ValueError) as context:
clf.fit(X, y)
assert_equal(ValueError, type(context.exception))