0
$\begingroup$

I have created a string array and called a method with that array along with Train, test data. The purpose of the method is to find Kfold results of each algorithm specifies in the array. Everything works fine except in cross_val_score(model,X,y), it considers the model variable as a simple string instead of a callable model. If I put it like this cross_val_score(RandomForestClassifier(),X,y) it works perfectly fine. Now, how to convert the string model to a callable model. I am new to ML and I may not be able to make you understand the problem properly. Please let me know if you have any question. Thank you.

strarray = ['RandomForestClassifier()','LogisticRegression()','SVC()']
def checkall(array,X,y,Kfold):
  for model in strarray:
    values = cross_val_score(model,X,y)
    print(values)
checkall(strarray,X,y,5)
$\endgroup$
2
  • $\begingroup$ You can simply change the values stored in the strarray to simply store either the model name or the initialized model: [RandomForestClassifier(), LogisticRegression(), SVC()]. $\endgroup$
    – Oxbowerce
    Jun 13 at 7:29
  • $\begingroup$ it worked. you can post it as answer. $\endgroup$ Jun 13 at 8:27

1 Answer 1

1
$\begingroup$

You can simply change the values stored in the strarray variable to simply store either the model name or the initialized model:

strarray = [RandomForestClassifier(), LogisticRegression(), SVC()]

def checkall(array,X,y,Kfold):
    for model in strarray:
        values = cross_val_score(model,X,y)
        print(values)

checkall(strarray,X,y,5)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.