Reputation: 1
I'd like to specify which prediction method to use via function argument. Something like:
from sklearn.linear_model import LinearRegression
def Process(data_y_train, data_x_train, data_x_test,
model=LinearRegression, predict_method=predict):
model_fit = model().fit(data_x_train, data_y_train)
predicted_values = model_fit.predict_method(data_x_test)
return predicted_values
Passing the model function via arugment model
(e.g., LinearRegression, LogisticRegression) works well, but I'm having trouble passing the predict method (e.g., predict, predict_proba) via argument predict_method
.
When I specify predict_method=predict
, I get an error of 'name 'predict' is not defined'; if I specify predict_method=LinearRegression.predict
, I get an error saying ''LinearRegression' object has no attribute 'predict_function''.
Per this discussion, I also tried
import sklearn.linear_model.LinearRegression
def Process(data_y_train, data_x_train, data_x_test,
model_module='sklearn.linear_model.LinearRegression',
model=LinearRegression, predict_method='predict'):
model_fit = model().fit(data_x_train, data_y_train)
predict_call = getattr(__import__(model_module), predict_method)
predicted_values = model_fit.predict_call(data_x_test)
return predicted_values
But here I get an error: No module named LinearRegression.
Thank you for your help!
Upvotes: 0
Views: 577
Reputation:
I notice that in your code, you're not using the predict_method
parameter that you passed in anywhere in your code, so I don't think what you have written is what you were trying to do.
Currently, in your code, you are storing the output of the function model().fit(data_x_train, data_y_train)
in the variable model_fit
and then calling the predict_method
attribute of that variable. If the above still doesn't work, that must be where the error is coming from, then.
I suspect what you want to do is the following:
def Process(data_y_train, data_x_train, data_x_test,
model=LinearRegression, predict_method=LinearRegression.predict):
model_instance = model() # create an instance of the class stored in the variable 'model'
model_instance.fit(data_x_train, data_y_train) # run the function 'fit' belonging to that instance
predicted_values = predict_method(model_instance,data_x_test) # run the method stored in the variable 'predict_method' - you have to pass the instance the method belongs to in the first parameter
return predicted_values
Some more information:
LinearRegression
is a class. It defines a bunch of methods, etc.inst = LinearRegression()
. The variable inst
is now an instance of the class LinearRegression
LinearRegression.predict
is an example of an instance method. This means it needs an instance to run (or can be thought of as to 'operate on' in this case)inst.predict(x,y,z)
but not LinearRegression.predict(x,y,z)
directly.LinearRegression.predict
, you have to pass in the instance in the first argument: LinearRegression.predict(inst,x,y,z)
Regarding what you tried afterwards: calling a function from a string holding the function's name is not necessary in this situation and only increases the overhead, so it's probably not the correct way to go here :)
Hope this helps.
Upvotes: 1