本文整理匯總了Python中scipy.stats.mstats.mode方法的典型用法代碼示例。如果您正苦於以下問題:Python mstats.mode方法的具體用法?Python mstats.mode怎麽用?Python mstats.mode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy.stats.mstats
的用法示例。
在下文中一共展示了mstats.mode方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_mode
# 需要導入模塊: from scipy.stats import mstats [as 別名]
# 或者: from scipy.stats.mstats import mode [as 別名]
def test_mode(self):
a1 = [0,0,0,1,1,1,2,3,3,3,3,4,5,6,7]
a2 = np.reshape(a1, (3,5))
a3 = np.array([1,2,3,4,5,6])
a4 = np.reshape(a3, (3,2))
ma1 = ma.masked_where(ma.array(a1) > 2, a1)
ma2 = ma.masked_where(a2 > 2, a2)
ma3 = ma.masked_where(a3 < 2, a3)
ma4 = ma.masked_where(ma.array(a4) < 2, a4)
assert_equal(mstats.mode(a1, axis=None), (3,4))
assert_equal(mstats.mode(a1, axis=0), (3,4))
assert_equal(mstats.mode(ma1, axis=None), (0,3))
assert_equal(mstats.mode(a2, axis=None), (3,4))
assert_equal(mstats.mode(ma2, axis=None), (0,3))
assert_equal(mstats.mode(a3, axis=None), (1,1))
assert_equal(mstats.mode(ma3, axis=None), (2,1))
assert_equal(mstats.mode(a2, axis=0), ([[0,0,0,1,1]], [[1,1,1,1,1]]))
assert_equal(mstats.mode(ma2, axis=0), ([[0,0,0,1,1]], [[1,1,1,1,1]]))
assert_equal(mstats.mode(a2, axis=-1), ([[0],[3],[3]], [[3],[3],[1]]))
assert_equal(mstats.mode(ma2, axis=-1), ([[0],[1],[0]], [[3],[1],[0]]))
assert_equal(mstats.mode(ma4, axis=0), ([[3,2]], [[1,1]]))
assert_equal(mstats.mode(ma4, axis=-1), ([[2],[3],[5]], [[1],[1],[1]]))
示例2: majority_voting_rule
# 需要導入模塊: from scipy.stats import mstats [as 別名]
# 或者: from scipy.stats.mstats import mode [as 別名]
def majority_voting_rule(votes):
"""Applies the majority voting rule to the estimated votes.
Parameters
----------
votes : array of shape (n_samples, n_classifiers),
The votes obtained by each classifier for each sample.
Returns
-------
predicted_label : array of shape (n_samples)
The label of each query sample predicted using the majority voting rule
"""
# Omitting nan value in the predictions as they comes from removed
# classifiers
return mode(votes, axis=1)[0][:, 0]
示例3: test_mode
# 需要導入模塊: from scipy.stats import mstats [as 別名]
# 或者: from scipy.stats.mstats import mode [as 別名]
def test_mode(self):
a1 = [0,0,0,1,1,1,2,3,3,3,3,4,5,6,7]
a2 = np.reshape(a1, (3,5))
a3 = np.array([1,2,3,4,5,6])
a4 = np.reshape(a3, (3,2))
ma1 = ma.masked_where(ma.array(a1) > 2, a1)
ma2 = ma.masked_where(a2 > 2, a2)
ma3 = ma.masked_where(a3 < 2, a3)
ma4 = ma.masked_where(ma.array(a4) < 2, a4)
assert_equal(mstats.mode(a1, axis=None), (3,4))
assert_equal(mstats.mode(a1, axis=0), (3,4))
assert_equal(mstats.mode(ma1, axis=None), (0,3))
assert_equal(mstats.mode(a2, axis=None), (3,4))
assert_equal(mstats.mode(ma2, axis=None), (0,3))
assert_equal(mstats.mode(a3, axis=None), (1,1))
assert_equal(mstats.mode(ma3, axis=None), (2,1))
assert_equal(mstats.mode(a2, axis=0), ([[0,0,0,1,1]], [[1,1,1,1,1]]))
assert_equal(mstats.mode(ma2, axis=0), ([[0,0,0,1,1]], [[1,1,1,1,1]]))
assert_equal(mstats.mode(a2, axis=-1), ([[0],[3],[3]], [[3],[3],[1]]))
assert_equal(mstats.mode(ma2, axis=-1), ([[0],[1],[0]], [[3],[1],[0]]))
assert_equal(mstats.mode(ma4, axis=0), ([[3,2]], [[1,1]]))
assert_equal(mstats.mode(ma4, axis=-1), ([[2],[3],[5]], [[1],[1],[1]]))
a1_res = mstats.mode(a1, axis=None)
# test for namedtuple attributes
attributes = ('mode', 'count')
check_named_results(a1_res, attributes, ma=True)
示例4: test_mode_modifies_input
# 需要導入模塊: from scipy.stats import mstats [as 別名]
# 或者: from scipy.stats.mstats import mode [as 別名]
def test_mode_modifies_input(self):
# regression test for gh-6428: mode(..., axis=None) may not modify
# the input array
im = np.zeros((100, 100))
im[:50, :] += 1
im[:, :50] += 1
cp = im.copy()
a = mstats.mode(im, None)
assert_equal(im, cp)
示例5: median
# 需要導入模塊: from scipy.stats import mstats [as 別名]
# 或者: from scipy.stats.mstats import mode [as 別名]
def median(x: pd.Series, w: Union[Window, int] = Window(None, 0)) -> pd.Series:
"""
Median value of series over given window
:param x: series: timeseries
:param w: Window or int: size of window and ramp up to use. e.g. Window(22, 10) where 22 is the window size
and 10 the ramp up value. Window size defaults to length of series.
:return: timeseries of median value
**Usage**
Computes the `median <https://en.wikipedia.org/wiki/Median>`_ value over a given window. For each window, this
function will return the middle value when all elements in the window are sorted. If the number of observations in
the window is even, will return the average of the middle two values. If the window size is greater than the
available data, will return median of available values:
:math:`d = \\frac{w-1}{2}`
:math:`R_t = \\frac{X_{\lfloor t-d \\rfloor} + X_{\lceil t-d \\rceil}}{2}`
where :math:`w` is the size of the rolling window. If window is not provided, computes median over the full series
**Examples**
Generate price series and compute median over :math:`22` observations
>>> prices = generate_series(100)
>>> median(prices, 22)
**See also**
:func:`mean` :func:`mode`
"""
w = normalize_window(x, w)
assert x.index.is_monotonic_increasing, "series index is monotonic increasing"
if isinstance(w.w, pd.DateOffset):
values = [x.loc[(x.index > idx - w.w) & (x.index <= idx)].median() for idx in x.index]
return apply_ramp(pd.Series(values, index=x.index, dtype=np.dtype(float)), w)
else:
return apply_ramp(x.rolling(w.w, 0).median(), w)
示例6: mode
# 需要導入模塊: from scipy.stats import mstats [as 別名]
# 或者: from scipy.stats.mstats import mode [as 別名]
def mode(x: pd.Series, w: Union[Window, int] = Window(None, 0)) -> pd.Series:
"""
Most common value in series over given window
:param x: series: timeseries
:param w: Window or int: size of window and ramp up to use. e.g. Window(22, 10) where 22 is the window size
and 10 the ramp up value. Window size defaults to length of series.
:return: timeseries of mode value
**Usage**
Computes the `mode <https://en.wikipedia.org/wiki/Mode_(statistics)>`_ over a given window. For each window, this
function will return the most common value of all elements in the window. If there are multiple values with the same
frequency of occurrence, will return the smallest value.
If window is not provided, computes mode over the full series.
**Examples**
Generate price series and compute mode over :math:`22` observations
>>> prices = generate_series(100)
>>> mode(prices, 22)
**See also**
:func:`mean` :func:`median`
"""
w = normalize_window(x, w)
assert x.index.is_monotonic_increasing, "series index is monotonic increasing"
if isinstance(w.w, pd.DateOffset):
values = [stats.mode(x.loc[(x.index > idx - w.w) & (x.index <= idx)]).mode[0] for idx in x.index]
return apply_ramp(pd.Series(values, index=x.index, dtype=np.dtype(float)), w)
else:
return apply_ramp(x.rolling(w.w, 0).apply(lambda y: stats.mode(y).mode, raw=True), w)
示例7: mean
# 需要導入模塊: from scipy.stats import mstats [as 別名]
# 或者: from scipy.stats.mstats import mode [as 別名]
def mean(x: Union[pd.Series, List[pd.Series]], w: Union[Window, int] = Window(None, 0)) -> pd.Series:
"""
Arithmetic mean of series over given window
:param x: series: a timeseries or an array of timeseries
:param w: Window or int: size of window and ramp up to use. e.g. Window(22, 10) where 22 is the window size
and 10 the ramp up value. Window size defaults to length of series.
:return: timeseries of mean value
**Usage**
Calculates `arithmetic mean <https://en.wikipedia.org/wiki/Arithmetic_mean>`_ of the series over a rolling window
If a timeseries is provided:
:math:`R_t = \\frac{\sum_{i=t-w+1}^{t} X_i}{N}`
where :math:`N` is the number of observations in each rolling window, :math:`w`.
If an array of timeseries is provided:
:math:`R_t = \\frac{\sum_{i=t-w+1}^{t} {\sum_{j=1}^{n}} X_{ij}}{N}`
where :math:`n` is the number of series, and :math:`N` is the number of observations in each rolling window,
:math:`w`.
If window is not provided, computes rolling mean over the full series. If the window size is greater than the
available data, will return mean of available values.
**Examples**
Generate price series and compute mean over :math:`22` observations
>>> prices = generate_series(100)
>>> mean(prices, 22)
**See also**
:func:`median` :func:`mode`
"""
if isinstance(x, list):
x = pd.concat(x, axis=1)
w = normalize_window(x, w)
assert x.index.is_monotonic_increasing, "series index is monotonic increasing"
if isinstance(w.w, pd.DateOffset):
values = [np.nanmean(x.loc[(x.index > idx - w.w) & (x.index <= idx)]) for idx in x.index]
else:
values = [np.nanmean(x.iloc[max(idx - w.w + 1, 0): idx + 1]) for idx in range(0, len(x))]
return apply_ramp(pd.Series(values, index=x.index, dtype=np.dtype(float)), w)