Python numpy.NAN Examples

The following are 30 code examples for showing how to use numpy.NAN(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: lambda-packs   Author: ryfeus   File: _multivariate.py    License: MIT License 6 votes vote down vote up
def mean(self, n, p):
        """
        Mean of the Multinomial distribution

        Parameters
        ----------
        %(_doc_default_callparams)s

        Returns
        -------
        mean : float
            The mean of the distribution
        """
        n, p, npcond = self._process_parameters(n, p)
        result = n[..., np.newaxis]*p
        return self._checkresult(result, npcond, np.NAN) 
Example 2
Project: chainer   Author: chainer   File: test_constants.py    License: MIT License 6 votes vote down vote up
def test_constants():
    assert chainerx.Inf is numpy.Inf
    assert chainerx.Infinity is numpy.Infinity
    assert chainerx.NAN is numpy.NAN
    assert chainerx.NINF is numpy.NINF
    assert chainerx.NZERO is numpy.NZERO
    assert chainerx.NaN is numpy.NaN
    assert chainerx.PINF is numpy.PINF
    assert chainerx.PZERO is numpy.PZERO
    assert chainerx.e is numpy.e
    assert chainerx.euler_gamma is numpy.euler_gamma
    assert chainerx.inf is numpy.inf
    assert chainerx.infty is numpy.infty
    assert chainerx.nan is numpy.nan
    assert chainerx.newaxis is numpy.newaxis
    assert chainerx.pi is numpy.pi 
Example 3
Project: bayestsa   Author: thalesians   File: unscented.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, x0, P0, Q, R, cor, f, h):        
        self.Q = Q
        self.R = R
        self.cor = cor
        self.fa = lambda col: f(col[0], col[2])
        self.ha = lambda col: h(col[0], col[1])
        
        Pxx = P0
        Pxv = 0.
        self.xa = np.array( ((x0,), (0.,), (0.,), (0.,)) )
        self.Pa = np.array( ((Pxx, Pxv   , 0.      , 0.      ),
                             (Pxv, self.R, 0.      , 0.      ),
                             (0. , 0.    , self.Q  , self.cor),
                             (0. , 0.    , self.cor, self.R  )) )
        
        self.lastobservation = np.NAN
        self.predictedobservation = np.NAN
        self.innov = np.NAN
        self.innovcov = np.NAN
        self.gain = np.NAN
        
        self.loglikelihood = 0.0 
Example 4
Project: GraphicDesignPatternByPython   Author: Relph1119   File: _multivariate.py    License: MIT License 6 votes vote down vote up
def mean(self, n, p):
        """
        Mean of the Multinomial distribution

        Parameters
        ----------
        %(_doc_default_callparams)s

        Returns
        -------
        mean : float
            The mean of the distribution
        """
        n, p, npcond = self._process_parameters(n, p)
        result = n[..., np.newaxis]*p
        return self._checkresult(result, npcond, np.NAN) 
Example 5
Project: CatLearn   Author: SUNCAT-Center   File: site_stability.py    License: GNU General Public License v3.0 6 votes vote down vote up
def get_site_index(material, defect):
    """
    Given two trajectories with equal atom positions and one atom difference,
    determines the site index of the defect site.
    :param material:
    :param defect:
    :return: site index integer
    """
    matlist = material.get_positions()
    deflist = defect.get_positions()
    site_detected = []
    for pos in matlist:
        boollist = [np.allclose(pos, defpos, rtol=1e-03) for defpos in deflist]
        site_detected.append(any(boollist))
    site_idx = [idx for idx, _ in enumerate(site_detected) if not _]
    if len(site_idx) == 0:
        site_idx = [np.NAN]
    return site_idx[0] 
Example 6
Project: CatLearn   Author: SUNCAT-Center   File: site_stability.py    License: GNU General Public License v3.0 6 votes vote down vote up
def get_DFT_site_stability(self, site):
        """
        Computes site stability based on material,
        defect and reference dict.
        :param site: SiteFeaturizer site.
        :return: Site stability in eV.
        """
        e_mat = site['material'].total_energy
        atom = site['material'].atoms[site['site_index']].symbol
        e_atom = self.reference_dict[atom]
        e_def = site['defect'].total_energy
        try:
            e_site = e_mat - e_def - e_atom
        except:
            # print('Check; site may not be converged: \n')
            # print(site)
            # print('\n')
            e_site = np.NAN
        return e_site 
Example 7
Project: spinmob   Author: Spinmob   File: _data.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _format_value_error(self, v, e, pm=" +/- "):
        """
        Returns a string v +/- e with the right number of sig figs.
        """
        # If we have weird stuff
        if not _s.fun.is_a_number(v) or not _s.fun.is_a_number(e) \
            or v in [_n.inf, _n.nan, _n.NAN] or e in [_n.inf, _n.nan, _n.NAN]: 
            return str(v)+pm+str(e)
        
        # Normal values.
        try:
            sig_figs = -int(_n.floor(_n.log10(abs(e))))+1            
            return str(_n.round(v, sig_figs)) + pm + str(_n.round(e, sig_figs))

        except:
            return str(v)+pm+str(e) 
Example 8
Project: pliers   Author: tyarkoni   File: diagnostics.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def mahalanobis_distances(df, axis=0):
    '''
    Returns a pandas Series with Mahalanobis distances for each sample on the
    axis.

    Note: does not work well when # of observations < # of dimensions
    Will either return NaN in answer
    or (in the extreme case) fail with a Singular Matrix LinAlgError

    Args:
        df: pandas DataFrame with columns to run diagnostics on
        axis: 0 to find outlier rows, 1 to find outlier columns
    '''
    df = df.transpose() if axis == 1 else df
    means = df.mean()
    try:
        inv_cov = np.linalg.inv(df.cov())
    except LinAlgError:
        return pd.Series([np.NAN] * len(df.index), df.index,
                         name='Mahalanobis')
    dists = []
    for i, sample in df.iterrows():
        dists.append(mahalanobis(sample, means, inv_cov))

    return pd.Series(dists, df.index, name='Mahalanobis') 
Example 9
Project: technical   Author: freqtrade   File: bouncyhouse.py    License: GNU General Public License v3.0 6 votes vote down vote up
def bounce(dataframe: DataFrame, level):
    """

    :param dataframe:
    :param level:
    :return:
      1 if it bounces up
      0 if no bounce
     -1 if it bounces below
    """

    from scipy.ndimage.interpolation import shift
    open = dataframe['open']
    close = dataframe['close']
    touch = shift(touches(dataframe, level), 1, cval=np.NAN)

    return np.vectorize(_bounce)(open, close, level, touch) 
Example 10
Project: velocyto.py   Author: velocyto-team   File: estimation.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _fit1_slope(y: np.ndarray, x: np.ndarray) -> float:
    """Simple function that fit a linear regression model without intercept
    """
    if not np.any(x):
        m = np.NAN  # It is definetelly not at steady state!!!
    elif not np.any(y):
        m = 0
    else:
        result, rnorm = scipy.optimize.nnls(x[:, None], y)  # Fastest but costrains result >= 0
        m = result[0]
        # Second fastest: m, _ = scipy.optimize.leastsq(lambda m: x*m - y, x0=(0,))
        # Third fastest: m = scipy.optimize.minimize_scalar(lambda m: np.sum((x*m - y)**2 )).x
        # Before I was doinf fastest: scipy.optimize.minimize_scalar(lambda m: np.sum((y - m * x)**2), bounds=(0, 3), method="bounded").x
        # Optionally one could clip m if high value make no sense
        # m = np.clip(m,0,3)
    return m 
Example 11
Project: velocyto.py   Author: velocyto-team   File: estimation.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _fit1_slope_weighted(y: np.ndarray, x: np.ndarray, w: np.ndarray, limit_gamma: bool=False, bounds: Tuple[float, float]=(0, 20)) -> float:
    """Simple function that fit a weighted linear regression model without intercept
    """
    if not np.any(x):
        m = np.NAN  # It is definetelly not at steady state!!!
    elif not np.any(y):
        m = 0
    else:
        if limit_gamma:
            if np.median(y) > np.median(x):
                high_x = x > np.percentile(x, 90)
                up_gamma = np.percentile(y[high_x], 10) / np.median(x[high_x])
                up_gamma = np.maximum(1.5, up_gamma)
            else:
                up_gamma = 1.5  # Just a bit more than 1
            m = scipy.optimize.minimize_scalar(lambda m: np.sum(w * (x * m - y)**2), bounds=(1e-8, up_gamma), method="bounded").x
        else:
            m = scipy.optimize.minimize_scalar(lambda m: np.sum(w * (x * m - y)**2), bounds=bounds, method="bounded").x
    return m 
Example 12
Project: velocyto.py   Author: velocyto-team   File: estimation.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _fit1_slope_offset(y: np.ndarray, x: np.ndarray, fixperc_q: bool=False) -> Tuple[float, float]:
    """Simple function that fit a linear regression model with intercept
    """
    if not np.any(x):
        m = (np.NAN, 0)  # It is definetelly not at steady state!!!
    elif not np.any(y):
        m = (0, 0)
    else:
        # result, rnorm = scipy.optimize.nnls(x[:, None], y)  # Fastest but costrains result >= 0
        # m = result[0]
        if fixperc_q:
            m1 = np.percentile(y[x <= np.percentile(x, 1)], 50)
            m0 = scipy.optimize.minimize_scalar(lambda m: np.sum((x * m - y + m1)**2), bounds=(0, 20), method="bounded").x
            m = (m0, m1)
        else:
            m, _ = scipy.optimize.leastsq(lambda m: -y + x * m[0] + m[1], x0=(0, 0))
        # Third fastest: m = scipy.optimize.minimize_scalar(lambda m: np.sum((x*m - y)**2 )).x
        # Before I was doinf fastest: scipy.optimize.minimize_scalar(lambda m: np.sum((y - m * x)**2), bounds=(0, 3), method="bounded").x
        # Optionally one could clip m if high value make no sense
        # m = np.clip(m,0,3)
    return m[0], m[1] 
Example 13
Project: Splunking-Crime   Author: nccgroup   File: _multivariate.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def mean(self, n, p):
        """
        Mean of the Multinomial distribution

        Parameters
        ----------
        %(_doc_default_callparams)s

        Returns
        -------
        mean : float
            The mean of the distribution
        """
        n, p, npcond = self._process_parameters(n, p)
        result = n[..., np.newaxis]*p
        return self._checkresult(result, npcond, np.NAN) 
Example 14
Project: diluvian   Author: aschampion   File: regions.py    License: MIT License 6 votes vote down vote up
def remask(self):
        """Reset the mask based on the seeded connected component.
        """
        body = self.to_body()
        if not body.is_seed_in_mask():
            return False
        new_mask_bin, bounds = body.get_seeded_component(CONFIG.postprocessing.closing_shape)
        new_mask_bin = new_mask_bin.astype(np.bool)

        mask_block = self.mask[list(map(slice, bounds[0], bounds[1]))].copy()
        # Clip any values not in the seeded connected component so that they
        # cannot not generate moves when rechecking.
        mask_block[~new_mask_bin] = np.clip(mask_block[~new_mask_bin], None, 0.9 * CONFIG.model.t_move)

        self.mask[:] = np.NAN
        self.mask[list(map(slice, bounds[0], bounds[1]))] = mask_block
        return True 
Example 15
Project: lambda-packs   Author: ryfeus   File: _multivariate.py    License: MIT License 5 votes vote down vote up
def logpmf(self, x, n, p):
        """
        Log of the Multinomial probability mass function.

        Parameters
        ----------
        x : array_like
            Quantiles, with the last axis of `x` denoting the components.
            Each quantile must be a symmetric positive definite matrix.
        %(_doc_default_callparams)s

        Returns
        -------
        logpmf : ndarray or scalar
            Log of the probability mass function evaluated at `x`

        Notes
        -----
        %(_doc_callparams_note)s
        """
        n, p, npcond = self._process_parameters(n, p)
        x, xcond = self._process_quantiles(x, n, p)

        result = self._logpmf(x, n, p)

        # replace values for which x was out of the domain; broadcast
        # xcond to the right shape
        xcond_ = xcond | np.zeros(npcond.shape, dtype=np.bool_)
        result = self._checkresult(result, xcond_, np.NINF)

        # replace values bad for n or p; broadcast npcond to the right shape
        npcond_ = npcond | np.zeros(xcond.shape, dtype=np.bool_)
        return self._checkresult(result, npcond_, np.NAN) 
Example 16
Project: systematictradingexamples   Author: robcarver17   File: equitycountrymodels.py    License: GNU General Public License v2.0 5 votes vote down vote up
def sign(x):
    if x is None:
        return None
    if np.isnan(x):
        return np.NAN
    if x==0:
        return 0.0
    if x<0:
        return -1.0
    if x>0:
        return 1.0 
Example 17
Project: systematictradingexamples   Author: robcarver17   File: conditionalforecast.py    License: GNU General Public License v2.0 5 votes vote down vote up
def sign(x):
    if x is None:
        return None
    if np.isnan(x):
        return np.NAN
    if x==0:
        return 0.0
    if x<0:
        return -1.0
    if x>0:
        return 1.0 
Example 18
def sign(x):
    if x is None:
        return None
    if np.isnan(x):
        return np.NAN
    if x==0:
        return 0.0
    if x<0:
        return -1.0
    if x>0:
        return 1.0 
Example 19
Project: systematictradingexamples   Author: robcarver17   File: yieldsprediction.py    License: GNU General Public License v2.0 5 votes vote down vote up
def sign(x):
    if x is None:
        return None
    if np.isnan(x):
        return np.NAN
    if x==0:
        return 0.0
    if x<0:
        return -1.0
    if x>0:
        return 1.0 
Example 20
Project: meterstick   Author: google   File: core_test.py    License: Apache License 2.0 5 votes vote down vote up
def testFlatIndexSeriesPercentageDifferenceJackknifeMelted(self):
    # Output is screenshot/xXqjxgZb6eH
    flat_idx = pd.Series(
        list(range(3)), index=pd.Index(list("abc"), name="foo"))
    metric = [
        metrics.Sum("X"),
        metrics.Metric("Constant", fn=lambda df: flat_idx)
    ]
    comparison = comparisons.PercentageDifference("U", 1)
    se_method = standard_errors.Jackknife("Y")
    output = core.Analyze(self.data).relative_to(
        comparison).with_standard_errors(se_method).calculate(metric).run(1)
    sum_x = core.Analyze(self.data).relative_to(
        comparison).with_standard_errors(se_method).calculate(metric[0]).run(1)
    sum_x["foo"] = ""
    sum_x.set_index("foo", append=True, inplace=True)
    sum_x = sum_x.reorder_levels(["Metric", "foo", "U"])
    constant = pd.DataFrame({
        "Metric": ["Constant"] * 6,
        "foo": list("abcabc"),
        "U": [2] * 3 + [3] * 3,
        "Percentage Difference": [np.NAN, 0, 0] * 2,
        "Percentage Difference Jackknife SE": [np.NAN, 0, 0] * 2,
    })
    constant.set_index(["Metric", "foo", "U"], inplace=True)
    correct = pd.concat([sum_x, constant])
    pd.util.testing.assert_frame_equal(output, correct) 
Example 21
Project: florence   Author: romeric   File: MaterialBase.py    License: MIT License 5 votes vote down vote up
def pprint(self):
        """Pretty print"""

        import pandas
        from copy import deepcopy
        Dict = deepcopy(self.__dict__)
        for key in Dict.keys():
            if Dict[key] is None:
                Dict[key] = np.NAN
            if isinstance(Dict[key],np.ndarray):
                del Dict[key]

        print(pandas.DataFrame(Dict,index=["Available parameters:"])) 
Example 22
Project: airflow   Author: apache   File: test_oracle.py    License: Apache License 2.0 5 votes vote down vote up
def test_insert_rows_with_fields(self):
        rows = [("'basestr_with_quote", None, numpy.NAN,
                 numpy.datetime64('2019-01-24T01:02:03'),
                 datetime(2019, 1, 24), 1, 10.24, 'str')]
        target_fields = ['basestring', 'none', 'numpy_nan', 'numpy_datetime64',
                         'datetime', 'int', 'float', 'str']
        self.db_hook.insert_rows('table', rows, target_fields)
        self.cur.execute.assert_called_once_with(
            "INSERT /*+ APPEND */ INTO table "
            "(basestring, none, numpy_nan, numpy_datetime64, datetime, int, float, str) "
            "VALUES ('''basestr_with_quote',NULL,NULL,'2019-01-24T01:02:03',"
            "to_date('2019-01-24 00:00:00','YYYY-MM-DD HH24:MI:SS'),1,10.24,'str')") 
Example 23
Project: airflow   Author: apache   File: test_oracle.py    License: Apache License 2.0 5 votes vote down vote up
def test_insert_rows_without_fields(self):
        rows = [("'basestr_with_quote", None, numpy.NAN,
                 numpy.datetime64('2019-01-24T01:02:03'),
                 datetime(2019, 1, 24), 1, 10.24, 'str')]
        self.db_hook.insert_rows('table', rows)
        self.cur.execute.assert_called_once_with(
            "INSERT /*+ APPEND */ INTO table "
            " VALUES ('''basestr_with_quote',NULL,NULL,'2019-01-24T01:02:03',"
            "to_date('2019-01-24 00:00:00','YYYY-MM-DD HH24:MI:SS'),1,10.24,'str')") 
Example 24
Project: CausE   Author: criteo-research   File: dataset_loading.py    License: Apache License 2.0 5 votes vote down vote up
def create_movielens_userproduct_matrix(userid, productid, rating):
    """Create the matrix from the movielens dataset"""

    model = "expomf"
    ratings_threshold = 5

    num_unique_users = int(np.amax(userid)+1)
    num_unique_products = int(np.amax(productid)+1)

    print(num_unique_users)
    print(num_unique_products)

    view_matrix = np.zeros(shape=(num_unique_users, num_unique_products))
    view_matrix[:] = np.NAN

    for i in range(userid.shape[0]):

        if rating[i] < ratings_threshold:
            view_matrix[userid[i], productid[i]] = 0

        elif rating[i] >= ratings_threshold:
            view_matrix[userid[i], productid[i]] = 1

        #return view_matrix
        np.savetxt('ML_view.txt', view_matrix)

    print("Data Saved") 
Example 25
Project: spotpy   Author: thouska   File: likelihoods.py    License: MIT License 5 votes vote down vote up
def InverseErrorVarianceShapingFactor(data, comparedata, G=10):
    """
    This function simply use the variance in the error values (:math:`E(X)=Y-Y(X)`) as a likelihood value as this formula
    shows:

    .. math::

            p=-G \\log(Var(E(x)))

    The factor `G` comes from the DREAMPar model. So this factor can be changed according to the used model.

    For more details see also: http://onlinelibrary.wiley.com/doi/10.1002/hyp.3360060305/epdf.

    `Usage:` Maximize the likelihood value guides to the best model.

    :param data: observed measurements as a numerical list
    :type data: list
    :param comparedata: simulated data from a model which should fit the original data somehow
    :type comparedata: list
    :param G: DREAMPar model parameter `G`
    :type G: float
    :return: the p value as a likelihood
    :rtype: float
    """

    __standartChecksBeforeStart(data, comparedata)

    errArr = np.nanvar(np.array(__calcSimpleDeviation(data, comparedata)))
    if errArr == 0.0:
        warnings.warn(
            "[InverseErrorVarianceShapingFactor] reaslized that the variance in y(x)-y is zero and that makes no sence and also impossible to calculate the likelihood.")
        return np.NAN
    else:
        # Gives an better convergence, so close values are more less and apart values are more great.
        # (0 is the best so to say).
        return -G * np.log(errArr) ** 3 
Example 26
Project: bayestsa   Author: thalesians   File: gaussian.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, x0, P0, params):
        self._params = params
        self.x = x0
        self.P = P0
        self._constterm = self._params.meanlogvar * (1. - self._params.persistence)
        self._cv = self._params.cor * self._params.voloflogvar
        self._cv2 = self._cv * self._cv
        self._p2 = self._params.persistence * self._params.persistence
        self._v2 = self._params.voloflogvar * self._params.voloflogvar
        self.predictedobservation = np.NAN
        self.lastobservation = None
        self.innov = np.NAN
        self.innovcov = np.NAN
        self.gain = np.NAN
        self.loglikelihood = 0.0 
Example 27
Project: bayestsa   Author: thalesians   File: collections.py    License: Apache License 2.0 5 votes vote down vote up
def tonumpyarray(self, fill=None, symmetric=False):
        import numpy as np
        if fill is None: fill = np.NAN
        res = np.empty((self.__dim, self.__dim))
        idx = 0
        for i in range(self.__dim):
            for j in range(i+1):
                res[i,j] = self._data[idx]
                if symmetric: res[j,i] = res[i,j]
                idx += 1
            if not symmetric: res[i,i+1:self.__dim] = fill
        return res 
Example 28
Project: bayestsa   Author: thalesians   File: collections.py    License: Apache License 2.0 5 votes vote down vote up
def tonumpyarray(self, fill=None, symmetric=False):
        import numpy as np
        if fill is None: fill = np.NAN
        res = np.empty((self.__dim, self.__dim))
        idx = 0
        for i in range(self.__dim):
            for j in range(i):
                res[i,j] = self._data[idx]
                if symmetric: res[j,i] = res[i,j]
                idx += 1
            res[i,i] = fill
            if not symmetric: res[i,i+1:self.__dim] = fill
        return res 
Example 29
Project: bayestsa   Author: thalesians   File: generation.py    License: Apache License 2.0 5 votes vote down vote up
def generate(self):
        self.__validate()
        self.__generatenoises()
        self.__generatejumps()
        processcount = len(self.__data._processnames)
        self.__data._processes = np.empty((self.__data._timecount, processcount))
        self.__data._processes[:] = np.NAN
        for time in range(self.__data._timecount):
            for pi, (pn, pf) in enumerate(zip(self.__data._processnames, self.__processfuncs)):
                self.__data._processes[time, pi] = pf(time, pn, self.__data)
        return self.__data.copy() 
Example 30
Project: GraphicDesignPatternByPython   Author: Relph1119   File: _multivariate.py    License: MIT License 5 votes vote down vote up
def logpmf(self, x, n, p):
        """
        Log of the Multinomial probability mass function.

        Parameters
        ----------
        x : array_like
            Quantiles, with the last axis of `x` denoting the components.
            Each quantile must be a symmetric positive definite matrix.
        %(_doc_default_callparams)s

        Returns
        -------
        logpmf : ndarray or scalar
            Log of the probability mass function evaluated at `x`

        Notes
        -----
        %(_doc_callparams_note)s
        """
        n, p, npcond = self._process_parameters(n, p)
        x, xcond = self._process_quantiles(x, n, p)

        result = self._logpmf(x, n, p)

        # replace values for which x was out of the domain; broadcast
        # xcond to the right shape
        xcond_ = xcond | np.zeros(npcond.shape, dtype=np.bool_)
        result = self._checkresult(result, xcond_, np.NINF)

        # replace values bad for n or p; broadcast npcond to the right shape
        npcond_ = npcond | np.zeros(xcond.shape, dtype=np.bool_)
        return self._checkresult(result, npcond_, np.NAN)