NumSharp icon indicating copy to clipboard operation
NumSharp copied to clipboard

[Bug] np.random.choice raise Exception

Open QingtaoLi1 opened this issue 5 years ago • 7 comments

Hi, when using np.random.choice, I got an Exception with message "Specified method is not supported.", and the stack trace is:

at NumSharp.NPTypeCodeExtensions.GetAccumulatingType(NPTypeCode typeCode) at NumSharp.Backends.DefaultEngine.cumsum_elementwise(NDArray& arr, Nullable1 typeCode) at NumSharp.Backends.DefaultEngine.ReduceCumAdd(NDArray& arr, Nullable1 axis_, Nullable1 typeCode) at NumSharp.np.cumsum(NDArray arr, Nullable1 axis, Nullable`1 typeCode)

QingtaoLi1 avatar Nov 30 '21 07:11 QingtaoLi1

It seems there is no UT for this function in this repo.

QingtaoLi1 avatar Nov 30 '21 07:11 QingtaoLi1

Besides, another argument replace is not used at all in both overrides.

QingtaoLi1 avatar Nov 30 '21 08:11 QingtaoLi1

I do a simple test on np.cumsum since it is in the stack trace:

    public static void RandomChoice()
    {
        var array = np.arange(1, 50265);
        var arrayDouble = 1.0 / array.astype(np.@double);
        var cumsum = np.cumsum(arrayDouble, typeCode: arrayDouble.typecode);  // OK
        var cumsum2 = np.cumsum(arrayDouble);                              // raise Exception mentioned above
    }

It looks like there's something wrong in the default typeCode. BTW, I'm using the latest NumSharp 0.30.0 version.

QingtaoLi1 avatar Nov 30 '21 08:11 QingtaoLi1

相同的问题,你解决了吗?

UCtreespring avatar Dec 14 '21 16:12 UCtreespring

目前发现的解决方案就是上面那样自己copy一个,加上typeCode就能跑了

QingtaoLi1 avatar Dec 15 '21 02:12 QingtaoLi1

是我没有留意到那个“Ok”的注释,按照你的方案,我复制了官方Numsharp-master中的相关方法,添加了相关的typeCode参数,问题解决。非常感谢!

UCtreespring avatar Dec 15 '21 05:12 UCtreespring

This has been fixed. np.random.choice now works correctly:

np.random.seed(42);
var result = np.random.choice(5, 3, true);
// Result: [3, 4, 2]

The underlying cumsum issue that caused the NotSupportedException has been resolved.

Resolved via #600, #602, #603, #596.

Nucs avatar Apr 12 '26 06:04 Nucs