Documentation
Classes
NearestCentroid

NearestCentroid

Nearest centroid classifier.

Each class is represented by its centroid, with test samples classified to the class with the nearest centroid.

Read more in the User Guide.

Python Reference (opens in a new tab)

Constructors

constructor()

Signature

new NearestCentroid(opts?: object): NearestCentroid;

Parameters

NameTypeDescription
opts?object-
opts.metric?stringMetric to use for distance computation. See the documentation of scipy.spatial.distance (opens in a new tab) and the metrics listed in distance\_metrics for valid metric values. Note that “wminkowski”, “seuclidean” and “mahalanobis” are not supported. The centroids for the samples corresponding to each class is the point from which the sum of the distances (according to the metric) of all samples that belong to that particular class are minimized. If the "manhattan" metric is provided, this centroid is the median and for all other metrics, the centroid is now set to be the mean. Default Value 'euclidean'
opts.shrink_threshold?numberThreshold for shrinking centroids to remove features.

Returns

NearestCentroid

Defined in: generated/neighbors/NearestCentroid.ts:25 (opens in a new tab)

Properties

_isDisposed

boolean = false

Defined in: generated/neighbors/NearestCentroid.ts:23 (opens in a new tab)

_isInitialized

boolean = false

Defined in: generated/neighbors/NearestCentroid.ts:22 (opens in a new tab)

_py

PythonBridge

Defined in: generated/neighbors/NearestCentroid.ts:21 (opens in a new tab)

id

string

Defined in: generated/neighbors/NearestCentroid.ts:18 (opens in a new tab)

opts

any

Defined in: generated/neighbors/NearestCentroid.ts:19 (opens in a new tab)

Accessors

centroids_

Centroid of each class.

Signature

centroids_(): Promise<ArrayLike[]>;

Returns

Promise<ArrayLike[]>

Defined in: generated/neighbors/NearestCentroid.ts:238 (opens in a new tab)

classes_

The unique classes labels.

Signature

classes_(): Promise<any[]>;

Returns

Promise<any[]>

Defined in: generated/neighbors/NearestCentroid.ts:263 (opens in a new tab)

feature_names_in_

Names of features seen during fit. Defined only when X has feature names that are all strings.

Signature

feature_names_in_(): Promise<ArrayLike>;

Returns

Promise<ArrayLike>

Defined in: generated/neighbors/NearestCentroid.ts:313 (opens in a new tab)

n_features_in_

Number of features seen during fit.

Signature

n_features_in_(): Promise<number>;

Returns

Promise<number>

Defined in: generated/neighbors/NearestCentroid.ts:288 (opens in a new tab)

py

Signature

py(): PythonBridge;

Returns

PythonBridge

Defined in: generated/neighbors/NearestCentroid.ts:44 (opens in a new tab)

Signature

py(pythonBridge: PythonBridge): void;

Parameters

NameType
pythonBridgePythonBridge

Returns

void

Defined in: generated/neighbors/NearestCentroid.ts:48 (opens in a new tab)

Methods

dispose()

Disposes of the underlying Python resources.

Once dispose() is called, the instance is no longer usable.

Signature

dispose(): Promise<void>;

Returns

Promise<void>

Defined in: generated/neighbors/NearestCentroid.ts:97 (opens in a new tab)

fit()

Fit the NearestCentroid model according to the given training data.

Signature

fit(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeTraining vector, where n\_samples is the number of samples and n\_features is the number of features. Note that centroid shrinking cannot be used with sparse matrices.
opts.y?ArrayLikeTarget values.

Returns

Promise<any>

Defined in: generated/neighbors/NearestCentroid.ts:114 (opens in a new tab)

init()

Initializes the underlying Python resources.

This instance is not usable until the Promise returned by init() resolves.

Signature

init(py: PythonBridge): Promise<void>;

Parameters

NameType
pyPythonBridge

Returns

Promise<void>

Defined in: generated/neighbors/NearestCentroid.ts:57 (opens in a new tab)

predict()

Perform classification on an array of test vectors X.

The predicted class C for each sample in X is returned.

Signature

predict(opts: object): Promise<ArrayLike>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeTest samples.

Returns

Promise<ArrayLike>

Defined in: generated/neighbors/NearestCentroid.ts:156 (opens in a new tab)

score()

Return the mean accuracy on the given test data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Signature

score(opts: object): Promise<number>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLike[]Test samples.
opts.sample_weight?ArrayLikeSample weights.
opts.y?ArrayLikeTrue labels for X.

Returns

Promise<number>

Defined in: generated/neighbors/NearestCentroid.ts:191 (opens in a new tab)