Numpy notation to replace an enumerate(zip(…))
I'm starting to use numpy. I get the slice notations & the element-wise computations, but I can't seem to wrap my head around this:
for i, (I,J) in enumerate(zip(data_list[0], data_list[1])):
joint_hist[int(np.floor(I/self.bin_size))][int(np.floor(J/self.bin_size))] += 1
Variables:
data_list
contains 2 np.array().flatten()
images (eventually more)
joint_hist
is displayed later with plt.imshow()
and is the joint histogram of those 2 images
bin_size
is the number of slots in the histogram
What I can't wrap my head around is the fact that the coordinate in the final histogram is I,J. So it's not just that the value at a position in joint_hist is the result of some slicing/element-wise computation. I need to take the result of that computation and use THAT as the indices in joint_hist...
EDIT:
I indeed do not use the i in the loop actually - it's a leftover from previous iterations and I simply hadn't noticed I didn't need it anymore
I do want to remain in control of the bin sizes & the details of how this is done, so not particularly looking to use histogramm2D. I will later be using that for further image processing, so I'd rather have the flexibility to adapt my approach than have to figure out if/how to do particular things with built-in functions.
python numpy
add a comment |
I'm starting to use numpy. I get the slice notations & the element-wise computations, but I can't seem to wrap my head around this:
for i, (I,J) in enumerate(zip(data_list[0], data_list[1])):
joint_hist[int(np.floor(I/self.bin_size))][int(np.floor(J/self.bin_size))] += 1
Variables:
data_list
contains 2 np.array().flatten()
images (eventually more)
joint_hist
is displayed later with plt.imshow()
and is the joint histogram of those 2 images
bin_size
is the number of slots in the histogram
What I can't wrap my head around is the fact that the coordinate in the final histogram is I,J. So it's not just that the value at a position in joint_hist is the result of some slicing/element-wise computation. I need to take the result of that computation and use THAT as the indices in joint_hist...
EDIT:
I indeed do not use the i in the loop actually - it's a leftover from previous iterations and I simply hadn't noticed I didn't need it anymore
I do want to remain in control of the bin sizes & the details of how this is done, so not particularly looking to use histogramm2D. I will later be using that for further image processing, so I'd rather have the flexibility to adapt my approach than have to figure out if/how to do particular things with built-in functions.
python numpy
1
I'm not sure I understand what you're asking. Your title refers toenumerate
, but since you never usei
in the loop, I'd think just dropping it is the best way to improve the code you've shown. It's not clear to me what the text in the body of the question is asking for. Are you just looking for a morenumpy
-natural way to do what the loop does?
– Blckknght
Nov 10 at 18:19
add a comment |
I'm starting to use numpy. I get the slice notations & the element-wise computations, but I can't seem to wrap my head around this:
for i, (I,J) in enumerate(zip(data_list[0], data_list[1])):
joint_hist[int(np.floor(I/self.bin_size))][int(np.floor(J/self.bin_size))] += 1
Variables:
data_list
contains 2 np.array().flatten()
images (eventually more)
joint_hist
is displayed later with plt.imshow()
and is the joint histogram of those 2 images
bin_size
is the number of slots in the histogram
What I can't wrap my head around is the fact that the coordinate in the final histogram is I,J. So it's not just that the value at a position in joint_hist is the result of some slicing/element-wise computation. I need to take the result of that computation and use THAT as the indices in joint_hist...
EDIT:
I indeed do not use the i in the loop actually - it's a leftover from previous iterations and I simply hadn't noticed I didn't need it anymore
I do want to remain in control of the bin sizes & the details of how this is done, so not particularly looking to use histogramm2D. I will later be using that for further image processing, so I'd rather have the flexibility to adapt my approach than have to figure out if/how to do particular things with built-in functions.
python numpy
I'm starting to use numpy. I get the slice notations & the element-wise computations, but I can't seem to wrap my head around this:
for i, (I,J) in enumerate(zip(data_list[0], data_list[1])):
joint_hist[int(np.floor(I/self.bin_size))][int(np.floor(J/self.bin_size))] += 1
Variables:
data_list
contains 2 np.array().flatten()
images (eventually more)
joint_hist
is displayed later with plt.imshow()
and is the joint histogram of those 2 images
bin_size
is the number of slots in the histogram
What I can't wrap my head around is the fact that the coordinate in the final histogram is I,J. So it's not just that the value at a position in joint_hist is the result of some slicing/element-wise computation. I need to take the result of that computation and use THAT as the indices in joint_hist...
EDIT:
I indeed do not use the i in the loop actually - it's a leftover from previous iterations and I simply hadn't noticed I didn't need it anymore
I do want to remain in control of the bin sizes & the details of how this is done, so not particularly looking to use histogramm2D. I will later be using that for further image processing, so I'd rather have the flexibility to adapt my approach than have to figure out if/how to do particular things with built-in functions.
python numpy
python numpy
edited Nov 10 at 19:26
asked Nov 10 at 18:12
LogicOnAbstractions
497
497
1
I'm not sure I understand what you're asking. Your title refers toenumerate
, but since you never usei
in the loop, I'd think just dropping it is the best way to improve the code you've shown. It's not clear to me what the text in the body of the question is asking for. Are you just looking for a morenumpy
-natural way to do what the loop does?
– Blckknght
Nov 10 at 18:19
add a comment |
1
I'm not sure I understand what you're asking. Your title refers toenumerate
, but since you never usei
in the loop, I'd think just dropping it is the best way to improve the code you've shown. It's not clear to me what the text in the body of the question is asking for. Are you just looking for a morenumpy
-natural way to do what the loop does?
– Blckknght
Nov 10 at 18:19
1
1
I'm not sure I understand what you're asking. Your title refers to
enumerate
, but since you never use i
in the loop, I'd think just dropping it is the best way to improve the code you've shown. It's not clear to me what the text in the body of the question is asking for. Are you just looking for a more numpy
-natural way to do what the loop does?– Blckknght
Nov 10 at 18:19
I'm not sure I understand what you're asking. Your title refers to
enumerate
, but since you never use i
in the loop, I'd think just dropping it is the best way to improve the code you've shown. It's not clear to me what the text in the body of the question is asking for. Are you just looking for a more numpy
-natural way to do what the loop does?– Blckknght
Nov 10 at 18:19
add a comment |
2 Answers
2
active
oldest
votes
You can indeed gussy up that for
loop using some numpy notation. Assuming you don't actually need i
(since it isn't used anywhere):
for I,J in (data_list.T // self.bin_size).astype(int):
joint_hist[I, J] += 1
Explanation
data_list.T
flips data_list
on its side. Each row of data_list.T
will contain the data for the pixels at a particular coordinate.
data_list.T // self.bin_size
will produce the same result as np.floor(I/self.bin_size)
, only it will operate on all of the pixels at once, instead of one at a time.
.astype(int)
does the same thing as int(...)
, but again operates on the entire array instead of a single element.
When you iterate over a 2D array with a for
loop, the rows are returned one at a time. Thus, the for I,J in arr
syntax will give you back one pair of pixels at a time, just like your zip
statement did originally.
Alternative
You could also just use histogramdd
to calculate joint_hist
, in place of your for
loop. For your application it would look like:
import numpy as np
joint_hist,edges = np.histogramdd(data_list.T)
This would have different bins than the ones you specified above, though (numpy would determine them automatically).
Good good. Yeah I did use histrogramdd initially but didn't see how to customize bins. I'll do further image manipulations on this so probably better to have my own implementation. .T, // and .astype() are exactly what I needed. I was trying to figure things out just with the slicing notations & so but obviously needed more. When you have a hammer everything looks like a nail... thanks.
– LogicOnAbstractions
Nov 10 at 19:34
add a comment |
If I understand, your goal is to make an histogram or correlated values in your images? Well, to achieve the right bin index, the computation that you used is not valid. Instead of np.floor(I/self.bin_size), use np.floor(I/(I_max/bin_size)).astype(int). You want to divide I and J by their respective resolution. The result that you will get is a diagonal matrix for joint_hist if both data_list[0] and data_list[1] are the same flattened image.
So all put together:
I_max = data_list[0].max()+1
J_max = data_list[1].max()+1
joint_hist = np.zeros((I_max, J_max))
bin_size = 256
for i, (I, J) in enumerate(zip(data_list[0], data_list[1])):
joint_hist[np.floor(I / (I_max / bin_size)).astype(int), np.floor(J / (J_max / bin_size)).astype(int)] += 1
Thanks for the solution - but the point really is to use numpy because I would like to optimize the code. The approach I had works but is not as fast as what I could achieve using proper numpy operations.
– LogicOnAbstractions
Nov 10 at 19:31
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%2f53241968%2fnumpy-notation-to-replace-an-enumeratezip%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
You can indeed gussy up that for
loop using some numpy notation. Assuming you don't actually need i
(since it isn't used anywhere):
for I,J in (data_list.T // self.bin_size).astype(int):
joint_hist[I, J] += 1
Explanation
data_list.T
flips data_list
on its side. Each row of data_list.T
will contain the data for the pixels at a particular coordinate.
data_list.T // self.bin_size
will produce the same result as np.floor(I/self.bin_size)
, only it will operate on all of the pixels at once, instead of one at a time.
.astype(int)
does the same thing as int(...)
, but again operates on the entire array instead of a single element.
When you iterate over a 2D array with a for
loop, the rows are returned one at a time. Thus, the for I,J in arr
syntax will give you back one pair of pixels at a time, just like your zip
statement did originally.
Alternative
You could also just use histogramdd
to calculate joint_hist
, in place of your for
loop. For your application it would look like:
import numpy as np
joint_hist,edges = np.histogramdd(data_list.T)
This would have different bins than the ones you specified above, though (numpy would determine them automatically).
Good good. Yeah I did use histrogramdd initially but didn't see how to customize bins. I'll do further image manipulations on this so probably better to have my own implementation. .T, // and .astype() are exactly what I needed. I was trying to figure things out just with the slicing notations & so but obviously needed more. When you have a hammer everything looks like a nail... thanks.
– LogicOnAbstractions
Nov 10 at 19:34
add a comment |
You can indeed gussy up that for
loop using some numpy notation. Assuming you don't actually need i
(since it isn't used anywhere):
for I,J in (data_list.T // self.bin_size).astype(int):
joint_hist[I, J] += 1
Explanation
data_list.T
flips data_list
on its side. Each row of data_list.T
will contain the data for the pixels at a particular coordinate.
data_list.T // self.bin_size
will produce the same result as np.floor(I/self.bin_size)
, only it will operate on all of the pixels at once, instead of one at a time.
.astype(int)
does the same thing as int(...)
, but again operates on the entire array instead of a single element.
When you iterate over a 2D array with a for
loop, the rows are returned one at a time. Thus, the for I,J in arr
syntax will give you back one pair of pixels at a time, just like your zip
statement did originally.
Alternative
You could also just use histogramdd
to calculate joint_hist
, in place of your for
loop. For your application it would look like:
import numpy as np
joint_hist,edges = np.histogramdd(data_list.T)
This would have different bins than the ones you specified above, though (numpy would determine them automatically).
Good good. Yeah I did use histrogramdd initially but didn't see how to customize bins. I'll do further image manipulations on this so probably better to have my own implementation. .T, // and .astype() are exactly what I needed. I was trying to figure things out just with the slicing notations & so but obviously needed more. When you have a hammer everything looks like a nail... thanks.
– LogicOnAbstractions
Nov 10 at 19:34
add a comment |
You can indeed gussy up that for
loop using some numpy notation. Assuming you don't actually need i
(since it isn't used anywhere):
for I,J in (data_list.T // self.bin_size).astype(int):
joint_hist[I, J] += 1
Explanation
data_list.T
flips data_list
on its side. Each row of data_list.T
will contain the data for the pixels at a particular coordinate.
data_list.T // self.bin_size
will produce the same result as np.floor(I/self.bin_size)
, only it will operate on all of the pixels at once, instead of one at a time.
.astype(int)
does the same thing as int(...)
, but again operates on the entire array instead of a single element.
When you iterate over a 2D array with a for
loop, the rows are returned one at a time. Thus, the for I,J in arr
syntax will give you back one pair of pixels at a time, just like your zip
statement did originally.
Alternative
You could also just use histogramdd
to calculate joint_hist
, in place of your for
loop. For your application it would look like:
import numpy as np
joint_hist,edges = np.histogramdd(data_list.T)
This would have different bins than the ones you specified above, though (numpy would determine them automatically).
You can indeed gussy up that for
loop using some numpy notation. Assuming you don't actually need i
(since it isn't used anywhere):
for I,J in (data_list.T // self.bin_size).astype(int):
joint_hist[I, J] += 1
Explanation
data_list.T
flips data_list
on its side. Each row of data_list.T
will contain the data for the pixels at a particular coordinate.
data_list.T // self.bin_size
will produce the same result as np.floor(I/self.bin_size)
, only it will operate on all of the pixels at once, instead of one at a time.
.astype(int)
does the same thing as int(...)
, but again operates on the entire array instead of a single element.
When you iterate over a 2D array with a for
loop, the rows are returned one at a time. Thus, the for I,J in arr
syntax will give you back one pair of pixels at a time, just like your zip
statement did originally.
Alternative
You could also just use histogramdd
to calculate joint_hist
, in place of your for
loop. For your application it would look like:
import numpy as np
joint_hist,edges = np.histogramdd(data_list.T)
This would have different bins than the ones you specified above, though (numpy would determine them automatically).
edited Nov 10 at 19:27
answered Nov 10 at 19:06
tel
5,76511430
5,76511430
Good good. Yeah I did use histrogramdd initially but didn't see how to customize bins. I'll do further image manipulations on this so probably better to have my own implementation. .T, // and .astype() are exactly what I needed. I was trying to figure things out just with the slicing notations & so but obviously needed more. When you have a hammer everything looks like a nail... thanks.
– LogicOnAbstractions
Nov 10 at 19:34
add a comment |
Good good. Yeah I did use histrogramdd initially but didn't see how to customize bins. I'll do further image manipulations on this so probably better to have my own implementation. .T, // and .astype() are exactly what I needed. I was trying to figure things out just with the slicing notations & so but obviously needed more. When you have a hammer everything looks like a nail... thanks.
– LogicOnAbstractions
Nov 10 at 19:34
Good good. Yeah I did use histrogramdd initially but didn't see how to customize bins. I'll do further image manipulations on this so probably better to have my own implementation. .T, // and .astype() are exactly what I needed. I was trying to figure things out just with the slicing notations & so but obviously needed more. When you have a hammer everything looks like a nail... thanks.
– LogicOnAbstractions
Nov 10 at 19:34
Good good. Yeah I did use histrogramdd initially but didn't see how to customize bins. I'll do further image manipulations on this so probably better to have my own implementation. .T, // and .astype() are exactly what I needed. I was trying to figure things out just with the slicing notations & so but obviously needed more. When you have a hammer everything looks like a nail... thanks.
– LogicOnAbstractions
Nov 10 at 19:34
add a comment |
If I understand, your goal is to make an histogram or correlated values in your images? Well, to achieve the right bin index, the computation that you used is not valid. Instead of np.floor(I/self.bin_size), use np.floor(I/(I_max/bin_size)).astype(int). You want to divide I and J by their respective resolution. The result that you will get is a diagonal matrix for joint_hist if both data_list[0] and data_list[1] are the same flattened image.
So all put together:
I_max = data_list[0].max()+1
J_max = data_list[1].max()+1
joint_hist = np.zeros((I_max, J_max))
bin_size = 256
for i, (I, J) in enumerate(zip(data_list[0], data_list[1])):
joint_hist[np.floor(I / (I_max / bin_size)).astype(int), np.floor(J / (J_max / bin_size)).astype(int)] += 1
Thanks for the solution - but the point really is to use numpy because I would like to optimize the code. The approach I had works but is not as fast as what I could achieve using proper numpy operations.
– LogicOnAbstractions
Nov 10 at 19:31
add a comment |
If I understand, your goal is to make an histogram or correlated values in your images? Well, to achieve the right bin index, the computation that you used is not valid. Instead of np.floor(I/self.bin_size), use np.floor(I/(I_max/bin_size)).astype(int). You want to divide I and J by their respective resolution. The result that you will get is a diagonal matrix for joint_hist if both data_list[0] and data_list[1] are the same flattened image.
So all put together:
I_max = data_list[0].max()+1
J_max = data_list[1].max()+1
joint_hist = np.zeros((I_max, J_max))
bin_size = 256
for i, (I, J) in enumerate(zip(data_list[0], data_list[1])):
joint_hist[np.floor(I / (I_max / bin_size)).astype(int), np.floor(J / (J_max / bin_size)).astype(int)] += 1
Thanks for the solution - but the point really is to use numpy because I would like to optimize the code. The approach I had works but is not as fast as what I could achieve using proper numpy operations.
– LogicOnAbstractions
Nov 10 at 19:31
add a comment |
If I understand, your goal is to make an histogram or correlated values in your images? Well, to achieve the right bin index, the computation that you used is not valid. Instead of np.floor(I/self.bin_size), use np.floor(I/(I_max/bin_size)).astype(int). You want to divide I and J by their respective resolution. The result that you will get is a diagonal matrix for joint_hist if both data_list[0] and data_list[1] are the same flattened image.
So all put together:
I_max = data_list[0].max()+1
J_max = data_list[1].max()+1
joint_hist = np.zeros((I_max, J_max))
bin_size = 256
for i, (I, J) in enumerate(zip(data_list[0], data_list[1])):
joint_hist[np.floor(I / (I_max / bin_size)).astype(int), np.floor(J / (J_max / bin_size)).astype(int)] += 1
If I understand, your goal is to make an histogram or correlated values in your images? Well, to achieve the right bin index, the computation that you used is not valid. Instead of np.floor(I/self.bin_size), use np.floor(I/(I_max/bin_size)).astype(int). You want to divide I and J by their respective resolution. The result that you will get is a diagonal matrix for joint_hist if both data_list[0] and data_list[1] are the same flattened image.
So all put together:
I_max = data_list[0].max()+1
J_max = data_list[1].max()+1
joint_hist = np.zeros((I_max, J_max))
bin_size = 256
for i, (I, J) in enumerate(zip(data_list[0], data_list[1])):
joint_hist[np.floor(I / (I_max / bin_size)).astype(int), np.floor(J / (J_max / bin_size)).astype(int)] += 1
answered Nov 10 at 19:10
Jean-Christophe
112
112
Thanks for the solution - but the point really is to use numpy because I would like to optimize the code. The approach I had works but is not as fast as what I could achieve using proper numpy operations.
– LogicOnAbstractions
Nov 10 at 19:31
add a comment |
Thanks for the solution - but the point really is to use numpy because I would like to optimize the code. The approach I had works but is not as fast as what I could achieve using proper numpy operations.
– LogicOnAbstractions
Nov 10 at 19:31
Thanks for the solution - but the point really is to use numpy because I would like to optimize the code. The approach I had works but is not as fast as what I could achieve using proper numpy operations.
– LogicOnAbstractions
Nov 10 at 19:31
Thanks for the solution - but the point really is to use numpy because I would like to optimize the code. The approach I had works but is not as fast as what I could achieve using proper numpy operations.
– LogicOnAbstractions
Nov 10 at 19: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%2f53241968%2fnumpy-notation-to-replace-an-enumeratezip%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
1
I'm not sure I understand what you're asking. Your title refers to
enumerate
, but since you never usei
in the loop, I'd think just dropping it is the best way to improve the code you've shown. It's not clear to me what the text in the body of the question is asking for. Are you just looking for a morenumpy
-natural way to do what the loop does?– Blckknght
Nov 10 at 18:19