Is it useful to set a fraction number here to the mask value?

MRAB python at mrabarnett.plus.com
Thu Nov 30 22:03:31 EST 2017


On 2017-12-01 01:43, Robert wrote:
> On Thursday, November 30, 2017 at 6:17:05 PM UTC-5, Robert wrote:
>> Hi,
>> 
>> I am new to Python. Now I follow a thread on mask array usage on line:
>> 
>> 
>> https://stackoverflow.com/questions/31563970/fitting-a-binomial-distribution-with-pymc-raises-zeroprobability-error-for-certa
>> 
>> 
>> I understand the problem, but I don't understand the answer follow the link.
>> 
>> Because the 'mask' array is composed of integer, if it is assigned a fraction
>> number as suggested a 1.5, it will be formatted to an integer 1.
>>  
>> 
>> observed_values = sp.random.binomial(n = 10.0, p = 0.1, size = 100)
>> ...
>> mask = sp.zeros_like(observed_values)
>> 
>> 
>> Are you clear the answer's meaning?
>> 
>> 
>> "You can give it a non-integer value in order to avoid the problem that you cite. 
>> For example, if you fill with, say, 1.5 that should work."
>> 
>> 
>> 
>> 
>> Thanks in advance
> 
> Excuse me for the top post. Now I find the more specific question from the below
> link:
> 
> https://pymc-devs.github.io/pymc/tutorial.html
> 
> 
> At almost the bottom part of the web page, when I run the script
> 
> "masked_values = masked_array(disasters_array, mask=disasters_array==-999)"
> 
> has an error:
> 
> ... masked_values = masked_array(disasters_array, mask=disasters_array==-999)
> Traceback (most recent call last):
>    File "<stdin>", line 7, in <module>
> NameError: name 'masked_array' is not defined
> 
> I use Python 2.7 on Ubuntu 16.04, 64-bit. Is there an error in the pymc
> tutorial web page?
> 
> Can you tell me what is wrong?
> 
> Thanks,
> 
> ===========================
> # Switchpoint
> switch = DiscreteUniform('switch', lower=0, upper=110)
> # Early mean
> early_mean = Exponential('early_mean', beta=1)
> # Late mean
> late_mean = Exponential('late_mean', beta=1)
> 
> @deterministic(plot=False)
> def rate(s=switch, e=early_mean, l=late_mean):
>      """Allocate appropriate mean to time series"""
>      out = np.empty(len(disasters_array))
>      # Early mean prior to switchpoint
>      out[:s] = e
>      # Late mean following switchpoint
>      out[s:] = l
>      return out
> 
> 
> # The inefficient way, using the Impute function:
> # D = Impute('D', Poisson, disasters_array, mu=r)
> #
> # The efficient way, using masked arrays:
> # Generate masked array. Where the mask is true,
> # the value is taken as missing.
> masked_values = masked_array(disasters_array, mask=disasters_array==-999)
> 
> # Pass masked array to data stochastic, and it does the right thing
> disasters = Poisson('disasters', mu=rate, value=masked_values, observed=True)
> 
Earlier in the tutorial there was:

 >>> masked_values = np.ma.masked_equal(x, value=None)
 >>> masked_values
masked_array(# etc

This shows that 'masked_values' is a 'masked_array' object.

A quick Google shows that it's numpy.ma.masked_array, i.e. defined in 
numpy.ma.



More information about the Python-list mailing list