someanonymousname
someanonymousname

Reputation: 3

How to predict with Affinity Propagation in Python

I am trying to train and predict with an affinity propagation model for the first time.

Here is my code:

import numpy as np
from sklearn.cluster import AffinityPropagation

data = np.array([[0.1,0.1,0.4], [0.0, 0.1, 0.5], [0.7,0.5,0.2]])

affprop = AffinityPropagation(affinity="euclidean", damping=0.7, random_state=0)
affprop.fit(data)

data_2 = np.array([[0.1,0.1], [0.0, 0.1], [0.7,0.5]])

affprop.predict(data_2)

However when I run this I get the following error:

ValueError: X has 2 features, but AffinityPropagation is expecting 3 features as input.

Am I misunderstanding how affinity propagation works with prediction? I want to be able to feed my trained model new data. Does the number of dimensions (i.e. length of each sub-list) need to be the same as the trained data?

Upvotes: 0

Views: 443

Answers (1)

Nassim Hafici
Nassim Hafici

Reputation: 460

Each element in data_2 should be of the same shape as each element in data_1 here (3,1)

Either data_2 = np.array([[0.1, 0.1, 0.1], [0.0, 0.1, 0.2], [0.7, 0.5, 0.25]]) or data_2 = np.array([[0.1,0.1, 0.1]]) would work.

You're trying to predict the cluster that your new data belongs to, so it must lies in the same n-dimensional space.

Upvotes: 1

Related Questions