[Numpy-discussion] np.histogram on arrays.
Thouis (Ray) Jones
thouis at gmail.com
Wed Mar 30 05:48:51 EDT 2011
How about something like this:
# numpy 1.6
def rowhist(A, bins=100):
assert (bins > 0)
assert isinstance(bins, int)
rownum = np.arange(A.shape[0]).reshape((-1, 1)).astype(int) * bins
intA = (bins * (A - A.min()) / float(A.max() - A.min())).astype(int)
intA[intA == bins] = bins - 1
return np.bincount((intA + rownum).flatten(),
minlength=(A.shape[0]).reshape((A.shape[0], bins))
# numpy 1.5
def rowhist(A, bins=100):
assert (bins > 0)
assert isinstance(bins, int)
rownum = np.arange(A.shape[0]).reshape((-1, 1)).astype(int) * bins
intA = (bins * (A - A.min()) / float(A.max() - A.min())).astype(int)
intA[intA == bins] = bins - 1
counts = np.zeros(A.shape[0] * bins)
bc = np.bincount((intA + rownum).flatten())
counts[:len(bc)] = bc
return counts.reshape((A.shape[0], bins))
On Wed, Mar 30, 2011 at 09:04, Éric Depagne <eric at depagne.org> wrote:
> Hi.
>
> Sorry for not having been clearer. I'll explain a little bit.
>
> I have 4k x 4k images that I want to analyse. I turn them into numpy arrays so
> I have 4k x 4k np.array.
>
> My analysis starts with determining the bias level. To do that, I compute for
> each line, and then for each row, an histogram.
> So I compute 8000 histograms.
>
> Here is the code I've used sofar:
>
> for i in range(self.data.shape[0]):
> #Compute an histogram along the columns
> # Gets counts and bounds
> self.countsC[i], self.boundsC[i] = np.histogram(data[i],
> bins=self.bins)
> for i in range(self.data.shape[1]):
> # Do the same, along the rows.
> self.countsR[i], self.boundsR[i] = np.histogram(data[:,i],
> bins=self.bins)
>
> And data.shape is (4000,4000).
>
> If histogram had an axis parameter, I could avoid the loop and I guess it
> would be faster.
>
> Éric.
>> So it seems that you give your array directly to histogramdd (asking a
>> 4000D histogram!). Surely that's not what you are trying to achieve. Can
>> you elaborate more on your objectives? Perhaps some code (slow but
>> working) to demonstrate the point.
>>
>> Regards,
>> eat
>>
>
> Un clavier azerty en vaut deux
> ----------------------------------------------------------
> Éric Depagne eric at depagne.org
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
More information about the NumPy-Discussion
mailing list