changing array value one at a time












0















I am trying to flip a binary array one at a time.



import numpy as np
k = np.array([0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1])


for example my out put should be like this;



[1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 1st output
[0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 2nd output
[0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 3rd output
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 4th output
[0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 5th output


In the first output, I want to flip only the first element of the array (other elements do not change), in the second output, the second element should change (the 1st and the remaining elements should not change).. etc.
Could anyone tell me how can flip one at a time? Thank you










share|improve this question


















  • 1





    What have you tried so far?

    – Ian Quah
    Nov 26 '18 at 20:06
















0















I am trying to flip a binary array one at a time.



import numpy as np
k = np.array([0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1])


for example my out put should be like this;



[1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 1st output
[0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 2nd output
[0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 3rd output
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 4th output
[0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 5th output


In the first output, I want to flip only the first element of the array (other elements do not change), in the second output, the second element should change (the 1st and the remaining elements should not change).. etc.
Could anyone tell me how can flip one at a time? Thank you










share|improve this question


















  • 1





    What have you tried so far?

    – Ian Quah
    Nov 26 '18 at 20:06














0












0








0








I am trying to flip a binary array one at a time.



import numpy as np
k = np.array([0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1])


for example my out put should be like this;



[1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 1st output
[0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 2nd output
[0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 3rd output
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 4th output
[0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 5th output


In the first output, I want to flip only the first element of the array (other elements do not change), in the second output, the second element should change (the 1st and the remaining elements should not change).. etc.
Could anyone tell me how can flip one at a time? Thank you










share|improve this question














I am trying to flip a binary array one at a time.



import numpy as np
k = np.array([0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1])


for example my out put should be like this;



[1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 1st output
[0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 2nd output
[0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 3rd output
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 4th output
[0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 5th output


In the first output, I want to flip only the first element of the array (other elements do not change), in the second output, the second element should change (the 1st and the remaining elements should not change).. etc.
Could anyone tell me how can flip one at a time? Thank you







python arrays python-3.x numpy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 20:02









Mass17Mass17

838




838








  • 1





    What have you tried so far?

    – Ian Quah
    Nov 26 '18 at 20:06














  • 1





    What have you tried so far?

    – Ian Quah
    Nov 26 '18 at 20:06








1




1





What have you tried so far?

– Ian Quah
Nov 26 '18 at 20:06





What have you tried so far?

– Ian Quah
Nov 26 '18 at 20:06












2 Answers
2






active

oldest

votes


















1














What you're describing is flipping the diagonal of a tiled version of your array. By stacking your array, you can operate on the entire array at once, using vectorized operations, rather than operating on each row individually.



Setup



arr = np.tile(k, 5).reshape(-1, k.shape[0])




array([[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1]])




Using numpy.diag_indices:



x, y = np.diag_indices(arr.shape[0])
arr[x, y] = 1 - arr[x, y]




array([[1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1]])





share|improve this answer



















  • 1





    Thanks @user3483203. Very helpful technique!!

    – Mass17
    Nov 26 '18 at 20:19



















1














You can use a generator to save memory and even time on big arrays :



k=np.array([0, 0, 1])

def flip_one(k):
k[0]=1-k[0]
yield k
for i in range(len(k)):
k[i:i+2]=1-k[i:i+2]
yield k

for f in flip_one(k) :
print (f) # or other useful things!
#[1 0 1]
#[0 1 1]
#[0 0 0]
#[0 0 1]


k is reset at the end of the loop.






share|improve this answer


























  • Thanks @B. M.. very helpful!!

    – Mass17
    Nov 26 '18 at 21:03











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488187%2fchanging-array-value-one-at-a-time%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














What you're describing is flipping the diagonal of a tiled version of your array. By stacking your array, you can operate on the entire array at once, using vectorized operations, rather than operating on each row individually.



Setup



arr = np.tile(k, 5).reshape(-1, k.shape[0])




array([[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1]])




Using numpy.diag_indices:



x, y = np.diag_indices(arr.shape[0])
arr[x, y] = 1 - arr[x, y]




array([[1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1]])





share|improve this answer



















  • 1





    Thanks @user3483203. Very helpful technique!!

    – Mass17
    Nov 26 '18 at 20:19
















1














What you're describing is flipping the diagonal of a tiled version of your array. By stacking your array, you can operate on the entire array at once, using vectorized operations, rather than operating on each row individually.



Setup



arr = np.tile(k, 5).reshape(-1, k.shape[0])




array([[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1]])




Using numpy.diag_indices:



x, y = np.diag_indices(arr.shape[0])
arr[x, y] = 1 - arr[x, y]




array([[1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1]])





share|improve this answer



















  • 1





    Thanks @user3483203. Very helpful technique!!

    – Mass17
    Nov 26 '18 at 20:19














1












1








1







What you're describing is flipping the diagonal of a tiled version of your array. By stacking your array, you can operate on the entire array at once, using vectorized operations, rather than operating on each row individually.



Setup



arr = np.tile(k, 5).reshape(-1, k.shape[0])




array([[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1]])




Using numpy.diag_indices:



x, y = np.diag_indices(arr.shape[0])
arr[x, y] = 1 - arr[x, y]




array([[1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1]])





share|improve this answer













What you're describing is flipping the diagonal of a tiled version of your array. By stacking your array, you can operate on the entire array at once, using vectorized operations, rather than operating on each row individually.



Setup



arr = np.tile(k, 5).reshape(-1, k.shape[0])




array([[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1]])




Using numpy.diag_indices:



x, y = np.diag_indices(arr.shape[0])
arr[x, y] = 1 - arr[x, y]




array([[1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1]])






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 20:11









user3483203user3483203

31.2k82655




31.2k82655








  • 1





    Thanks @user3483203. Very helpful technique!!

    – Mass17
    Nov 26 '18 at 20:19














  • 1





    Thanks @user3483203. Very helpful technique!!

    – Mass17
    Nov 26 '18 at 20:19








1




1





Thanks @user3483203. Very helpful technique!!

– Mass17
Nov 26 '18 at 20:19





Thanks @user3483203. Very helpful technique!!

– Mass17
Nov 26 '18 at 20:19













1














You can use a generator to save memory and even time on big arrays :



k=np.array([0, 0, 1])

def flip_one(k):
k[0]=1-k[0]
yield k
for i in range(len(k)):
k[i:i+2]=1-k[i:i+2]
yield k

for f in flip_one(k) :
print (f) # or other useful things!
#[1 0 1]
#[0 1 1]
#[0 0 0]
#[0 0 1]


k is reset at the end of the loop.






share|improve this answer


























  • Thanks @B. M.. very helpful!!

    – Mass17
    Nov 26 '18 at 21:03
















1














You can use a generator to save memory and even time on big arrays :



k=np.array([0, 0, 1])

def flip_one(k):
k[0]=1-k[0]
yield k
for i in range(len(k)):
k[i:i+2]=1-k[i:i+2]
yield k

for f in flip_one(k) :
print (f) # or other useful things!
#[1 0 1]
#[0 1 1]
#[0 0 0]
#[0 0 1]


k is reset at the end of the loop.






share|improve this answer


























  • Thanks @B. M.. very helpful!!

    – Mass17
    Nov 26 '18 at 21:03














1












1








1







You can use a generator to save memory and even time on big arrays :



k=np.array([0, 0, 1])

def flip_one(k):
k[0]=1-k[0]
yield k
for i in range(len(k)):
k[i:i+2]=1-k[i:i+2]
yield k

for f in flip_one(k) :
print (f) # or other useful things!
#[1 0 1]
#[0 1 1]
#[0 0 0]
#[0 0 1]


k is reset at the end of the loop.






share|improve this answer















You can use a generator to save memory and even time on big arrays :



k=np.array([0, 0, 1])

def flip_one(k):
k[0]=1-k[0]
yield k
for i in range(len(k)):
k[i:i+2]=1-k[i:i+2]
yield k

for f in flip_one(k) :
print (f) # or other useful things!
#[1 0 1]
#[0 1 1]
#[0 0 0]
#[0 0 1]


k is reset at the end of the loop.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 20:41

























answered Nov 26 '18 at 20:35









B. M.B. M.

13.4k12034




13.4k12034













  • Thanks @B. M.. very helpful!!

    – Mass17
    Nov 26 '18 at 21:03



















  • Thanks @B. M.. very helpful!!

    – Mass17
    Nov 26 '18 at 21:03

















Thanks @B. M.. very helpful!!

– Mass17
Nov 26 '18 at 21:03





Thanks @B. M.. very helpful!!

– Mass17
Nov 26 '18 at 21:03


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488187%2fchanging-array-value-one-at-a-time%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)