'Piling' an Array, Collapse by Summation












1















Given a numpy array, I'd like to sum up uniform chunks of elements to form a new, smaller, array. It's similar to binning, but not by frequency. I'm not sure how else to describe it other than by example (below).



The question: Is there either a function for this or cleaner approach (using numpy/scipy)? I've looked into digitize and histogram, but think their implementations are lengthy. I've also thought about crafty indexing, but it's beyond me and might make a long ugly line of code.



import numpy as np  

old_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])
bin_size = 3
new_data = np.zeros(int(np.size(old_data) / bin_size))
for ind, val in enumerate(new_data):
leap = ind*bin_size
new_data[ind] =
np.sum(old_data[leap:leap+bin_size])
print(old_data, '->', bin_size, ':', new_data)

# [0 1 2 3 4 5 6 7 8] -> 3 : [ 3. 12. 21.]









share|improve this question























  • maybe np.array([sum(old_data[bin_size*i: bin_size*(i+1)]) for i in range(old_data.size // bin_size)])

    – William Lee
    Nov 22 '18 at 2:58
















1















Given a numpy array, I'd like to sum up uniform chunks of elements to form a new, smaller, array. It's similar to binning, but not by frequency. I'm not sure how else to describe it other than by example (below).



The question: Is there either a function for this or cleaner approach (using numpy/scipy)? I've looked into digitize and histogram, but think their implementations are lengthy. I've also thought about crafty indexing, but it's beyond me and might make a long ugly line of code.



import numpy as np  

old_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])
bin_size = 3
new_data = np.zeros(int(np.size(old_data) / bin_size))
for ind, val in enumerate(new_data):
leap = ind*bin_size
new_data[ind] =
np.sum(old_data[leap:leap+bin_size])
print(old_data, '->', bin_size, ':', new_data)

# [0 1 2 3 4 5 6 7 8] -> 3 : [ 3. 12. 21.]









share|improve this question























  • maybe np.array([sum(old_data[bin_size*i: bin_size*(i+1)]) for i in range(old_data.size // bin_size)])

    – William Lee
    Nov 22 '18 at 2:58














1












1








1








Given a numpy array, I'd like to sum up uniform chunks of elements to form a new, smaller, array. It's similar to binning, but not by frequency. I'm not sure how else to describe it other than by example (below).



The question: Is there either a function for this or cleaner approach (using numpy/scipy)? I've looked into digitize and histogram, but think their implementations are lengthy. I've also thought about crafty indexing, but it's beyond me and might make a long ugly line of code.



import numpy as np  

old_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])
bin_size = 3
new_data = np.zeros(int(np.size(old_data) / bin_size))
for ind, val in enumerate(new_data):
leap = ind*bin_size
new_data[ind] =
np.sum(old_data[leap:leap+bin_size])
print(old_data, '->', bin_size, ':', new_data)

# [0 1 2 3 4 5 6 7 8] -> 3 : [ 3. 12. 21.]









share|improve this question














Given a numpy array, I'd like to sum up uniform chunks of elements to form a new, smaller, array. It's similar to binning, but not by frequency. I'm not sure how else to describe it other than by example (below).



The question: Is there either a function for this or cleaner approach (using numpy/scipy)? I've looked into digitize and histogram, but think their implementations are lengthy. I've also thought about crafty indexing, but it's beyond me and might make a long ugly line of code.



import numpy as np  

old_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])
bin_size = 3
new_data = np.zeros(int(np.size(old_data) / bin_size))
for ind, val in enumerate(new_data):
leap = ind*bin_size
new_data[ind] =
np.sum(old_data[leap:leap+bin_size])
print(old_data, '->', bin_size, ':', new_data)

# [0 1 2 3 4 5 6 7 8] -> 3 : [ 3. 12. 21.]






python numpy scipy sum bin






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 2:52









Captain MorganCaptain Morgan

1406




1406













  • maybe np.array([sum(old_data[bin_size*i: bin_size*(i+1)]) for i in range(old_data.size // bin_size)])

    – William Lee
    Nov 22 '18 at 2:58



















  • maybe np.array([sum(old_data[bin_size*i: bin_size*(i+1)]) for i in range(old_data.size // bin_size)])

    – William Lee
    Nov 22 '18 at 2:58

















maybe np.array([sum(old_data[bin_size*i: bin_size*(i+1)]) for i in range(old_data.size // bin_size)])

– William Lee
Nov 22 '18 at 2:58





maybe np.array([sum(old_data[bin_size*i: bin_size*(i+1)]) for i in range(old_data.size // bin_size)])

– William Lee
Nov 22 '18 at 2:58












1 Answer
1






active

oldest

votes


















1














Assuming there's an integral number of bins, you can accomplish this with a reshape:



old_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])
bin_size = 3

new_data = old_data.reshape(-1, bin_size).sum(axis=1)


new_data will then have the desired value of:



array([ 3, 12, 21])


If bin_size doesn't divide evenly into old_data.size, you can instead use resize:



old_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
bin_size = 3

old_data.resize(old_data.size//bin_size + 1, bin_size)
new_data = old_data.sum(axis=1)


new_data will then have a value of:



array([ 3, 12, 21, 19])


Using resize has the downside of modifying old_data in place, so if you want to keep old_data around you should probably make a copy of it before you do the resize.






share|improve this answer


























  • This is concise and does exactly as desired in a very legible manner. Thank you!

    – Captain Morgan
    Nov 22 '18 at 3:16











  • Is there a clean way to do this without altering the original array? One could simply np.copy it into a dummy array, but surely there's a slick way around this.

    – Captain Morgan
    Nov 22 '18 at 3:22











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%2f53423217%2fpiling-an-array-collapse-by-summation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Assuming there's an integral number of bins, you can accomplish this with a reshape:



old_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])
bin_size = 3

new_data = old_data.reshape(-1, bin_size).sum(axis=1)


new_data will then have the desired value of:



array([ 3, 12, 21])


If bin_size doesn't divide evenly into old_data.size, you can instead use resize:



old_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
bin_size = 3

old_data.resize(old_data.size//bin_size + 1, bin_size)
new_data = old_data.sum(axis=1)


new_data will then have a value of:



array([ 3, 12, 21, 19])


Using resize has the downside of modifying old_data in place, so if you want to keep old_data around you should probably make a copy of it before you do the resize.






share|improve this answer


























  • This is concise and does exactly as desired in a very legible manner. Thank you!

    – Captain Morgan
    Nov 22 '18 at 3:16











  • Is there a clean way to do this without altering the original array? One could simply np.copy it into a dummy array, but surely there's a slick way around this.

    – Captain Morgan
    Nov 22 '18 at 3:22
















1














Assuming there's an integral number of bins, you can accomplish this with a reshape:



old_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])
bin_size = 3

new_data = old_data.reshape(-1, bin_size).sum(axis=1)


new_data will then have the desired value of:



array([ 3, 12, 21])


If bin_size doesn't divide evenly into old_data.size, you can instead use resize:



old_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
bin_size = 3

old_data.resize(old_data.size//bin_size + 1, bin_size)
new_data = old_data.sum(axis=1)


new_data will then have a value of:



array([ 3, 12, 21, 19])


Using resize has the downside of modifying old_data in place, so if you want to keep old_data around you should probably make a copy of it before you do the resize.






share|improve this answer


























  • This is concise and does exactly as desired in a very legible manner. Thank you!

    – Captain Morgan
    Nov 22 '18 at 3:16











  • Is there a clean way to do this without altering the original array? One could simply np.copy it into a dummy array, but surely there's a slick way around this.

    – Captain Morgan
    Nov 22 '18 at 3:22














1












1








1







Assuming there's an integral number of bins, you can accomplish this with a reshape:



old_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])
bin_size = 3

new_data = old_data.reshape(-1, bin_size).sum(axis=1)


new_data will then have the desired value of:



array([ 3, 12, 21])


If bin_size doesn't divide evenly into old_data.size, you can instead use resize:



old_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
bin_size = 3

old_data.resize(old_data.size//bin_size + 1, bin_size)
new_data = old_data.sum(axis=1)


new_data will then have a value of:



array([ 3, 12, 21, 19])


Using resize has the downside of modifying old_data in place, so if you want to keep old_data around you should probably make a copy of it before you do the resize.






share|improve this answer















Assuming there's an integral number of bins, you can accomplish this with a reshape:



old_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])
bin_size = 3

new_data = old_data.reshape(-1, bin_size).sum(axis=1)


new_data will then have the desired value of:



array([ 3, 12, 21])


If bin_size doesn't divide evenly into old_data.size, you can instead use resize:



old_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
bin_size = 3

old_data.resize(old_data.size//bin_size + 1, bin_size)
new_data = old_data.sum(axis=1)


new_data will then have a value of:



array([ 3, 12, 21, 19])


Using resize has the downside of modifying old_data in place, so if you want to keep old_data around you should probably make a copy of it before you do the resize.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 3:13

























answered Nov 22 '18 at 3:01









teltel

7,41621431




7,41621431













  • This is concise and does exactly as desired in a very legible manner. Thank you!

    – Captain Morgan
    Nov 22 '18 at 3:16











  • Is there a clean way to do this without altering the original array? One could simply np.copy it into a dummy array, but surely there's a slick way around this.

    – Captain Morgan
    Nov 22 '18 at 3:22



















  • This is concise and does exactly as desired in a very legible manner. Thank you!

    – Captain Morgan
    Nov 22 '18 at 3:16











  • Is there a clean way to do this without altering the original array? One could simply np.copy it into a dummy array, but surely there's a slick way around this.

    – Captain Morgan
    Nov 22 '18 at 3:22

















This is concise and does exactly as desired in a very legible manner. Thank you!

– Captain Morgan
Nov 22 '18 at 3:16





This is concise and does exactly as desired in a very legible manner. Thank you!

– Captain Morgan
Nov 22 '18 at 3:16













Is there a clean way to do this without altering the original array? One could simply np.copy it into a dummy array, but surely there's a slick way around this.

– Captain Morgan
Nov 22 '18 at 3:22





Is there a clean way to do this without altering the original array? One could simply np.copy it into a dummy array, but surely there's a slick way around this.

– Captain Morgan
Nov 22 '18 at 3:22




















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%2f53423217%2fpiling-an-array-collapse-by-summation%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

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules