17
правок
Изменения
Нет описания правки
== Пример использования (через scikit-learn) ==
* Делим данные на тренировочное и тестовое множество
'''from''' sklearn.model_selection '''import''' train_test_split
X_train, X_validation, y_train, y_validation = train_test_split(X, y, '''train_size'''test_size=0.12, '''random_state'''=1234) '''print'''(X_train.shape, X_validation.shape)
* Создаем классификатор
'''from''' sklearn.model_selection '''import''' GridSearchCV
tuned_params['n_neighbors'] = range(1, 30)
clf = GridSearchCV(KNeighborsClassifier(), tuned_params, cv=10, n_jobs=-1)
best_model = KNeighborsClassifier(**best_params)
best_model.fit(X_train, y_train)
predicted = best_model.predict(X_validation)
print('Evaluation:\n', metrics.classification_report(y_validation, predicted))
> '''Used params''': {'metric_params': None, 'metric': 'euclidean', 'weights': 'distance', 'n_neighbors': 23}9, 'leaf_size': 30, 'n_jobs': 4, 'p': 2, 'algorithm': 'auto'}
'''Evaluation:'''
precision recall f1-score support
== См. также ==
# [https://en.wikipedia.org/wiki/Kernel_(statistics) Функции ядер] - примеры ядер с Википедии
# [https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html sklearn] - документация по scikit-learn
# [https://www.kaggle.com/jeffbrown/knn-classifier/data kaggle example] - пример по работе с датасетом с kaggle