Classification¶
This module supports following supervised algorithms:
- Abundance Classification
- Normalized Cross Correlation (NormXCorr)
- Spectral Angle Mapper (SAM)
- Spectral Information Divergence (SID)
- Suppot Vector Supervised Classification (SVC)
For each of these classifiers, three different plotting methods are availables. For NormXCorr, SAM and SID, the fusions of classification maps use a best score win approach.
See also
See the file test_cls.py
for an example and for SVC see test_SVC.py
.
Apart from these supervised algorithms, following unsupervised algorithm is also supported:
See also
See the file test_kmeans.py
for an example.
Abundance Classification¶
-
class
pysptools.classification.
AbundanceClassification
[source]¶ Classify abundance maps.
-
classify
(M, threshold=0.1)¶ Use a group of abundance maps generated by UCLS, NNLS or FCLS, to construct a classification map.
Parameters: - amaps – numpy array A HSI cube (m x n x p).
- threshold –
float [default 0.1] or list * If float, threshold is applied on all the spectra. * If a list, individual threshold is applied on each
spectrum, in this case the list must have the same number of threshold values than the number of spectra.- Threshold have values between 0.0 and 1.0.
- Returns: numpy array
- A class map (m x n x 1).
-
display
(labels=None, mask=None, interpolation='none', colorMap='Accent', suffix=None)¶ Display the class map to a IPython Notebook.
Parameters: - labels – list of string [default None] The legend labels. Can be used only if the input spectral library E have more than 1 pixel.
- mask – numpy array [default None] A binary mask, when True the corresponding pixel is displayed.
- interpolation – string [default none] A matplotlib interpolation method.
- colorMap – string [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
- suffix – string [default None] Add a suffix to the title.
-
plot
(path, labels=None, mask=None, interpolation='none', colorMap='Accent', suffix=None)¶ Plot the class map.
Parameters: - path – string The path where to put the plot.
- labels – list of string [default None] The legend labels. Can be used only if the input spectral library E have more than 1 pixel.
- mask – numpy array [default None] A binary mask, when True the corresponding pixel is displayed.
- interpolation – string [default none] A matplotlib interpolation method.
- colorMap – string [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
- suffix – string [default None] Add a suffix to the file name.
-
Normalized Cross Correlation (NormXCorr)¶
-
class
pysptools.classification.
NormXCorr
[source]¶ Classify a HSI cube using the normalized cross correlation algorithm and a spectral library.
-
classify
(M, E, threshold=0.1, mask=None)¶ Classify the HSI cube M with the spectral library E.
Parameters: - M – numpy array A HSI cube (m x n x p).
- E – numpy array A spectral library (N x p).
- threshold –
float [default 0.1] or list * If float, threshold is applied on all the spectra. * If a list, individual threshold is applied on each
spectrum, in this case the list must have the same number of threshold values than the number of spectra.- Threshold have values between 0.0 and 1.0.
- mask – numpy array [default None] A binary mask, when True the selected pixel is classified.
- Returns: numpy array
- A class map (m x n x 1).
-
display
(labels=None, mask=None, interpolation='none', colorMap='Accent', suffix=None)¶ Display the class map to a IPython Notebook.
Parameters: - labels – list of string [default None] The legend labels. Can be used only if the input spectral library E have more than 1 pixel.
- mask – numpy array [default None] A binary mask, when True the corresponding pixel is displayed.
- interpolation – string [default none] A matplotlib interpolation method.
- colorMap – string [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
- suffix – string [default None] Add a suffix to the title.
-
display_single_map
(lib_idx, constrained=True, stretch=False, colorMap='spectral', suffix=None)¶ Display individual classified map to a IPython Notebook. One for each spectrum. Note that each individual map is constrained by the others. This function is usefull to see the individual map that compose the final class map returned by the classify method. It help to define the spectra library. See the constrained parameter below.
Parameters: - lib_idx – int or string * A number between 1 and the number of spectra in the library. * ‘all’, plot all the individual maps.
- constrained –
boolean [default True] * If constrained is True, print the individual maps as they compose the
final class map. Any potential intersection is removed in favor of the lower value level for SAM and SID, or the nearest to 1 for NormXCorr. Use this one to understand the final class map.- If constrained is False, print the individual maps without intersection removed, as they are generated. Use this one to have the real match.
- stretch – boolean [default False] Stretch the map between 0 and 1 giving a good distribution of the color map.
- colorMap – string [default ‘spectral’] A matplotlib color map.
- suffix – string [default None] Add a suffix to the title.
-
get_NormXCorr_map
()¶ - Returns: numpy array
- The NormXCorr array (m x n x spectra number).
-
get_single_map
(lib_idx, constrained=True)¶ Get individual classified map. See plot_single_map for a description.
Parameters: - lib_idx – int or string A number between 1 and the number of spectra in the library.
- constrained – boolean [default True] See plot_single_map for a description.
- Returns: numpy array
- The individual map (m x n x 1) associated to the lib_idx endmember.
-
plot
(path, labels=None, mask=None, interpolation='none', colorMap='Accent', suffix=None)¶ Plot the class map.
Parameters: - path – string The path where to put the plot.
- labels – list of string [default None] The legend labels. Can be used only if the input spectral library E have more than 1 pixel.
- mask – numpy array [default None] A binary mask, when True the corresponding pixel is displayed.
- interpolation – string [default none] A matplotlib interpolation method.
- colorMap – string [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
- suffix – string [default None] Add a suffix to the file name.
-
plot_histo
(path, suffix=None)¶ Plot the histogram.
Parameters: - path – string The path where to put the plot.
- suffix – string [default None] Add a suffix to the file name.
-
plot_single_map
(path, lib_idx, constrained=True, stretch=False, colorMap='spectral', suffix=None)¶ Plot individual classified map. One for each spectrum. Note that each individual map is constrained by the others. This function is usefull to see the individual map that compose the final class map returned by the classify method. It help to define the spectra library. See the constrained parameter below.
Parameters: - path – string The path where to put the plot.
- lib_idx – int or string * A number between 1 and the number of spectra in the library. * ‘all’, plot all the individual maps.
- constrained –
boolean [default True] * If constrained is True, print the individual maps as they compose the
final class map. Any potential intersection is removed in favor of the lower value level for SAM and SID, or the nearest to 1 for NormXCorr. Use this one to understand the final class map.- If constrained is False, print the individual maps without intersection removed, as they are generated. Use this one to have the real match.
- stretch – boolean [default False] Stretch the map between 0 and 1 giving a good distribution of the color map.
- colorMap – string [default ‘spectral’] A matplotlib color map.
- suffix – string [default None] Add a suffix to the file name.
-
Spectral Angle Mapper (SAM)¶
-
class
pysptools.classification.
SAM
[source]¶ Classify a HSI cube using the spectral angle mapper algorithm and a spectral library.
-
classify
(M, E, threshold=0.1, mask=None)¶ Classify the HSI cube M with the spectral library E.
Parameters: - M – numpy array A HSI cube (m x n x p).
- E – numpy array A spectral library (N x p).
- threshold –
float [default 0.1] or list * If float, threshold is applied on all the spectra. * If a list, individual threshold is applied on each
spectrum, in this case the list must have the same number of threshold values than the number of spectra.- Threshold have values between 0.0 and 1.0.
- mask – numpy array [default None] A binary mask, when True the selected pixel is classified.
- Returns: numpy array
- A class map (m x n x 1).
-
display
(labels=None, mask=None, interpolation='none', colorMap='Accent', suffix=None)¶ Display the class map to a IPython Notebook.
Parameters: - labels – list of string [default None] The legend labels. Can be used only if the input spectral library E have more than 1 pixel.
- mask – numpy array [default None] A binary mask, when True the corresponding pixel is displayed.
- interpolation – string [default none] A matplotlib interpolation method.
- colorMap – string [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
- suffix – string [default None] Add a suffix to the title.
-
display_single_map
(lib_idx, constrained=True, stretch=False, colorMap='spectral', suffix=None)¶ Display individual classified map to a IPython Notebook. One for each spectrum. Note that each individual map is constrained by the others. This function is usefull to see the individual map that compose the final class map returned by the classify method. It help to define the spectra library. See the constrained parameter below.
Parameters: - lib_idx – int or string * A number between 1 and the number of spectra in the library. * ‘all’, plot all the individual maps.
- constrained –
boolean [default True] * If constrained is True, print the individual maps as they compose the
final class map. Any potential intersection is removed in favor of the lower value level for SAM and SID, or the nearest to 1 for NormXCorr. Use this one to understand the final class map.- If constrained is False, print the individual maps without intersection removed, as they are generated. Use this one to have the real match.
- stretch – boolean [default False] Stretch the map between 0 and 1 giving a good distribution of the color map.
- colorMap – string [default ‘spectral’] A matplotlib color map.
- suffix – string [default None] Add a suffix to the title.
-
get_angles_map
()¶ - Returns: numpy array
- The angles array (m x n x spectra number).
-
get_angles_stats
()¶ - Returns: dic
- Angles stats.
-
get_single_map
(lib_idx, constrained=True)¶ Get individual classified map. See plot_single_map for a description.
Parameters: - lib_idx – int or string A number between 1 and the number of spectra in the library.
- constrained – boolean [default True] See plot_single_map for a description.
- Returns: numpy array
- The individual map (m x n x 1) associated to the lib_idx endmember.
-
plot
(path, labels=None, mask=None, interpolation='none', colorMap='Accent', suffix=None)¶ Plot the class map.
Parameters: - path – string The path where to put the plot.
- labels – list of string [default None] The legend labels. Can be used only if the input spectral library E have more than 1 pixel.
- mask – numpy array [default None] A binary mask, when True the corresponding pixel is displayed.
- interpolation – string [default none] A matplotlib interpolation method.
- colorMap – string [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
- suffix – string [default None] Add a suffix to the file name.
-
plot_histo
(path, suffix=None)¶ Plot the histogram.
Parameters: - path – string The path where to put the plot.
- suffix – string [default None] Add a suffix to the file name.
-
plot_single_map
(path, lib_idx, constrained=True, stretch=False, colorMap='spectral', suffix=None)¶ Plot individual classified map. One for each spectrum. Note that each individual map is constrained by the others. This function is usefull to see the individual map that compose the final class map returned by the classify method. It help to define the spectra library. See the constrained parameter below.
Parameters: - path – string The path where to put the plot.
- lib_idx – int or string * A number between 1 and the number of spectra in the library. * ‘all’, plot all the individual maps.
- constrained –
boolean [default True] * If constrained is True, print the individual maps as they compose the
final class map. Any potential intersection is removed in favor of the lower value level for SAM and SID, or the nearest to 1 for NormXCorr. Use this one to understand the final class map.- If constrained is False, print the individual maps without intersection removed, as they are generated. Use this one to have the real match.
- stretch – boolean [default False] Stretch the map between 0 and 1 giving a good distribution of the color map.
- colorMap – string [default ‘spectral’] A matplotlib color map.
- suffix – string [default None] Add a suffix to the file name.
-
Spectral Information Divergence (SID)¶
-
class
pysptools.classification.
SID
[source]¶ Classify a HSI cube using the spectral information divergence algorithm and a spectral library.
-
classify
(M, E, threshold=0.1, mask=None)¶ Classify the HSI cube M with the spectral library E.
Parameters: - M – numpy array A HSI cube (m x n x p).
- E – numpy array A spectral library (N x p).
- threshold –
float [default 0.1] or list * If float, threshold is applied on all the spectra. * If a list, individual threshold is applied on each
spectrum, in this case the list must have the same number of threshold values than the number of spectra.- Threshold have values between 0.0 and 1.0.
- mask – numpy array [default None] A binary mask, when True the selected pixel is classified.
- Returns: numpy array
- A class map (m x n x 1).
-
display
(labels=None, mask=None, interpolation='none', colorMap='Accent', suffix=None)¶ Display the class map to a IPython Notebook.
Parameters: - labels – list of string [default None] The legend labels. Can be used only if the input spectral library E have more than 1 pixel.
- mask – numpy array [default None] A binary mask, when True the corresponding pixel is displayed.
- interpolation – string [default none] A matplotlib interpolation method.
- colorMap – string [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
- suffix – string [default None] Add a suffix to the title.
-
display_single_map
(lib_idx, constrained=True, stretch=False, colorMap='spectral', suffix=None)¶ Display individual classified map to a IPython Notebook. One for each spectrum. Note that each individual map is constrained by the others. This function is usefull to see the individual map that compose the final class map returned by the classify method. It help to define the spectra library. See the constrained parameter below.
Parameters: - lib_idx – int or string * A number between 1 and the number of spectra in the library. * ‘all’, plot all the individual maps.
- constrained –
boolean [default True] * If constrained is True, print the individual maps as they compose the
final class map. Any potential intersection is removed in favor of the lower value level for SAM and SID, or the nearest to 1 for NormXCorr. Use this one to understand the final class map.- If constrained is False, print the individual maps without intersection removed, as they are generated. Use this one to have the real match.
- stretch – boolean [default False] Stretch the map between 0 and 1 giving a good distribution of the color map.
- colorMap – string [default ‘spectral’] A matplotlib color map.
- suffix – string [default None] Add a suffix to the title.
-
get_SID_map
()¶ - Returns: numpy array
- The SID array (m x n x spectra number).
-
get_single_map
(lib_idx, constrained=True)¶ Get individual classified map. See plot_single_map for a description.
Parameters: - lib_idx – int or string A number between 1 and the number of spectra in the library.
- constrained – boolean [default True] See plot_single_map for a description.
- Returns: numpy array
- The individual map (m x n x 1) associated to the lib_idx endmember.
-
plot
(path, labels=None, mask=None, interpolation='none', colorMap='Accent', suffix=None)¶ Plot the class map.
Parameters: - path – string The path where to put the plot.
- labels – list of string [default None] The legend labels. Can be used only if the input spectral library E have more than 1 pixel.
- mask – numpy array [default None] A binary mask, when True the corresponding pixel is displayed.
- interpolation – string [default none] A matplotlib interpolation method.
- colorMap – string [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
- suffix – string [default None] Add a suffix to the file name.
-
plot_histo
(path, suffix=None)¶ Plot the histogram.
Parameters: - path – string The path where to put the plot.
- suffix – string [default None] Add a suffix to the file name.
-
plot_single_map
(path, lib_idx, constrained=True, stretch=False, colorMap='spectral', suffix=None)¶ Plot individual classified map. One for each spectrum. Note that each individual map is constrained by the others. This function is usefull to see the individual map that compose the final class map returned by the classify method. It help to define the spectra library. See the constrained parameter below.
Parameters: - path – string The path where to put the plot.
- lib_idx – int or string * A number between 1 and the number of spectra in the library. * ‘all’, plot all the individual maps.
- constrained –
boolean [default True] * If constrained is True, print the individual maps as they compose the
final class map. Any potential intersection is removed in favor of the lower value level for SAM and SID, or the nearest to 1 for NormXCorr. Use this one to understand the final class map.- If constrained is False, print the individual maps without intersection removed, as they are generated. Use this one to have the real match.
- stretch – boolean [default False] Stretch the map between 0 and 1 giving a good distribution of the color map.
- colorMap – string [default ‘spectral’] A matplotlib color map.
- suffix – string [default None] Add a suffix to the file name.
-
Suppot Vector Supervised Classification (SVC)¶
see test_SVC.py
for an example
-
class
pysptools.classification.
SVC
[source]¶ Suppot Vector Supervised Classification (SVC) of a HSI cube with the use of regions of interest (ROIs).
This class is largely a wrapper to the scikit-learn SVC class. The goal is to ease the use of the scikit-learn SVM implementation when applied to hyperspectral cubes.
The ROIs classifiers can be rectangles or polygons. They must be VALID, no check is made upon the validity of these geometric figures.
-
classify
(M)¶ Classify a hyperspectral cube using the ROIs defined clusters by the fit method.
Parameters: M – numpy array A HSI cube (m x n x p). - Returns: numpy array
- A class map (m x n x 1).
-
display
(labels=None, interpolation='none', colorMap='Accent', suffix=None)¶ Display the class map.
Parameters: - labels – string list A labels list.
- interpolation – string [default none] A matplotlib interpolation method.
- colorMap – string [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
- suffix – string [default None] Add a suffix to the file name.
-
fit
(M, ROIs, class_weight=None, cache_size=200, coef0=0.0, degree=3, gamma='auto', kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False)[source]¶ Fit the HS cube M with the use of ROIs. The parameters following ‘M’ and ‘ROIs’ are the one defined by the scikit-learn sklearn.svm.SVC class.
Parameters: - M – numpy array A HSI cube (m x n x p).
- ROIs – ROIs type Regions of interest instance.
- parameters (Others) – see the sklearn.svm.SVC class parameters Note: the C parameter is set to 1, the result of this setting is that the class_weight is relative to C and that the first value of class_weight is the background. An example: you wish to fit two classes “1” and “2” with the help of one ROI for each, you declare class_weight like this: class_weight={0:1,1:10,2:10} 0: is always the background and is set to 1, 1: is the first class, 2: is the second. A value of 10 for both classes give good results.
- Returns: class
- The sklearn.svm.SVC class is returned.
-
plot
(path, labels=None, interpolation='none', colorMap='Accent', suffix=None)¶ Plot the class map.
Parameters: - path – string The path where to put the plot.
- labels – string list A labels list.
- interpolation – string [default none] A matplotlib interpolation method.
- colorMap – string [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
- suffix – string [default None] Add a suffix to the file name.
-
KMeans¶
See the file test_kmeans.py
for an example.
-
class
pysptools.classification.
KMeans
[source]¶ KMeans clustering algorithm adapted to hyperspectral imaging
-
display
(interpolation='none', colorMap='Accent', suffix=None)¶ Display the cluster map.
Parameters: - path – string The path where to put the plot.
- interpolation – string [default none] A matplotlib interpolation method.
- colorMap – string [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
- suffix – string [default None] Add a suffix to the title.
-
plot
(path, interpolation='none', colorMap='Accent', suffix=None)¶ Plot the cluster map.
Parameters: - path – string The path where to put the plot.
- interpolation – string [default none] A matplotlib interpolation method.
- colorMap – string [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
- suffix – string [default None] Add a suffix to the file name.
-
predict
(M, n_clusters=5, n_jobs=1, init='k-means++')¶ KMeans clustering algorithm adapted to hyperspectral imaging. It is a simple wrapper to the scikit-learn version.
Parameters: - M – numpy array A HSI cube (m x n x p).
- n_clusters – int [default 5] The number of clusters to generate.
- n_jobs – int [default 1] Taken from scikit-learn doc: The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel. If -1 all CPUs are used. If 1 is given, no parallel computing code is used at all, which is useful for debugging. For n_jobs below -1, (n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one are used.
- init – string or array [default ‘k-means++’] Taken from scikit-learn doc: Method for initialization, defaults to k-means++: k-means++ : selects initial cluster centers for k-mean clustering in a smart way to speed up convergence. See section Notes in k_init for more details. random: choose k observations (rows) at random from data for the initial centroids. If an ndarray is passed, it should be of shape (n_clusters, n_features) and gives the initial centers.
- Returns: numpy array
- A cluster map (m x n x c), c is the clusters number .
-
Output¶
-
class
pysptools.classification.
Output
(label)[source]¶ Add plot and display capacity to the classifiers classes.
-
display
(img, n_classes, labels=None, mask=None, interpolation='none', colorMap='jet', firstBlack=False, suffix='')[source]¶ Display a classification map using matplotlib.
Parameters: - img – numpy array A classified map, (m x n x 1), the classes start at 0.
- n_classes – int The number of classes found in img.
- labels – list of string [default None] The legend labels.
- mask – numpy array [default None] A binary mask, when True the corresponding pixel is displayed.
- interpolation – string [default none] A matplotlib interpolation method.
- colorMap – string [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
- firstBlack – bool [default False] Display the first legend element in black if True. If it is the case, the corresponding classification class value is zero and it can be use when the meaning is nothing to classify (example: a background).
- suffix – string [default None] Add a suffix to the file name.
-
plot
(img, n_classes, path=None, labels=None, mask=None, interpolation='none', colorMap='jet', firstBlack=False, suffix='')[source]¶ Plot a classification map using matplotlib.
Parameters: - img – numpy array A classified map, (m x n x 1), the classes start at 0.
- n_classes – int The number of classes found in img.
- path – string The path where to put the plot.
- labels – list of string [default None] The legend labels.
- mask – numpy array [default None] A binary mask, when True the corresponding pixel is displayed.
- interpolation – string [default none] A matplotlib interpolation method.
- colorMap – string [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
- firstBlack – bool [default False] Display the first legend element in black if True. If it is the case, the corresponding classification class value is zero and it can be use when the meaning is nothing to classify (example: a background).
- suffix – string [default None] Add a suffix to the file name.
-