Indexing empty array, Numba vs. Numpy
up vote
3
down vote
favorite
I was experimenting with the behavior of Numba
vs Numpy
for array indexing, and I came across something which I do not quite understand; so I hoped someone can point me in the right direction for what is probably a very simple question. Below are two functions, both of which create an empty array using the np.arange command. I then "append" (experimenting with a variety of methods to see how both Numba
and Numpy
perform/break) to the array using an index of 0, example[0] = 1
.
The Numba
function with jit
runs without error, but the Numpy
example gives the error:
IndexError: index 0 is out of bounds for axis 0 with size 0
The Numpy
error makes sense, but I am unsure as to why Numba
with jit
enabled allows the operation without error.
import numba as nb
import numpy as np
@nb.jit()
def funcnumba():
'''
Add item to position 0 using Numba
'''
example = np.arange(0)
example[0] = 1
return example
def funcnumpy():
'''
Add item to position 0 using Numpy. This produces an error which makes sense
'''
example = np.arange(0)
example[0] = 1
return example
print(funcnumba())
print(funcnumpy())
python numpy numba
add a comment |
up vote
3
down vote
favorite
I was experimenting with the behavior of Numba
vs Numpy
for array indexing, and I came across something which I do not quite understand; so I hoped someone can point me in the right direction for what is probably a very simple question. Below are two functions, both of which create an empty array using the np.arange command. I then "append" (experimenting with a variety of methods to see how both Numba
and Numpy
perform/break) to the array using an index of 0, example[0] = 1
.
The Numba
function with jit
runs without error, but the Numpy
example gives the error:
IndexError: index 0 is out of bounds for axis 0 with size 0
The Numpy
error makes sense, but I am unsure as to why Numba
with jit
enabled allows the operation without error.
import numba as nb
import numpy as np
@nb.jit()
def funcnumba():
'''
Add item to position 0 using Numba
'''
example = np.arange(0)
example[0] = 1
return example
def funcnumpy():
'''
Add item to position 0 using Numpy. This produces an error which makes sense
'''
example = np.arange(0)
example[0] = 1
return example
print(funcnumba())
print(funcnumpy())
python numpy numba
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I was experimenting with the behavior of Numba
vs Numpy
for array indexing, and I came across something which I do not quite understand; so I hoped someone can point me in the right direction for what is probably a very simple question. Below are two functions, both of which create an empty array using the np.arange command. I then "append" (experimenting with a variety of methods to see how both Numba
and Numpy
perform/break) to the array using an index of 0, example[0] = 1
.
The Numba
function with jit
runs without error, but the Numpy
example gives the error:
IndexError: index 0 is out of bounds for axis 0 with size 0
The Numpy
error makes sense, but I am unsure as to why Numba
with jit
enabled allows the operation without error.
import numba as nb
import numpy as np
@nb.jit()
def funcnumba():
'''
Add item to position 0 using Numba
'''
example = np.arange(0)
example[0] = 1
return example
def funcnumpy():
'''
Add item to position 0 using Numpy. This produces an error which makes sense
'''
example = np.arange(0)
example[0] = 1
return example
print(funcnumba())
print(funcnumpy())
python numpy numba
I was experimenting with the behavior of Numba
vs Numpy
for array indexing, and I came across something which I do not quite understand; so I hoped someone can point me in the right direction for what is probably a very simple question. Below are two functions, both of which create an empty array using the np.arange command. I then "append" (experimenting with a variety of methods to see how both Numba
and Numpy
perform/break) to the array using an index of 0, example[0] = 1
.
The Numba
function with jit
runs without error, but the Numpy
example gives the error:
IndexError: index 0 is out of bounds for axis 0 with size 0
The Numpy
error makes sense, but I am unsure as to why Numba
with jit
enabled allows the operation without error.
import numba as nb
import numpy as np
@nb.jit()
def funcnumba():
'''
Add item to position 0 using Numba
'''
example = np.arange(0)
example[0] = 1
return example
def funcnumpy():
'''
Add item to position 0 using Numpy. This produces an error which makes sense
'''
example = np.arange(0)
example[0] = 1
return example
print(funcnumba())
print(funcnumpy())
python numpy numba
python numpy numba
asked Nov 19 at 14:04
Yeti
145112
145112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
See the Numba documentation on arrays:
Currently there are no bounds checking for array indexing and slicing (...)
That means that you will be writing out of the bounds of the array in this case. Since it is just one element you may be lucky and get away with it, but you can also crash your program or, even worse, silently overwrite some other value. See issue #730 for a discussion about it.
I'm confused, why does it throw the error when there are no bounds checks? Isn't this more to do with numpy treating single item arrays as scalars?
– roganjosh
Nov 19 at 14:19
In fact,np.arange(0)
seems to me to be inviting wonky behaviour tbh. Does it have a purpose?
– roganjosh
Nov 19 at 14:22
@roganjosh It's the NumPy function the one that raises the error, not the Numba one (which makes sense, because NumPy does implement bounds checking).np.arange(0)
is an empty array, not a single-item one. Wrt its utility, I'd say it's a matter of consistence. I have probably used it when I needed something to work whether the result should be empty or not.
– jdehesa
Nov 19 at 14:29
Oops, that was a bad misread on my part, sorry!
– roganjosh
Nov 19 at 14:31
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
See the Numba documentation on arrays:
Currently there are no bounds checking for array indexing and slicing (...)
That means that you will be writing out of the bounds of the array in this case. Since it is just one element you may be lucky and get away with it, but you can also crash your program or, even worse, silently overwrite some other value. See issue #730 for a discussion about it.
I'm confused, why does it throw the error when there are no bounds checks? Isn't this more to do with numpy treating single item arrays as scalars?
– roganjosh
Nov 19 at 14:19
In fact,np.arange(0)
seems to me to be inviting wonky behaviour tbh. Does it have a purpose?
– roganjosh
Nov 19 at 14:22
@roganjosh It's the NumPy function the one that raises the error, not the Numba one (which makes sense, because NumPy does implement bounds checking).np.arange(0)
is an empty array, not a single-item one. Wrt its utility, I'd say it's a matter of consistence. I have probably used it when I needed something to work whether the result should be empty or not.
– jdehesa
Nov 19 at 14:29
Oops, that was a bad misread on my part, sorry!
– roganjosh
Nov 19 at 14:31
add a comment |
up vote
4
down vote
accepted
See the Numba documentation on arrays:
Currently there are no bounds checking for array indexing and slicing (...)
That means that you will be writing out of the bounds of the array in this case. Since it is just one element you may be lucky and get away with it, but you can also crash your program or, even worse, silently overwrite some other value. See issue #730 for a discussion about it.
I'm confused, why does it throw the error when there are no bounds checks? Isn't this more to do with numpy treating single item arrays as scalars?
– roganjosh
Nov 19 at 14:19
In fact,np.arange(0)
seems to me to be inviting wonky behaviour tbh. Does it have a purpose?
– roganjosh
Nov 19 at 14:22
@roganjosh It's the NumPy function the one that raises the error, not the Numba one (which makes sense, because NumPy does implement bounds checking).np.arange(0)
is an empty array, not a single-item one. Wrt its utility, I'd say it's a matter of consistence. I have probably used it when I needed something to work whether the result should be empty or not.
– jdehesa
Nov 19 at 14:29
Oops, that was a bad misread on my part, sorry!
– roganjosh
Nov 19 at 14:31
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
See the Numba documentation on arrays:
Currently there are no bounds checking for array indexing and slicing (...)
That means that you will be writing out of the bounds of the array in this case. Since it is just one element you may be lucky and get away with it, but you can also crash your program or, even worse, silently overwrite some other value. See issue #730 for a discussion about it.
See the Numba documentation on arrays:
Currently there are no bounds checking for array indexing and slicing (...)
That means that you will be writing out of the bounds of the array in this case. Since it is just one element you may be lucky and get away with it, but you can also crash your program or, even worse, silently overwrite some other value. See issue #730 for a discussion about it.
answered Nov 19 at 14:17
jdehesa
21.9k43150
21.9k43150
I'm confused, why does it throw the error when there are no bounds checks? Isn't this more to do with numpy treating single item arrays as scalars?
– roganjosh
Nov 19 at 14:19
In fact,np.arange(0)
seems to me to be inviting wonky behaviour tbh. Does it have a purpose?
– roganjosh
Nov 19 at 14:22
@roganjosh It's the NumPy function the one that raises the error, not the Numba one (which makes sense, because NumPy does implement bounds checking).np.arange(0)
is an empty array, not a single-item one. Wrt its utility, I'd say it's a matter of consistence. I have probably used it when I needed something to work whether the result should be empty or not.
– jdehesa
Nov 19 at 14:29
Oops, that was a bad misread on my part, sorry!
– roganjosh
Nov 19 at 14:31
add a comment |
I'm confused, why does it throw the error when there are no bounds checks? Isn't this more to do with numpy treating single item arrays as scalars?
– roganjosh
Nov 19 at 14:19
In fact,np.arange(0)
seems to me to be inviting wonky behaviour tbh. Does it have a purpose?
– roganjosh
Nov 19 at 14:22
@roganjosh It's the NumPy function the one that raises the error, not the Numba one (which makes sense, because NumPy does implement bounds checking).np.arange(0)
is an empty array, not a single-item one. Wrt its utility, I'd say it's a matter of consistence. I have probably used it when I needed something to work whether the result should be empty or not.
– jdehesa
Nov 19 at 14:29
Oops, that was a bad misread on my part, sorry!
– roganjosh
Nov 19 at 14:31
I'm confused, why does it throw the error when there are no bounds checks? Isn't this more to do with numpy treating single item arrays as scalars?
– roganjosh
Nov 19 at 14:19
I'm confused, why does it throw the error when there are no bounds checks? Isn't this more to do with numpy treating single item arrays as scalars?
– roganjosh
Nov 19 at 14:19
In fact,
np.arange(0)
seems to me to be inviting wonky behaviour tbh. Does it have a purpose?– roganjosh
Nov 19 at 14:22
In fact,
np.arange(0)
seems to me to be inviting wonky behaviour tbh. Does it have a purpose?– roganjosh
Nov 19 at 14:22
@roganjosh It's the NumPy function the one that raises the error, not the Numba one (which makes sense, because NumPy does implement bounds checking).
np.arange(0)
is an empty array, not a single-item one. Wrt its utility, I'd say it's a matter of consistence. I have probably used it when I needed something to work whether the result should be empty or not.– jdehesa
Nov 19 at 14:29
@roganjosh It's the NumPy function the one that raises the error, not the Numba one (which makes sense, because NumPy does implement bounds checking).
np.arange(0)
is an empty array, not a single-item one. Wrt its utility, I'd say it's a matter of consistence. I have probably used it when I needed something to work whether the result should be empty or not.– jdehesa
Nov 19 at 14:29
Oops, that was a bad misread on my part, sorry!
– roganjosh
Nov 19 at 14:31
Oops, that was a bad misread on my part, sorry!
– roganjosh
Nov 19 at 14:31
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376356%2findexing-empty-array-numba-vs-numpy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown