Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain only the name of a model's object in SciKitLearn?

Tags:

I have a silly question.

I have done Cross-validation in scikit learn and would like to make a more visual information with the values ​​I got for each model.

However, I can not access only the template name to insert into the dataframe. Always comes with the parameters together. Is there some method of objects created to access only the name of the model, without its parameters. Or will I have to create an external list with the names for it?

I use:

for model in models:    scores = cross_val_score(model, X, y, cv=5)    print(f'Name model: {model} , Mean score: {scores.mean()}') 

But I obtain the name with the parameters:

Name model: LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False), Mean score: 0.8066782865537986 

In fact I want to get the information this way:

Name Model: LinearRegression, Mean Score: 0.8066782865537986 

Thanks!

like image 761
Paulo Henrique Zen Avatar asked Oct 11 '18 15:10

Paulo Henrique Zen


People also ask

What is fit () in sklearn?

The fit() method takes the training data as arguments, which can be one array in the case of unsupervised learning, or two arrays in the case of supervised learning. Note that the model is fitted using X and y , but the object holds no reference to X and y .

How do you reference scikit-learn?

Citing scikit-learn If you use scikit-learn in a scientific publication, we would appreciate citations to the following paper: Scikit-learn: Machine Learning in Python, Pedregosa et al., JMLR 12, pp. 2825-2830, 2011.


1 Answers

You can do this:

model_name = type(model).__name__ 

as in

Python 3.5.5 | packaged by conda-forge | (default, Jul 23 2018, 23:45:11)  [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from sklearn.linear_model import LinearRegression >>> model = LinearRegression() >>> model_name = type(model).__name__ >>> print(model_name) LinearRegression 
like image 62
Jonathan Avatar answered Jan 05 '23 03:01

Jonathan