本文整理汇总了Python中sklearn.neighbors.classification.KNeighborsClassifier类的典型用法代码示例。如果您正苦于以下问题:Python KNeighborsClassifier类的具体用法?Python KNeighborsClassifier怎么用?Python KNeighborsClassifier使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了KNeighborsClassifier类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: DCS
class DCS(object):
@abstractmethod
def select(self, ensemble, x):
pass
def __init__(self, Xval, yval, K=5, weighted=False, knn=None):
self.Xval = Xval
self.yval = yval
self.K = K
if knn == None:
self.knn = KNeighborsClassifier(n_neighbors=K, algorithm='brute')
else:
self.knn = knn
self.knn.fit(Xval, yval)
self.weighted = weighted
def get_neighbors(self, x, return_distance=False):
# obtain the K nearest neighbors of test sample in the validation set
if not return_distance:
[idx] = self.knn.kneighbors(x,
return_distance=return_distance)
else:
[dists], [idx] = self.knn.kneighbors(x,
return_distance=return_distance)
X_nn = self.Xval[idx] # k neighbors
y_nn = self.yval[idx] # k neighbors target
if return_distance:
return X_nn, y_nn, dists
else:
return X_nn, y_nn
开发者ID:guilhermepaiva,项目名称:brew,代码行数:35,代码来源:base.py
示例2: _pruning
def _pruning(self):
if len(self.groups) < 2:
return self.groups
pruned, fst = False, True
knn = KNeighborsClassifier(n_neighbors = 1, algorithm='brute')
while pruned or fst:
index = 0
pruned, fst = False, False
while index < len(self.groups):
group = self.groups[index]
mask = np.ones(len(self.groups), dtype=bool)
mask[index] = False
reps_x = np.asarray([g.rep_x for g in self.groups])[mask]
reps_y = np.asarray([g.label for g in self.groups])[mask]
labels = knn.fit(reps_x, reps_y).predict(group.X)
if (labels == group.label).all():
self.groups.remove(group)
pruned = True
else:
index = index + 1
if len(self.groups) == 1:
index = len(self.groups)
pruned = False
return self.groups
开发者ID:antonlarin,项目名称:scikit-protopy,代码行数:32,代码来源:sgp.py
示例3: __init__
class RawModel:
def __init__(self):
# 2015-05-15 GEL Found that n_components=20 gives a nice balance of
# speed (substantial improvement), accuracy, and reduced memory usage
# (25% decrease).
self.decomposer = TruncatedSVD(n_components=20)
# 2015-05-15 GEL algorithm='ball_tree' uses less memory on average than
# algorithm='kd_tree'
# 2015-05-15 GEL Evaluation of metrics by accuracy (based on 8000 training examples)
# euclidean 0.950025
# manhattan 0.933533
# chebyshev 0.675662
# hamming 0.708646
# canberra 0.934033
# braycurtis 0.940530
self.model = KNeighborsClassifier(n_neighbors=5, algorithm='ball_tree', metric='euclidean')
def fit(self, trainExamples):
X = self.decomposer.fit_transform( vstack( [reshape(x.X, (1, x.WIDTH * x.HEIGHT)) for x in trainExamples] ) )
Y = [x.Y for x in trainExamples]
self.model.fit(X, Y)
return self
def predict(self, examples):
X = self.decomposer.transform( vstack( [reshape(x.X, (1, x.WIDTH * x.HEIGHT)) for x in examples] ) )
return self.model.predict( X )
开发者ID:lewellen,项目名称:digit-recognizer,代码行数:29,代码来源:rawModel.py
示例4: _main_loop
def _main_loop(self):
exit_count = 0
knn = KNeighborsClassifier(n_neighbors = 1, algorithm='brute')
while exit_count < len(self.groups):
index, exit_count = 0, 0
while index < len(self.groups):
group = self.groups[index]
reps_x = np.asarray([g.rep_x for g in self.groups])
reps_y = np.asarray([g.label for g in self.groups])
knn.fit(reps_x, reps_y)
nn_idx = knn.kneighbors(group.X, n_neighbors=1, return_distance=False)
nn_idx = nn_idx.T[0]
mask = nn_idx == index
# if all are correctly classified
if not (False in mask):
exit_count = exit_count + 1
# if all are misclasified
elif not (group.label in reps_y[nn_idx]):
pca = PCA(n_components=1)
pca.fit(group.X)
# maybe use a 'for' instead of creating array
d = pca.transform(reps_x[index])
dis = [pca.transform(inst)[0] for inst in group.X]
mask_split = (dis < d).flatten()
new_X = group.X[mask_split]
self.groups.append(_Group(new_X, group.label))
group.X = group.X[~mask_split]
elif (reps_y[nn_idx] == group.label).all() and (nn_idx != index).any():
mask_mv = nn_idx != index
index_mv = np.asarray(range(len(group)))[mask_mv]
X_mv = group.remove_instances(index_mv)
G_mv = nn_idx[mask_mv]
for x, g in zip(X_mv, G_mv):
self.groups[g].add_instances([x])
elif (reps_y[nn_idx] != group.label).sum()/float(len(group)) > self.r_mis:
mask_mv = reps_y[nn_idx] != group.label
new_X = group.X[mask_mv]
self.groups.append(_Group(new_X, group.label))
group.X = group.X[~mask_mv]
else:
exit_count = exit_count + 1
if len(group) == 0:
self.groups.remove(group)
else:
index = index + 1
for g in self.groups:
g.update_all()
return self.groups
开发者ID:antonlarin,项目名称:scikit-protopy,代码行数:59,代码来源:sgp.py
示例5: evaluate
def evaluate(Xtra, ytra, Xtst, ytst, k=1, positive_label=1):
knn = KNeighborsClassifier(n_neighbors=k, algorithm='brute')
knn.fit(Xtra, ytra)
y_true = ytst
y_pred = knn.predict(Xtst)
return evaluate_results(y_true, y_pred, positive_label=positive_label)
开发者ID:dvro,项目名称:ml,代码行数:8,代码来源:metrics.py
示例6: predict
def predict(self, X, n_neighbors=1):
"""Perform classification on an array of test vectors X.
The predicted class C for each sample in X is returned.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Returns
-------
C : array, shape = [n_samples]
Notes
-----
The default prediction is using KNeighborsClassifier, if the
instance reducition algorithm is to be performed with another
classifier, it should be explicited overwritten and explained
in the documentation.
"""
X = atleast2d_or_csr(X)
if not hasattr(self, "X_") or self.X_ is None:
raise AttributeError("Model has not been trained yet.")
if not hasattr(self, "y_") or self.y_ is None:
raise AttributeError("Model has not been trained yet.")
if self.classifier == None:
self.classifier = KNeighborsClassifier(n_neighbors=n_neighbors)
self.classifier.fit(self.X_, self.y_)
return self.classifier.predict(X)
开发者ID:dvro,项目名称:ml,代码行数:32,代码来源:baseNew.py
示例7: reduce_data
def reduce_data(self, X, y):
X, y = check_X_y(X, y, accept_sparse="csr")
if self.classifier == None:
self.classifier = KNeighborsClassifier(n_neighbors=self.n_neighbors)
prots_s = []
labels_s = []
classes = np.unique(y)
self.classes_ = classes
for cur_class in classes:
mask = y == cur_class
insts = X[mask]
prots_s = prots_s + [insts[np.random.randint(0, insts.shape[0])]]
labels_s = labels_s + [cur_class]
self.classifier.fit(prots_s, labels_s)
for sample, label in zip(X, y):
if self.classifier.predict(sample) != [label]:
prots_s = prots_s + [sample]
labels_s = labels_s + [label]
self.classifier.fit(prots_s, labels_s)
self.X_ = np.asarray(prots_s)
self.y_ = np.asarray(labels_s)
self.reduction_ = 1.0 - float(len(self.y_))/len(y)
return self.X_, self.y_
开发者ID:antonlarin,项目名称:scikit-protopy,代码行数:31,代码来源:cnn.py
示例8: reduce_data
def reduce_data(self, X, y):
X, y = check_X_y(X, y, accept_sparse="csr")
if self.classifier == None:
self.classifier = KNeighborsClassifier(n_neighbors=self.n_neighbors)
if self.classifier.n_neighbors != self.n_neighbors:
self.classifier.n_neighbors = self.n_neighbors
classes = np.unique(y)
self.classes_ = classes
# loading inicial groups
self.groups = []
for label in classes:
mask = y == label
self.groups = self.groups + [_Group(X[mask], label)]
self._main_loop()
self._generalization_step()
self._merge()
self._pruning()
self.X_ = np.asarray([g.rep_x for g in self.groups])
self.y_ = np.asarray([g.label for g in self.groups])
self.reduction_ = 1.0 - float(len(self.y_))/len(y)
return self.X_, self.y_
开发者ID:antonlarin,项目名称:scikit-protopy,代码行数:25,代码来源:sgp.py
示例9: reduce_data
def reduce_data(self, X, y):
if self.classifier == None:
self.classifier = KNeighborsClassifier(n_neighbors=self.n_neighbors)
if self.classifier.n_neighbors != self.n_neighbors:
self.classifier.n_neighbors = self.n_neighbors
X, y = check_arrays(X, y, sparse_format="csr")
classes = np.unique(y)
self.classes_ = classes
if self.n_neighbors >= len(X):
self.X_ = np.array(X)
self.y_ = np.array(y)
self.reduction_ = 0.0
mask = np.zeros(y.size, dtype=bool)
tmp_m = np.ones(y.size, dtype=bool)
for i in xrange(y.size):
tmp_m[i] = not tmp_m[i]
self.classifier.fit(X[tmp_m], y[tmp_m])
sample, label = X[i], y[i]
if self.classifier.predict(sample) == [label]:
mask[i] = not mask[i]
tmp_m[i] = not tmp_m[i]
self.X_ = np.asarray(X[mask])
self.y_ = np.asarray(y[mask])
self.reduction_ = 1.0 - float(len(self.y_)) / len(y)
return self.X_, self.y_
开发者ID:viisar,项目名称:scikit-protopy,代码行数:33,代码来源:enn.py
示例10: index_nearest_neighbor
def index_nearest_neighbor(self, S, X, y):
classifier = KNeighborsClassifier(n_neighbors=1)
U = []
S_mask = np.array(S, dtype=bool, copy=True)
indexs = np.asarray(range(len(y)))[S_mask]
X_tra, y_tra = X[S_mask], y[S_mask]
for i in range(len(y)):
real_indexes = np.asarray(range(len(y)))[S_mask]
X_tra, y_tra = X[S_mask], y[S_mask]
#print len(X_tra), len(y_tra)
classifier.fit(X_tra, y_tra)
[[index]] = classifier.kneighbors(X[i], return_distance=False)
U = U + [real_indexes[index]]
return U
开发者ID:dvro,项目名称:scikit-protopy,代码行数:17,代码来源:ssma.py
示例11: __init__
def __init__(self, Xval, yval, K=5, weighted=False, knn=None):
self.Xval = Xval
self.yval = yval
self.K = K
if knn == None:
self.knn = KNeighborsClassifier(n_neighbors=K, algorithm='brute')
else:
self.knn = knn
self.knn.fit(Xval, yval)
self.weighted = weighted
开发者ID:guilhermepaiva,项目名称:brew,代码行数:12,代码来源:base.py
示例12: compute_cnn
def compute_cnn(X, y):
"condenced nearest neighbor. the cnn removes reduntant instances, maintaining the samples in the decision boundaries."
classifier = KNeighborsClassifier(n_neighbors=3)
prots_s = []
labels_s = []
classes = np.unique(y)
classes_ = classes
for cur_class in classes:
mask = y == cur_class
insts = X[mask]
prots_s = prots_s + [insts[np.random.randint(0, insts.shape[0])]]
labels_s = labels_s + [cur_class]
classifier.fit(prots_s, labels_s)
for sample, label in zip(X, y):
if classifier.predict(sample) != [label]:
prots_s = prots_s + [sample]
labels_s = labels_s + [label]
classifier.fit(prots_s, labels_s)
X_ = np.asarray(prots_s)
y_ = np.asarray(labels_s)
reduction_ = 1.0 - float(len(y_)/len(y))
print reduction_
开发者ID:guilhermepaiva,项目名称:mlstuffs,代码行数:29,代码来源:cnn.py
示例13: compute_enn
def compute_enn(X, y):
"""
the edited nearest neighbors removes the instances in the boundaries, maintaining reduntant samples
"""
classifier = KNeighborsClassifier(n_neighbors=3)
classes = np.unique(y)
classes_ = classes
mask = np.zeros(y.size, dtype=bool)
classifier.fit(X, y)
for i in xrange(y.size):
sample, label = X[i], y[i]
if classifier.predict(sample) == [label]:
mask[i] = not mask[i]
X_ = np.asarray(X[mask])
y_ = np.asarray(y[mask])
reduction_ = 1.0 - float(len(y_)) / len(y)
print reduction_
开发者ID:guilhermepaiva,项目名称:mlstuffs,代码行数:22,代码来源:enn.py
示例14: __init__
def __init__(self, n_neighbors=1, alpha=0.6, max_loop=1000, threshold=0, chromosomes_count=10):
self.n_neighbors = n_neighbors
self.alpha = alpha
self.max_loop = max_loop
self.threshold = threshold
self.chromosomes_count = chromosomes_count
self.evaluations = None
self.chromosomes = None
self.best_chromosome_ac = -1
self.best_chromosome_rd = -1
self.classifier = KNeighborsClassifier(n_neighbors = n_neighbors)
开发者ID:dvro,项目名称:scikit-protopy,代码行数:14,代码来源:ssma.py
示例15: reduce_data
def reduce_data(self, X, y):
if self.classifier == None:
self.classifier = KNeighborsClassifier(n_neighbors=self.n_neighbors, algorithm='brute')
if self.classifier.n_neighbors != self.n_neighbors:
self.classifier.n_neighbors = self.n_neighbors
X, y = check_arrays(X, y, sparse_format="csr")
classes = np.unique(y)
self.classes_ = classes
self.classifier.fit(X, y)
nn_idx = self.classifier.kneighbors(X, n_neighbors=2, return_distance=False)
nn_idx = nn_idx.T[1]
mask = [nn_idx[nn_idx[index]] == index and y[index] != y[nn_idx[index]] for index in xrange(nn_idx.shape[0])]
mask = ~np.asarray(mask)
if self.keep_class != None and self.keep_class in self.classes_:
mask[y==self.keep_class] = True
self.X_ = np.asarray(X[mask])
self.y_ = np.asarray(y[mask])
self.reduction_ = 1.0 - float(len(self.y_)) / len(y)
return self.X_, self.y_
开发者ID:guilhermepaiva,项目名称:scikit-protopy,代码行数:24,代码来源:tomek_links.py
示例16: ENN
class ENN(InstanceReductionMixin):
"""Edited Nearest Neighbors.
The Edited Nearest Neighbors removes the instances in de
boundaries, maintaining redudant samples.
Parameters
----------
n_neighbors : int, optional (default = 3)
Number of neighbors to use by default for :meth:`k_neighbors` queries.
Attributes
----------
`X_` : array-like, shape = [indeterminated, n_features]
Selected prototypes.
`y_` : array-like, shape = [indeterminated]
Labels of the selected prototypes.
`reduction_` : float, percentual of reduction.
Examples
--------
>>> from protopy.selection.enn import ENN
>>> import numpy as np
>>> X = np.array([[-1, 0], [-0.8, 1], [-0.8, -1], [-0.5, 0] , [0.5, 0], [1, 0], [0.8, 1], [0.8, -1]])
>>> y = np.array([1, 1, 1, 2, 1, 2, 2, 2])
>>> editednn = ENN()
>>> editednn.fit(X, y)
ENN(n_neighbors=3)
>>> print(editednn.predict([[-0.6, 0.6]]))
[1]
>>> print editednn.reduction_
0.75
See also
--------
sklearn.neighbors.KNeighborsClassifier: nearest neighbors classifier
References
----------
Ruiqin Chang, Zheng Pei, and Chao Zhang. A modified editing k-nearest
neighbor rule. JCP, 6(7):1493–1500, 2011.
"""
def __init__(self, n_neighbors=3):
self.n_neighbors = n_neighbors
self.classifier = None
def reduce_data(self, X, y):
if self.classifier == None:
self.classifier = KNeighborsClassifier(n_neighbors=self.n_neighbors)
if self.classifier.n_neighbors != self.n_neighbors:
self.classifier.n_neighbors = self.n_neighbors
X, y = check_arrays(X, y, sparse_format="csr")
classes = np.unique(y)
self.classes_ = classes
if self.n_neighbors >= len(X):
self.X_ = np.array(X)
self.y_ = np.array(y)
self.reduction_ = 0.0
mask = np.zeros(y.size, dtype=bool)
tmp_m = np.ones(y.size, dtype=bool)
for i in xrange(y.size):
tmp_m[i] = not tmp_m[i]
self.classifier.fit(X[tmp_m], y[tmp_m])
sample, label = X[i], y[i]
if self.classifier.predict(sample) == [label]:
mask[i] = not mask[i]
tmp_m[i] = not tmp_m[i]
self.X_ = np.asarray(X[mask])
self.y_ = np.asarray(y[mask])
self.reduction_ = 1.0 - float(len(self.y_)) / len(y)
return self.X_, self.y_
开发者ID:viisar,项目名称:scikit-protopy,代码行数:85,代码来源:enn.py
示例17: SGP2
class SGP2(SGP):
"""Self-Generating Prototypes 2
The Self-Generating Prototypes 2 is the second version of the
Self-Generating Prototypes algorithm.
It has a higher generalization power, including the procedures
merge and pruning.
Parameters
----------
r_min: float, optional (default = 0.0)
Determine the minimum size of a cluster [0.00, 0.20]
r_mis: float, optional (default = 0.0)
Determine the error tolerance before split a group
Attributes
----------
`X_` : array-like, shape = [indeterminated, n_features]
Selected prototypes.
`y_` : array-like, shape = [indeterminated]
Labels of the selected prototypes.
`reduction_` : float, percentual of reduction.
Examples
--------
>>> from protopy.generation.sgp import SGP2
>>> import numpy as np
>>> X = [np.asarray(range(1,13)) + np.asarray([0.1,0,-0.1,0.1,0,-0.1,0.1,-0.1,0.1,-0.1,0.1,-0.1])]
>>> X = np.asarray(X).T
>>> y = np.array([1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 1, 1])
>>> sgp2 = SGP2()
>>> sgp2.fit(X, y)
SGP2(r_min=0.0, r_mis=0.0)
>>> print sgp2.reduction_
0.5
See also
--------
protopy.generation.SGP: self-generating prototypes
protopy.generation.sgp.ASGP: adaptive self-generating prototypes
References
----------
Hatem A. Fayed, Sherif R Hashem, and Amir F Atiya. Self-generating prototypes
for pattern classification. Pattern Recognition, 40(5):1498–1509, 2007.
"""
def __init__(self, r_min=0.0, r_mis=0.0):
self.groups = None
self.r_min = r_min
self.r_mis = r_mis
self.n_neighbors = 1
self.classifier = None
self.groups = None
def reduce_data(self, X, y):
X, y = check_X_y(X, y, accept_sparse="csr")
if self.classifier == None:
self.classifier = KNeighborsClassifier(n_neighbors=self.n_neighbors)
if self.classifier.n_neighbors != self.n_neighbors:
self.classifier.n_neighbors = self.n_neighbors
classes = np.unique(y)
self.classes_ = classes
# loading inicial groups
self.groups = []
for label in classes:
mask = y == label
self.groups = self.groups + [_Group(X[mask], label)]
self._main_loop()
self._generalization_step()
self._merge()
self._pruning()
self.X_ = np.asarray([g.rep_x for g in self.groups])
self.y_ = np.asarray([g.label for g in self.groups])
self.reduction_ = 1.0 - float(len(self.y_))/len(y)
return self.X_, self.y_
def _merge(self):
if len(self.groups) < 2:
return self.groups
merged = False
for group in self.groups:
reps_x = np.asarray([g.rep_x for g in self.groups])
reps_y = np.asarray([g.label for g in self.groups])
self.classifier.fit(reps_x, reps_y)
nn2_idx = self.classifier.kneighbors(group.X, n_neighbors=2, return_distance=False)
nn2_idx = nn2_idx.T[1]
# could use a threshold
#.........这里部分代码省略.........
开发者ID:antonlarin,项目名称:scikit-protopy,代码行数:101,代码来源:sgp.py
示例18: nearest_fit
def nearest_fit(X,y):
clf = KNeighborsClassifier(7, 'distance')
return clf.fit(X, y)
开发者ID:abhi-shek,项目名称:Comment-Classification,代码行数:3,代码来源:Classification.py
示例19: knn_score
def knn_score(X, y, neighbors):
knn5 = KNeighborsClassifier(n_neighbors=neighbors)
knn5.fit(X, y)
y_pred = knn5.predict(X)
print "KNN{} accuracy_score: {}".format(neighbors,
metrics.accuracy_score(y, y_pred))
开发者ID:laurogama,项目名称:mlpython,代码行数:6,代码来源:main.py
示例20: SSMA
class SSMA(InstanceReductionMixin):
"""Steady State Memetic Algorithm
The Steady-State Memetic Algorithm is an evolutionary prototype
selection algorithm. It uses a memetic algorithm in order to
perform a local search in the code.
Parameters
----------
n_neighbors : int, optional (default = 3)
Number of neighbors to use by default for :meth:`k_neighbors` queries.
alpha : float (default = 0.6)
Parameter that ponderates the fitness function.
max_loop : int (default = 1000)
Number of maximum loops performed by the algorithm.
threshold : int (default = 0)
Threshold that regulates the substitution condition;
chromosomes_count: int (default = 10)
number of chromosomes used to find the optimal solution.
Attributes
----------
`X_` : array-like, shape = [indeterminated, n_features]
Selected prototypes.
`y_` : array-like, shape = [indeterminated]
Labels of the selected prototypes.
`reduction_` : float, percentual of reduction.
Examples
--------
>>> from protopy.selection.ssma import SSMA
>>> import numpy as np
>>> X = np.array([[i] for i in range(100)])
>>> y = np.asarray(50 * [0] + 50 * [1])
>>> ssma = SSMA()
>>> ssma.fit(X, y)
SSMA(alpha=0.6, chromosomes_count=10, max_loop=1000, threshold=0)
>>> print ssma.predict([[40],[60]])
[0 1]
>>> print ssma.reduction_
0.98
See also
--------
sklearn.neighbors.KNeighborsClassifier: nearest neighbors classifier
References
----------
Joaquín Derrac, Salvador García, and Francisco Herrera. Stratified prototype
selection based on a steady-state memetic algorithm: a study of scalability.
Memetic Computing, 2(3):183–199, 2010.
"""
def __init__(self, n_neighbors=1, alpha=0.6, max_loop=1000, threshold=0, chromosomes_count=10):
self.n_neighbors = n_neighbors
self.alpha = alpha
self.max_loop = max_loop
self.threshold = threshold
self.chromosomes_count = chromosomes_count
self.evaluations = None
self.chromosomes = None
self.best_chromosome_ac = -1
self.best_chromosome_rd = -1
self.classifier = KNeighborsClassifier(n_neighbors = n_neighbors)
def accuracy(self, chromosome, X, y):
mask = np.asarray(chromosome, dtype=bool)
cX, cy = X[mask], y[mask]
#print len(cX), len(cy), sum(chromosome)
self.classifier.fit(cX, cy)
labels = self.classifier.predict(X)
accuracy = (labels == y).sum()
return float(accuracy)/len(y)
def fitness(self, chromosome, X, y):
#TODO add the possibility of use AUC for factor1
ac = self.accuracy(chromosome, X, y)
rd = 1.0 - (float(sum(chromosome))/len(chromosome))
return self.alpha * ac + (1.0 - self.alpha) * rd
def fitness_gain(self, gain, n):
return self.alpha * (float(gain)/n) + (1 - self.alpha) * (1.0 / n)
def update_threshold(self, X, y):
#.........这里部分代码省略.........
开发者ID:dvro,项目名称:scikit-protopy,代码行数:101,代码来源:ssma.py
注:本文中的sklearn.neighbors.classification.KNeighborsClassifier类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论