Pass prediction method as function argument












0















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!










share|improve this question























  • Can you edit in the expected output and try to make this a bit more minimal? (see how to make a Minimal, Complete, and Verifiable example)

    – Ethan K888
    Jan 2 at 23:39


















0















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!










share|improve this question























  • Can you edit in the expected output and try to make this a bit more minimal? (see how to make a Minimal, Complete, and Verifiable example)

    – Ethan K888
    Jan 2 at 23:39
















0












0








0








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!










share|improve this question














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!







python scikit-learn arguments predict






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 23:34









nancynancy

1




1













  • Can you edit in the expected output and try to make this a bit more minimal? (see how to make a Minimal, Complete, and Verifiable example)

    – Ethan K888
    Jan 2 at 23:39





















  • Can you edit in the expected output and try to make this a bit more minimal? (see how to make a Minimal, Complete, and Verifiable example)

    – Ethan K888
    Jan 2 at 23:39



















Can you edit in the expected output and try to make this a bit more minimal? (see how to make a Minimal, Complete, and Verifiable example)

– Ethan K888
Jan 2 at 23:39







Can you edit in the expected output and try to make this a bit more minimal? (see how to make a Minimal, Complete, and Verifiable example)

– Ethan K888
Jan 2 at 23:39














1 Answer
1






active

oldest

votes


















1














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.

  • To create an instance of that class, you must do something like 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)

  • I can therefore call inst.predict(x,y,z) but not LinearRegression.predict(x,y,z) directly.

  • If you want to call 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.






share|improve this answer


























  • I did use LinearRegression.predict and just double checked with predict_method=LogisticRegression.predict_proba, I'm getting a similar error ''LogisticRegression' object has no attribute 'predict_method''. I'm using sklearn 0.20.0. Thanks Abhinav!

    – nancy
    Jan 3 at 0:00













  • @nancy I edited my answer, see if that helps

    – user10859576
    Jan 3 at 0:22












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014566%2fpass-prediction-method-as-function-argument%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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.

  • To create an instance of that class, you must do something like 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)

  • I can therefore call inst.predict(x,y,z) but not LinearRegression.predict(x,y,z) directly.

  • If you want to call 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.






share|improve this answer


























  • I did use LinearRegression.predict and just double checked with predict_method=LogisticRegression.predict_proba, I'm getting a similar error ''LogisticRegression' object has no attribute 'predict_method''. I'm using sklearn 0.20.0. Thanks Abhinav!

    – nancy
    Jan 3 at 0:00













  • @nancy I edited my answer, see if that helps

    – user10859576
    Jan 3 at 0:22
















1














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.

  • To create an instance of that class, you must do something like 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)

  • I can therefore call inst.predict(x,y,z) but not LinearRegression.predict(x,y,z) directly.

  • If you want to call 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.






share|improve this answer


























  • I did use LinearRegression.predict and just double checked with predict_method=LogisticRegression.predict_proba, I'm getting a similar error ''LogisticRegression' object has no attribute 'predict_method''. I'm using sklearn 0.20.0. Thanks Abhinav!

    – nancy
    Jan 3 at 0:00













  • @nancy I edited my answer, see if that helps

    – user10859576
    Jan 3 at 0:22














1












1








1







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.

  • To create an instance of that class, you must do something like 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)

  • I can therefore call inst.predict(x,y,z) but not LinearRegression.predict(x,y,z) directly.

  • If you want to call 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.






share|improve this answer















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.

  • To create an instance of that class, you must do something like 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)

  • I can therefore call inst.predict(x,y,z) but not LinearRegression.predict(x,y,z) directly.

  • If you want to call 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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 19:51

























answered Jan 2 at 23:45







user10859576




















  • I did use LinearRegression.predict and just double checked with predict_method=LogisticRegression.predict_proba, I'm getting a similar error ''LogisticRegression' object has no attribute 'predict_method''. I'm using sklearn 0.20.0. Thanks Abhinav!

    – nancy
    Jan 3 at 0:00













  • @nancy I edited my answer, see if that helps

    – user10859576
    Jan 3 at 0:22



















  • I did use LinearRegression.predict and just double checked with predict_method=LogisticRegression.predict_proba, I'm getting a similar error ''LogisticRegression' object has no attribute 'predict_method''. I'm using sklearn 0.20.0. Thanks Abhinav!

    – nancy
    Jan 3 at 0:00













  • @nancy I edited my answer, see if that helps

    – user10859576
    Jan 3 at 0:22

















I did use LinearRegression.predict and just double checked with predict_method=LogisticRegression.predict_proba, I'm getting a similar error ''LogisticRegression' object has no attribute 'predict_method''. I'm using sklearn 0.20.0. Thanks Abhinav!

– nancy
Jan 3 at 0:00







I did use LinearRegression.predict and just double checked with predict_method=LogisticRegression.predict_proba, I'm getting a similar error ''LogisticRegression' object has no attribute 'predict_method''. I'm using sklearn 0.20.0. Thanks Abhinav!

– nancy
Jan 3 at 0:00















@nancy I edited my answer, see if that helps

– user10859576
Jan 3 at 0:22





@nancy I edited my answer, see if that helps

– user10859576
Jan 3 at 0:22




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014566%2fpass-prediction-method-as-function-argument%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith