The Amazon SageMaker K-means algorithm.
KMeans(role, train_instance_count, train_instance_type, k, init_method=None, max_iterations=None, tol=None, num_trials=None, local_init_method=None, half_life_time_size=None, epochs=None, center_factor=None, **kwargs)¶
A k-means clustering
AmazonAlgorithmEstimatorBase. Finds k clusters of data in an unlabeled dataset.
This Estimator may be fit via calls to
fit(). The former allows a KMeans model to be fit on a 2-dimensional numpy array. The latter requires Amazon
Recordprotobuf serialized data to be stored in S3.
To learn more about the Amazon protobuf Record class and how to prepare bulk data in this format, please consult AWS technical documentation: https://alpha-docs-aws.amazon.com/sagemaker/latest/dg/cdf-training.html
After this Estimator is fit, model data is stored in S3. The model may be deployed to an Amazon SageMaker Endpoint by invoking
deploy(). As well as deploying an Endpoint,
KMeansPredictorobject that can be used to k-means cluster assignments, using the trained k-means model hosted in the SageMaker Endpoint.
KMeans Estimators can be configured by setting hyperparameters. The available hyperparameters for KMeans are documented below. For further information on the AWS KMeans algorithm, please consult AWS technical documentation: https://alpha-docs-aws.amazon.com/sagemaker/latest/dg/k-means.html
- role (str) – An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role, if accessing AWS resource. For more information, see <link>???.
- train_instance_count (int) – Number of Amazon EC2 instances to use for training.
- train_instance_type (str) – Type of EC2 instance to use for training, for example, ‘ml.c4.xlarge’.
- k (int) – The number of clusters to produce.
- init_method (str) – How to initialize cluster locations. One of ‘random’ or ‘kmeans++’.
- max_iterations (int) – Maximum iterations for Lloyds EM procedure in the local kmeans used in finalize stage.
- tol (int) – Tolerance for change in ssd for early stopping in local kmeans.
- num_trials (int) – Local version is run multiple times and the one with the best loss is chosen. This determines how many times.
- local_init_method (str) – Initialization method for local version. One of ‘random’, ‘kmeans++’
- half_life_time_size (int) – The points can have a decayed weight. When a point is observed its weight,
with regard to the computation of the cluster mean is 1. This weight will decay exponentially as we
observe more points. The exponent coefficient is chosen such that after observing
half_life_time_sizepoints after the mentioned point, its weight will become 1/2. If set to 0, there will be no decay.
- epochs (int) – Number of passes done over the training data.
- center_factor (int) – The algorithm will create
num_clusters * extra_center_factoras it runs and reduce the number of centers to
- **kwargs – base class keyword argument values.
Delete an Amazon SageMaker
ValueError– If the endpoint does not exist.
deploy(initial_instance_count, instance_type, endpoint_name=None, **kwargs)¶
Deploy the trained model to an Amazon SageMaker endpoint and return a
- initial_instance_count (int) – Minimum number of EC2 instances to deploy to an endpoint for prediction.
- instance_type (str) – Type of EC2 instance to deploy to an endpoint for prediction, for example, ‘ml.c4.xlarge’.
- endpoint_name (str) – Name to use for creating an Amazon SageMaker endpoint. If not specified, the name of the training job is used.
- **kwargs – Passed to invocation of
create_model(). Implementations may customize
**kwargsto customize model creation during deploy. For more, see the implementation docs.
- A predictor that provides a
which can be used to send requests to the Amazon SageMaker endpoint and obtain inferences.
fit(records, mini_batch_size=None, **kwargs)¶
Fit this Estimator on serialized Record objects, stored in S3.
recordsshould be an instance of
RecordSet. This defines a collection of s3 data files to train this
Training data is expected to be encoded as dense or sparse vectors in the “values” feature on each Record. If the data is labeled, the label is expected to be encoded as a list of scalas in the “values” feature of the Record label.
More information on the Amazon Record format is available at: https://docs.aws.amazon.com/sagemaker/latest/dg/cdf-training.html
record_set()to construct a
str – The model location in S3. Only set if Estimator has been
record_set(train, labels=None, channel='train')¶
RecordSetfrom a numpy
ndarraymatrix and label vector.
For the 2D
train, each row is converted to a
Recordobject. The vector is stored in the “values” entry of the
featuresproperty of each Record. If
labelsis not None, each corresponding label is assigned to the “values” entry of the
labelsproperty of each Record.
The collection of
Recordobjects are protobuf serialized and uploaded to new S3 locations. A manifest file is generated containing the list of objects created and also stored in S3.
The number of S3 objects created is controlled by the
train_instance_countproperty on this Estimator. One S3 object is created per training instance.
- train (numpy.ndarray) – A 2D numpy array of training data.
- labels (numpy.ndarray) – A 1D numpy array of labels. Its length must be equal to the
number of rows in
- channel (str) – The SageMaker TrainingJob channel this RecordSet should be assigned to.
A RecordSet referencing the encoded, uploading training and label data.
KMeansModelreferencing the latest s3 model data produced by this Estimator.
Return the SageMaker hyperparameters for training this KMeans Estimator
KMeansModel(model_data, role, sagemaker_session=None)¶
Reference KMeans s3 model data. Calling
deploy()creates an Endpoint and return a Predictor to performs k-means cluster assignment.
Assigns input vectors to their closest cluster in a KMeans model.
The implementation of
predict()in this RealTimePredictor requires a numpy
ndarrayas input. The array should contain the same number of columns as the feature-dimension of the data used to fit the model this Predictor performs inference on.
predict()returns a list of
Recordobjects, one for each row in the input
ndarray. The nearest cluster is stored in the
closest_clusterkey of the