Multiply numpy int and float arrays: Cannot cast ufunc multiply output from dtype
I'd like to multiply an int16
array but a float
array, with auto rounding, but this fails :
import numpy
A = numpy.array([1, 2, 3, 4], dtype=numpy.int16)
B = numpy.array([0.5, 2.1, 3, 4], dtype=numpy.float64)
A *= B
I get:
TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('int16') with casting rule 'same_kind'
python arrays numpy floating-point int
add a comment |
I'd like to multiply an int16
array but a float
array, with auto rounding, but this fails :
import numpy
A = numpy.array([1, 2, 3, 4], dtype=numpy.int16)
B = numpy.array([0.5, 2.1, 3, 4], dtype=numpy.float64)
A *= B
I get:
TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('int16') with casting rule 'same_kind'
python arrays numpy floating-point int
It seems it's possible withnumpy.multiply(A, B, out=A, casting='unsafe')
but that's way much longer syntax! Is there a way to set casting='unsafe' in numpy by default?
– Basj
Jul 30 '16 at 12:10
See github.com/numpy/numpy/pull/6499/files
– Basj
Jul 30 '16 at 12:11
add a comment |
I'd like to multiply an int16
array but a float
array, with auto rounding, but this fails :
import numpy
A = numpy.array([1, 2, 3, 4], dtype=numpy.int16)
B = numpy.array([0.5, 2.1, 3, 4], dtype=numpy.float64)
A *= B
I get:
TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('int16') with casting rule 'same_kind'
python arrays numpy floating-point int
I'd like to multiply an int16
array but a float
array, with auto rounding, but this fails :
import numpy
A = numpy.array([1, 2, 3, 4], dtype=numpy.int16)
B = numpy.array([0.5, 2.1, 3, 4], dtype=numpy.float64)
A *= B
I get:
TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('int16') with casting rule 'same_kind'
python arrays numpy floating-point int
python arrays numpy floating-point int
edited Nov 23 '18 at 22:37
seralouk
5,71522339
5,71522339
asked Jul 30 '16 at 11:39
BasjBasj
5,58729104226
5,58729104226
It seems it's possible withnumpy.multiply(A, B, out=A, casting='unsafe')
but that's way much longer syntax! Is there a way to set casting='unsafe' in numpy by default?
– Basj
Jul 30 '16 at 12:10
See github.com/numpy/numpy/pull/6499/files
– Basj
Jul 30 '16 at 12:11
add a comment |
It seems it's possible withnumpy.multiply(A, B, out=A, casting='unsafe')
but that's way much longer syntax! Is there a way to set casting='unsafe' in numpy by default?
– Basj
Jul 30 '16 at 12:10
See github.com/numpy/numpy/pull/6499/files
– Basj
Jul 30 '16 at 12:11
It seems it's possible with
numpy.multiply(A, B, out=A, casting='unsafe')
but that's way much longer syntax! Is there a way to set casting='unsafe' in numpy by default?– Basj
Jul 30 '16 at 12:10
It seems it's possible with
numpy.multiply(A, B, out=A, casting='unsafe')
but that's way much longer syntax! Is there a way to set casting='unsafe' in numpy by default?– Basj
Jul 30 '16 at 12:10
See github.com/numpy/numpy/pull/6499/files
– Basj
Jul 30 '16 at 12:11
See github.com/numpy/numpy/pull/6499/files
– Basj
Jul 30 '16 at 12:11
add a comment |
3 Answers
3
active
oldest
votes
2 ways to solve this:
You can solve this by replacing
A *= B
with
A = (A * B)
or with
numpy.multiply(A, B, out=A, casting='unsafe')
add a comment |
You could use broadcasting
to multiply the two arrays and take only the integer part as follows:
In [2]: (A*B).astype(int)
Out[2]: array([ 0, 4, 9, 16])
Timing Constraints:
In [8]: %timeit (A*B).astype(int)
1000000 loops, best of 3: 1.65 µs per loop
In [9]: %timeit np.multiply(A, B, out=A, casting='unsafe')
100000 loops, best of 3: 2.01 µs per loop
OP wants to do the multiplication in place
– ali_m
Jul 30 '16 at 18:45
How fast is it compared tonumpy.multiply(A, B, out=A, casting='unsafe')
?
– Basj
Jul 30 '16 at 18:45
@ali_m Nowhere does the OP mention they want to do the multiplication in place. Their only comment was that the longer way was "way much longer syntax".
– SethMMorton
Jul 30 '16 at 18:51
@SethMMorton Well, both the example given in the question and the workaround the OP posted in the comments are explicitly performing in place multiplication.
– ali_m
Jul 30 '16 at 18:56
2
@ali_m I don't disagree with that, but based on this question and the other question it seems the OP is more interested in terseness than an in-place operation. Having said that, I think the assumption that they want an in place operation is good because of the examples given and that in-place saves having to typeA =
, but I always think it is safer to wait for the OP to explicitly make it clear what they want rather than make assumptions.
– SethMMorton
Jul 30 '16 at 18:59
|
show 1 more comment
import numpy as np
A = np.float_(A)
A *= B
try this. I think are different array type you get fail.
Cast
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f38673531%2fmultiply-numpy-int-and-float-arrays-cannot-cast-ufunc-multiply-output-from-dtyp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
2 ways to solve this:
You can solve this by replacing
A *= B
with
A = (A * B)
or with
numpy.multiply(A, B, out=A, casting='unsafe')
add a comment |
2 ways to solve this:
You can solve this by replacing
A *= B
with
A = (A * B)
or with
numpy.multiply(A, B, out=A, casting='unsafe')
add a comment |
2 ways to solve this:
You can solve this by replacing
A *= B
with
A = (A * B)
or with
numpy.multiply(A, B, out=A, casting='unsafe')
2 ways to solve this:
You can solve this by replacing
A *= B
with
A = (A * B)
or with
numpy.multiply(A, B, out=A, casting='unsafe')
edited Jul 22 '18 at 18:14
answered Jun 30 '17 at 14:12
seraloukseralouk
5,71522339
5,71522339
add a comment |
add a comment |
You could use broadcasting
to multiply the two arrays and take only the integer part as follows:
In [2]: (A*B).astype(int)
Out[2]: array([ 0, 4, 9, 16])
Timing Constraints:
In [8]: %timeit (A*B).astype(int)
1000000 loops, best of 3: 1.65 µs per loop
In [9]: %timeit np.multiply(A, B, out=A, casting='unsafe')
100000 loops, best of 3: 2.01 µs per loop
OP wants to do the multiplication in place
– ali_m
Jul 30 '16 at 18:45
How fast is it compared tonumpy.multiply(A, B, out=A, casting='unsafe')
?
– Basj
Jul 30 '16 at 18:45
@ali_m Nowhere does the OP mention they want to do the multiplication in place. Their only comment was that the longer way was "way much longer syntax".
– SethMMorton
Jul 30 '16 at 18:51
@SethMMorton Well, both the example given in the question and the workaround the OP posted in the comments are explicitly performing in place multiplication.
– ali_m
Jul 30 '16 at 18:56
2
@ali_m I don't disagree with that, but based on this question and the other question it seems the OP is more interested in terseness than an in-place operation. Having said that, I think the assumption that they want an in place operation is good because of the examples given and that in-place saves having to typeA =
, but I always think it is safer to wait for the OP to explicitly make it clear what they want rather than make assumptions.
– SethMMorton
Jul 30 '16 at 18:59
|
show 1 more comment
You could use broadcasting
to multiply the two arrays and take only the integer part as follows:
In [2]: (A*B).astype(int)
Out[2]: array([ 0, 4, 9, 16])
Timing Constraints:
In [8]: %timeit (A*B).astype(int)
1000000 loops, best of 3: 1.65 µs per loop
In [9]: %timeit np.multiply(A, B, out=A, casting='unsafe')
100000 loops, best of 3: 2.01 µs per loop
OP wants to do the multiplication in place
– ali_m
Jul 30 '16 at 18:45
How fast is it compared tonumpy.multiply(A, B, out=A, casting='unsafe')
?
– Basj
Jul 30 '16 at 18:45
@ali_m Nowhere does the OP mention they want to do the multiplication in place. Their only comment was that the longer way was "way much longer syntax".
– SethMMorton
Jul 30 '16 at 18:51
@SethMMorton Well, both the example given in the question and the workaround the OP posted in the comments are explicitly performing in place multiplication.
– ali_m
Jul 30 '16 at 18:56
2
@ali_m I don't disagree with that, but based on this question and the other question it seems the OP is more interested in terseness than an in-place operation. Having said that, I think the assumption that they want an in place operation is good because of the examples given and that in-place saves having to typeA =
, but I always think it is safer to wait for the OP to explicitly make it clear what they want rather than make assumptions.
– SethMMorton
Jul 30 '16 at 18:59
|
show 1 more comment
You could use broadcasting
to multiply the two arrays and take only the integer part as follows:
In [2]: (A*B).astype(int)
Out[2]: array([ 0, 4, 9, 16])
Timing Constraints:
In [8]: %timeit (A*B).astype(int)
1000000 loops, best of 3: 1.65 µs per loop
In [9]: %timeit np.multiply(A, B, out=A, casting='unsafe')
100000 loops, best of 3: 2.01 µs per loop
You could use broadcasting
to multiply the two arrays and take only the integer part as follows:
In [2]: (A*B).astype(int)
Out[2]: array([ 0, 4, 9, 16])
Timing Constraints:
In [8]: %timeit (A*B).astype(int)
1000000 loops, best of 3: 1.65 µs per loop
In [9]: %timeit np.multiply(A, B, out=A, casting='unsafe')
100000 loops, best of 3: 2.01 µs per loop
edited Jul 30 '16 at 18:49
answered Jul 30 '16 at 18:40
Nickil MaveliNickil Maveli
17.4k43448
17.4k43448
OP wants to do the multiplication in place
– ali_m
Jul 30 '16 at 18:45
How fast is it compared tonumpy.multiply(A, B, out=A, casting='unsafe')
?
– Basj
Jul 30 '16 at 18:45
@ali_m Nowhere does the OP mention they want to do the multiplication in place. Their only comment was that the longer way was "way much longer syntax".
– SethMMorton
Jul 30 '16 at 18:51
@SethMMorton Well, both the example given in the question and the workaround the OP posted in the comments are explicitly performing in place multiplication.
– ali_m
Jul 30 '16 at 18:56
2
@ali_m I don't disagree with that, but based on this question and the other question it seems the OP is more interested in terseness than an in-place operation. Having said that, I think the assumption that they want an in place operation is good because of the examples given and that in-place saves having to typeA =
, but I always think it is safer to wait for the OP to explicitly make it clear what they want rather than make assumptions.
– SethMMorton
Jul 30 '16 at 18:59
|
show 1 more comment
OP wants to do the multiplication in place
– ali_m
Jul 30 '16 at 18:45
How fast is it compared tonumpy.multiply(A, B, out=A, casting='unsafe')
?
– Basj
Jul 30 '16 at 18:45
@ali_m Nowhere does the OP mention they want to do the multiplication in place. Their only comment was that the longer way was "way much longer syntax".
– SethMMorton
Jul 30 '16 at 18:51
@SethMMorton Well, both the example given in the question and the workaround the OP posted in the comments are explicitly performing in place multiplication.
– ali_m
Jul 30 '16 at 18:56
2
@ali_m I don't disagree with that, but based on this question and the other question it seems the OP is more interested in terseness than an in-place operation. Having said that, I think the assumption that they want an in place operation is good because of the examples given and that in-place saves having to typeA =
, but I always think it is safer to wait for the OP to explicitly make it clear what they want rather than make assumptions.
– SethMMorton
Jul 30 '16 at 18:59
OP wants to do the multiplication in place
– ali_m
Jul 30 '16 at 18:45
OP wants to do the multiplication in place
– ali_m
Jul 30 '16 at 18:45
How fast is it compared to
numpy.multiply(A, B, out=A, casting='unsafe')
?– Basj
Jul 30 '16 at 18:45
How fast is it compared to
numpy.multiply(A, B, out=A, casting='unsafe')
?– Basj
Jul 30 '16 at 18:45
@ali_m Nowhere does the OP mention they want to do the multiplication in place. Their only comment was that the longer way was "way much longer syntax".
– SethMMorton
Jul 30 '16 at 18:51
@ali_m Nowhere does the OP mention they want to do the multiplication in place. Their only comment was that the longer way was "way much longer syntax".
– SethMMorton
Jul 30 '16 at 18:51
@SethMMorton Well, both the example given in the question and the workaround the OP posted in the comments are explicitly performing in place multiplication.
– ali_m
Jul 30 '16 at 18:56
@SethMMorton Well, both the example given in the question and the workaround the OP posted in the comments are explicitly performing in place multiplication.
– ali_m
Jul 30 '16 at 18:56
2
2
@ali_m I don't disagree with that, but based on this question and the other question it seems the OP is more interested in terseness than an in-place operation. Having said that, I think the assumption that they want an in place operation is good because of the examples given and that in-place saves having to type
A =
, but I always think it is safer to wait for the OP to explicitly make it clear what they want rather than make assumptions.– SethMMorton
Jul 30 '16 at 18:59
@ali_m I don't disagree with that, but based on this question and the other question it seems the OP is more interested in terseness than an in-place operation. Having said that, I think the assumption that they want an in place operation is good because of the examples given and that in-place saves having to type
A =
, but I always think it is safer to wait for the OP to explicitly make it clear what they want rather than make assumptions.– SethMMorton
Jul 30 '16 at 18:59
|
show 1 more comment
import numpy as np
A = np.float_(A)
A *= B
try this. I think are different array type you get fail.
Cast
add a comment |
import numpy as np
A = np.float_(A)
A *= B
try this. I think are different array type you get fail.
Cast
add a comment |
import numpy as np
A = np.float_(A)
A *= B
try this. I think are different array type you get fail.
Cast
import numpy as np
A = np.float_(A)
A *= B
try this. I think are different array type you get fail.
Cast
answered Jul 30 '16 at 19:18
Ahmet İlginAhmet İlgin
254
254
add a comment |
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.
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%2f38673531%2fmultiply-numpy-int-and-float-arrays-cannot-cast-ufunc-multiply-output-from-dtyp%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
It seems it's possible with
numpy.multiply(A, B, out=A, casting='unsafe')
but that's way much longer syntax! Is there a way to set casting='unsafe' in numpy by default?– Basj
Jul 30 '16 at 12:10
See github.com/numpy/numpy/pull/6499/files
– Basj
Jul 30 '16 at 12:11