0
$\begingroup$

I want to get the two most probable labels for each sample in my X.

A little context: I am working on a clustering project where I have 1.6M samples that have to be clustered into 12 clusters. First, I did KMeans and it works fine, except that KMeans is a hard clusterer model so it only gives one exclusive label per sample. For my project, I need to get not one but the top 2 most probable labels, which is why I changed to Gaussian Mixture Model.

I was thinking of doing this by looking through the array of probabilities for each component per sample and find the 2 highest values and this way, assign the labels.

So two questions:

  1. How can I find the two highest values in an array and their location?

or.. if there is an easier way:

  1. How can I assign the top two labels to each sample?
$\endgroup$
0
$\begingroup$

You can use the predict_proba method along with numpy argsort

predict_proba(X)
Predict posterior probability of each component given the data.

import numpy as np, pandas as pd
from sklearn.mixture import GaussianMixture

X = pd.read_csv("/content/sample_data/california_housing_test.csv")

# Fit on all but the last sample
gm = GaussianMixture(n_components=12, random_state=0).fit(X.iloc[:-1,:])

# Predict for the last sample
prob = gm.predict_proba(X.iloc[-1:,:])

# Top 2
np.argsort(prob[0])[-2:]

Output - array([7, 3])

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.