python类logical_or()的实例源码

experiment.py 文件源码 项目:Enrich2 作者: FowlerLab 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def calc_shared(self, label):
        """
        Get the subset of scores that are shared across all Selections in each
        Condition.
        """
        if self.check_store("/main/{}/scores_shared".format(label)):
            return

        idx = pd.IndexSlice

        logging.info("Identifying subset shared across all Selections ({})"
                     "".format(label), extra={'oname': self.name})
        data = self.store.select("/main/{}/scores_shared_full".format(label))

        # identify variants found in all selections in at least one condition
        complete = np.full(len(data.index), False, dtype=bool)
        for cnd in data.columns.levels[0]:
            complete = np.logical_or(complete,
                                     data.loc[:, idx[cnd, :, :]].notnull().all(
                                         axis='columns'))

        data = data.loc[complete]

        self.store.put("/main/{}/scores_shared".format(label), data,
                       format="table")
numerics.py 文件源码 项目:npstreams 作者: LaurentRDC 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def iany(arrays, axis = -1):
    """ 
    Test whether any array elements along a given axis evaluate to True.

    Parameters
    ----------
    arrays : iterable
        Arrays to be reduced.
    axis : int or None, optional
        Axis along which a logical OR reduction is performed. The default
        is to perform a logical AND along the 'stream axis', as if all arrays in ``array``
        were stacked along a new dimension. If ``axis = None``, arrays in ``arrays`` are flattened
        before reduction.

    Yields
    ------
    any : ndarray, dtype bool 
    """
    yield from ireduce_ufunc(arrays, ufunc = np.logical_or, axis = axis)
learn_parameters.py 文件源码 项目:OrbWeaver 作者: rajanil 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def compute_test_accuracy(X_test, Y_test, model, prediction_type, cellgroup_map_array):

    prediction = model.predict(X_test)
    auc = []

    if prediction_type=="cellgroup":

        prediction = np.dot(prediction, cellgroup_map_array)
        Y_test = np.dot(Y_test, cellgroup_map_array)

    mask = ~np.logical_or(Y_test.sum(1)==0, Y_test.sum(1)==Y_test.shape[1])

    for y,pred in zip(Y_test.T,prediction.T):
        pos = np.logical_and(mask, y==1)
        neg = np.logical_and(mask, y==0)
        try:
            U = stats.mannwhitneyu(pred[pos], pred[neg])[0]
            auc.append(1.-U/(np.count_nonzero(pos)*np.count_nonzero(neg)))
        except ValueError:
            auc.append(0.5)

    return auc
test_umath.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def test_truth_table_logical(self):
        # 2, 3 and 4 serves as true values
        input1 = [0, 0, 3, 2]
        input2 = [0, 4, 0, 2]

        typecodes = (np.typecodes['AllFloat']
                     + np.typecodes['AllInteger']
                     + '?')     # boolean
        for dtype in map(np.dtype, typecodes):
            arg1 = np.asarray(input1, dtype=dtype)
            arg2 = np.asarray(input2, dtype=dtype)

            # OR
            out = [False, True, True, True]
            for func in (np.logical_or, np.maximum):
                assert_equal(func(arg1, arg2).astype(bool), out)
            # AND
            out = [False, False, False, True]
            for func in (np.logical_and, np.minimum):
                assert_equal(func(arg1, arg2).astype(bool), out)
            # XOR
            out = [False, True, True, False]
            for func in (np.logical_xor, np.not_equal):
                assert_equal(func(arg1, arg2).astype(bool), out)
test_ufunc.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_object_logical(self):
        a = np.array([3, None, True, False, "test", ""], dtype=object)
        assert_equal(np.logical_or(a, None),
                        np.array([x or None for x in a], dtype=object))
        assert_equal(np.logical_or(a, True),
                        np.array([x or True for x in a], dtype=object))
        assert_equal(np.logical_or(a, 12),
                        np.array([x or 12 for x in a], dtype=object))
        assert_equal(np.logical_or(a, "blah"),
                        np.array([x or "blah" for x in a], dtype=object))

        assert_equal(np.logical_and(a, None),
                        np.array([x and None for x in a], dtype=object))
        assert_equal(np.logical_and(a, True),
                        np.array([x and True for x in a], dtype=object))
        assert_equal(np.logical_and(a, 12),
                        np.array([x and 12 for x in a], dtype=object))
        assert_equal(np.logical_and(a, "blah"),
                        np.array([x and "blah" for x in a], dtype=object))

        assert_equal(np.logical_not(a),
                        np.array([not x for x in a], dtype=object))

        assert_equal(np.logical_or.reduce(a), 3)
        assert_equal(np.logical_and.reduce(a), None)
test_ufunc.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def test_NotImplemented_not_returned(self):
        # See gh-5964 and gh-2091. Some of these functions are not operator
        # related and were fixed for other reasons in the past.
        binary_funcs = [
            np.power, np.add, np.subtract, np.multiply, np.divide,
            np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
            np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
            np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
            np.logical_and, np.logical_or, np.logical_xor, np.maximum,
            np.minimum, np.mod
            ]

        # These functions still return NotImplemented. Will be fixed in
        # future.
        # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]

        a = np.array('1')
        b = 1
        for f in binary_funcs:
            assert_raises(TypeError, f, a, b)
core.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def outer(self, a, b):
        """
        Return the function applied to the outer product of a and b.

        """
        (da, db) = (getdata(a), getdata(b))
        d = self.f.outer(da, db)
        ma = getmask(a)
        mb = getmask(b)
        if ma is nomask and mb is nomask:
            m = nomask
        else:
            ma = getmaskarray(a)
            mb = getmaskarray(b)
            m = umath.logical_or.outer(ma, mb)
        if (not m.ndim) and m:
            return masked
        if m is not nomask:
            np.copyto(d, da, where=m)
        if not d.shape:
            return d
        masked_d = d.view(get_masked_subclass(a, b))
        masked_d._mask = m
        return masked_d
dust_extinction.py 文件源码 项目:dust_extinction 作者: karllark 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def _test_valid_x_range(x, x_range, outname):
    """
    Test if any of the x values are outside of the valid range

    Parameters
    ----------
    x : float array
       wavenumbers in inverse microns

    x_range: 2 floats
       allowed min/max of x

    outname: str
       name of curve for error message
    """
    if np.logical_or(np.any(x < x_range[0]),
                     np.any(x > x_range[1])):
        raise ValueError('Input x outside of range defined for ' + outname \
                         + ' ['
                         + str(x_range[0])
                         +  ' <= x <= '
                         + str(x_range[1])
                         + ', x has units 1/micron]')
PConsC2.py 文件源码 项目:Master-Thesis 作者: AntoinePassemiers 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def get_metrics(predictions, targets):
    assert(np.logical_or(predictions == 1, predictions == 0).all())
    assert(np.logical_or(targets == 1, targets == 0).all())
    TP  = np.logical_and(predictions == 1, targets == 1).sum()
    FP  = np.logical_and(predictions == 1, targets == 0).sum()
    FN  = np.logical_and(predictions == 0, targets == 1).sum()
    TN  = np.logical_and(predictions == 0, targets == 0).sum()
    N   = TP + FP + FN + TN

    PPV = float(TP) / float(TP + FP) if TP != 0.0 else 0.0
    FPV = float(TN) / float(TN + FN) if TN != 0.0 else 0.0
    ACC = float(TP + TN) / float(N)
    TPR = float(TP) / float(TP + FN) if TP != 0.0 else 0.0
    FPR = float(FP) / float(FP + TN) if FP != 0.0 else 0.0
    tp, tn, fp, fn = float(TP) / N, float(TN) / N, float(FP) / N, float(FN) / N
    MCC = float(tp*tn - fp*fn) / (np.sqrt(tp+fp)*np.sqrt(tp+fn)*np.sqrt(tn+fp)*np.sqrt(tn+fn))
    F1 = 2 * TP / float(2 * TP + FP + FN)
    metrics = {
        "TP": TP, "FP": FP, "FN": FN, "TN": TN, "N": N,
        "PPV": PPV, "FPV": FPV, "MCC": MCC, "ACC": ACC,
        "F1": F1
    }
    return metrics
PConsC2.py 文件源码 项目:Master-Thesis 作者: AntoinePassemiers 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def get_metrics_per_contact_type(predicted_cmap, target_cmap):
    L = predicted_cmap.shape[0]
    short_range  = np.zeros((L, L), dtype = np.bool)
    medium_range = np.zeros((L, L), dtype = np.bool)
    long_range   = np.zeros((L, L), dtype = np.bool)
    more_than_6  = np.tril_indices(L, -6)
    more_than_12 = np.tril_indices(L, -12)
    more_than_24 = np.tril_indices(L, -24)
    short_range[more_than_6] = True
    short_range[more_than_12] = False
    medium_range[more_than_12] = True
    medium_range[more_than_24] = False
    long_range[more_than_24] = True
    short_range = np.logical_or(short_range, short_range.T)
    medium_range = np.logical_or(medium_range, medium_range.T)
    long_range = np.logical_or(long_range, long_range.T)
    short_range_metrics = get_metrics(predicted_cmap[short_range], target_cmap[short_range])
    medium_range_metrics = get_metrics(predicted_cmap[medium_range], target_cmap[medium_range])
    long_range_metrics = get_metrics(predicted_cmap[long_range], target_cmap[long_range])
    return short_range_metrics, medium_range_metrics, long_range_metrics
ConsIndShockModel.py 文件源码 项目:HARK 作者: econ-ark 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def simDeath(self):
        '''
        Determines which agents die this period and must be replaced.  Uses the sequence in LivPrb
        to determine survival probabilities for each agent.

        Parameters
        ----------
        None

        Returns
        -------
        which_agents : np.array(bool)
            Boolean array of size AgentCount indicating which agents die.
        '''
        # Determine who dies
        DiePrb_by_t_cycle = 1.0 - np.asarray(self.LivPrb)
        DiePrb = DiePrb_by_t_cycle[self.t_cycle-1] # Time has already advanced, so look back one
        DeathShks = drawUniform(N=self.AgentCount,seed=self.RNG.randint(0,2**31-1))
        which_agents = DeathShks < DiePrb
        if self.T_age is not None: # Kill agents that have lived for too many periods
            too_old = self.t_age >= self.T_age
            which_agents = np.logical_or(which_agents,too_old)
        return which_agents
ConsMarkovModel.py 文件源码 项目:HARK 作者: econ-ark 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def simDeath(self):
        '''
        Determines which agents die this period and must be replaced.  Uses the sequence in LivPrb
        to determine survival probabilities for each agent.

        Parameters
        ----------
        None

        Returns
        -------
        which_agents : np.array(bool)
            Boolean array of size AgentCount indicating which agents die.
        '''
        # Determine who dies
        LivPrb = np.array(self.LivPrb)[self.t_cycle-1,self.MrkvNow] # Time has already advanced, so look back one
        DiePrb = 1.0 - LivPrb
        DeathShks = drawUniform(N=self.AgentCount,seed=self.RNG.randint(0,2**31-1))
        which_agents = DeathShks < DiePrb
        if self.T_age is not None: # Kill agents that have lived for too many periods
            too_old = self.t_age >= self.T_age
            which_agents = np.logical_or(which_agents,too_old)
        return which_agents
windeval.py 文件源码 项目:POWER 作者: pennelise 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def save_total_power(data,times,SCADA_faults,filename):
    total_power = np.array([])
    new_times = np.array([])
    percent_active = np.array([])    
    for time in np.unique(times):
        state_fault = SCADA_faults[times == time]
        fault_mask = [state_fault == 2,state_fault == 1]
        fault_mask = reduce(np.logical_or,fault_mask)

        total_power = np.append(total_power,np.sum(data[times == time]))
        new_times = np.append(new_times,time)
        percent_active = np.append(percent_active,float(np.sum(fault_mask))/float(len(fault_mask)))


    total_dictionary = {}
    total_dictionary['total_power'] = total_power
    total_dictionary['time'] = new_times
    total_dictionary['percent_active'] = percent_active

    file_path = os.path.normpath('%s/FormattedData/%s' % (os.getcwd(),filename))
    np.savez(file_path,**total_dictionary)
geometry.py 文件源码 项目:xdesign 作者: tomography 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def contains(self, other):
        if isinstance(other, Point):
            x = other._x
        elif isinstance(other, np.ndarray):
            x = other
        elif isinstance(other, Polygon):
            x = _points_to_array(other.vertices)
            return np.all(self.contains(x))
        else:
            raise TypeError("P must be point or ndarray")

        # keep track of whether each point is contained in a face
        bools = np.full(x.shape[0], False, dtype=bool)
        for f in self.faces:
            bools = np.logical_or(bools, f.contains(x))
        return bools
linalg.py 文件源码 项目:sporco 作者: bwohlberg 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def zdivide(x, y):
    """
    Return x/y, with 0 instead of NaN where y is 0.

    Parameters
    ----------
    x : array_like
      Numerator
    y : array_like
      Denominator

    Returns
    -------
    z : ndarray
      Quotient `x`/`y`
    """

    with np.errstate(divide='ignore', invalid='ignore'):
        div = x / y
    div[np.logical_or(np.isnan(div), np.isinf(div))] = 0
    return div
operations.py 文件源码 项目:blmath 作者: bodylabs 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def zero_safe_divide(a, b, default_error_value=0.):
    """Element-wise division that accounts for floating point errors.

    Both invalid floating-point (e.g. 0. / 0.) and divide be zero errors are
    suppressed. Resulting values (NaN and Inf respectively) are replaced with
    `default_error_value`.

    """
    import numpy as np

    with np.errstate(invalid='ignore', divide='ignore'):
        quotient = np.true_divide(a, b)
        bad_value_indices = np.logical_or(
            np.isnan(quotient), np.isinf(quotient))
        quotient[bad_value_indices] = default_error_value

    return quotient
test_umath.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_truth_table_logical(self):
        # 2, 3 and 4 serves as true values
        input1 = [0, 0, 3, 2]
        input2 = [0, 4, 0, 2]

        typecodes = (np.typecodes['AllFloat']
                     + np.typecodes['AllInteger']
                     + '?')     # boolean
        for dtype in map(np.dtype, typecodes):
            arg1 = np.asarray(input1, dtype=dtype)
            arg2 = np.asarray(input2, dtype=dtype)

            # OR
            out = [False, True, True, True]
            for func in (np.logical_or, np.maximum):
                assert_equal(func(arg1, arg2).astype(bool), out)
            # AND
            out = [False, False, False, True]
            for func in (np.logical_and, np.minimum):
                assert_equal(func(arg1, arg2).astype(bool), out)
            # XOR
            out = [False, True, True, False]
            for func in (np.logical_xor, np.not_equal):
                assert_equal(func(arg1, arg2).astype(bool), out)
test_ufunc.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def test_object_logical(self):
        a = np.array([3, None, True, False, "test", ""], dtype=object)
        assert_equal(np.logical_or(a, None),
                        np.array([x or None for x in a], dtype=object))
        assert_equal(np.logical_or(a, True),
                        np.array([x or True for x in a], dtype=object))
        assert_equal(np.logical_or(a, 12),
                        np.array([x or 12 for x in a], dtype=object))
        assert_equal(np.logical_or(a, "blah"),
                        np.array([x or "blah" for x in a], dtype=object))

        assert_equal(np.logical_and(a, None),
                        np.array([x and None for x in a], dtype=object))
        assert_equal(np.logical_and(a, True),
                        np.array([x and True for x in a], dtype=object))
        assert_equal(np.logical_and(a, 12),
                        np.array([x and 12 for x in a], dtype=object))
        assert_equal(np.logical_and(a, "blah"),
                        np.array([x and "blah" for x in a], dtype=object))

        assert_equal(np.logical_not(a),
                        np.array([not x for x in a], dtype=object))

        assert_equal(np.logical_or.reduce(a), 3)
        assert_equal(np.logical_and.reduce(a), None)
test_ufunc.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_NotImplemented_not_returned(self):
        # See gh-5964 and gh-2091. Some of these functions are not operator
        # related and were fixed for other reasons in the past.
        binary_funcs = [
            np.power, np.add, np.subtract, np.multiply, np.divide,
            np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
            np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
            np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
            np.logical_and, np.logical_or, np.logical_xor, np.maximum,
            np.minimum, np.mod
            ]

        # These functions still return NotImplemented. Will be fixed in
        # future.
        # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]

        a = np.array('1')
        b = 1
        for f in binary_funcs:
            assert_raises(TypeError, f, a, b)
core.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def outer(self, a, b):
        """
        Return the function applied to the outer product of a and b.

        """
        (da, db) = (getdata(a), getdata(b))
        d = self.f.outer(da, db)
        ma = getmask(a)
        mb = getmask(b)
        if ma is nomask and mb is nomask:
            m = nomask
        else:
            ma = getmaskarray(a)
            mb = getmaskarray(b)
            m = umath.logical_or.outer(ma, mb)
        if (not m.ndim) and m:
            return masked
        if m is not nomask:
            np.copyto(d, da, where=m)
        if not d.shape:
            return d
        masked_d = d.view(get_masked_subclass(a, b))
        masked_d._mask = m
        return masked_d
est_rel_entro_HJW.py 文件源码 项目:HJW_KL_divergence_estimator 作者: Mathegineer 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def log_mat(x, n, g_coeff, c_1, const):
    with np.errstate(divide='ignore', invalid='ignore'):
        K = g_coeff.shape[0] - 1
        thres = 2 * c_1 * math.log(n) / n
        [T, X] = np.meshgrid(thres, x)
        ratio = np.clip(2*X/T - 1, 0, 1)
        # force MATLAB-esque behavior with NaN, inf
        ratio[T == 0] = 1.0
        ratio[X == 0] = 0.0
        q = np.reshape(np.arange(K), [1, 1, K])
        g = np.tile(np.reshape(g_coeff, [1, 1, K + 1]), [c_1.shape[1], 1])
        g[:, :, 0] = g[:, :, 0] + np.log(thres)
        MLE = np.log(X) + (1-X) / (2*X*n)
        MLE[X == 0] = -np.log(n) - const
        tmp = (n*X[:,:,np.newaxis] - q)/(T[:,:,np.newaxis]*(n - q))
        polyApp = np.sum(np.cumprod(np.dstack([np.ones(T.shape + (1,)), tmp]),
                                    axis=2) * g, axis=2)
        polyFail = np.logical_or(np.isnan(polyApp), np.isinf(polyApp))
        polyApp[polyFail] = MLE[polyFail]
        return ratio*MLE + (1-ratio)*polyApp
RATSforClassification.py 文件源码 项目:python_scripting_for_spatial_data_processing 作者: upsdeepak 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def classifyLevel1Assign(classLevel1Img):
    # Create Output Array
    level1 = numpy.empty_like(classLevel1Img, dtype = numpy.dtype('a255'))
    level1[...] = "NA"
    # Non Vegetated
    level1 = numpy.where(numpy.logical_or(classLevel1Img == "NA",
                                          numpy.logical_or(classLevel1Img == "Water",
                                                           classLevel1Img == "Urban")),
                         "Non Vegetated", level1)

    # Vegetated
    level1 = numpy.where(numpy.logical_or(classLevel1Img == "Photosynthetic Vegetated",
                                          classLevel1Img == "Non Photosynthetic Vegetated",
                                          classLevel1Img == "Non Submerged Aquatic Vegetated"),
                         "Vegetated", level1)

    return level1

# A function for classifying level 2
synthesis.py 文件源码 项目:smhr 作者: andycasey 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def _verify_species(self, elements):
        # Format the elements and then check that all are real.
        if isinstance(elements, string_types):
            elements = [elements]

        elements = [str(element).title() for element in elements]
        species = []
        transitions = self.transitions
        for element in elements:
            # Get the species associated with this element
            ii = np.logical_or(
                transitions["elem1"] == element,
                transitions["elem2"] == element)

            # Note plurality/singularity of specie/species.
            # APJ modified to remove isotopes in species
            specie = transitions[ii]["species"]
            specie = (specie*10).astype(int)/10.0
            specie = list(np.unique(specie))
            species.append(specie)

        return species
functional.py 文件源码 项目:mriqc 作者: poldracklab 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def find_spikes(data, spike_thresh):
    data -= np.median(np.median(np.median(data, axis=0), axis=0), axis=0)
    slice_mean = np.median(np.median(data, axis=0), axis=0)
    t_z = _robust_zscore(slice_mean)
    spikes = np.abs(t_z) > spike_thresh
    spike_inds = np.transpose(spikes.nonzero())
    # mask out the spikes and recompute z-scores using variance uncontaminated with spikes.
    # This will catch smaller spikes that may have been swamped by big
    # ones.
    data.mask[:, :, spike_inds[:, 0], spike_inds[:, 1]] = True
    slice_mean2 = np.median(np.median(data, axis=0), axis=0)
    t_z = _robust_zscore(slice_mean2)

    spikes = np.logical_or(spikes, np.abs(t_z) > spike_thresh)
    spike_inds = [tuple(i) for i in np.transpose(spikes.nonzero())]
    return spike_inds, t_z
categorical.py 文件源码 项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda 作者: SignalMedia 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def isnull(self):
        """
        Detect missing values

        Both missing values (-1 in .codes) and NA as a category are detected.

        Returns
        -------
        a boolean array of whether my values are null

        See also
        --------
        pandas.isnull : pandas version
        Categorical.notnull : boolean inverse of Categorical.isnull
        """

        ret = self._codes == -1

        # String/object and float categories can hold np.nan
        if self.categories.dtype.kind in ['S', 'O', 'f']:
            if np.nan in self.categories:
                nan_pos = np.where(isnull(self.categories))[0]
                # we only have one NA in categories
                ret = np.logical_or(ret, self._codes == nan_pos)
        return ret
test_umath.py 文件源码 项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda 作者: SignalMedia 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_truth_table_logical(self):
        # 2, 3 and 4 serves as true values
        input1 = [0, 0, 3, 2]
        input2 = [0, 4, 0, 2]

        typecodes = (np.typecodes['AllFloat']
                     + np.typecodes['AllInteger']
                     + '?')     # boolean
        for dtype in map(np.dtype, typecodes):
            arg1 = np.asarray(input1, dtype=dtype)
            arg2 = np.asarray(input2, dtype=dtype)

            # OR
            out = [False, True, True, True]
            for func in (np.logical_or, np.maximum):
                assert_equal(func(arg1, arg2).astype(bool), out)
            # AND
            out = [False, False, False, True]
            for func in (np.logical_and, np.minimum):
                assert_equal(func(arg1, arg2).astype(bool), out)
            # XOR
            out = [False, True, True, False]
            for func in (np.logical_xor, np.not_equal):
                assert_equal(func(arg1, arg2).astype(bool), out)
test_ufunc.py 文件源码 项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda 作者: SignalMedia 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def test_NotImplemented_not_returned(self):
        # See gh-5964 and gh-2091. Some of these functions are not operator
        # related and were fixed for other reasons in the past.
        binary_funcs = [
            np.power, np.add, np.subtract, np.multiply, np.divide,
            np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
            np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
            np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
            np.logical_and, np.logical_or, np.logical_xor, np.maximum,
            np.minimum, np.mod
            ]

        # These functions still return NotImplemented. Will be fixed in
        # future.
        # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]

        a = np.array('1')
        b = 1
        for f in binary_funcs:
            assert_raises(TypeError, f, a, b)
build_dataset.py 文件源码 项目:hgru4rec 作者: mquad 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def make_sessions(data, session_th=30 * 60, is_ordered=False, user_key='user_id', item_key='item_id', time_key='ts'):
    """Assigns session ids to the events in data without grouping keys"""
    if not is_ordered:
        # sort data by user and time
        data.sort_values(by=[user_key, time_key], ascending=True, inplace=True)
    # compute the time difference between queries
    tdiff = np.diff(data[time_key].values)
    # check which of them are bigger then session_th
    split_session = tdiff > session_th
    split_session = np.r_[True, split_session]
    # check when the user chenges is data
    new_user = data['user_id'].values[1:] != data['user_id'].values[:-1]
    new_user = np.r_[True, new_user]
    # a new sessions stars when at least one of the two conditions is verified
    new_session = np.logical_or(new_user, split_session)
    # compute the session ids
    session_ids = np.cumsum(new_session)
    data['session_id'] = session_ids
    return data
test_umath.py 文件源码 项目:aws-lambda-numpy 作者: vitolimandibhrata 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
def test_truth_table_logical(self):
        # 2, 3 and 4 serves as true values
        input1 = [0, 0, 3, 2]
        input2 = [0, 4, 0, 2]

        typecodes = (np.typecodes['AllFloat']
                     + np.typecodes['AllInteger']
                     + '?')     # boolean
        for dtype in map(np.dtype, typecodes):
            arg1 = np.asarray(input1, dtype=dtype)
            arg2 = np.asarray(input2, dtype=dtype)

            # OR
            out = [False, True, True, True]
            for func in (np.logical_or, np.maximum):
                assert_equal(func(arg1, arg2).astype(bool), out)
            # AND
            out = [False, False, False, True]
            for func in (np.logical_and, np.minimum):
                assert_equal(func(arg1, arg2).astype(bool), out)
            # XOR
            out = [False, True, True, False]
            for func in (np.logical_xor, np.not_equal):
                assert_equal(func(arg1, arg2).astype(bool), out)
test_ufunc.py 文件源码 项目:aws-lambda-numpy 作者: vitolimandibhrata 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_object_logical(self):
        a = np.array([3, None, True, False, "test", ""], dtype=object)
        assert_equal(np.logical_or(a, None),
                        np.array([x or None for x in a], dtype=object))
        assert_equal(np.logical_or(a, True),
                        np.array([x or True for x in a], dtype=object))
        assert_equal(np.logical_or(a, 12),
                        np.array([x or 12 for x in a], dtype=object))
        assert_equal(np.logical_or(a, "blah"),
                        np.array([x or "blah" for x in a], dtype=object))

        assert_equal(np.logical_and(a, None),
                        np.array([x and None for x in a], dtype=object))
        assert_equal(np.logical_and(a, True),
                        np.array([x and True for x in a], dtype=object))
        assert_equal(np.logical_and(a, 12),
                        np.array([x and 12 for x in a], dtype=object))
        assert_equal(np.logical_and(a, "blah"),
                        np.array([x and "blah" for x in a], dtype=object))

        assert_equal(np.logical_not(a),
                        np.array([not x for x in a], dtype=object))

        assert_equal(np.logical_or.reduce(a), 3)
        assert_equal(np.logical_and.reduce(a), None)


问题


面经


文章

微信
公众号

扫码关注公众号