Python numpy.ediff1d() Examples

The following are 30 code examples for showing how to use numpy.ediff1d(). 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: recruit   Author: Frank-qlu   File: test_arraysetops.py    License: Apache License 2.0 6 votes vote down vote up
def test_ediff1d(self):
        zero_elem = np.array([])
        one_elem = np.array([1])
        two_elem = np.array([1, 2])

        assert_array_equal([], ediff1d(zero_elem))
        assert_array_equal([0], ediff1d(zero_elem, to_begin=0))
        assert_array_equal([0], ediff1d(zero_elem, to_end=0))
        assert_array_equal([-1, 0], ediff1d(zero_elem, to_begin=-1, to_end=0))
        assert_array_equal([], ediff1d(one_elem))
        assert_array_equal([1], ediff1d(two_elem))
        assert_array_equal([7,1,9], ediff1d(two_elem, to_begin=7, to_end=9))
        assert_array_equal([5,6,1,7,8], ediff1d(two_elem, to_begin=[5,6], to_end=[7,8]))
        assert_array_equal([1,9], ediff1d(two_elem, to_end=9))
        assert_array_equal([1,7,8], ediff1d(two_elem, to_end=[7,8]))
        assert_array_equal([7,1], ediff1d(two_elem, to_begin=7))
        assert_array_equal([5,6,1], ediff1d(two_elem, to_begin=[5,6])) 
Example 2
Project: mars   Author: mars-project   File: test_base_execute.py    License: Apache License 2.0 6 votes vote down vote up
def testEdiff1d(self):
        data = np.array([1, 2, 4, 7, 0])
        x = tensor(data, chunk_size=2)

        t = ediff1d(x)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.ediff1d(data)
        np.testing.assert_equal(res, expected)

        to_begin = tensor(-99, chunk_size=2)
        to_end = tensor([88, 99], chunk_size=2)
        t = ediff1d(x, to_begin=to_begin, to_end=to_end)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.ediff1d(data, to_begin=-99, to_end=np.array([88, 99]))
        np.testing.assert_equal(res, expected)

        data = [[1, 2, 4], [1, 6, 24]]

        t = ediff1d(tensor(data, chunk_size=2))

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.ediff1d(data)
        np.testing.assert_equal(res, expected) 
Example 3
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: test_arraysetops.py    License: MIT License 6 votes vote down vote up
def test_ediff1d(self):
        zero_elem = np.array([])
        one_elem = np.array([1])
        two_elem = np.array([1, 2])

        assert_array_equal([], ediff1d(zero_elem))
        assert_array_equal([0], ediff1d(zero_elem, to_begin=0))
        assert_array_equal([0], ediff1d(zero_elem, to_end=0))
        assert_array_equal([-1, 0], ediff1d(zero_elem, to_begin=-1, to_end=0))
        assert_array_equal([], ediff1d(one_elem))
        assert_array_equal([1], ediff1d(two_elem))
        assert_array_equal([7,1,9], ediff1d(two_elem, to_begin=7, to_end=9))
        assert_array_equal([5,6,1,7,8], ediff1d(two_elem, to_begin=[5,6], to_end=[7,8]))
        assert_array_equal([1,9], ediff1d(two_elem, to_end=9))
        assert_array_equal([1,7,8], ediff1d(two_elem, to_end=[7,8]))
        assert_array_equal([7,1], ediff1d(two_elem, to_begin=7))
        assert_array_equal([5,6,1], ediff1d(two_elem, to_begin=[5,6])) 
Example 4
Project: pyMeteo   Author: cwebster2   File: dynamics.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def integral_dt(i, t):
   n = len(t)

   iavg = np.empty((n-1), np.float32)
   dt = np.empty((n-1), np.float32)

   iavg[0:n-1] = 0.5 * (i[0:n-1] + i[1:n])

   dt = np.ediff1d(t)

   integral = np.sum(iavg * dt)

   return integral


# helper functions 
Example 5
def test_ediff1d(self):
        zero_elem = np.array([])
        one_elem = np.array([1])
        two_elem = np.array([1, 2])

        assert_array_equal([], ediff1d(zero_elem))
        assert_array_equal([0], ediff1d(zero_elem, to_begin=0))
        assert_array_equal([0], ediff1d(zero_elem, to_end=0))
        assert_array_equal([-1, 0], ediff1d(zero_elem, to_begin=-1, to_end=0))
        assert_array_equal([], ediff1d(one_elem))
        assert_array_equal([1], ediff1d(two_elem))
        assert_array_equal([7,1,9], ediff1d(two_elem, to_begin=7, to_end=9))
        assert_array_equal([5,6,1,7,8], ediff1d(two_elem, to_begin=[5,6], to_end=[7,8]))
        assert_array_equal([1,9], ediff1d(two_elem, to_end=9))
        assert_array_equal([1,7,8], ediff1d(two_elem, to_end=[7,8]))
        assert_array_equal([7,1], ediff1d(two_elem, to_begin=7))
        assert_array_equal([5,6,1], ediff1d(two_elem, to_begin=[5,6])) 
Example 6
Project: PyChemia   Author: MaterialsDiscovery   File: ljcluster.py    License: MIT License 6 votes vote down vote up
def get_duplicates(self, ids, tolerance=None, fast=True):
        ret = {}
        selection = self.ids_sorted(ids)
        values = np.array([self.value(i) for i in selection])
        if len(values) == 0:
            return ret
        diffs = np.ediff1d(values)

        for i in range(len(diffs)):
            idiff = diffs[i]
            if idiff < self.value_tol:
                ident1 = selection[i]
                ident2 = selection[i + 1]
                pcm_log.debug('Testing distances between %s and %s' % (str(ident1), str(ident2)))
                distance = self.distance(ident1, ident2)
                if distance < self.distance_tolerance:
                    pcm_log.debug('Distance %7.3f < %7.3f' % (distance, self.distance_tolerance))
                    ret[ident2] = ident1
        if len(ret) > 0:
            pcm_log.debug('Number of duplicates %d' % len(ret))
        return ret 
Example 7
def _estimate_user_factors(self, ITEM_factors_Y):

        profile_length = np.ediff1d(self.URM_train.indptr)
        profile_length_sqrt = np.sqrt(profile_length)

        # Estimating the USER_factors using ITEM_factors_Y
        if self.verbose:
            print("{}: Estimating user factors... ".format(self.algorithm_name))

        USER_factors = self.URM_train.dot(ITEM_factors_Y)

        #Divide every row for the sqrt of the profile length
        for user_index in range(self.n_users):

            if profile_length_sqrt[user_index] > 0:

                USER_factors[user_index, :] /= profile_length_sqrt[user_index]

        if self.verbose:
            print("{}: Estimating user factors... done!".format(self.algorithm_name))

        return USER_factors 
Example 8
def __init__(self, URM_train, batch_size):
        self.batch_size = batch_size

        URM_train = sps.csr_matrix(URM_train)
        self.n_users, self.n_items = URM_train.shape

        self.R = np.zeros((self.n_users, self.n_items), dtype=np.float32)

        self._users_with_interactions = np.ediff1d(URM_train.indptr)>=1
        self._users_with_interactions = np.arange(self.n_users, dtype=np.int64)[self._users_with_interactions]
        self._users_with_interactions = list(self._users_with_interactions)

        self.train_items, self.test_set = {}, {}

        for user_index in range(self.n_users):

            start_pos = URM_train.indptr[user_index]
            end_pos = URM_train.indptr[user_index+1]

            train_items = URM_train.indices[start_pos:end_pos]

            self.R[user_index][train_items] = 1
            self.train_items[user_index] = list(train_items) 
Example 9
def cold_items_statistics(URM_train, URM_validation, URM_test, URM_test_negative):

    # Cold items experiment
    import  scipy.sparse as sps

    URM_train_validation = URM_train + URM_validation
    n_users, n_items = URM_train_validation.shape

    item_in_train_flag = np.ediff1d(sps.csc_matrix(URM_train_validation).indptr) > 0
    item_in_test_flag = np.ediff1d(sps.csc_matrix(URM_test).indptr) > 0

    test_item_not_in_train_flag = np.logical_and(item_in_test_flag, np.logical_not(item_in_train_flag))
    test_item_in_train_flag = np.logical_and(item_in_test_flag, item_in_train_flag)

    print("The test data contains {} unique items, {} ({:.2f} %) of them never appear in train data".format(
        item_in_test_flag.sum(),
        test_item_not_in_train_flag.sum(),
        test_item_not_in_train_flag.sum()/item_in_test_flag.sum()*100,
    )) 
Example 10
Project: cornac   Author: PreferredAI   File: recom_most_pop.py    License: Apache License 2.0 6 votes vote down vote up
def fit(self, train_set, val_set=None):
        """Fit the model to observations.

        Parameters
        ----------
        train_set: :obj:`cornac.data.Dataset`, required
            User-Item preference data as well as additional modalities.

        val_set: :obj:`cornac.data.Dataset`, optional, default: None
            User-Item preference data for model selection purposes (e.g., early stopping).

        Returns
        -------
        self : object
        """
        Recommender.fit(self, train_set, val_set)

        self.item_pop = np.ediff1d(train_set.csc_matrix.indptr)

        return self 
Example 11
Project: coffeegrindsize   Author: jgagneastro   File: test_arraysetops.py    License: MIT License 6 votes vote down vote up
def test_ediff1d(self):
        zero_elem = np.array([])
        one_elem = np.array([1])
        two_elem = np.array([1, 2])

        assert_array_equal([], ediff1d(zero_elem))
        assert_array_equal([0], ediff1d(zero_elem, to_begin=0))
        assert_array_equal([0], ediff1d(zero_elem, to_end=0))
        assert_array_equal([-1, 0], ediff1d(zero_elem, to_begin=-1, to_end=0))
        assert_array_equal([], ediff1d(one_elem))
        assert_array_equal([1], ediff1d(two_elem))
        assert_array_equal([7,1,9], ediff1d(two_elem, to_begin=7, to_end=9))
        assert_array_equal([5,6,1,7,8], ediff1d(two_elem, to_begin=[5,6], to_end=[7,8]))
        assert_array_equal([1,9], ediff1d(two_elem, to_end=9))
        assert_array_equal([1,7,8], ediff1d(two_elem, to_end=[7,8]))
        assert_array_equal([7,1], ediff1d(two_elem, to_begin=7))
        assert_array_equal([5,6,1], ediff1d(two_elem, to_begin=[5,6])) 
Example 12
Project: NeuroKit   Author: neuropsychology   File: tests_signal_fixpeaks.py    License: MIT License 5 votes vote down vote up
def compute_rmssd(peaks):

    rr = np.ediff1d(peaks, to_begin=0)
    rr[0] = np.mean(rr[1:])
    rmssd = np.sqrt(np.mean(rr ** 2))

    return rmssd 
Example 13
Project: lattice   Author: tensorflow   File: premade_lib.py    License: Apache License 2.0 5 votes vote down vote up
def build_output_calibration_layer(output_calibration_input, model_config,
                                   dtype):
  """Creates a monotonic output calibration layer with inputs range [0, 1].

  Args:
    output_calibration_input: Input to the output calibration layer.
    model_config: Model configuration object describing model architecture.
      Should be one of the model configs in `tfl.configs`.
    dtype: dtype

  Returns:
    A `tfl.layers.PWLCalibration` instance.
  """
  # kernel format: bias followed by diffs between consecutive keypoint outputs.
  kernel_init_values = np.ediff1d(
      model_config.output_initialization,
      to_begin=model_config.output_initialization[0])
  input_keypoints = np.linspace(0.0, 1.0, num=len(kernel_init_values))
  kernel_initializer = tf.keras.initializers.Constant(kernel_init_values)
  kernel_regularizer = _output_calibration_regularizers(model_config)
  return pwl_calibration_layer.PWLCalibration(
      input_keypoints=input_keypoints,
      output_min=model_config.output_min,
      output_max=model_config.output_max,
      kernel_initializer=kernel_initializer,
      kernel_regularizer=kernel_regularizer,
      monotonicity=1,
      dtype=dtype,
      name=OUTPUT_CALIB_LAYER_NAME)(
          output_calibration_input) 
Example 14
Project: recruit   Author: Frank-qlu   File: test_interaction.py    License: Apache License 2.0 5 votes vote down vote up
def test_ediff1d_matrix():
    # 2018-04-29: moved here from core.tests.test_arraysetops.
    assert(isinstance(np.ediff1d(np.matrix(1)), np.matrix))
    assert(isinstance(np.ediff1d(np.matrix(1), to_begin=1), np.matrix)) 
Example 15
Project: recruit   Author: Frank-qlu   File: test_arraysetops.py    License: Apache License 2.0 5 votes vote down vote up
def test_ediff1d_forbidden_type_casts(self, ary, prepend, append):
        # verify resolution of gh-11490

        # specifically, raise an appropriate
        # Exception when attempting to append or
        # prepend with an incompatible type
        msg = 'cannot convert'
        with assert_raises_regex(ValueError, msg):
            ediff1d(ary=ary,
                    to_end=append,
                    to_begin=prepend) 
Example 16
Project: recruit   Author: Frank-qlu   File: test_arraysetops.py    License: Apache License 2.0 5 votes vote down vote up
def test_ediff1d_scalar_handling(self,
                                     ary,
                                     prepend,
                                     append,
                                     expected):
        # maintain backwards-compatibility
        # of scalar prepend / append behavior
        # in ediff1d following fix for gh-11490
        actual = np.ediff1d(ary=ary,
                            to_end=append,
                            to_begin=prepend)
        assert_equal(actual, expected) 
Example 17
Project: recruit   Author: Frank-qlu   File: test_regression.py    License: Apache License 2.0 5 votes vote down vote up
def test_eff1d_casting(self):
        # gh-12711
        x = np.array([1, 2, 4, 7, 0], dtype=np.int16)
        res = np.ediff1d(x, to_begin=-99, to_end=np.array([88, 99]))
        assert_equal(res, [-99,   1,   2,   3,  -7,  88,  99])
        assert_raises(ValueError, np.ediff1d, x, to_begin=(1<<20))
        assert_raises(ValueError, np.ediff1d, x, to_end=(1<<20)) 
Example 18
Project: rankeval   Author: hpclab   File: dataset.py    License: Mozilla Public License 2.0 5 votes vote down vote up
def get_query_sizes(self):
        """
        This method return the size of each query set.

        Returns
        -------
        sizes : numpy 1d array of int
            It is a ndarray of shape (n_queries,)
        """
        return np.ediff1d(self.query_offsets) 
Example 19
Project: rankeval   Author: hpclab   File: test_metrics.py    License: Mozilla Public License 2.0 5 votes vote down vote up
def test_MAP_eval_per_query_countercheck(self):
        idx_y_pred_sorted = np.argsort(self.y_pred_query1)[::-1]
        sorted_y = self.y_query1[idx_y_pred_sorted]

        precision_at_i = np.zeros(self.y_query1.size, dtype=np.float32)
        recall_at_i = np.zeros(self.y_query1.size, dtype=np.float32)
        n_relevant = np.count_nonzero(self.y_query1)

        for i in np.arange(self.y_query1.size):
            relevants_at_i = float(np.count_nonzero(sorted_y[:i+1]))
            precision_at_i[i] = relevants_at_i / (i+1)
            recall_at_i[i] = relevants_at_i / n_relevant

        change_recall_at_i = np.ediff1d(recall_at_i, to_begin=recall_at_i[0])

        map_wo_cut = MAP()
        score1 = map_wo_cut.eval_per_query(self.y_query1, self.y_pred_query1)
        score2 = np.dot(precision_at_i, change_recall_at_i)
        assert_almost_equal(score1, score2, decimal=3)

        for i in np.arange(self.y_query1.size):
            idx_pos_k = np.argwhere(sorted_y[:i+1]).flatten()
            map_at_k = MAP(cutoff=i+1)
            score1 = map_at_k.eval_per_query(self.y_query1, self.y_pred_query1)
            score2 = np.sum(precision_at_i[idx_pos_k]) / min(i+1, n_relevant)
            assert_almost_equal(score1, score2, decimal=3) 
Example 20
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: test_interaction.py    License: MIT License 5 votes vote down vote up
def test_ediff1d_matrix():
    # 2018-04-29: moved here from core.tests.test_arraysetops.
    assert(isinstance(np.ediff1d(np.matrix(1)), np.matrix))
    assert(isinstance(np.ediff1d(np.matrix(1), to_begin=1), np.matrix)) 
Example 21
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: test_arraysetops.py    License: MIT License 5 votes vote down vote up
def test_ediff1d_forbidden_type_casts(self, ary, prepend, append):
        # verify resolution of gh-11490

        # specifically, raise an appropriate
        # Exception when attempting to append or
        # prepend with an incompatible type
        msg = 'cannot convert'
        with assert_raises_regex(ValueError, msg):
            ediff1d(ary=ary,
                    to_end=append,
                    to_begin=prepend) 
Example 22
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: test_arraysetops.py    License: MIT License 5 votes vote down vote up
def test_ediff1d_scalar_handling(self,
                                     ary,
                                     prepend,
                                     append,
                                     expected):
        # maintain backwards-compatibility
        # of scalar prepend / append behavior
        # in ediff1d following fix for gh-11490
        actual = np.ediff1d(ary=ary,
                            to_end=append,
                            to_begin=prepend)
        assert_equal(actual, expected) 
Example 23
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: test_regression.py    License: MIT License 5 votes vote down vote up
def test_eff1d_casting(self):
        # gh-12711
        x = np.array([1, 2, 4, 7, 0], dtype=np.int16)
        res = np.ediff1d(x, to_begin=-99, to_end=np.array([88, 99]))
        assert_equal(res, [-99,   1,   2,   3,  -7,  88,  99])
        assert_raises(ValueError, np.ediff1d, x, to_begin=(1<<20))
        assert_raises(ValueError, np.ediff1d, x, to_end=(1<<20)) 
Example 24
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_interaction.py    License: MIT License 5 votes vote down vote up
def test_ediff1d_matrix():
    # 2018-04-29: moved here from core.tests.test_arraysetops.
    assert(isinstance(np.ediff1d(np.matrix(1)), np.matrix))
    assert(isinstance(np.ediff1d(np.matrix(1), to_begin=1), np.matrix)) 
Example 25
Project: pyMeteo   Author: cwebster2   File: dynamics.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def integral_Bdz(th, thp, z):

  n = len(z)

  th_avg = np.empty((n), np.float32)
  thp_avg = np.empty((n), np.float32)
  dz = np.empty((n), np.float32)

  intBdz = 0.0

  if (th == missingval).any() or \
     (thp == missingval).any() or \
     (z == missingval).any():

     intBdz = missingval
     del th_avg, thp_avg, dz
     return intBdz

  th_avg[0:n-1] = 0.5 * (th[0:n-1] + th[1:n])
  thp_avg[0:n-1] = 0.5 * (thp[0:n-1] + thp[1:n])

  th_avg[n-1] = 0.5*(th[0] + th[n-1])
  thp_avg[n-1] = 0.5*(thp[0] + thp[n-1])

  dz = np.ediff1d(z, to_end=z[0]-z[n-1])

  intBdz = np.sum(-dz * gravity * thp_avg/(th_avg - thp_avg))

  del th_avg, thp_avg, dz
  intBdz = intBdz * km2m  # units now m2/s2

  return intBdz 
Example 26
Project: pyMeteo   Author: cwebster2   File: dynamics.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def srh(_u,_v,_z, zbot, ztop, cx, cy):
    """Calculates the storm relative helicity in the layer between zbot and ztop

    :param _u: U winds (1D vector in z)
    :param _u: V winds (1D vector in z)
    :param _z: z heights (1D vector in z)
    :param zbot: Bottom of the layer
    :param ztop: Top of the layer
    :param cx: u component of storm motion
    :param cy: v component of storm motion

    """

    if zbot < _z[0]:
        zbot = _z[0]

    dz = 10.
    z = np.arange(zbot, ztop+dz, dz)
    nk = len(z)
    u = np.empty(nk, np.float32)
    v = np.empty(nk, np.float32)
    du = np.empty(nk-1, np.float32)
    dv = np.empty(nk-1, np.float32)
    uavg = np.empty(nk-1, np.float32)
    vavg = np.empty(nk-1, np.float32)

    for k in range(nk):
        u[k] = pymeteo.interp.linear(_z,_u,z[k]) 
        v[k] = pymeteo.interp.linear(_z,_v,z[k]) 

    du = np.ediff1d(u)
    dv = np.ediff1d(v)

    uavg[0:nk-1] = 0.5 * (u[0:nk-1] + u[1:nk])
    vavg[0:nk-1] = 0.5 * (v[0:nk-1] + v[1:nk])

    srh = np.sum(-(uavg-cx)*dv + (vavg-cy)*du)
    return srh 
Example 27
def test_ediff1d_matrix():
    # 2018-04-29: moved here from core.tests.test_arraysetops.
    assert(isinstance(np.ediff1d(np.matrix(1)), np.matrix))
    assert(isinstance(np.ediff1d(np.matrix(1), to_begin=1), np.matrix)) 
Example 28
def test_ediff1d_forbidden_type_casts(self, ary, prepend, append):
        # verify resolution of gh-11490

        # specifically, raise an appropriate
        # Exception when attempting to append or
        # prepend with an incompatible type
        msg = 'cannot convert'
        with assert_raises_regex(ValueError, msg):
            ediff1d(ary=ary,
                    to_end=append,
                    to_begin=prepend) 
Example 29
def test_ediff1d_scalar_handling(self,
                                     ary,
                                     prepend,
                                     append,
                                     expected):
        # maintain backwards-compatibility
        # of scalar prepend / append behavior
        # in ediff1d following fix for gh-11490
        actual = np.ediff1d(ary=ary,
                            to_end=append,
                            to_begin=prepend)
        assert_equal(actual, expected) 
Example 30
def test_eff1d_casting(self):
        # gh-12711
        x = np.array([1, 2, 4, 7, 0], dtype=np.int16)
        res = np.ediff1d(x, to_begin=-99, to_end=np.array([88, 99]))
        assert_equal(res, [-99,   1,   2,   3,  -7,  88,  99])
        assert_raises(ValueError, np.ediff1d, x, to_begin=(1<<20))
        assert_raises(ValueError, np.ediff1d, x, to_end=(1<<20))