View Source Scholar.Cluster.AffinityPropagation (Scholar v0.2.1)
Model representing affinity propagation clustering. The first dimension
of :clusters_centers
is set to the number of samples in the dataset.
The artificial centers are filled with :infinity
values. To fillter
them out use prune
function.
Summary
Functions
Cluster the dataset using affinity propagation.
Predict the closest cluster each sample in x
belongs to.
Optionally prune clusters, indices, and labels to only valid entries.
Functions
Cluster the dataset using affinity propagation.
Options
:iterations
(pos_integer/0
) - Number of iterations of the algorithm. The default value is300
.:damping_factor
(float/0
) - Damping factor in the range [0.5, 1.0) is the extent to which the current value is maintained relative to incoming values (weighted 1 - damping). The default value is0.5
.:self_preference
- Self preference.:key
- Determines random number generation for centroid initialization. If the key is not provided, it is set toNx.Random.key(System.system_time())
.:learning_loop_unroll
(boolean/0
) - Iftrue
, the learning loop is unrolled. The default value isfalse
.
Return Values
The function returns a struct with the following parameters:
:affinity_matrix
- Affinity matrix. It is a negated squared euclidean distance of each pair of points.:clusters_centers
- Cluster centers from the initial data.:cluster_centers_indices
- Indices of cluster centers.:num_clusters
- Number of clusters.
Examples
iex> key = Nx.Random.key(42)
iex> x = Nx.tensor([[12,5,78,2], [1,-5,7,32], [-1,3,6,1], [1,-2,5,2]])
iex> Scholar.Cluster.AffinityPropagation.fit(x, key: key)
%Scholar.Cluster.AffinityPropagation{
labels: Nx.tensor([0, 3, 3, 3]),
cluster_centers_indices: Nx.tensor([0, -1, -1, 3]),
affinity_matrix: Nx.tensor(
[
[-0.0, -6162.0, -5358.0, -5499.0],
[-6162.0, -0.0, -1030.0, -913.0],
[-5358.0, -1030.0, -0.0, -31.0],
[-5499.0, -913.0, -31.0, -0.0]
]),
cluster_centers: Nx.tensor(
[
[12.0, 5.0, 78.0, 2.0],
[:infinity, :infinity, :infinity, :infinity],
[:infinity, :infinity, :infinity, :infinity],
[1.0, -2.0, 5.0, 2.0]
]
),
num_clusters: Nx.tensor(2, type: :u64)
}
Predict the closest cluster each sample in x
belongs to.
Examples
iex> key = Nx.Random.key(42)
iex> x = Nx.tensor([[12,5,78,2], [1,5,7,32], [1,3,6,1], [1,2,5,2]])
iex> model = Scholar.Cluster.AffinityPropagation.fit(x, key: key)
iex> model = Scholar.Cluster.AffinityPropagation.prune(model)
iex> Scholar.Cluster.AffinityPropagation.predict(model, Nx.tensor([[1,6,2,6], [8,3,8,2]]))
#Nx.Tensor<
s64[2]
[1, 1]
>
Optionally prune clusters, indices, and labels to only valid entries.
It returns an updated and pruned model.
Examples
iex> key = Nx.Random.key(42)
iex> x = Nx.tensor([[12,5,78,2], [1,-5,7,32], [-1,3,6,1], [1,-2,5,2]])
iex> model = Scholar.Cluster.AffinityPropagation.fit(x, key: key)
iex> Scholar.Cluster.AffinityPropagation.prune(model)
%Scholar.Cluster.AffinityPropagation{
labels: Nx.tensor([0, 1, 1, 1]),
cluster_centers_indices: Nx.tensor([0, 3]),
affinity_matrix: Nx.tensor(
[
[-0.0, -6162.0, -5358.0, -5499.0],
[-6162.0, -0.0, -1030.0, -913.0],
[-5358.0, -1030.0, -0.0, -31.0],
[-5499.0, -913.0, -31.0, -0.0]
]),
cluster_centers: Nx.tensor(
[
[12.0, 5.0, 78.0, 2.0],
[1.0, -2.0, 5.0, 2.0]
]
),
num_clusters: Nx.tensor(2, type: :u64)
}