本文整理汇总了Python中sklearn.neighbors.classification.KNeighborsClassifier.predict方法的典型用法代码示例。如果您正苦于以下问题:Python KNeighborsClassifier.predict方法的具体用法?Python KNeighborsClassifier.predict怎么用?Python KNeighborsClassifier.predict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.neighbors.classification.KNeighborsClassifier
的用法示例。
在下文中一共展示了KNeighborsClassifier.predict方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from sklearn.neighbors.classification import KNeighborsClassifier [as 别名]
# 或者: from sklearn.neighbors.classification.KNeighborsClassifier import predict [as 别名]
class PatchedRawModel:
def __init__(self):
self.baseModel = RawModel()
self.model49 = KNeighborsClassifier(n_neighbors=10)
self.model35 = KNeighborsClassifier(n_neighbors=10)
def fit(self, trainExamples):
self.baseModel.fit(trainExamples)
X49 = vstack ( [reshape(x.X, (1, x.WIDTH * x.HEIGHT)) for x in trainExamples if x.Y in [4, 9]] )
Y49 = [x.Y for x in trainExamples if x.Y in [4, 9]]
self.model49.fit(X49, Y49)
X35 = vstack ( [reshape(x.X, (1, x.WIDTH * x.HEIGHT)) for x in trainExamples if x.Y in [3, 5]] )
Y35 = [x.Y for x in trainExamples if x.Y in [3, 5]]
self.model35.fit(X35, Y35)
def predict(self, examples):
basePredictions = self.baseModel.predict(examples)
for (x, y, i) in zip(examples, basePredictions, range(0, len(examples))):
if y in [4, 9]:
specializedPrediction = self.model49.predict(reshape(x.X, (1, x.WIDTH * x.HEIGHT)))
if specializedPrediction != y:
basePredictions[i] = specializedPrediction
elif y in [3, 5]:
specializedPrediction = self.model35.predict(reshape(x.X, (1, x.WIDTH * x.HEIGHT)))
if specializedPrediction != y:
basePredictions[i] = specializedPrediction
return basePredictions
示例2: compute_cnn
# 需要导入模块: from sklearn.neighbors.classification import KNeighborsClassifier [as 别名]
# 或者: from sklearn.neighbors.classification.KNeighborsClassifier import predict [as 别名]
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_
示例3: evaluate
# 需要导入模块: from sklearn.neighbors.classification import KNeighborsClassifier [as 别名]
# 或者: from sklearn.neighbors.classification.KNeighborsClassifier import predict [as 别名]
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)
示例4: compute_enn
# 需要导入模块: from sklearn.neighbors.classification import KNeighborsClassifier [as 别名]
# 或者: from sklearn.neighbors.classification.KNeighborsClassifier import predict [as 别名]
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_
示例5: ENN
# 需要导入模块: from sklearn.neighbors.classification import KNeighborsClassifier [as 别名]
# 或者: from sklearn.neighbors.classification.KNeighborsClassifier import predict [as 别名]
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_
示例6: knn_score
# 需要导入模块: from sklearn.neighbors.classification import KNeighborsClassifier [as 别名]
# 或者: from sklearn.neighbors.classification.KNeighborsClassifier import predict [as 别名]
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))
示例7: CNN
# 需要导入模块: from sklearn.neighbors.classification import KNeighborsClassifier [as 别名]
# 或者: from sklearn.neighbors.classification.KNeighborsClassifier import predict [as 别名]
class CNN(InstanceReductionMixin):
"""Condensed Nearest Neighbors.
Each class is represented by a set of prototypes, with test samples
classified to the class with the nearest prototype.
The Condensed Nearest Neighbors removes the redundant instances,
maintaining the samples in the decision boundaries.
Parameters
----------
n_neighbors : int, optional (default = 1)
Number of neighbors to use by default for :meth:`k_neighbors` queries.
Attributes
----------
`prototypes_` : array-like, shape = [indeterminated, n_features]
Selected prototypes.
`labels_` : array-like, shape = [indeterminated]
Labels of the selected prototypes.
`reduction_` : float, percentual of reduction.
Examples
--------
>>> from protopy.selection.cnn import CNN
>>> import numpy as np
>>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
>>> y = np.array([1, 1, 1, 2, 2, 2])
>>> cnn = CNN()
>>> cnn.fit(X, y)
CNN(n_neighbors=1)
>>> print(cnn.predict([[-0.8, -1]]))
[1]
See also
--------
sklearn.neighbors.KNeighborsClassifier: nearest neighbors classifier
Notes
-----
The Condensed Nearest Neighbor is one the first prototype selection
technique in literature.
References
----------
P. E. Hart, The condensed nearest neighbor rule, IEEE Transactions on
Information Theory 14 (1968) 515–516.
"""
def __init__(self, n_neighbors=1):
self.n_neighbors = n_neighbors
self.classifier = 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)
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_
示例8: InstanceReductionMixin
# 需要导入模块: from sklearn.neighbors.classification import KNeighborsClassifier [as 别名]
# 或者: from sklearn.neighbors.classification.KNeighborsClassifier import predict [as 别名]
class InstanceReductionMixin(InstanceReductionBase, ClassifierMixin):
"""Mixin class for all instance reduction techniques"""
def set_classifier(self):
"""Sets the classified to be used in the instance reduction process
and classification.
Parameters
----------
classifier : classifier, following the KNeighborsClassifier style
(default = KNN)
y : array-like, shape = [n_samples]
Labels for X.
Returns
-------
P : array-like, shape = [indeterminated, n_features]
Resulting training set.
q : array-like, shape = [indertaminated]
Labels for P
"""
self.classifier = classifier
def reduce_data(self, X, y):
"""Perform the instance reduction procedure on the given training data.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Training set.0
y : array-like, shape = [n_samples]
Labels for X.
Returns
-------
X_ : array-like, shape = [indeterminated, n_features]
Resulting training set.
y_ : array-like, shape = [indertaminated]
Labels for X_
"""
pass
def get_prototypes(self):
return self.X_, self.y_
def fit(self, X, y, reduce_data=True):
"""
Fit the InstanceReduction model according to the given training data.
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Training vector, where n_samples in the number of samples and
n_features is the number of features.
Note that centroid shrinking cannot be used with sparse matrices.
y : array, shape = [n_samples]
Target values (integers)
reduce_data : bool, flag indicating if the reduction would be performed
"""
self.X = X
self.y = y
self.labels = set(y)
self.prototypes = None
self.prototypes_labels = None
self.reduction_ratio = 0.0
if reduce_data:
self.reduce_data(X, y)
return self
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)
#.........这里部分代码省略.........
示例9: SSMA
# 需要导入模块: from sklearn.neighbors.classification import KNeighborsClassifier [as 别名]
# 或者: from sklearn.neighbors.classification.KNeighborsClassifier import predict [as 别名]
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):
#.........这里部分代码省略.........