I'm trying to write a function that runs through various iterations of classification algorithm (k-Means).
In sklearn.neighbors.KNeighborsClassifier, there are a few parameters to adjust: n_neighbors and leaf_size. I'd like to know if there is a way to specify which parameter to adjustment during a particular iteration.
from sklearn.neighbors import KNeighborsClassifier
def useNeighbors(iterations, *args):
print(iterations) #normal argument
for arg in args:
KNeighborsClassifier(arg=20)
useNeighbors(2, "n_neighbors", "leaf_size")
I want this to essentially instantiate a KNeighborsClassifer instance twice- the first time with the # of neighbors at 20, and then the second time with the leaf size at 20 (default values for # of neighbors is 5, and default leaf size is 30).
This, however, unsurprisingly yields
2
TypeError: _init_params() got an unexpected keyword argument 'arg'
It prints the iterations argument as expected, but then KNeighborsClassifer is not recognizing the string argument 'n_neighbors' as my attempt to specify which parameter to adjust.
How do I switch which parameter/argument I want to adjust across many different iterations?
Also, obviously this is a toy case- I'm asking because I'm hoping to integrate different ML classification algorithms into an ensemble package with hyperparameters tuned through a Markov Chain Monte Carlo iterative method. But in order to do that, I need to be able to specify which parameters in each algorithm take the "steps" found in the Markov Chain across each iteration.