# How to get model attributes in scikit learn (not hyper parameters)

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 _)?

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_']

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.


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.

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