本文整理汇总了Python中statistics.variance方法的典型用法代码示例。如果您正苦于以下问题:Python statistics.variance方法的具体用法?Python statistics.variance怎么用?Python statistics.variance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类statistics
的用法示例。
在下文中一共展示了statistics.variance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mu
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def mu(text):
'''
Muñoz Baquedano and Muñoz Urra's readability score (2006)
'''
n = count_words(text)
# Delete all digits
text = ''.join(filter(lambda x: not x.isdigit(), text))
# Cleans it all
clean = re.compile('\W+')
text = clean.sub(' ', text).strip()
text = text.split() # word list
word_lengths = []
for word in text:
word_lengths.append(len(word))
# The mean calculation needs at least 1 value on the list, and the variance, two. If somebody enters only one word or, what is worse, a figure, the calculation breaks, so this is a 'fix'
try:
mean = statistics.mean(word_lengths)
variance = statistics.variance(word_lengths)
mu = (n / (n - 1)) * (mean / variance) * 100
return round(mu, 2)
except:
return 0
示例2: GetStats
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def GetStats(self, request: qrl_pb2.GetStatsReq, context) -> qrl_pb2.GetStatsResp:
response = qrl_pb2.GetStatsResp()
response.node_info.CopyFrom(self.qrlnode.get_node_info())
response.epoch = self.qrlnode.epoch
response.uptime_network = self.qrlnode.uptime_network
response.block_last_reward = self.qrlnode.block_last_reward
response.coins_total_supply = int(self.qrlnode.coin_supply_max)
response.coins_emitted = int(self.qrlnode.coin_supply)
response.block_time_mean = 0
response.block_time_sd = 0
if request.include_timeseries:
tmp = list(self.qrlnode.get_block_timeseries(config.dev.block_timeseries_size))
response.block_timeseries.extend(tmp)
if len(tmp) > 2:
vals = [v.time_last for v in tmp[1:]]
response.block_time_mean = int(mean(vals))
response.block_time_sd = int(variance(vals) ** 0.5)
return response
示例3: test_select_balanced_subset
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def test_select_balanced_subset():
categories = ['a', 'b', 'c']
items = {
'utt-1': {'a': 1, 'b': 0, 'c': 0},
'utt-2': {'a': 1, 'c': 2},
'utt-3': {'a': 1, 'b': 1, 'c': 1},
'utt-4': {'a': 1, 'b': 1, 'c': 0},
'utt-5': {'b': 1, 'c': 0},
'utt-6': {'b': 2, 'c': 1},
'utt-7': {'a': 1, 'b': 0, 'c': 1},
'utt-8': {'a': 1, 'b': 1, 'c': 0},
'utt-9': {'a': 1, 'b': 0, 'c': 0},
'utt-10': {'c': 2},
'utt-11': {'a': 1, 'b': 1, 'c': 1},
'utt-12': {'b': 1, 'c': 0},
'utt-13': {'b': 1, 'c': 0},
'utt-14': {'b': 2, 'c': 1},
'utt-15': {'a': 1, 'b': 0, 'c': 1},
'utt-16': {'a': 1, 'b': 1, 'c': 0}
}
item_ids = utils.select_balanced_subset(items, 10, categories, seed=33)
weights = [0 for __ in categories]
for item_id in item_ids:
for cat, weight in items[item_id].items():
weights[categories.index(cat)] += weight
assert len(item_ids) == 10
assert statistics.variance(weights) <= 1
示例4: crdb_stats
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def crdb_stats(self, data):
print('=> class_refresh_db fixture total: %s' % timedelta(
seconds=sum(data)
))
data = sorted(data)
print('\tCalled %d times' % len(data))
mu = statistics.mean(data)
print('\tMean runtime: %s' % mu)
print('\tMedian runtime: %s' % statistics.median(data))
print('\tVariance: %s' % statistics.variance(data))
示例5: variance
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def variance(self):
return statistics.variance(self.price)
# 标准差
示例6: pvariance
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def pvariance(self):
'返回DataStruct.price的方差 variance'
res = self.price.groupby(level=1
).apply(lambda x: statistics.pvariance(x))
res.name = 'pvariance'
return res
# 方差
示例7: variance
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def variance(self):
'返回DataStruct.price的方差 variance'
res = self.price.groupby(level=1
).apply(lambda x: statistics.variance(x))
res.name = 'variance'
return res
# 标准差
示例8: test_domain_error_regression
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def test_domain_error_regression(self):
# Regression test for a domain error exception.
# (Thanks to Geremy Condra.)
data = [0.123456789012345]*10000
# All the items are identical, so variance should be exactly zero.
# We allow some small round-off error, but not much.
result = self.func(data)
self.assertApproxEqual(result, 0.0, tol=5e-17)
self.assertGreaterEqual(result, 0) # A negative result must fail.
示例9: test_shift_data
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def test_shift_data(self):
# Test that shifting the data by a constant amount does not affect
# the variance or stdev. Or at least not much.
# Due to rounding, this test should be considered an ideal. We allow
# some tolerance away from "no change at all" by setting tol and/or rel
# attributes. Subclasses may set tighter or looser error tolerances.
raw = [1.03, 1.27, 1.94, 2.04, 2.58, 3.14, 4.75, 4.98, 5.42, 6.78]
expected = self.func(raw)
# Don't set shift too high, the bigger it is, the more rounding error.
shift = 1e5
data = [x + shift for x in raw]
self.assertApproxEqual(self.func(data), expected)
示例10: test_iter_list_same
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def test_iter_list_same(self):
# Test that iter data and list data give the same result.
# This is an explicit test that iterators and lists are treated the
# same; justification for this test over and above the similar test
# in UnivariateCommonMixin is that an earlier design had variance and
# friends swap between one- and two-pass algorithms, which would
# sometimes give different results.
data = [random.uniform(-3, 8) for _ in range(1000)]
expected = self.func(data)
self.assertEqual(self.func(iter(data)), expected)
示例11: test_exact_uniform
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def test_exact_uniform(self):
# Test the variance against an exact result for uniform data.
data = list(range(10000))
random.shuffle(data)
expected = (10000**2 - 1)/12 # Exact value.
self.assertEqual(self.func(data), expected)
示例12: test_ints
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def test_ints(self):
# Test population variance with int data.
data = [4, 7, 13, 16]
exact = 22.5
self.assertEqual(self.func(data), exact)
示例13: test_decimals
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def test_decimals(self):
# Test population variance with Decimal data.
D = Decimal
data = [D("12.1"), D("12.2"), D("12.5"), D("12.9")]
exact = D('0.096875')
result = self.func(data)
self.assertEqual(result, exact)
self.assertIsInstance(result, Decimal)
示例14: test_fractions
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def test_fractions(self):
# Test sample variance with Fraction data.
F = Fraction
data = [F(1, 4), F(1, 4), F(3, 4), F(7, 4)]
exact = F(1, 2)
result = self.func(data)
self.assertEqual(result, exact)
self.assertIsInstance(result, Fraction)
示例15: test_compare_to_variance
# 需要导入模块: import statistics [as 别名]
# 或者: from statistics import variance [as 别名]
def test_compare_to_variance(self):
# Test that stdev is, in fact, the square root of variance.
data = [random.uniform(-17, 24) for _ in range(1000)]
expected = math.sqrt(statistics.pvariance(data))
self.assertEqual(self.func(data), expected)