本文整理汇总了Python中statistics.variance函数的典型用法代码示例。如果您正苦于以下问题:Python variance函数的具体用法?Python variance怎么用?Python variance使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了variance函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
def run(data):
f = open("analyzer.log", 'a+')
c = costs(data)
total = total_cost(data)
f.write("\n############# COST #############\n")
f.write("Total Cost : {0}\n".format(total))
f.write("Total Cost Mean: {0}\n".format(mean(c)))
f.write("Total Cost Median: {0}\n".format(median(c)))
f.write("Total Cost Mode: {0}\n".format(mode(c)))
f.write("Total Cost Variance: {0}\n".format(variance(c)))
cost_action = action(data)
f.write("Cost by Action: \n")
for k, v in cost_action.iteritems():
f.write("\t{0} -> {1} units\n".format(k, v))
f.write("Percentage Cost by Action: \n")
for k, v in cost_action.iteritems():
f.write("\t{0} -> {1} %\n".format(k, round(((v * 100.) / total), 2)))
f.write("Cost Variance by Action: \n")
for k, v in cost_action.iteritems():
c_action = costs_action(data, k)
if len(c_action) > 1:
f.write("\t{0} -> {1} units\n".format(k, round(variance(c_action), 2)))
else:
f.write("\t{0} -> {1} units\n".format(k, round(c_action[0], 2)))
key_max, max_value = max_action_value(cost_action)
f.write("More Expensive Action by value: {0} -> {1}\n".format(key_max[0], cost_action.get(key_max[0])))
key_max, max_value = max_action_percentage(cost_action, total)
f.write("More Expensive Action by percentage: {0} -> {1} %\n".format(key_max, round(max_value, 2)))
f.close()
示例2: model_analysis
def model_analysis(x, x_matrix, y, line, y_hat, b):
n = len(x) # number of samples
s_x = stats.stdev(x) # standard deviation of x values
s_y = stats.stdev(y) # standard deviation of y values
s2_x = stats.variance(x) # variance of x values
s2_y = stats.variance(y) # variance of y values
s_xy = b * s2_x # covariance of VM
mad_temp = 0
SSE = 0
for i in range(len(y)):
temp = abs(y[i] - y_hat[i])
mad_temp += temp
SSE += temp**2 # sum of squares for error
MAD = mad_temp / n
s_err = math.sqrt(SSE / (n - 2)) # standard error of estimate
s_b = s_err / math.sqrt((n - 1) * s2_x)
r = s_xy / (s_x * s_y) # sample coefficient of correlation
R_2 = line.score(x_matrix, y) # coefficient of determination
R_2calc = s_xy**2 / (s2_x * s2_y)
t = b / s_b # t-value for slope assuming true slope = 0
f1.write('\nSkew = ' + str(b) + '\n')
f1.write('Coefficient of correlation (r) = ' + str(r) + '\n')
#f1.write('Coefficient of determination (R^2) via scikit = ' + str(R_2) + '\n')
f1.write('Coefficient of determination (R^2) calculate = ' + str(R_2calc) + '\n')
f1.write('Test statistic for clock skew (t) = ' + str(t) + '\n')
f1.write('Mean Absolute Deviation (MAD) = ' + str(MAD) + '\n')
f1.write('Sum of Squares for Forecast Error (SSE) = ' + str(SSE) + '\n')
return
示例3: main
def main():
statistics_file = open("statics.txt", "w")
stats = {}
final_statistics = {}
entropy_kills = []
entropy_dies = []
initialize_server()
stats = simulate_population()
print(statistics)
for key, val in stats.items() :
entropy_kills += val[0]
entropy_dies += val[1]
print(str(entropy_kills))
statistics_file.write(str(entropy_kills) + "\n")
print(str(entropy_dies))
statistics_file.write(str(entropy_dies) + "\n")
final_statistics.update({"mean entropy kill " : statistics.mean(entropy_kills)})
final_statistics.update({"variance entropy kill " : statistics.variance(entropy_kills)})
final_statistics.update({"mean entropy_dies " : statistics.mean(entropy_dies) })
final_statistics.update({"variance entropy_dies " : statistics.variance(entropy_dies) })
for key, val in final_statistics.items():
print(str(key) + " : " + str(val))
statistics_file.write(str(key) + " : " + str(val) + "\n")
plt.figure(1)
plt.subplot(211)
plt.xlabel('id bot')
plt.ylabel('kill')
plt.boxplot(entropy_kills, labels=list('K'))
plt.subplot(212)
plt.xlabel('id bot')
plt.ylabel('dies')
plt.boxplot(entropy_dies, labels=list('D'))
plt.show()
statistics_file.close()
示例4: countXYZ
def countXYZ(data):
x=[]
y=[]
z=[]
for tmp in data:
x.append(float(int(tmp[0:3],16))*12.0/4096.0)
y.append(float(int(tmp[3:6],16))*14.0/4096.0)
z.append(float(int(tmp[6:9],16))*36.0/4096.0)
return [6.0-sum(x)/len(x),7.0-sum(y)/len(y),sum(z)/len(z),\
statistics.variance(x),statistics.variance(y),statistics.variance(z)]
示例5: caculation
def caculation(data):
trt_1 = data[data['trt'] == 1]
trt_0 = data[data['trt'] == 0]
medi = statistics.median(trt_1['y']) - statistics.median(trt_0['y'])
mean = statistics.mean(trt_1['y']) - statistics.mean(trt_0['y'])
peop = len(trt_1) + len(trt_0)
vari = statistics.variance(trt_1['y']) + statistics.variance(trt_0['y'])
z_stat, p_val = stats.ranksums(trt_0['y'], trt_1['y'])
return [medi, mean, peop, p_val]
示例6: adjRating
def adjRating(self, ratings, VERBOSE=False):
new_ratings = {}
change = False
max_change = 0.0
for team in self.teams:
# Set up arrays for ORating and DRating
ODiff = []
DDiff = []
ct = self.teams[team]
if VERBOSE:
print("%s" % ct.name)
print(" Home Games:")
for game in ct.home_games:
ODiff.append(game.hs + ratings[game.at.name][1])
DDiff.append(game.aws - ratings[game.at.name][0])
if VERBOSE:
print(" %s: "
"ODiff Entry=%.2f, "
"DDiff Entry=%.2f" % (game, ODiff[len(ODiff)-1],
DDiff[len(DDiff)-1]))
if VERBOSE:
print(" Away Games:")
for game in ct.away_games:
ODiff.append(game.aws + ratings[game.ht.name][1])
DDiff.append(game.hs - ratings[game.ht.name][0])
if VERBOSE:
print(" %s: "
"ODiff Entry=%.2f, "
"DDiff Entry=%.2f" % (game, ODiff[len(ODiff)-1],
DDiff[len(DDiff)-1]))
temp_AdjO = (sum(ODiff)/float(len(ODiff))) - self.average
temp_AdjD = self.average - (sum(DDiff)/float(len(DDiff)))
ct.AOV = statistics.variance(ODiff)/len(ODiff)
ct.ADV = statistics.variance(DDiff)/len(DDiff)
new_ratings.update({ct.name: [temp_AdjO, temp_AdjD]})
ch_th_O = abs((temp_AdjO-ratings[ct.name][0])/ratings[ct.name][0])
ch_th_D = abs((temp_AdjD-ratings[ct.name][1])/ratings[ct.name][1])
max_change = max(ch_th_D, ch_th_O, max_change)
if ch_th_O > .0025 or ch_th_D > .0025:
change = True
if VERBOSE:
print(" Team Stats")
print(" AdjPointsScored: %s" % ODiff)
print(" AdjPointsAllowed: %s" % DDiff)
print(" AdjO=%.2f, AdjD=%.2f, "
"ChO=%.4f, ChD=%.4f" %
(temp_AdjO, temp_AdjD, ch_th_O, ch_th_D))
print(" AOV=%.4f, ADV=%.4f" % (ct.AOV, ct.ADV))
# print("%.4f" % max_change)
return change, new_ratings
示例7: get_statistics
def get_statistics(self):
"""
Returns various statistics about the benchmark run.
:return: See description.
"""
ret_val = {}
not_applicable = 'N/A'
if len(self.rollovers) > 1: # Skip the last rollover...it could've been smaller than chunk_size
rollover_times = []
last_time = self.started
for i in xrange(len(self.rollovers) - 1):
rollover_times.append(
(self.rollovers[i] - last_time).total_seconds()
)
last_time = self.rollovers[i]
ret_val['rollover_mean'] = self.num_to_seconds(mean(rollover_times))
ret_val['rollover_stdev'] = self.num_to_seconds(stdev(rollover_times))
ret_val['rollover_variance'] = self.num_to_seconds(variance(rollover_times))
else:
ret_val['rollover_mean'] = not_applicable
ret_val['rollover_stdev'] = not_applicable
ret_val['rollover_variance'] = not_applicable
if self.initial_mem_usage is None:
ret_val['initial_mem_usage'] = not_applicable
else:
ret_val['initial_mem_usage'] = self.num_to_megabytes(self.initial_mem_usage)
if len(self.resources) > 0:
cpu_util_list = [x[1] for x in self.resources]
ret_val['cpu_util_mean'] = self.num_to_percent(mean(cpu_util_list))
ret_val['cpu_util_stdev'] = self.num_to_percent(stdev(cpu_util_list))
if len(cpu_util_list) > 1:
ret_val['cpu_util_variance'] = self.num_to_percent(variance(cpu_util_list))
else:
ret_val['cpu_util_variance'] = not_applicable
mem_usage_list = [x[2] for x in self.resources]
ret_val['mem_usage_mean'] = self.num_to_megabytes(mean(mem_usage_list))
else:
ret_val['cpu_util_mean'] = not_applicable
ret_val['cpu_util_stdev'] = not_applicable
ret_val['cpu_util_variance'] = not_applicable
ret_val['mem_usage_mean'] = not_applicable
return ret_val
示例8: DemoStatistic
def DemoStatistic(dataParam):
#calculate mean
mean = statistics.mean(dataParam);
#calculate median
median = statistics.median(dataParam);
#calculate standard deviation
stdv = statistics.variance(dataParam);
#count values outside 3 sigma range
noiseCount = 0;
for value in dataParam:
if value < (-3* stdv) + mean or value > (3*stdv) + mean:
#print(" %.4f" %(value));
noiseCount += 1;
print("-----------------Simple------------------------");
print("Data length: %d" %(len(dataParam)) );
print("Values outside 3sigma: %d" %(noiseCount) );
print("Mean: %.7f" %(mean));
print("Median: %.7f"%(median));
print("Standard deviation: %.7f"%(stdv));
print("------------------------------------------------");
示例9: analyze
def analyze(graphs):
"""summary stats for the graphs:
>>> graphs = [{'win': ['a', 'b', 'philosophy']}, {'win': ['c', 'd', 'philosophy']}, {'fail': ['e', 'f', 'g', 'h', 'i', 'j', 'k']}]
>>> analyze(graphs)
... {'min': 2, 'max': 2, 'mean': 2.0, 'median': 2.0, 'var': 0.0}
"""
win_path_lengths = []
fail_path_lengths = []
for graph in graphs:
if graph.get('win'):
win_path_lengths.append(len(graph['win']) - 1)
if graph.get('fail'):
fail_path_lengths.append(len(graph['fail']) - 1)
#stats
win_perc = sum(win_path_lengths)/sum([sum(win_path_lengths), sum(fail_path_lengths)])
min_path_length = min(win_path_lengths)
max_path_length = max(win_path_lengths)
mean_path_length = mean(win_path_lengths)
median_path_length = median(win_path_lengths)
var_path_length = variance(win_path_lengths)
print('Cache is enabled by default, turning it off will affect the distributions')
print('Percentage of pages leading to Philosophy: {}'.format(win_perc))
print('Distribution of paths leading to Philosophy: min {}, max {}, mean {}, median {}, var {}'.format(
min_path_length, max_path_length, mean_path_length, median_path_length, var_path_length))
return dict(min=min_path_length,
max=max_path_length,
mean=mean_path_length,
median=median_path_length,
var=var_path_length)
示例10: discard_spurious_lines
def discard_spurious_lines(lines, expected):
""" Discards the discordant line(s).
Discards the line or lines that minimizes the variance
of the distances between the set of the other lines
(of size expected).
The hypothesis is that, when more lines than expected
were detected, the variance of the distances bewteen
lines will be minimized when the spurious lines are
out of the set of selected lines.
Be aware of the number of possible combinations before
calling this function. Having just one or two extra lines
should generally be ok.
"""
best_variance = math.inf
for combination in itertools.combinations(lines, expected):
diffs = [b[0] - a[0]
for a, b in zip(combination[:-1], combination[1:])]
variance = statistics.variance(diffs)
if variance < best_variance:
best_combination = combination
best_variance = variance
return best_combination
示例11: makeCharactOutOfEndDifferencesList
def makeCharactOutOfEndDifferencesList(listdiffs, type=STAT_MEAN):
"""
type=0, mean
1, variance
2, sigma
"""
import copy
import statistics
import scipy.stats as scps
lst = copy.deepcopy(listdiffs)
func = {
-1: statistics.median,
0: statistics.mean,
1: statistics.variance,
2: lambda x: math.sqrt(statistics.variance(x)),
3: scps.skewtest,
4: scps.kurtosistest,
}
for j in range(len(lst)):
for k in range(len(lst[j])):
lst[j][k] = func[type](lst[j][k])
return lst
示例12: compute
def compute(self, ydata):
y = ydata.T
var_y = statistics.variance(y)
self.summary = self.summary+var_y
self.var_y.append(var_y)
return { 'summary': self.summary,
'var_y': self.var_y }
示例13: async_update
def async_update(self):
"""Get the latest data and updates the states."""
if self._max_age is not None:
self._purge_old()
if not self.is_binary:
try: # require only one data point
self.mean = round(statistics.mean(self.states), 2)
self.median = round(statistics.median(self.states), 2)
except statistics.StatisticsError as err:
_LOGGER.error(err)
self.mean = self.median = STATE_UNKNOWN
try: # require at least two data points
self.stdev = round(statistics.stdev(self.states), 2)
self.variance = round(statistics.variance(self.states), 2)
except statistics.StatisticsError as err:
_LOGGER.error(err)
self.stdev = self.variance = STATE_UNKNOWN
if self.states:
self.count = len(self.states)
self.total = round(sum(self.states), 2)
self.min = min(self.states)
self.max = max(self.states)
self.change = self.states[-1] - self.states[0]
self.average_change = self.change
if len(self.states) > 1:
self.average_change /= len(self.states) - 1
if self._max_age is not None:
self.max_age = max(self.ages)
self.min_age = min(self.ages)
else:
self.min = self.max = self.total = STATE_UNKNOWN
self.average_change = self.change = STATE_UNKNOWN
示例14: getVariances
def getVariances(optimalParameters):
params = optimalParameters[0].keys()
variances = {}
for parameter in params:
allValues = [x[parameter] for x in optimalParameters]
variances[parameter] = statistics.variance(allValues)
return variances
示例15: get
def get(self):
values = sorted(self.reservoir.values)
count = len(values)
# instead of failing return empty / subset so that json2insert & co
# don't fail
if count == 0:
return dict(n=0)
elif count == 1:
return dict(min=values[0], max=values[0], mean=values[0], n=count)
percentiles = [percentile(values, p) for p in self.plevels]
min_ = values[0]
max_ = values[-1]
stdev = statistics.stdev(values)
return dict(
min=min_,
max=max_,
mean=statistics.mean(values),
median=statistics.median(values),
variance=statistics.variance(values),
error_margin=error_margin(95, stdev, self.reservoir.count),
stdev=stdev,
# replace . with _ so that the output can be inserted into crate
# crate doesn't allow dots in column names
percentile={str(i[0]).replace('.', '_'): i[1] for i in
zip(self.plevels, percentiles)},
n=self.reservoir.count,
samples=self.reservoir.values
)