0

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.

0

2 Answers 2

1

You just need to use a spread:

from sklearn.neighbors import KNeighborsClassifier
def useNeighbors(iterations, *args):
    print(iterations) #normal argument
    for arg in args:
        my_dict = {}
        my_dict[arg] = 20
        KNeighborsClassifier(**my_dict)

useNeighbors(2, "n_neighbors", "leaf_size")
Sign up to request clarification or add additional context in comments.

Comments

1

If I understand what you want, you can use partials for this. Example

from functools import partial
from sklearn.neighbors import KNeighborsClassifier

    classifiers = [partial(KNeighborsClassifier, n_neighbors=20),
                   partial(KNeighborsClassifier, leaf_size=20)]
    for classifier in classifiers:
        classifier()

Here's a good explanation of using partials.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.