Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73449 > unrolled thread

Problem with numpy 2D Histogram

Started byJamie Mitchell <jamiemitchell1604@gmail.com>
First post2014-06-20 01:46 -0700
Last post2014-06-20 06:54 -0700
Articles 7 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Problem with numpy 2D Histogram Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-06-20 01:46 -0700
    Re: Problem with numpy 2D Histogram Peter Otten <__peter__@web.de> - 2014-06-20 11:25 +0200
      Re: Problem with numpy 2D Histogram Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-06-20 02:57 -0700
        Re: Problem with numpy 2D Histogram Peter Otten <__peter__@web.de> - 2014-06-20 13:00 +0200
          Re: Problem with numpy 2D Histogram Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-06-20 04:58 -0700
            Re: Problem with numpy 2D Histogram Peter Otten <__peter__@web.de> - 2014-06-20 14:30 +0200
    Re: Problem with numpy 2D Histogram Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-06-20 06:54 -0700

#73449 — Problem with numpy 2D Histogram

FromJamie Mitchell <jamiemitchell1604@gmail.com>
Date2014-06-20 01:46 -0700
SubjectProblem with numpy 2D Histogram
Message-ID<f6254d0b-4803-47c8-8b58-aadc9d762610@googlegroups.com>
Hi folks,

I'm trying to plot a 2D histogram but I'm having some issues:
from pylab import *
import numpy as np
import netCDF4
hist,xedges,yedges=np.histogram2d(x,y,bins=10)
extent=[xedges[0],xedges[-1],yedges[0],yedges[-1]]
imshow(hist.T,extent=extent,interpolation='nearest')
colorbar()
show()

After the first line of code I get:
TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'

I'm using python2.7, x and y are type 'numpy.ndarray'

Cheers,
Jamie

[toc] | [next] | [standalone]


#73450

FromPeter Otten <__peter__@web.de>
Date2014-06-20 11:25 +0200
Message-ID<mailman.11162.1403256365.18130.python-list@python.org>
In reply to#73449
Jamie Mitchell wrote:

> Hi folks,
> 
> I'm trying to plot a 2D histogram but I'm having some issues:
> from pylab import *
> import numpy as np
> import netCDF4
> hist,xedges,yedges=np.histogram2d(x,y,bins=10)
> extent=[xedges[0],xedges[-1],yedges[0],yedges[-1]]
> imshow(hist.T,extent=extent,interpolation='nearest')
> colorbar()
> show()
> 
> After the first line of code I get:
> TypeError: Cannot cast array data from dtype('O') to dtype('float64')
> according to the rule 'safe'
> 
> I'm using python2.7, x and y are type 'numpy.ndarray'

The error message complains about the dtype, i. e. the type of the elements 
in the array, not the array itself. Make sure the elements are floating 
point numbers or something compatible, not arbitrary Python objects.
As a baseline the following works

from pylab import *
import numpy as np

x, y = np.random.randn(2, 100)
print "x", type(x), x.dtype
print "y", type(y), y.dtype

hist, xedges, yedges = np.histogram2d(x, y, bins=10)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
imshow(hist.T, extent=extent, interpolation='nearest')
colorbar()
show()

while this doesn't:

#...
x, y = np.random.randn(2, 100)
import decimal
y = np.array([decimal.Decimal.from_float(v) for v in y])
#...

[toc] | [prev] | [next] | [standalone]


#73451

FromJamie Mitchell <jamiemitchell1604@gmail.com>
Date2014-06-20 02:57 -0700
Message-ID<ba14b490-1f5e-4079-9acb-b15f7b72afc9@googlegroups.com>
In reply to#73450
On Friday, June 20, 2014 10:25:44 AM UTC+1, Peter Otten wrote:
> Jamie Mitchell wrote:
> 
> 
> 
> > Hi folks,
> 
> > 
> 
> > I'm trying to plot a 2D histogram but I'm having some issues:
> 
> > from pylab import *
> 
> > import numpy as np
> 
> > import netCDF4
> 
> > hist,xedges,yedges=np.histogram2d(x,y,bins=10)
> 
> > extent=[xedges[0],xedges[-1],yedges[0],yedges[-1]]
> 
> > imshow(hist.T,extent=extent,interpolation='nearest')
> 
> > colorbar()
> 
> > show()
> 
> > 
> 
> > After the first line of code I get:
> 
> > TypeError: Cannot cast array data from dtype('O') to dtype('float64')
> 
> > according to the rule 'safe'
> 
> > 
> 
> > I'm using python2.7, x and y are type 'numpy.ndarray'
> 
> 
> 
> The error message complains about the dtype, i. e. the type of the elements 
> 
> in the array, not the array itself. Make sure the elements are floating 
> 
> point numbers or something compatible, not arbitrary Python objects.
> 
> As a baseline the following works
> 
> 
> 
> from pylab import *
> 
> import numpy as np
> 
> 
> 
> x, y = np.random.randn(2, 100)
> 
> print "x", type(x), x.dtype
> 
> print "y", type(y), y.dtype
> 
> 
> 
> hist, xedges, yedges = np.histogram2d(x, y, bins=10)
> 
> extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
> 
> imshow(hist.T, extent=extent, interpolation='nearest')
> 
> colorbar()
> 
> show()
> 
> 
> 
> while this doesn't:
> 
> 
> 
> #...
> 
> x, y = np.random.randn(2, 100)
> 
> import decimal
> 
> y = np.array([decimal.Decimal.from_float(v) for v in y])
> 
> #...

Thanks Peter.

I have changed my x and y data to float64 types but I am still getting the same error message?

Cheers,
Jamie

[toc] | [prev] | [next] | [standalone]


#73452

FromPeter Otten <__peter__@web.de>
Date2014-06-20 13:00 +0200
Message-ID<mailman.11163.1403262032.18130.python-list@python.org>
In reply to#73451
Jamie Mitchell wrote:

> I have changed my x and y data to float64 types but I am still getting the
> same error message?

Please double-check by adding

assert x.dtype == np.float64
assert y.dtype == np.float64

If none of these assertions fail try to make a minimal script including some 
data that provokes the TypeError and post it here.

[toc] | [prev] | [next] | [standalone]


#73453

FromJamie Mitchell <jamiemitchell1604@gmail.com>
Date2014-06-20 04:58 -0700
Message-ID<aedf37ab-7787-4f63-9437-faa1da980bdf@googlegroups.com>
In reply to#73452
On Friday, June 20, 2014 12:00:15 PM UTC+1, Peter Otten wrote:
> Jamie Mitchell wrote:
> 
> 
> 
> > I have changed my x and y data to float64 types but I am still getting the
> 
> > same error message?
> 
> 
> 
> Please double-check by adding
> 
> 
> 
> assert x.dtype == np.float64
> 
> assert y.dtype == np.float64
> 
> 
> 
> If none of these assertions fail try to make a minimal script including some 
> 
> data that provokes the TypeError and post it here.

OK this is my code:

swh_Q0_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/south_west/swhcontrol_swest_annavg.nc','r')
hs_Q0_con_sw=swh_Q0_con_sw.variables['hs'][:]
x=hs_Q0_con_sw.astype(float64)
# When I print the dtype of x here it says 'float64'
mwp_Q0_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q0/mean_wave_period/south_west/controlperiod/mwpcontrol_swest_annavg1D.nc','r')
te_Q0_con_sw=mwp_Q0_con_sw.variables['te'][:]
y=te_Q0_con_sw.astype(float64)
If I try assert x.dtype == np.float64 I get:
AssertionError

hist,xedges,yedges=np.histogram2d(x,y,bins=10)
TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe' 

Thanks,

Jamie

[toc] | [prev] | [next] | [standalone]


#73454

FromPeter Otten <__peter__@web.de>
Date2014-06-20 14:30 +0200
Message-ID<mailman.11164.1403267450.18130.python-list@python.org>
In reply to#73453
Jamie Mitchell wrote:

> On Friday, June 20, 2014 12:00:15 PM UTC+1, Peter Otten wrote:
>> Jamie Mitchell wrote:
>> 
>> 
>> 
>> > I have changed my x and y data to float64 types but I am still getting
>> > the
>> 
>> > same error message?
>> 
>> 
>> 
>> Please double-check by adding
>> 
>> 
>> 
>> assert x.dtype == np.float64
>> 
>> assert y.dtype == np.float64
>> 
>> 
>> 
>> If none of these assertions fail try to make a minimal script including
>> some
>> 
>> data that provokes the TypeError and post it here.
> 
> OK this is my code:
> 
> 
swh_Q0_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/south_west/swhcontrol_swest_annavg.nc','r')
> hs_Q0_con_sw=swh_Q0_con_sw.variables['hs'][:]
> x=hs_Q0_con_sw.astype(float64)
> # When I print the dtype of x here it says 'float64'
> 
mwp_Q0_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q0/mean_wave_period/south_west/controlperiod/mwpcontrol_swest_annavg1D.nc','r')
> te_Q0_con_sw=mwp_Q0_con_sw.variables['te'][:]
> y=te_Q0_con_sw.astype(float64)
> If I try assert x.dtype == np.float64 I get:
> AssertionError

That means the dtype is not np.float64. As I have neither the data nor the 
netCDF4 library to replicate your problem I'm out.

What you might try is to construct a numpy array explicitly

x = np.array([float(v) for v in swh_Q0_con_sw.variables["hs"]])

but that's just a stab in the dark.

> hist,xedges,yedges=np.histogram2d(x,y,bins=10)
> TypeError: Cannot cast array data from dtype('O') to dtype('float64')
> according to the rule 'safe'
> 
> Thanks,
> 
> Jamie

[toc] | [prev] | [next] | [standalone]


#73458

FromJamie Mitchell <jamiemitchell1604@gmail.com>
Date2014-06-20 06:54 -0700
Message-ID<4e8ad51a-71df-4bf5-8d9b-a6898b505f66@googlegroups.com>
In reply to#73449
On Friday, June 20, 2014 9:46:29 AM UTC+1, Jamie Mitchell wrote:
> Hi folks,
> 
> 
> 
> I'm trying to plot a 2D histogram but I'm having some issues:
> 
> from pylab import *
> 
> import numpy as np
> 
> import netCDF4
> 
> hist,xedges,yedges=np.histogram2d(x,y,bins=10)
> 
> extent=[xedges[0],xedges[-1],yedges[0],yedges[-1]]
> 
> imshow(hist.T,extent=extent,interpolation='nearest')
> 
> colorbar()
> 
> show()
> 
> 
> 
> After the first line of code I get:
> 
> TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'
> 
> 
> 
> I'm using python2.7, x and y are type 'numpy.ndarray'
> 
> 
> 
> Cheers,
> 
> Jamie

Thanks for your help Peter.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web