def PVARIANCE(df, n, price='Close', mu=None):
"""
Population variance of data
"""
pvariance_list = []
i = 0
while i < len(df[price]):
if i + 1 < n:
pvariance = float('NaN')
else:
start = i + 1 - n
end = i + 1
pvariance = statistics.pvariance(df[price][start:end], mu)
pvariance_list.append(pvariance)
i += 1
return pvariance_list
python类variance()的实例源码
def VARIANCE(df, n, price='Close', xbar=None):
"""
Sample variance of data
"""
variance_list = []
i = 0
while i < len(df[price]):
if i + 1 < n:
variance = float('NaN')
else:
start = i + 1 - n
end = i + 1
variance = statistics.variance(df[price][start:end], xbar)
variance_list.append(variance)
i += 1
return variance_list
def math_stats_calculations(point_map):
point_array = []
for team in team_array:
point_array.append(point_map[team])
# Calculates mean
mean_val = str(round(statistics.mean(point_array), 2))
# Calculates median
median_val = str(round(statistics.median(point_array), 2))
# Calculates standard deviation
stdev_val = str(round(statistics.stdev(point_array), 2))
# Calculates variance
var_val = str(round(statistics.variance(point_array), 2))
return (mean_val,median_val,stdev_val,var_val)
# Calls my function
def update(self,new):
# Preload
if(self.index < self.N):
self.window[self.index] = new
self.index += 1
# If Window preloaded - start rolling statistics
if(self.index == self.N):
self.average = statistics.mean(self.window)
self.variance = statistics.variance(self.window)
return
# Push element into window list and remove the old element
old = self.window[0]
self.window.pop(0)
self.window.append(new)
oldavg = self.average
newavg = oldavg + (new - old)/self.N
self.average = newavg
if(self.N > 1):
self.variance += (new-old)*(new-newavg+old-oldavg)/(self.N-1)
Exercises4.py 文件源码
项目:Python-Programming-A-Concise-Introduction
作者: abdullahaalam
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def temp_stat(temps):
""" prints the average, median, std dev, and variance of temps """
import statistics
print(temps)
print("Mean: ", statistics.mean(temps))
print("Median: ", statistics.median(temps))
print("Standard deviation: ", statistics.stdev(temps))
print("Variance: ", statistics.variance(temps))
#%%
Exercises4.py 文件源码
项目:Python-Programming-A-Concise-Introduction
作者: abdullahaalam
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def temp_stat(temps):
""" computes the average, median, std dev, and variance of temps """
import statistics
print(temps)
print("Mean: ", statistics.mean(temps))
print("Median: ", statistics.median(temps))
print("Standard deviation: ", statistics.stdev(temps))
print("Variance: ", statistics.variance(temps))
try:
print("Mode: ", statistics.mode(temps))
except statistics.StatisticsError as e:
print("Mode error: ", e)
#%%
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
def async_update(self):
"""Get the latest data and updates the states."""
if not self.is_binary:
try:
self.mean = round(statistics.mean(self.states), 2)
self.median = round(statistics.median(self.states), 2)
self.stdev = round(statistics.stdev(self.states), 2)
self.variance = round(statistics.variance(self.states), 2)
except statistics.StatisticsError as err:
_LOGGER.warning(err)
self.mean = self.median = STATE_UNKNOWN
self.stdev = self.variance = STATE_UNKNOWN
if self.states:
self.total = round(sum(self.states), 2)
self.min = min(self.states)
self.max = max(self.states)
else:
self.min = self.max = self.total = STATE_UNKNOWN
def variance(self):
return statistics.variance(self.price)
# ???
def __init__(self, window_size):
self.N = window_size
self.window = window_size * [0]
self.average = 0
self.variance = 0
self.stddev = 0
self.index = 0
def getVar(self):
if(self.index == 1):
return 0
elif(self.index < self.N):
return statistics.variance(self.window[0:self.index]) # Make return 0?
return self.variance
def get_score_variance(self, *args, **kwargs):
Score = apps.get_model('ddm_core', 'Score')
scores = Score.objects.filter(criterion=self, *args, **kwargs).values_list('value', flat=True)
try:
return statistics.variance(scores)
except statistics.StatisticsError:
return 0
def get_weight_variance(self, *args, **kwargs):
Weight = apps.get_model('ddm_core', 'Weight')
weights = Weight.objects.filter(criterion=self, *args, **kwargs).values_list('value', flat=True)
try:
return statistics.variance(weights)
except statistics.StatisticsError:
return 0
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))
def main():
print(stats.mean(range(6)))
print(stats.median(range(6)))
print(stats.median_low(range(6)))
print(stats.median_high(range(6)))
print(stats.median_grouped(range(6)))
try:
print(stats.mode(range(6)))
except Exception as e:
print(e)
print(stats.mode(list(range(6)) + [3]))
print(stats.pstdev(list(range(6)) + [3]))
print(stats.stdev(list(range(6)) + [3]))
print(stats.pvariance(list(range(6)) + [3]))
print(stats.variance(list(range(6)) + [3]))
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.
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)
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)
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)
def test_ints(self):
# Test population variance with int data.
data = [4, 7, 13, 16]
exact = 22.5
self.assertEqual(self.func(data), exact)
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)
def test_ints(self):
# Test sample variance with int data.
data = [4, 7, 13, 16]
exact = 30
self.assertEqual(self.func(data), exact)
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)
def test_decimals(self):
# Test sample variance with Decimal data.
D = Decimal
data = [D(2), D(2), D(7), D(9)]
exact = 4*D('9.5')/D(3)
result = self.func(data)
self.assertEqual(result, exact)
self.assertIsInstance(result, Decimal)
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)
Exercises4.py 文件源码
项目:Python-Programming-A-Concise-Introduction
作者: abdullahaalam
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def my_stats(slis):
import statistics
print("Mean: ", statistics.mean(slis))
print("Median: ", statistics.median(slis))
# print("Mode: ", statistics.mode(slis))
try:
print("Mode: ", statistics.mode(slis))
except statistics.StatisticsError as e:
print("Mode error: ", e)
print("Standard Deviation: ", statistics.stdev(slis))
print("Variance: ", statistics.variance(slis))
#%%
Exercises4.py 文件源码
项目:Python-Programming-A-Concise-Introduction
作者: abdullahaalam
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def temp_stat(temps):
""" prints the average, median, std dev, and variance of temps """
pass # replace this pass (a do-nothing) statement with your code
#%%
Exercises4.py 文件源码
项目:Python-Programming-A-Concise-Introduction
作者: abdullahaalam
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def temp_stat(temps):
""" computes the average, median, std dev, and variance of temps """
pass # replace this pass (a do-nothing) statement with your code
#%%
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.
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)