mizzou541
mizzou541

Reputation: 35

Passing list of parameters as one string to python function

I'm trying to write one generic function to run most sklearn models that I can use to quickly explore different models in one line. The below code works if I replace leaf_size=30, n_neighbors=6 with a number. It seems to expect the first argument to be n_neighbors and requires a number. I want to be able to pass the function two pieces of information: a) the model name b) one string that contains all of the parameters I want to pass to the model.

Is there something simple I'm missing or is this not possible?

def sklearn_mod(mod_name,param_list):
    mod = mod_name(param_list)
    mod.fit(features_train, target_train)
    print(mod)
    expected = target_test
    predicted_mod = mod.predict(features_test)
    print('-----')
    print "Accuracy of Model:", accuracy_score(target_test, predicted_mod)
    print('-----')
    print(classification_report(target_test, predicted_mod))
    y_pred = predicted_mod
    y_true = expected
    print(confusion_matrix(y_true, y_pred))
    print('-----')
    print('Cross Validation:')
    scores = cross_val_score(mod, features_train, target_train, cv=10)
    print(scores)
    print"Mean CV Accuracy:",scores.mean()
    print('-----');

sklearn_mod(KNeighborsClassifier,'leaf_size=30, n_neighbors=6')

Upvotes: 0

Views: 430

Answers (1)

Alex
Alex

Reputation: 1522

You wouldn't want to pass in a csv string as your parameter, but you can use **kwargs.

Create a dict with the parameter name and value, and then pass that into your function prepended with **.

For example:

params = {'leaf_size': 30, 'n_neighbors': 6}
sklearn_mod(KNeighborsClassifier, **params)

Upvotes: 1

Related Questions