# module hackathon.image_knn¶

## Short summary¶

module ensae_projects.hackathon.image_knn

Builds a knn classifier for image in order to find close images.

source on GitHub

## Classes¶

class truncated documentation
ImageNearestNeighbors Builds a model on the top of NearestNeighbors in order to find close images.

## Properties¶

property truncated documentation
_pairwise

## Methods¶

method truncated documentation
__init__
_folder2matrix Converts images stored in a folder into a matrix of features.
_get_transform Returns the associated transform function with self.transform_.
_imglist2matrix Converts a list of images into a matrix of features.
_private_kn Commun private function to handle the same kind of inputs in all transform functions.
fit Fits the model. X can be a folder.
get_image_classes Returns images classes for the given list of indices.
get_image_names Returns images names for the given list of indices.
kneighbors See NearestNeighbors, method kneighbors. Parameter X can be a file, the image is then loaded and …
kneighbors_graph See NearestNeighbors, method kneighbors_graph. Parameter X can be a file, the image is then loaded …
plot_neighbors Calls plot_gallery_images with information on the neighbors.
radius_neighbors See NearestNeighbors, method radius_neighbors. Parameter X can be a file, the image is then loaded …

## Documentation¶

@file @brief Builds a knn classifier for image in order to find close images.

class ensae_projects.hackathon.image_knn.ImageNearestNeighbors(transform='gray', size=(10, 10), **kwargs)[source]

Bases: sklearn.neighbors.unsupervised.NearestNeighbors

Builds a model on the top of NearestNeighbors in order to find close images.

@param transform function which transform every image @param size every image is zoomed to keep the same dimension @param kwargs see NearestNeighbors

__abstractmethods__ = frozenset()
__init__(transform='gray', size=(10, 10), **kwargs)[source]

@param transform function which transform every image @param size every image is zoomed to keep the same dimension @param kwargs see NearestNeighbors

_abc_impl = <_abc_data object>
_folder2matrix(folder, fLOG)[source]

Converts images stored in a folder into a matrix of features.

_get_transform()[source]

Returns the associated transform function with self.transform_.

_imglist2matrix(list_of_images, fLOG)[source]

Converts a list of images into a matrix of features.

_private_kn(method, X, *args, fLOG=None, **kwargs)[source]

Commun private function to handle the same kind of inputs in all transform functions.

@param method method to run @param X inputs, matrix, folder or list of images @param args additional positinal arguments @param fLOG logging function @param kwargs additional named arguements @return depends on method

fit(X, y=None, fLOG=None)[source]

Fits the model. X can be a folder.

@param X matrix or str for a subfolder of images @param y unused @param fLOG logging function

If X is a folder, the method relies on function @see fct enumerate_image_class. In that case, the method also creates attributes:

• image_names_: all image names
• image_classes_: subfolder the image belongs too
get_image_classes(indices)[source]

Returns images classes for the given list of indices.

@param indices indices can be a single array or a matrix. @return same shape

get_image_names(indices)[source]

Returns images names for the given list of indices.

@param indices indices can be a single array or a matrix. @return same shape

kneighbors(X=None, n_neighbors=None, return_distance=True, fLOG=None)[source]

See NearestNeighbors, method kneighbors. Parameter X can be a file, the image is then loaded and converted with the same transform. X can also be an Image from PIL.

kneighbors_graph(X=None, n_neighbors=None, mode='connectivity', fLOG=None)[source]

See NearestNeighbors, method kneighbors_graph. Parameter X can be a file, the image is then loaded and converted with the same transform. X can also be an Image from PIL.

plot_neighbors(neighbors, distances=None, obs=None, return_figure=False, format_distance='%1.2f', folder_or_images=None)[source]

Calls plot_gallery_images with information on the neighbors.

@param neighbors matrix of indices @param distances distances to display @param obs original image, if not None, will be placed

on the first row

@param return_figure returns fig, ax instead of ax @param format_distance used to format distances @param folder_or_images image paths may be relative to some folder,

in that case, they should be relative to this folder, it can also be a list of images

@return ax or fix, ax if return_figure is True

radius_neighbors(X=None, radius=None, return_distance=True, fLOG=None)[source]

See NearestNeighbors, method radius_neighbors. Parameter X can be a file, the image is then loaded and converted with the same transform. X can also be an Image from PIL.