NPEET icon indicating copy to clipboard operation
NPEET copied to clipboard

ValueError: not enough values to unpack (expected 2, got 1) in calculating the micd

Open Angela446-lgtm opened this issue 3 years ago • 9 comments

I observed this ValueError: not enough values to unpack (expected 2, got 1) when I tried to calculate the mutual info between a continuous and a discrete. Can anybody help me?

import npeet.entropy_estimators as ee ee.micd(cont.iloc[:,1].values.tolist(),disc.iloc[:,[1]].values.tolist()))

Angela446-lgtm avatar Sep 28 '21 09:09 Angela446-lgtm

It could be a question of which quantities it expects to be lists of vectors, and which not. I'd try modifying where the brackets are, e.g. this: ee.micd(cont.iloc[:,[1]].values.tolist(),disc.iloc[:,1].values.tolist())) That way, the continuous one is a (n_samples, 1) and the discrete on is just (n_samples,). I think that's right. If it doesn't work, print out the dimensions of cont and disc, and I'll think about it a little more.

gregversteeg avatar Sep 28 '21 14:09 gregversteeg

(I'm pretty sure discrete expects just a single discrete quantity, not a vector, but continuous does expect a vector.)

gregversteeg avatar Sep 28 '21 14:09 gregversteeg

Yes!it works!thank you!

Angela446-lgtm avatar Oct 06 '21 12:10 Angela446-lgtm

(I'm pretty sure discrete expects just a single discrete quantity, not a vector, but continuous does expect a vector.)

Can you please provide a working example for micd? I can use one for mi:

x = [[1.3], [3.7], [5.1], [2.4], [3.4]]
y = [[1.5], [3.32], [5.3], [2.3], [3.3]]
ee.mi(x, y)

0.16831442143704642

Now, according to your recommendations:

x = [[1.3], [3.7], [5.1], [2.4], [3.4]] y = [5, 3, 5, 2, 3] ee.micd(x, y)

C:\ProgramData\Anaconda3\lib\site-packages\npeet\entropy_estimators.py in micd(x, y, k, base, warning) 223 entropy_x_given_y = 0.0 224 for yval, py in zip(y_unique, y_proba): --> 225 x_given_y = x[(y == yval).all(axis=1)] 226 if k <= len(x_given_y) - 1: 227 entropy_x_given_y += py * entropy(x_given_y, k, base)

C:\ProgramData\Anaconda3\lib\site-packages\numpy\core_methods.py in _all(a, axis, dtype, out, keepdims, where) 62 # Parsing keyword arguments is currently fairly slow, so avoid it for now 63 if where is True: ---> 64 return umr_all(a, axis, dtype, out, keepdims) 65 return umr_all(a, axis, dtype, out, keepdims, where=where) 66

AxisError: axis 1 is out of bounds for array of dimension 1

fingoldo avatar Aug 08 '23 20:08 fingoldo

Oh, how annoying! The way you called it seems more natural, but at a glance it seems like it also expects "vectors" for the discrete values y = np.array([[5], [3]...]). Also it seems like it would only work if y is a numpy array. I can't believe I didn't just put in a check, as y = np.asarray(y) would be an efficient way to avoid problems like this. Let me know if this works.

gregversteeg avatar Aug 08 '23 21:08 gregversteeg

ld only work if y is a numpy array. I can't believe I didn't just put in a check, as y = np.asarray(y) would be an efficient way to avoid

mm, then I get

x = [[1.3], [3.7], [5.1], [2.4], [3.4]]
y =np.array([[5], [3], [5], [2], [3]])
ee.micd(x, y)

TypeError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_19412\3742637936.py in 1 x = [[1.3], [3.7], [5.1], [2.4], [3.4]] 2 y =np.array([[5], [3], [5], [2], [3]]) ----> 3 ee.micd(x, y)

C:\ProgramData\Anaconda3\lib\site-packages\npeet\entropy_estimators.py in micd(x, y, k, base, warning) 223 entropy_x_given_y = 0.0 224 for yval, py in zip(y_unique, y_proba): --> 225 x_given_y = x[(y == yval).all(axis=1)] 226 if k <= len(x_given_y) - 1: 227 entropy_x_given_y += py * entropy(x_given_y, k, base)

TypeError: only integer scalar arrays can be converted to a scalar index

fingoldo avatar Aug 08 '23 21:08 fingoldo

Can you make x an np.array([]) too?

gregversteeg avatar Aug 08 '23 21:08 gregversteeg

oh wait. it's actually x that has to be a numpy array. now it works:

x = np.array([[1.3], [3.7], [5.1], [2.4], [3.4]]) y =np.array([[5], [3], [5], [2], [3]]) ee.micd(x, y)

0.0

One more question if possible: the fact that we are using list of lists implies that in npeet functions we can use m-dimensional arrays for x and y, right? so we can estimate a MI of a 3-dimensional x on 2-dimensional y and it will be supported?

fingoldo avatar Aug 08 '23 21:08 fingoldo

Yes, I think so!

gregversteeg avatar Aug 08 '23 21:08 gregversteeg