Clustering based on model parameters

130 Views Asked by At

I have been trying to clustering based on the SGD model parameters (Coefficient and Intercept). coef_ holds the weights w and intercept_ holds b. How can those parameters be used with clustering (KMedoids) on a group of the learned model?

import numpy as np
from sklearn import linear_model
X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
Y = np.array([1, 1, 2, 2])
clf = linear_model.SGDClassifier()
clf.fit(X, Y)

So I want to make clustering based on clf.coef_ (array([[19.47419669, 9.73709834]])) and clf.intercept_ (array([-10.])) for each learned model.

1

There are 1 best solutions below

4
On

Build your X dataset for clustering by appending the coeffs and intercept arrays every time after you train a model, ie.:

X = np.vstack((X, np.hstack((clf.coeff_, clf.intercept_))))

Once you have all your data in X feed it a KMedoids model, ie.:

from sklearn_extra.cluster import KMedoids

kmed = KMedoids(n_clusters=N).fit(X)

Note that you have specify N and you should probably test the clustering results for a number of values of N before choosing the best one based on one or more of clustering metrics.