indexing with 0D array
Happypig375 opened this issue · 5 comments
Happypig375 commented
using NumpyDotNet;
np.arange(7)[np.argmax(np.arange(5))]Error: System.IndexOutOfRangeException: (NpyExc_IndexError) NpyArray_ArrayItem: 0-d arrays can't be indexed
at NumpyDotNet.NpyUtil_IndexProcessing.ConvertSingleIndex(ndarray arr, Object arg, NpyIndexes indexes, Int32 index)
at NumpyDotNet.ndarray.get_Item(Object[] args)
import numpy as np
print(np.arange(7)[np.argmax(np.arange(5))])4
KevinBaselinesw commented
python unit test:
def test_HadrianTang_11(self):
a = np.argmax(np.arange(5))
print(a)
b = np.arange(7)[a]
print(b)
c# unit test
In C#, you can only have 1 return type for a function. Since argmax can be an ndarray I have no choice but to return an ndarray. Python can be either an ndarray or a scalar but I don't have that option.
To make it work, you simply need to cast the ndarray to the type you are expecting like below (Int64). This will get the first element of the array and cast it to that that. It throws an exception if you cast to the wrong type.
It is equivalent to var c = (Int64)np.argmax(np.arange(5)).GetItem(0);
[TestMethod]
public void test_HadrianTang_11()
{
var a = (Int64)np.argmax(np.arange(5));
print(a);
var b = np.arange(7)[a];
print(b);
return;
}
Happypig375 commented
I expect the indexer to allow 0D arrays. Extracting the value is not needed for my case.
Happypig375 commented
import numpy as np
print(np.array(4).shape)
print(np.arange(7)[np.array(4)])()
4
using NumpyDotNet;
System.Console.WriteLine(np.array(4).shape);
System.Console.WriteLine(np.arange(7)[np.array(4)]);()
Error: System.IndexOutOfRangeException: (NpyExc_IndexError) NpyArray_ArrayItem: 0-d arrays can't be indexed
at NumpyDotNet.NpyUtil_IndexProcessing.ConvertSingleIndex(ndarray arr, Object arg, NpyIndexes indexes, Int32 index)
at NumpyDotNet.ndarray.get_Item(Object[] args)
KevinBaselinesw commented
I sent you an email with a fix for this problem
Happypig375 commented
fixed