我们从Python开源项目中,提取了以下46个代码示例,用于说明如何使用statistics.variance()。
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
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)
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)) #%%
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)
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)) #%%
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 #%%
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 pvariance(text): """ Finds the population variance of a space-separated list of numbers. Example:: /pvariance 33 54 43 65 43 62 """ return format_output(statistics.pvariance(parse_numeric_list(text)))
def variance(text): """ Finds the variance of a space-separated list of numbers. Example:: /variance 33 54 43 65 43 62 """ return format_output(statistics.variance(parse_numeric_list(text)))
def setup(): commands.add(mean) commands.add(median) commands.add(median_low) commands.add(median_high) commands.add(median_grouped) commands.add(mode) commands.add(pstdev) commands.add(pvariance) commands.add(stdev) commands.add(variance)
def setup_method(self, method): """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() self.values = [17, 20, 15.2, 5, 3.8, 9.2, 6.7, 14, 6] self.count = len(self.values) self.min = min(self.values) self.max = max(self.values) self.total = sum(self.values) self.mean = round(sum(self.values) / len(self.values), 2) self.median = round(statistics.median(self.values), 2) self.deviation = round(statistics.stdev(self.values), 2) self.variance = round(statistics.variance(self.values), 2)
def test_sensor_source(self): """Test if source is a sensor.""" assert setup_component(self.hass, 'sensor', { 'sensor': { 'platform': 'statistics', 'name': 'test', 'entity_id': 'sensor.test_monitored', } }) for value in self.values: self.hass.states.set('sensor.test_monitored', value, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}) self.hass.block_till_done() state = self.hass.states.get('sensor.test_mean') self.assertEqual(str(self.mean), state.state) self.assertEqual(self.min, state.attributes.get('min_value')) self.assertEqual(self.max, state.attributes.get('max_value')) self.assertEqual(self.variance, state.attributes.get('variance')) self.assertEqual(self.median, state.attributes.get('median')) self.assertEqual(self.deviation, state.attributes.get('standard_deviation')) self.assertEqual(self.mean, state.attributes.get('mean')) self.assertEqual(self.count, state.attributes.get('count')) self.assertEqual(self.total, state.attributes.get('total')) self.assertEqual('°C', state.attributes.get('unit_of_measurement'))
def __init__(self, hass, entity_id, name, sampling_size): """Initialize the Statistics sensor.""" self._hass = hass self._entity_id = entity_id self.is_binary = True if self._entity_id.split('.')[0] == \ 'binary_sensor' else False if not self.is_binary: self._name = '{} {}'.format(name, ATTR_MEAN) else: self._name = '{} {}'.format(name, ATTR_COUNT) self._sampling_size = sampling_size self._unit_of_measurement = None if self._sampling_size == 0: self.states = deque() else: self.states = deque(maxlen=self._sampling_size) self.median = self.mean = self.variance = self.stdev = 0 self.min = self.max = self.total = self.count = 0 @callback # pylint: disable=invalid-name def async_stats_sensor_state_listener(entity, old_state, new_state): """Called when the sensor changes state.""" self._unit_of_measurement = new_state.attributes.get( ATTR_UNIT_OF_MEASUREMENT) try: self.states.append(float(new_state.state)) self.count = self.count + 1 except ValueError: self.count = self.count + 1 hass.async_add_job(self.async_update_ha_state, True) async_track_state_change( hass, entity_id, async_stats_sensor_state_listener)
def state_attributes(self): """Return the state attributes of the sensor.""" if not self.is_binary: return { ATTR_MEAN: self.mean, ATTR_COUNT: self.count, ATTR_MAX_VALUE: self.max, ATTR_MEDIAN: self.median, ATTR_MIN_VALUE: self.min, ATTR_SAMPLING_SIZE: 'unlimited' if self._sampling_size is 0 else self._sampling_size, ATTR_STANDARD_DEVIATION: self.stdev, ATTR_TOTAL: self.total, ATTR_VARIANCE: self.variance, }
def attachment_marker(stream_id: uuid, CC_obj: CerebralCortex, config: dict, start_time=None, end_time=None): """ Label sensor data as sensor-on-body, sensor-off-body, or improper-attachment. All the labeled data (st, et, label) with its metadata are then stored in a datastore :param stream_id: UUID :param CC_obj: CerebralCortex object :param config: Data diagnostics configurations """ stream = CC_obj.get_datastream(stream_id, data_type=DataSet.COMPLETE, start_time=start_time, end_time=end_time) results = OrderedDict() threshold_val = None stream_name = stream._name if stream_name == config["stream_names"]["autosense_ecg"]: threshold_val = config['attachment_marker']['ecg_on_body'] label_on = config['labels']['ecg_on_body'] label_off = config['labels']['ecg_off_body'] elif stream_name == config["stream_names"]["autosense_rip"]: threshold_val = config['attachment_marker']['rip_on_body'] label_on = config['labels']['rip_on_body'] label_off = config['labels']['rip_off_body'] else: raise ValueError("Incorrect sensor type.") windowed_data = window(stream.data, config['general']['window_size'], False) for key, data in windowed_data.items(): # remove outliers from a window data normal_values = outlier_detection(data) if stat.variance(normal_values) < threshold_val: results[key] = label_off else: results[key] = label_on merged_windows = merge_consective_windows(results) input_streams = [{"id": str(stream_id), "name": stream_name}] store(input_streams, merged_windows, CC_obj, config, config["algo_names"]["attachment_marker"]) # TODO: gsr_response method is not being used. Need to make sure whether GSR values actually respresent GSR data.