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')