Replace multiple elements in numpy array with 1
Clash Royale CLAN TAG#URR8PPP
up vote
9
down vote
favorite
In a given numpy array X
:
X = array([1,2,3,4,5,6,7,8,9,10])
I would like to replace indices (2, 3)
and (7, 8)
with a single element -1
respectively, like:
X = array([1,2,-1,5,6,7,-1,10])
In other words, I replaced values at indices (2, 3)
and (7,8)
of the original array with a singular value.
Question is: Is there a numpy-ish way (i.e. without for loops and usage of python lists) around it? Thanks.
Note: This is NOT equivalent of replacing a single element in-place with another. Its about replacing multiple values with a "singular" value. Thanks.
python arrays numpy
 |Â
show 6 more comments
up vote
9
down vote
favorite
In a given numpy array X
:
X = array([1,2,3,4,5,6,7,8,9,10])
I would like to replace indices (2, 3)
and (7, 8)
with a single element -1
respectively, like:
X = array([1,2,-1,5,6,7,-1,10])
In other words, I replaced values at indices (2, 3)
and (7,8)
of the original array with a singular value.
Question is: Is there a numpy-ish way (i.e. without for loops and usage of python lists) around it? Thanks.
Note: This is NOT equivalent of replacing a single element in-place with another. Its about replacing multiple values with a "singular" value. Thanks.
python arrays numpy
2
you don't have to be rude, especially when you are referring to a manual!
â khan
1 hour ago
Possible duplicate of Replace an element in a numpy array at specific index
â awiebe
1 hour ago
1
You should google then, if you have a different meaning in your mind.
â khan
1 hour ago
1
@awiebe Not sure what this means ... the question is looking for a numpy-ish way of replacing two entities in an array with a single entity (demo-ed by the examples too in which you can clearly see the size of array reduced by 2 elements).
â khan
1 hour ago
1
Ah, I see the question was resizing the array, not replacing the value. That makes more sense then how I read it.
â awiebe
1 hour ago
 |Â
show 6 more comments
up vote
9
down vote
favorite
up vote
9
down vote
favorite
In a given numpy array X
:
X = array([1,2,3,4,5,6,7,8,9,10])
I would like to replace indices (2, 3)
and (7, 8)
with a single element -1
respectively, like:
X = array([1,2,-1,5,6,7,-1,10])
In other words, I replaced values at indices (2, 3)
and (7,8)
of the original array with a singular value.
Question is: Is there a numpy-ish way (i.e. without for loops and usage of python lists) around it? Thanks.
Note: This is NOT equivalent of replacing a single element in-place with another. Its about replacing multiple values with a "singular" value. Thanks.
python arrays numpy
In a given numpy array X
:
X = array([1,2,3,4,5,6,7,8,9,10])
I would like to replace indices (2, 3)
and (7, 8)
with a single element -1
respectively, like:
X = array([1,2,-1,5,6,7,-1,10])
In other words, I replaced values at indices (2, 3)
and (7,8)
of the original array with a singular value.
Question is: Is there a numpy-ish way (i.e. without for loops and usage of python lists) around it? Thanks.
Note: This is NOT equivalent of replacing a single element in-place with another. Its about replacing multiple values with a "singular" value. Thanks.
python arrays numpy
python arrays numpy
edited 35 mins ago
user3483203
27.4k72350
27.4k72350
asked 2 hours ago
khan
1,66583049
1,66583049
2
you don't have to be rude, especially when you are referring to a manual!
â khan
1 hour ago
Possible duplicate of Replace an element in a numpy array at specific index
â awiebe
1 hour ago
1
You should google then, if you have a different meaning in your mind.
â khan
1 hour ago
1
@awiebe Not sure what this means ... the question is looking for a numpy-ish way of replacing two entities in an array with a single entity (demo-ed by the examples too in which you can clearly see the size of array reduced by 2 elements).
â khan
1 hour ago
1
Ah, I see the question was resizing the array, not replacing the value. That makes more sense then how I read it.
â awiebe
1 hour ago
 |Â
show 6 more comments
2
you don't have to be rude, especially when you are referring to a manual!
â khan
1 hour ago
Possible duplicate of Replace an element in a numpy array at specific index
â awiebe
1 hour ago
1
You should google then, if you have a different meaning in your mind.
â khan
1 hour ago
1
@awiebe Not sure what this means ... the question is looking for a numpy-ish way of replacing two entities in an array with a single entity (demo-ed by the examples too in which you can clearly see the size of array reduced by 2 elements).
â khan
1 hour ago
1
Ah, I see the question was resizing the array, not replacing the value. That makes more sense then how I read it.
â awiebe
1 hour ago
2
2
you don't have to be rude, especially when you are referring to a manual!
â khan
1 hour ago
you don't have to be rude, especially when you are referring to a manual!
â khan
1 hour ago
Possible duplicate of Replace an element in a numpy array at specific index
â awiebe
1 hour ago
Possible duplicate of Replace an element in a numpy array at specific index
â awiebe
1 hour ago
1
1
You should google then, if you have a different meaning in your mind.
â khan
1 hour ago
You should google then, if you have a different meaning in your mind.
â khan
1 hour ago
1
1
@awiebe Not sure what this means ... the question is looking for a numpy-ish way of replacing two entities in an array with a single entity (demo-ed by the examples too in which you can clearly see the size of array reduced by 2 elements).
â khan
1 hour ago
@awiebe Not sure what this means ... the question is looking for a numpy-ish way of replacing two entities in an array with a single entity (demo-ed by the examples too in which you can clearly see the size of array reduced by 2 elements).
â khan
1 hour ago
1
1
Ah, I see the question was resizing the array, not replacing the value. That makes more sense then how I read it.
â awiebe
1 hour ago
Ah, I see the question was resizing the array, not replacing the value. That makes more sense then how I read it.
â awiebe
1 hour ago
 |Â
show 6 more comments
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
Try np.put
:
np.put(X, [2,3,7,8], [-1,0]) # `0` can be changed to anything that's not in the array
print(X[X!=0]) # whatever You put as an number in `put`
So basically use put
to do the values for the indexes, then drop the zero-values.
Or as @khan says, can do something that's out of range:
np.put(X, [2,3,7,8], [-1,np.max(X)+1])
print(X[X!=X.max()])
All Output:
[ 1 2 -1 5 6 7 -1 10]
2
what if the list contains 0 initially?
â Ashutosh Chapagain
1 hour ago
2
@AshutoshChapagain Oh yeah smart..., i'll edit
â U9-Forward
1 hour ago
1
This zero can actually be anything out of the range of the array..that will help it. So,X=numpy.arange(1,11); numpy.put(X, [2,3,7,8], [-1,numpy.max(X)+1]); X[X!=X.max()]
..
â khan
1 hour ago
1
Ohh you already edited. Great answer. :-)
â khan
1 hour ago
1
A food for thought: What if we like to replace indices(2,3,7,8)
with-1
and indices(9,10)
with-2
..
â khan
1 hour ago
 |Â
show 6 more comments
up vote
3
down vote
I'm not sure if this can be done in one step, but here's a way using np.delete
:
import numpy as np
from operator import itemgetter
X = np.array(range(1,11))
to_replace = [[2,3], [7,8]]
X[list(map(itemgetter(0), to_replace))] = -1
X = np.delete(X, list(map(lambda x: x[1:], to_replace)))
print(X)
#[ 1 2 -1 5 6 7 -1 10]
First we replace the first element of each pair with -1
. Then we delete the remaining elements.
Amm.. this is not fully a numpy-ish solution tho...
â U9-Forward
1 hour ago
add a comment |Â
up vote
3
down vote
A solution using numpy.delete
, similar to @pault, but more efficient as it uses pure numpy indexing. However, because of this efficient indexing, it means that you cannot pass jagged arrays as indices
Setup
a = np.array([1,2,3,4,5,6,7,8,9,10])
idx = np.stack([[2, 3], [7, 8]])
a[idx] = -1
np.delete(a, idx[:, 1:])
array([ 1, 2, -1, 5, 6, 7, -1, 10])
Definitely seems like a cleaner and more efficient way.
â BernardL
53 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Try np.put
:
np.put(X, [2,3,7,8], [-1,0]) # `0` can be changed to anything that's not in the array
print(X[X!=0]) # whatever You put as an number in `put`
So basically use put
to do the values for the indexes, then drop the zero-values.
Or as @khan says, can do something that's out of range:
np.put(X, [2,3,7,8], [-1,np.max(X)+1])
print(X[X!=X.max()])
All Output:
[ 1 2 -1 5 6 7 -1 10]
2
what if the list contains 0 initially?
â Ashutosh Chapagain
1 hour ago
2
@AshutoshChapagain Oh yeah smart..., i'll edit
â U9-Forward
1 hour ago
1
This zero can actually be anything out of the range of the array..that will help it. So,X=numpy.arange(1,11); numpy.put(X, [2,3,7,8], [-1,numpy.max(X)+1]); X[X!=X.max()]
..
â khan
1 hour ago
1
Ohh you already edited. Great answer. :-)
â khan
1 hour ago
1
A food for thought: What if we like to replace indices(2,3,7,8)
with-1
and indices(9,10)
with-2
..
â khan
1 hour ago
 |Â
show 6 more comments
up vote
0
down vote
accepted
Try np.put
:
np.put(X, [2,3,7,8], [-1,0]) # `0` can be changed to anything that's not in the array
print(X[X!=0]) # whatever You put as an number in `put`
So basically use put
to do the values for the indexes, then drop the zero-values.
Or as @khan says, can do something that's out of range:
np.put(X, [2,3,7,8], [-1,np.max(X)+1])
print(X[X!=X.max()])
All Output:
[ 1 2 -1 5 6 7 -1 10]
2
what if the list contains 0 initially?
â Ashutosh Chapagain
1 hour ago
2
@AshutoshChapagain Oh yeah smart..., i'll edit
â U9-Forward
1 hour ago
1
This zero can actually be anything out of the range of the array..that will help it. So,X=numpy.arange(1,11); numpy.put(X, [2,3,7,8], [-1,numpy.max(X)+1]); X[X!=X.max()]
..
â khan
1 hour ago
1
Ohh you already edited. Great answer. :-)
â khan
1 hour ago
1
A food for thought: What if we like to replace indices(2,3,7,8)
with-1
and indices(9,10)
with-2
..
â khan
1 hour ago
 |Â
show 6 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Try np.put
:
np.put(X, [2,3,7,8], [-1,0]) # `0` can be changed to anything that's not in the array
print(X[X!=0]) # whatever You put as an number in `put`
So basically use put
to do the values for the indexes, then drop the zero-values.
Or as @khan says, can do something that's out of range:
np.put(X, [2,3,7,8], [-1,np.max(X)+1])
print(X[X!=X.max()])
All Output:
[ 1 2 -1 5 6 7 -1 10]
Try np.put
:
np.put(X, [2,3,7,8], [-1,0]) # `0` can be changed to anything that's not in the array
print(X[X!=0]) # whatever You put as an number in `put`
So basically use put
to do the values for the indexes, then drop the zero-values.
Or as @khan says, can do something that's out of range:
np.put(X, [2,3,7,8], [-1,np.max(X)+1])
print(X[X!=X.max()])
All Output:
[ 1 2 -1 5 6 7 -1 10]
edited 1 hour ago
answered 1 hour ago
U9-Forward
7,8342731
7,8342731
2
what if the list contains 0 initially?
â Ashutosh Chapagain
1 hour ago
2
@AshutoshChapagain Oh yeah smart..., i'll edit
â U9-Forward
1 hour ago
1
This zero can actually be anything out of the range of the array..that will help it. So,X=numpy.arange(1,11); numpy.put(X, [2,3,7,8], [-1,numpy.max(X)+1]); X[X!=X.max()]
..
â khan
1 hour ago
1
Ohh you already edited. Great answer. :-)
â khan
1 hour ago
1
A food for thought: What if we like to replace indices(2,3,7,8)
with-1
and indices(9,10)
with-2
..
â khan
1 hour ago
 |Â
show 6 more comments
2
what if the list contains 0 initially?
â Ashutosh Chapagain
1 hour ago
2
@AshutoshChapagain Oh yeah smart..., i'll edit
â U9-Forward
1 hour ago
1
This zero can actually be anything out of the range of the array..that will help it. So,X=numpy.arange(1,11); numpy.put(X, [2,3,7,8], [-1,numpy.max(X)+1]); X[X!=X.max()]
..
â khan
1 hour ago
1
Ohh you already edited. Great answer. :-)
â khan
1 hour ago
1
A food for thought: What if we like to replace indices(2,3,7,8)
with-1
and indices(9,10)
with-2
..
â khan
1 hour ago
2
2
what if the list contains 0 initially?
â Ashutosh Chapagain
1 hour ago
what if the list contains 0 initially?
â Ashutosh Chapagain
1 hour ago
2
2
@AshutoshChapagain Oh yeah smart..., i'll edit
â U9-Forward
1 hour ago
@AshutoshChapagain Oh yeah smart..., i'll edit
â U9-Forward
1 hour ago
1
1
This zero can actually be anything out of the range of the array..that will help it. So,
X=numpy.arange(1,11); numpy.put(X, [2,3,7,8], [-1,numpy.max(X)+1]); X[X!=X.max()]
..â khan
1 hour ago
This zero can actually be anything out of the range of the array..that will help it. So,
X=numpy.arange(1,11); numpy.put(X, [2,3,7,8], [-1,numpy.max(X)+1]); X[X!=X.max()]
..â khan
1 hour ago
1
1
Ohh you already edited. Great answer. :-)
â khan
1 hour ago
Ohh you already edited. Great answer. :-)
â khan
1 hour ago
1
1
A food for thought: What if we like to replace indices
(2,3,7,8)
with -1
and indices (9,10)
with -2
..â khan
1 hour ago
A food for thought: What if we like to replace indices
(2,3,7,8)
with -1
and indices (9,10)
with -2
..â khan
1 hour ago
 |Â
show 6 more comments
up vote
3
down vote
I'm not sure if this can be done in one step, but here's a way using np.delete
:
import numpy as np
from operator import itemgetter
X = np.array(range(1,11))
to_replace = [[2,3], [7,8]]
X[list(map(itemgetter(0), to_replace))] = -1
X = np.delete(X, list(map(lambda x: x[1:], to_replace)))
print(X)
#[ 1 2 -1 5 6 7 -1 10]
First we replace the first element of each pair with -1
. Then we delete the remaining elements.
Amm.. this is not fully a numpy-ish solution tho...
â U9-Forward
1 hour ago
add a comment |Â
up vote
3
down vote
I'm not sure if this can be done in one step, but here's a way using np.delete
:
import numpy as np
from operator import itemgetter
X = np.array(range(1,11))
to_replace = [[2,3], [7,8]]
X[list(map(itemgetter(0), to_replace))] = -1
X = np.delete(X, list(map(lambda x: x[1:], to_replace)))
print(X)
#[ 1 2 -1 5 6 7 -1 10]
First we replace the first element of each pair with -1
. Then we delete the remaining elements.
Amm.. this is not fully a numpy-ish solution tho...
â U9-Forward
1 hour ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
I'm not sure if this can be done in one step, but here's a way using np.delete
:
import numpy as np
from operator import itemgetter
X = np.array(range(1,11))
to_replace = [[2,3], [7,8]]
X[list(map(itemgetter(0), to_replace))] = -1
X = np.delete(X, list(map(lambda x: x[1:], to_replace)))
print(X)
#[ 1 2 -1 5 6 7 -1 10]
First we replace the first element of each pair with -1
. Then we delete the remaining elements.
I'm not sure if this can be done in one step, but here's a way using np.delete
:
import numpy as np
from operator import itemgetter
X = np.array(range(1,11))
to_replace = [[2,3], [7,8]]
X[list(map(itemgetter(0), to_replace))] = -1
X = np.delete(X, list(map(lambda x: x[1:], to_replace)))
print(X)
#[ 1 2 -1 5 6 7 -1 10]
First we replace the first element of each pair with -1
. Then we delete the remaining elements.
edited 59 mins ago
answered 1 hour ago
pault
12.6k31541
12.6k31541
Amm.. this is not fully a numpy-ish solution tho...
â U9-Forward
1 hour ago
add a comment |Â
Amm.. this is not fully a numpy-ish solution tho...
â U9-Forward
1 hour ago
Amm.. this is not fully a numpy-ish solution tho...
â U9-Forward
1 hour ago
Amm.. this is not fully a numpy-ish solution tho...
â U9-Forward
1 hour ago
add a comment |Â
up vote
3
down vote
A solution using numpy.delete
, similar to @pault, but more efficient as it uses pure numpy indexing. However, because of this efficient indexing, it means that you cannot pass jagged arrays as indices
Setup
a = np.array([1,2,3,4,5,6,7,8,9,10])
idx = np.stack([[2, 3], [7, 8]])
a[idx] = -1
np.delete(a, idx[:, 1:])
array([ 1, 2, -1, 5, 6, 7, -1, 10])
Definitely seems like a cleaner and more efficient way.
â BernardL
53 mins ago
add a comment |Â
up vote
3
down vote
A solution using numpy.delete
, similar to @pault, but more efficient as it uses pure numpy indexing. However, because of this efficient indexing, it means that you cannot pass jagged arrays as indices
Setup
a = np.array([1,2,3,4,5,6,7,8,9,10])
idx = np.stack([[2, 3], [7, 8]])
a[idx] = -1
np.delete(a, idx[:, 1:])
array([ 1, 2, -1, 5, 6, 7, -1, 10])
Definitely seems like a cleaner and more efficient way.
â BernardL
53 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
A solution using numpy.delete
, similar to @pault, but more efficient as it uses pure numpy indexing. However, because of this efficient indexing, it means that you cannot pass jagged arrays as indices
Setup
a = np.array([1,2,3,4,5,6,7,8,9,10])
idx = np.stack([[2, 3], [7, 8]])
a[idx] = -1
np.delete(a, idx[:, 1:])
array([ 1, 2, -1, 5, 6, 7, -1, 10])
A solution using numpy.delete
, similar to @pault, but more efficient as it uses pure numpy indexing. However, because of this efficient indexing, it means that you cannot pass jagged arrays as indices
Setup
a = np.array([1,2,3,4,5,6,7,8,9,10])
idx = np.stack([[2, 3], [7, 8]])
a[idx] = -1
np.delete(a, idx[:, 1:])
array([ 1, 2, -1, 5, 6, 7, -1, 10])
edited 53 mins ago
answered 55 mins ago
user3483203
27.4k72350
27.4k72350
Definitely seems like a cleaner and more efficient way.
â BernardL
53 mins ago
add a comment |Â
Definitely seems like a cleaner and more efficient way.
â BernardL
53 mins ago
Definitely seems like a cleaner and more efficient way.
â BernardL
53 mins ago
Definitely seems like a cleaner and more efficient way.
â BernardL
53 mins ago
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53000462%2freplace-multiple-elements-in-numpy-array-with-1%23new-answer', 'question_page');
);
Post as a guest
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
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
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
2
you don't have to be rude, especially when you are referring to a manual!
â khan
1 hour ago
Possible duplicate of Replace an element in a numpy array at specific index
â awiebe
1 hour ago
1
You should google then, if you have a different meaning in your mind.
â khan
1 hour ago
1
@awiebe Not sure what this means ... the question is looking for a numpy-ish way of replacing two entities in an array with a single entity (demo-ed by the examples too in which you can clearly see the size of array reduced by 2 elements).
â khan
1 hour ago
1
Ah, I see the question was resizing the array, not replacing the value. That makes more sense then how I read it.
â awiebe
1 hour ago