Python numpy.nansum() Examples
The following are 30 code examples for showing how to use numpy.nansum(). 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: NeuroKit Author: neuropsychology File: tests_emg.py License: 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 2
Project: missingpy Author: epsilon-machine File: test_knnimpute.py License: 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
Project: vnpy_crypto Author: birforce File: test_analytics.py License: 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 4
Project: BrainSpace Author: MICA-MNI File: utils.py License: 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 5
Project: neuropythy Author: noahbenson File: cmag.py License: GNU Affero General Public License v3.0 | 5 votes |
def __call__(self, x, y=None): (d,ii) = self.nearest(x, y) n = self.spatial_hash.n bd = (ii == n) ii[bd] = 0 carea = np.reshape(self.surface_area[ii.flatten()], ii.shape) carea[bd] = np.nan d[bd] = np.nan if len(d.shape) == 1: varea = np.pi * np.nanmax(d)**2 carea = np.nansum(carea) else: varea = np.pi * np.nanmax(d, axis=1)**2 carea = np.nansum(carea, axis=1) return carea / varea
Example 6
Project: transferlearning Author: jindongwang File: EasyTL.py License: 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
Example 7
Project: differential-privacy-library Author: IBM File: test_nansum.py License: MIT License | 5 votes |
def test_not_none(self): mech = nansum self.assertIsNotNone(mech)
Example 8
Project: differential-privacy-library Author: IBM File: test_nansum.py License: MIT License | 5 votes |
def test_no_params(self): a = np.array([1, 2, 3]) with self.assertWarns(PrivacyLeakWarning): res = nansum(a) self.assertIsNotNone(res)
Example 9
Project: differential-privacy-library Author: IBM File: test_nansum.py License: MIT License | 5 votes |
def test_no_bounds(self): a = np.array([1, 2, 3]) with self.assertWarns(PrivacyLeakWarning): res = nansum(a, epsilon=1) self.assertIsNotNone(res)
Example 10
Project: differential-privacy-library Author: IBM File: test_nansum.py License: MIT License | 5 votes |
def test_mis_ordered_bounds(self): a = np.array([1, 2, 3]) with self.assertRaises(ValueError): nansum(a, epsilon=1, bounds=(1, 0))
Example 11
Project: differential-privacy-library Author: IBM File: test_nansum.py License: MIT License | 5 votes |
def test_missing_bounds(self): a = np.array([1, 2, 3]) with self.assertWarns(PrivacyLeakWarning): res = nansum(a, epsilon=1, bounds=None) self.assertIsNotNone(res)
Example 12
Project: differential-privacy-library Author: IBM File: test_nansum.py License: MIT License | 5 votes |
def test_inf_epsilon(self): a = np.random.random(1000) res = float(np.nansum(a)) res_dp = nansum(a, epsilon=float("inf"), bounds=(0, 1)) self.assertAlmostEqual(res, res_dp)
Example 13
Project: differential-privacy-library Author: IBM File: test_nansum.py License: MIT License | 5 votes |
def test_large_epsilon(self): a = np.random.random(1000) res = float(np.nansum(a)) res_dp = nansum(a, epsilon=1, bounds=(0, 1)) self.assertAlmostEqual(res, res_dp, delta=0.01 * res)
Example 14
Project: differential-privacy-library Author: IBM File: test_nansum.py License: MIT License | 5 votes |
def test_axis(self): a = np.random.random((1000, 5)) res_dp = nansum(a, epsilon=1, axis=0, bounds=(0, 1)) self.assertEqual(res_dp.shape, (5,))
Example 15
Project: differential-privacy-library Author: IBM File: test_nansum.py License: MIT License | 5 votes |
def test_clipped_output(self): a = np.random.random((10,)) for i in range(100): self.assertTrue(0 <= nansum(a, epsilon=1e-5, bounds=(0, 1)) <= 10)
Example 16
Project: differential-privacy-library Author: IBM File: test_nansum.py License: MIT License | 5 votes |
def test_int_output(self): a = np.random.random(1000) * 10 res_int = nansum(a, dtype=int, bounds=(0, 10)) self.assertIsInstance(res_int, int) res = np.nansum(a, dtype=int) res_inf = nansum(a, epsilon=float("inf"), dtype=int, bounds=(0, 10)) self.assertEqual(res, res_inf)
Example 17
Project: differential-privacy-library Author: IBM File: test_nansum.py License: MIT License | 5 votes |
def test_nan(self): a = np.random.random((5, 5)) a[2, 2] = np.nan res = nansum(a, bounds=(0, 1)) self.assertFalse(np.isnan(res)) a = np.array([np.nan] * 10) res = nansum(a, epsilon=float("inf"), bounds=(0, 1)) self.assertEqual(0, res)
Example 18
Project: differential-privacy-library Author: IBM File: test_nansum.py License: MIT License | 5 votes |
def test_accountant(self): from diffprivlib.accountant import BudgetAccountant acc = BudgetAccountant(1.5, 0) a = np.random.random((1000, 5)) nansum(a, epsilon=1, bounds=(0, 1), accountant=acc) self.assertEqual((1.0, 0), acc.total()) with acc: with self.assertRaises(BudgetError): nansum(a, epsilon=1, bounds=(0, 1))
Example 19
Project: python-esppy Author: sassoftware File: jmp_score.py License: Apache License 2.0 | 5 votes |
def sum(S): """ Return the sum of array elements treating missing (NaN) as zero. To match JMP's behavior, check if all elements are missing in which case missing is returned. """ if np.all(np.isnan(S)): return np.nan return np.nansum(S)
Example 20
Project: NeuroKit Author: neuropsychology File: eda_intervalrelated.py License: MIT License | 5 votes |
def _eda_intervalrelated_formatinput(interval, output={}): """Format input for dictionary.""" # Sanitize input colnames = interval.columns.values if len([i for i in colnames if "SCR_Peaks" in i]) == 0: raise ValueError( "NeuroKit error: eda_intervalrelated(): Wrong" "input, we couldn't extract SCR peaks." "Please make sure your DataFrame" "contains an `SCR_Peaks` column." ) return output # pylint: disable=W0101 if len([i for i in colnames if "SCR_Amplitude" in i]) == 0: raise ValueError( "NeuroKit error: eda_intervalrelated(): Wrong" "input we couldn't extract SCR peak amplitudes." "Please make sure your DataFrame" "contains an `SCR_Amplitude` column." ) return output # pylint: disable=W0101 peaks = interval["SCR_Peaks"].values amplitude = interval["SCR_Amplitude"].values output["SCR_Peaks_N"] = np.sum(peaks) output["SCR_Peaks_Amplitude_Mean"] = np.nansum(amplitude) / np.sum(peaks) return output
Example 21
Project: quail Author: ContextLab File: fingerprint.py License: MIT License | 5 votes |
def update(self, egg, permute=False, nperms=1000, parallel=False): """ In-place method that updates fingerprint with new data Parameters ---------- egg : quail.Egg Data to update fingerprint Returns ---------- None """ # increment n self.n+=1 next_weights = np.nanmean(_analyze_chunk(egg, analysis=fingerprint_helper, analysis_type='fingerprint', pass_features=True, permute=permute, n_perms=nperms, parallel=parallel).values, 0) if self.state is not None: # multiply states by n c = self.state*self.n # update state self.state = np.nansum(np.array([c, next_weights]), axis=0)/(self.n+1) else: self.state = next_weights # update the history self.history.append(next_weights)
Example 22
Project: recruit Author: Frank-qlu File: test_interaction.py License: Apache License 2.0 | 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 23
Project: recruit Author: Frank-qlu File: test_nanfunctions.py License: Apache License 2.0 | 5 votes |
def test_nansum(self): tgt = np.sum(self.mat) for mat in self.integer_arrays(): assert_equal(np.nansum(mat), tgt)
Example 24
Project: recruit Author: Frank-qlu File: test_nanfunctions.py License: Apache License 2.0 | 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
Project: recruit Author: Frank-qlu File: test_nanfunctions.py License: Apache License 2.0 | 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 26
Project: recruit Author: Frank-qlu File: test_regression.py License: Apache License 2.0 | 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 27
Project: recruit Author: Frank-qlu File: test_panel.py License: Apache License 2.0 | 5 votes |
def test_sum(self): self._check_stat_op('sum', np.sum, skipna_alternative=np.nansum)
Example 28
Project: recruit Author: Frank-qlu File: test_nanops.py License: Apache License 2.0 | 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 29
Project: recruit Author: Frank-qlu File: test_transform.py License: Apache License 2.0 | 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 30
Project: recruit Author: Frank-qlu File: test_other.py License: Apache License 2.0 | 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)