Issue
I have a list of models as parameters in a YAML file. When I read them into my Python script, they appear there as strings:
my_model = "LinearRegression()"
print(type(my_model)) #output: <class 'str'>
I want to initialize my sklearn model as:
model = LinearRegression()
but with LinearRegression()
being a string variable.
At the moment, naturally, model = my_model
is a string, and can't be fitted, etc.
How to instantiate the sklearn model from a string variable?
My use case: I want to put different models in a YAML file as params in order to sweep over them in the model fine-tuning procedure (a simple AutoML).
Solution
You have few options:
- Use
eval
as a quick&dirty solution:
from sklearn.linear_model import LinearRegression
model_str = 'LinearRegression()'
model = eval(model_str)
print(model)
Note: using eval
is not a safe because it will execute any string so if you have no control about a string variable being executed a malicious code may be executed.
Also you need to import exact the same class before using eval. Without the first line the code will not work.
- use
pickle.dumps
from sklearn.linear_model import LinearRegression
model_class_serialized = pickle.dumps(LinearRegression)
print(model_class_serialized)
model_class = pickle.loads(model_class_serialized)
print(model_class)
In that case you do not need explicitly import classes from sklearn.
An example of saving models to YAML:
import pickle
import yaml
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import LinearSVC
models = [LinearRegression, RandomForestClassifier, LinearSVC]
with open('models.yml', 'w') as out_file:
yaml.dump({m.__name__: pickle.dumps(m) for m in models}, out_file)
After that you can read YAML file like that:
import pickle
import yaml
with open('models.yml', 'r') as in_file:
models = yaml.load(in_file, Loader=yaml.FullLoader)
models = {name: pickle.loads(serialized_cls) for name, serialized_cls in models.items()}
print(models)
Which produces:
{'LinearRegression': <class 'sklearn.linear_model._base.LinearRegression'>, 'LinearSVC': <class 'sklearn.svm._classes.LinearSVC'>, 'RandomForestClassifier': <class 'sklearn.ensemble._forest.RandomForestClassifier'>}
so you can easily instantiate these models:
model = models['RandomForestClassifier']()
print(model)
# model.fit(...)
Answered By - u1234x1234
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.