nd.classify package

class nd.classify.Classifier(clf, feature_dims=[], scale=False)[source]

Bases: object

Parameters
  • clf (sklearn classifier) – An initialized classifier object as provided by scikit-learn. Must provide methods fit and predict.

  • feature_dims (list, optional) – A list of additional dimensions to use as features. For example, if the dataset has a 'time' dimension and 'time' is in feature_dims, every time step will be treated as an independent variable for classification purposes. Otherwise, all time steps will be treated as additional data dimensions just like 'x' and 'y'.

  • scale (bool, optional) – If True, scale the input data before clustering to zero mean and unit variance (default: False).

fit(ds, labels=None)[source]
Parameters
  • ds (xarray.Dataset) – The dataset on which to train the classifier.

  • labels (xarray.DataArray, optional) – The class labels to train the classifier. To be omitted if the classifier is unsupervised, such as KMeans.

fit_predict(ds, labels=None)[source]
make_Xy(ds, labels=None)[source]

Generate scikit-learn compatible X and y arrays from ds and labels.

Parameters
  • ds (xarray.Dataset) – The input dataset.

  • labels (xarray.DataArray) – The corresponding class labels.

Returns

X and y

Return type

tuple(np.array, np.array)

predict(ds, func='predict')[source]
Parameters
  • ds (xarray.Dataset) – The dataset for which to predict the class labels.

  • func (str, optional) – The method of the classifier to use for prediction (default: 'predict').

Returns

The predicted class labels.

Return type

xarray.DataArray

score(ds, labels=None, method='accuracy')[source]

Compute the classification score.

Parameters
  • ds (xarray.Dataset) – The dataset for which to compute the score.

  • labels (xarray.DataArray) – The corresponding true class labels.

  • method (str, optional) – The scoring method as implemented in scikit-learn (default: ‘accuracy’)

Returns

The classification score.

Return type

float

nd.classify.class_mean(ds, labels)[source]

Replace every pixel of the dataset with the mean of its corresponding class (or cluster, or segment).

Parameters
  • ds (xarray.Dataset) – The dataset.

  • labels (xarray.DataArray) – The labels indicating the class each pixel in ds belongs to. The label dimensions may be a subset of the dimensions of ds (such as ('y', 'x') for a dataset that also contains a time dimension but with time-independent class labels).

Returns

A dataset with the corresponding mean class values.

Return type

xarray.Dataset