Python numpy.nansum() Examples
The following are 30
code examples of numpy.nansum().
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 also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: utils.py From BrainSpace with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _dominant_set_dense(s, k, is_thresh=False, norm=False, copy=True): """Compute dominant set for a dense matrix.""" if is_thresh: s = s.copy() if copy else s s[s <= k] = 0 else: # keep top k nr, nc = s.shape idx = np.argpartition(s, nc - k, axis=1) row = np.arange(nr)[:, None] if copy: col = idx[:, -k:] # idx largest data = s[row, col] s = np.zeros_like(s) s[row, col] = data else: col = idx[:, :-k] # idx smallest s[row, col] = 0 if norm: s /= np.nansum(s, axis=1, keepdims=True) return s
Example #2
Source File: test_knnimpute.py From missingpy with GNU General Public License v3.0 | 6 votes |
def test_callable_metric(): # Define callable metric that returns the l1 norm: def custom_callable(x, y, missing_values="NaN", squared=False): x = np.ma.array(x, mask=np.isnan(x)) y = np.ma.array(y, mask=np.isnan(y)) dist = np.nansum(np.abs(x-y)) return dist X = np.array([ [4, 3, 3, np.nan], [6, 9, 6, 9], [4, 8, 6, 9], [np.nan, 9, 11, 10.] ]) X_imputed = np.array([ [4, 3, 3, 9], [6, 9, 6, 9], [4, 8, 6, 9], [5, 9, 11, 10.] ]) imputer = KNNImputer(n_neighbors=2, metric=custom_callable) assert_array_equal(imputer.fit_transform(X), X_imputed)
Example #3
Source File: tests_emg.py From NeuroKit with MIT License | 6 votes |
def test_emg_eventrelated(): emg = nk.emg_simulate(duration=20, sampling_rate=1000, burst_number=3) emg_signals, info = nk.emg_process(emg, sampling_rate=1000) epochs = nk.epochs_create( emg_signals, events=[3000, 6000, 9000], sampling_rate=1000, epochs_start=-0.1, epochs_end=1.9 ) emg_eventrelated = nk.emg_eventrelated(epochs) # Test amplitude features no_activation = np.where(emg_eventrelated["EMG_Activation"] == 0)[0][0] assert int(pd.DataFrame(emg_eventrelated.values[no_activation]).isna().sum()) == 4 assert np.alltrue( np.nansum(np.array(emg_eventrelated["EMG_Amplitude_Mean"])) < np.nansum(np.array(emg_eventrelated["EMG_Amplitude_Max"])) ) assert len(emg_eventrelated["Label"]) == 3
Example #4
Source File: test_analytics.py From vnpy_crypto with MIT License | 6 votes |
def test_sum_inf(self): s = Series(np.random.randn(10)) s2 = s.copy() s[5:8] = np.inf s2[5:8] = np.nan assert np.isinf(s.sum()) arr = np.random.randn(100, 100).astype('f4') arr[:, 2] = np.inf with pd.option_context("mode.use_inf_as_na", True): assert_almost_equal(s.sum(), s2.sum()) res = nanops.nansum(arr, axis=1) assert np.isinf(res).all()
Example #5
Source File: test_transform.py From vnpy_crypto with MIT License | 5 votes |
def test_transform_length(): # GH 9697 df = pd.DataFrame({'col1': [1, 1, 2, 2], 'col2': [1, 2, 3, np.nan]}) expected = pd.Series([3.0] * 4) def nsum(x): return np.nansum(x) results = [df.groupby('col1').transform(sum)['col2'], df.groupby('col1')['col2'].transform(sum), df.groupby('col1').transform(nsum)['col2'], df.groupby('col1')['col2'].transform(nsum)] for result in results: assert_series_equal(result, expected, check_names=False)
Example #6
Source File: test_regression.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_nansum_with_boolean(self): # gh-2978 a = np.zeros(2, dtype=bool) try: np.nansum(a) except Exception: raise AssertionError()
Example #7
Source File: test_regression.py From Computable with MIT License | 5 votes |
def test_nansum_with_boolean(self): # gh-2978 a = np.zeros(2, dtype=np.bool) try: np.nansum(a) except: raise AssertionError()
Example #8
Source File: math_ops.py From trax with Apache License 2.0 | 5 votes |
def nanmean(a, axis=None, dtype=None, keepdims=None): # pylint: disable=missing-docstring a = array_ops.array(a) if np.issubdtype(a.dtype, np.bool_) or np.issubdtype(a.dtype, np.integer): return array_ops.mean(a, axis=axis, dtype=dtype, keepdims=keepdims) nan_mask = logical_not(isnan(a)) if dtype is None: dtype = a.dtype normalizer = array_ops.sum( nan_mask, axis=axis, dtype=dtype, keepdims=keepdims) return nansum(a, axis=axis, dtype=dtype, keepdims=keepdims) / normalizer
Example #9
Source File: occurrence_blackbox.py From ibeis with Apache License 2.0 | 5 votes |
def timespace_distance_sec(pt1, pt2, km_per_sec=KM_PER_SEC): # Return in seconds sec1, latlon1 = pt1[0], pt1[1:] sec2, latlon2 = pt2[0], pt2[1:] # Get pure gps distance and convert to seconds km_dist = haversine(latlon1, latlon2) km_dist = km_dist / km_per_sec # Get distance in seconds sec_dist = np.abs(sec1 - sec2) # Add distances # (return nan if points are not comparable, otherwise nansum) parts = np.array([km_dist, sec_dist]) timespace_dist = np.nan if np.all(np.isnan(parts)) else np.nansum(parts) return timespace_dist
Example #10
Source File: test_nanfunctions.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_allnans(self): # Check for FutureWarning with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') res = np.nansum([np.nan]*3, axis=None) assert_(res == 0, 'result is not 0') assert_(len(w) == 0, 'warning raised') # Check scalar res = np.nansum(np.nan) assert_(res == 0, 'result is not 0') assert_(len(w) == 0, 'warning raised') # Check there is no warning for not all-nan np.nansum([0]*3, axis=None) assert_(len(w) == 0, 'unwanted warning raised')
Example #11
Source File: test_nanfunctions.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_nansum(self): tgt = np.sum(self.mat) for mat in self.integer_arrays(): assert_equal(np.nansum(mat), tgt)
Example #12
Source File: test_interaction.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_nanfunctions_matrices_general(): # Check that it works and that type and # shape are preserved # 2018-04-29: moved here from core.tests.test_nanfunctions mat = np.matrix(np.eye(3)) for f in (np.nanargmin, np.nanargmax, np.nansum, np.nanprod, np.nanmean, np.nanvar, np.nanstd): res = f(mat, axis=0) assert_(isinstance(res, np.matrix)) assert_(res.shape == (1, 3)) res = f(mat, axis=1) assert_(isinstance(res, np.matrix)) assert_(res.shape == (3, 1)) res = f(mat) assert_(np.isscalar(res)) for f in np.nancumsum, np.nancumprod: res = f(mat, axis=0) assert_(isinstance(res, np.matrix)) assert_(res.shape == (3, 3)) res = f(mat, axis=1) assert_(isinstance(res, np.matrix)) assert_(res.shape == (3, 3)) res = f(mat) assert_(isinstance(res, np.matrix)) assert_(res.shape == (1, 3*3))
Example #13
Source File: distance.py From Computable with MIT License | 5 votes |
def canberra(u, v): """ Computes the Canberra distance between two 1-D arrays. The Canberra distance is defined as .. math:: d(u,v) = \\sum_i \\frac{|u_i-v_i|} {|u_i|+|v_i|}. Parameters ---------- u : (N,) array_like Input array. v : (N,) array_like Input array. Returns ------- canberra : double The Canberra distance between vectors `u` and `v`. Notes ----- When `u[i]` and `v[i]` are 0 for given i, then the fraction 0/0 = 0 is used in the calculation. """ u = _validate_vector(u) v = _validate_vector(v, dtype=np.float64) olderr = np.seterr(invalid='ignore') try: d = np.nansum(abs(u - v) / (abs(u) + abs(v))) finally: np.seterr(**olderr) return d
Example #14
Source File: test_nanfunctions.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_empty(self): for f, tgt_value in zip([np.nansum, np.nanprod], [0, 1]): mat = np.zeros((0, 3)) tgt = [tgt_value]*3 res = f(mat, axis=0) assert_equal(res, tgt) tgt = [] res = f(mat, axis=1) assert_equal(res, tgt) tgt = tgt_value res = f(mat, axis=None) assert_equal(res, tgt)
Example #15
Source File: test_nanfunctions.py From Computable with MIT License | 5 votes |
def test_nansum(self): tgt = np.sum(self.mat) for mat in self.integer_arrays(): assert_equal(np.nansum(mat), tgt)
Example #16
Source File: testSecurityValues.py From Finance-Python with MIT License | 5 votes |
def testSecurityValuesUnit(self): data = np.array([3, -2, np.nan, np.nan, 4, 5]) index = [1, 2, 3, 4, 5, 6] test = SeriesValues(data, index) test = test.unit() expected = SeriesValues(data / np.nansum(np.abs(data)), dict(zip(index, range(len(index))))) for name in test.index(): if np.isnan(test[name]): self.assertTrue(np.isnan(expected[name])) else: self.assertEqual(test[name], expected[name])
Example #17
Source File: test_other.py From vnpy_crypto with MIT License | 5 votes |
def test_agg_category_nansum(observed): categories = ['a', 'b', 'c'] df = pd.DataFrame({"A": pd.Categorical(['a', 'a', 'b'], categories=categories), 'B': [1, 2, 3]}) result = df.groupby("A", observed=observed).B.agg(np.nansum) expected = pd.Series([3, 3, 0], index=pd.CategoricalIndex(['a', 'b', 'c'], categories=categories, name='A'), name='B') if observed: expected = expected[expected != 0] tm.assert_series_equal(result, expected)
Example #18
Source File: test_nanfunctions.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_nansum(self): tgt = np.sum(self.mat) for mat in self.integer_arrays(): assert_equal(np.nansum(mat), tgt)
Example #19
Source File: test_analytics.py From vnpy_crypto with MIT License | 5 votes |
def test_nansum_buglet(self): s = Series([1.0, np.nan], index=[0, 1]) result = np.nansum(s) assert_almost_equal(result, 1)
Example #20
Source File: test_nanops.py From vnpy_crypto with MIT License | 5 votes |
def test_nansum(self): self.check_funs(nanops.nansum, np.sum, allow_str=False, allow_date=False, allow_tdelta=True, check_dtype=False, empty_targfunc=np.nansum)
Example #21
Source File: test_window.py From vnpy_crypto with MIT License | 5 votes |
def test_rolling_sum(self): self._check_moment_func(np.nansum, name='sum', zero_min_periods_equal=False)
Example #22
Source File: test_regression.py From vnpy_crypto with MIT License | 5 votes |
def test_nansum_with_boolean(self): # gh-2978 a = np.zeros(2, dtype=bool) try: np.nansum(a) except Exception: raise AssertionError()
Example #23
Source File: test_nanfunctions.py From vnpy_crypto with MIT License | 5 votes |
def test_empty(self): for f, tgt_value in zip([np.nansum, np.nanprod], [0, 1]): mat = np.zeros((0, 3)) tgt = [tgt_value]*3 res = f(mat, axis=0) assert_equal(res, tgt) tgt = [] res = f(mat, axis=1) assert_equal(res, tgt) tgt = tgt_value res = f(mat, axis=None) assert_equal(res, tgt)
Example #24
Source File: test_nanfunctions.py From vnpy_crypto with MIT License | 5 votes |
def test_allnans(self): # Check for FutureWarning with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') res = np.nansum([np.nan]*3, axis=None) assert_(res == 0, 'result is not 0') assert_(len(w) == 0, 'warning raised') # Check scalar res = np.nansum(np.nan) assert_(res == 0, 'result is not 0') assert_(len(w) == 0, 'warning raised') # Check there is no warning for not all-nan np.nansum([0]*3, axis=None) assert_(len(w) == 0, 'unwanted warning raised')
Example #25
Source File: test_nanfunctions.py From vnpy_crypto with MIT License | 5 votes |
def test_nansum(self): tgt = np.sum(self.mat) for mat in self.integer_arrays(): assert_equal(np.nansum(mat), tgt)
Example #26
Source File: np_deterministic.py From xskillscore with Apache License 2.0 | 5 votes |
def _get_numpy_funcs(skipna): """ Returns nansum and nanmean if skipna is True; Returns sum and mean if skipna is False. """ if skipna: return np.nansum, np.nanmean else: return np.sum, np.mean
Example #27
Source File: test_regression.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_nansum_with_boolean(self): # gh-2978 a = np.zeros(2, dtype=np.bool) try: np.nansum(a) except: raise AssertionError()
Example #28
Source File: test_nanfunctions.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_empty(self): for f, tgt_value in zip([np.nansum, np.nanprod], [0, 1]): mat = np.zeros((0, 3)) tgt = [tgt_value]*3 res = f(mat, axis=0) assert_equal(res, tgt) tgt = [] res = f(mat, axis=1) assert_equal(res, tgt) tgt = tgt_value res = f(mat, axis=None) assert_equal(res, tgt)
Example #29
Source File: test_nanfunctions.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_allnans(self): # Check for FutureWarning with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') res = np.nansum([np.nan]*3, axis=None) assert_(res == 0, 'result is not 0') assert_(len(w) == 0, 'warning raised') # Check scalar res = np.nansum(np.nan) assert_(res == 0, 'result is not 0') assert_(len(w) == 0, 'warning raised') # Check there is no warning for not all-nan np.nansum([0]*3, axis=None) assert_(len(w) == 0, 'unwanted warning raised')
Example #30
Source File: EasyTL.py From transferlearning with MIT License | 5 votes |
def get_class_center(Xs,Ys,Xt,dist): source_class_center = np.array([]) Dct = np.array([]) for i in np.unique(Ys): sel_mask = Ys == i X_i = Xs[sel_mask.flatten()] mean_i = np.mean(X_i, axis=0) if len(source_class_center) == 0: source_class_center = mean_i.reshape(-1, 1) else: source_class_center = np.hstack((source_class_center, mean_i.reshape(-1, 1))) if dist == "ma": Dct_c = get_ma_dist(Xt, X_i) elif dist == "euclidean": Dct_c = np.sqrt(np.nansum((mean_i - Xt)**2, axis=1)) elif dist == "sqeuc": Dct_c = np.nansum((mean_i - Xt)**2, axis=1) elif dist == "cosine": Dct_c = get_cosine_dist(Xt, mean_i) elif dist == "rbf": Dct_c = np.nansum((mean_i - Xt)**2, axis=1) Dct_c = np.exp(- Dct_c / 1); if len(Dct) == 0: Dct = Dct_c.reshape(-1, 1) else: Dct = np.hstack((Dct, Dct_c.reshape(-1, 1))) return source_class_center, Dct