0

I have set like that in my function

def kmean(data, n_clusters, max_iter, epsilon):
    if epsilon is None:
        epsilon = 1e-3    # minimum distance of centroid moving
    if max_iter is None:
        max_iter = 50    # maximum number of iterations
...

However, when I use it in the main:

y_pred = kmean(data = jain_cluster, n_clusters = 2)

it still show the error "kmean() missing 2 required positional arguments: 'max_iter' and 'epsilon'" how to solve it?

4daJKong
  • 1,825
  • 9
  • 21

3 Answers3

2

If you don't supply a default for a parameter, it is considered a positional parameter & is required, regardless of what you do with it in the function.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

It's because of default parameters are not given. use this-

def kmean(data, n_clusters, max_iter=50, epsilon=1e-3):

# below code not needed
    if epsilon is None:
        epsilon = 1e-3    # minimum distance of centroid moving
    if max_iter is None:
        max_iter = 50  
Pankaj Mishra
  • 445
  • 6
  • 15
0

When you define kmean like this

def kmean(data, n_clusters, max_iter, epsilon)

it expects 4 arguments. If you want to call the function like this

y_pred = kmean(data = jain_cluster, n_clusters = 2)

Then your function definition needs to look like this

def kmean(data = None, n_clusters = None, max_iter = None, epsilon = None)

But I wouldn't recommend this, so you should try this instead

def kmean(data, n_clusters, max_iter = 50, epsilon = 1e-3)

and call the function like this

y_pred = kmean(data, n_clusters) #set data and n_clusters, while max-iter is 50 and epsilon is 1e-3 by default
Luke-zhang-04
  • 782
  • 7
  • 11