1

Package:
scipy
8546
Exception Class:
ValueError

## Raise code

``````        return _kmeans(obs, guess, thresh=thresh)

# k_or_guess is a scalar, now verify that it's an integer
k = int(k_or_guess)
if k != k_or_guess:
raise ValueError("If k_or_guess is a scalar, it must be an integer.")
if k < 1:
raise ValueError("Asked for %d clusters." % k)

rng = check_random_state(seed)

# initialize best distance value to a large value
best_dist = np.inf
for i in range(iter):
# the initial code book is randomly selected from observations``````
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

## Ways to fix

2

The parameter `k_or_guess should be greater or equal to 1. Any less value causes an exception.`

## `Steps to reproduce the exception:`

Install scipy and numpy

```\$ pipenv install scipy numpy
```

```\$ pipenv shell
```

```from numpy import array
from scipy.cluster.vq import vq, kmeans, whiten
features  = array([[ 1.9,2.3],[ 1.5,2.5],[ 0.8,0.6],[ 0.4,1.8],[ 0.1,0.1],[ 0.2,1.8],[ 2.0,0.5],[ 0.3,1.5],[ 1.0,1.0]])
whitened = whiten(features)
kmeans(whitened,k_or_guess=-3)
```

```---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-21-80147dd11208> in <module>()  3 features = array([[ 1.9,2.3],[ 1.5,2.5],[ 0.8,0.6],[ 0.4,1.8],[ 0.1,0.1],[ 0.2,1.8],[ 2.0,0.5],[ 0.3,1.5],[ 1.0,1.0]])  4 whitened = whiten(features) ----> 5 kmeans(whitened,k_or_guess=-3)
/usr/local/lib/python3.7/dist-packages/scipy/cluster/vq.py in kmeans(obs, k_or_guess, iter, thresh, check_finite, seed)  470 raise ValueError("If k_or_guess is a scalar, it must be an integer.")  471 if k < 1: --> 472 raise ValueError("Asked for %d clusters." % k)  473   474 rng = check_random_state(seed)
```

## Fixed version of the code:

```from numpy import array
from scipy.cluster.vq import vq, kmeans, whiten
features  = array([[ 1.9,2.3],[ 1.5,2.5],[ 0.8,0.6],[ 0.4,1.8],[ 0.1,0.1],[ 0.2,1.8],[ 2.0,0.5],[ 0.3,1.5],[ 1.0,1.0]])
whitened = whiten(features)
kmeans(whitened,k_or_guess=3)
```

```(array([[2.45121811, 3.03653641], [0.4325679 , 2.15087996], [1.40584568, 0.69587293]]), 0.5511808116483707)
```

Oct 07, 2021
kellemnegasi 31.6k