cytopy.flow.neighbours

This module houses some two convenient functions for wrapping the Scikit-Learn implementation of K nearest neighbours classification algorithm.

Copyright 2020 Ross Burton

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Functions:

calculate_optimal_neighbours(x, y, scoring, …)

Calculate the optimal n_neighbours parameter for KNeighborsClassifier using GridSearchCV.

knn(data, labels, features, n_neighbours[, …])

Train a nearest neighbours classifier (scikit-learn implementation) and return the balanced accuracy score for both training and validation.

cytopy.flow.neighbours.calculate_optimal_neighbours(x: pandas.core.frame.DataFrame, y: numpy.array, scoring: str, **kwargs)

Calculate the optimal n_neighbours parameter for KNeighborsClassifier using GridSearchCV. Returns optimal n and highest score

Parameters
  • x (Pandas.DataFrame) –

  • y (np.array) –

  • scoring (str) –

  • kwargs (dict) –

Returns

Return type

int, float

Raises

AssertionError – Less than 5 observations provided

cytopy.flow.neighbours.knn(data: pandas.core.frame.DataFrame, labels: numpy.array, features: list, n_neighbours: int, holdout_size: float = 0.2, random_state: int = 42, return_model: bool = False, **kwargs)

Train a nearest neighbours classifier (scikit-learn implementation) and return the balanced accuracy score for both training and validation.

Parameters
  • data (Pandas.DataFrame) –

  • labels (numpy.ndarray) –

  • features (list) –

  • n_neighbours (int) –

  • holdout_size (float (default=0.2)) –

  • random_state (int (default=42)) –

  • return_model (bool (default=False)) –

  • kwargs (dict) – Keyword arguments passed to KNeighborsClassifier initialisation

Returns

Training balanced accuracy score, Validation balanced accuracy score, Classifier (if return_model is True)

Return type

(float, float) or (float, float, object)