2
$\begingroup$

How to get model attributes list (not hyper parameters passed to Estimator's class)?

For ex:

kmeans = KMeans(n_clusters=5) 
kmeans.fit(X)
kmeans.labels_ 

how to get list of the attributes like labels_ from model object (ending with _)?

$\endgroup$

4 Answers 4

2
$\begingroup$

I believe you are trying to access "labels_" before fitting the data.

from sklearn.cluster import KMeans
import numpy as np
X = np.array([[1, 2], [1, 4], [1, 0],
               [10, 2], [10, 4], [10, 0]])
kmeans = KMeans(n_clusters=2, random_state=0).fit(X)

def get_properies(model):   
  return [i for i in model.__dict__ if i.endswith(‘_’)] 

get_properies(kmeans)

['n_clusters', 'init', 'max_iter', 'tol', 'precompute_distances', 'n_init', 'verbose', 'random_state', 'copy_x', 'n_jobs', 'algorithm', 'cluster_centers_', 'labels_', 'inertia_', 'n_iter_']

$\endgroup$
0
$\begingroup$

you can find the list of an algorithm's attributes from the sklearn page for the corresponding algorithm.

For Kmeans:

Attributes

cluster_centers_ :ndarray of shape (n_clusters, n_features)
Coordinates of cluster centers. If the algorithm stops before fully converging (see tol and max_iter), these will not be consistent with labels_.

labels_ :ndarray of shape (n_samples,)
Labels of each point

inertia_ :float
Sum of squared distances of samples to their closest cluster center.

n_iter_ :int
Number of iterations run.

Reference: https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html

$\endgroup$
0
$\begingroup$

I think that your question is how to find the attributes of a model (parameters are the ones used to tune the model).

You can find the Model attributes from the Scikit-learn documentation of that model in the Attributes section.

Attributes for K-Means:

  • cluster_centers_: ndarray of shape (n_clusters, n_features) Coordinates of cluster centres. If the algorithm stops before fully converging (see tol and max_iter), these will not be consistent with labels_.

  • labels_: ndarray of shape (n_samples,) Labels of each point

  • inertia_: Sum of squared distances of samples to their closest cluster center.

  • n_iter_: The number of iterations run.

$\endgroup$
0
$\begingroup$

When ever you fit the model (let say regression )

model.fit(x,y)

To get model parameters you can try

model.params_

This gives the values of b0,b1 in y=b0+b1*x

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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