python类StatisticsError()的实例源码

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)







#%%
metrics.py 文件源码 项目:open-synthesis 作者: twschiller 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def diagnosticity(evaluations):
    """Return the diagnosticity of a piece of evidence given its evaluations against a set of hypotheses.

    :param evaluations: an iterable of iterables of Eval for a piece of evidence
    """
    # The "diagnosticity" needs to capture how well the evidence separates/distinguishes the hypotheses. If we don't
    # show a preference between consistent/inconsistent, STDDEV captures this intuition OK. However, in the future,
    # we may want to favor evidence for which hypotheses are inconsistent. Additionally, we may want to calculate
    # "marginal diagnosticity" which takes into the rest of the evidence.
    # (1) calculate the consensus for each hypothesis
    # (2) map N/A to neutral because N/A doesn't help determine consistency of the evidence
    # (3) calculate the population standard deviation of the evidence. It's more reasonable to consider the set of
    #     hypotheses at a given time to be the population of hypotheses than as a "sample" (although it doesn't matter
    #     much because we're comparing across hypothesis sets of the same size)
    na_neutral = map(mean_na_neutral_vote, evaluations)  # pylint: disable=bad-builtin
    try:
        return statistics.pstdev(filter(None.__ne__, na_neutral))  # pylint: disable=bad-builtin
    except statistics.StatisticsError:
        return 0.0
fields.py 文件源码 项目:formpack 作者: kobotoolbox 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def get_stats(self, metrics, lang=UNSPECIFIED_TRANSLATION, limit=100):

        stats = super(NumField, self).get_stats(metrics, lang, limit)

        stats.update({
            'median': '*',
            'mean': '*',
            'mode': '*',
            'stdev': '*'
        })

        try:
            # require a non empty dataset
            stats['mean'] = statistics.mean(self.flatten_dataset(metrics))
            stats['median'] = statistics.median(self.flatten_dataset(metrics))
            # requires at least 2 values in the dataset
            stats['stdev'] = statistics.stdev(self.flatten_dataset(metrics),
                                              xbar=stats['mean'])
            # requires a non empty dataset and a unique mode
            stats['mode'] = statistics.mode(self.flatten_dataset(metrics))
        except statistics.StatisticsError:
            pass

        return stats
statistics.py 文件源码 项目:homeassistant 作者: NAStools 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 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
order_book.py 文件源码 项目:trading_package 作者: abrahamchaibi 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def get_mode_trade_size(self, side: OrderSide, order_type: OrderType, seconds_ago: int,
                            group_by_period: Optional[int] = None) -> Optional[float]:
        order_quantities = self.get_trade_quantities(side, order_type, seconds_ago, group_by_period)
        if len(order_quantities) == 0:
            return None
        try:
            return mode(order_quantities)
        except StatisticsError:
            return None
views.py 文件源码 项目:zenhub-charts 作者: Adphorus 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def get_median(self, totals):
        try:
            return statistics.median(totals)
        except statistics.StatisticsError:
            return 0
antlang.py 文件源码 项目:antlang4python 作者: AntLang-Software 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def stat(f):
    def g(x):
        if not isinstance(x, list): x = [x]
        try:
            return f(x)
        except statistics.StatisticsError:
            raise Exception('Statistics Error')
    return ccfy(g)
abstract_models.py 文件源码 项目:django-decision-matrix 作者: adamcharnock 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
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
abstract_models.py 文件源码 项目:django-decision-matrix 作者: adamcharnock 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 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
test_statistics.py 文件源码 项目:ouroboros 作者: pybee 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_has_exception(self):
        errmsg = (
                "Expected StatisticsError to be a ValueError, but got a"
                " subclass of %r instead."
                )
        self.assertTrue(hasattr(statistics, 'StatisticsError'))
        self.assertTrue(
                issubclass(statistics.StatisticsError, ValueError),
                errmsg % statistics.StatisticsError.__base__
                )


# === Tests for private utility functions ===
test_statistics.py 文件源码 项目:ouroboros 作者: pybee 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_empty_data(self):
        # Fail when the data argument (first argument) is empty.
        for empty in ([], (), iter([])):
            self.assertRaises(statistics.StatisticsError, self.func, empty)
test_statistics.py 文件源码 项目:ouroboros 作者: pybee 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_range_data(self):
        # Override test from UnivariateCommonMixin.
        data = range(20, 50, 3)
        self.assertRaises(statistics.StatisticsError, self.func, data)
test_statistics.py 文件源码 项目:ouroboros 作者: pybee 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def test_bimodal_data(self):
        # Test mode with bimodal data.
        data = [1, 1, 2, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6, 7, 8, 9, 9]
        assert data.count(2) == data.count(6) == 4
        # Check for an exception.
        self.assertRaises(statistics.StatisticsError, self.func, data)
test_statistics.py 文件源码 项目:ouroboros 作者: pybee 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def test_unique_data_failure(self):
        # Test mode exception when data points are all unique.
        data = list(range(10))
        self.assertRaises(statistics.StatisticsError, self.func, data)
test_statistics.py 文件源码 项目:ouroboros 作者: pybee 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_single_value(self):
        # Override method from VarianceStdevMixin.
        for x in (35, 24.7, 8.2e15, Fraction(19, 30), Decimal('4.2084')):
            self.assertRaises(statistics.StatisticsError, self.func, [x])
test_statistics.py 文件源码 项目:ouroboros 作者: pybee 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_single_value(self):
        # Override method from VarianceStdevMixin.
        for x in (81, 203.74, 3.9e14, Fraction(5, 21), Decimal('35.719')):
            self.assertRaises(statistics.StatisticsError, self.func, [x])
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))

#%%
test_statistics.py 文件源码 项目:kbe_server 作者: xiaohaoppy 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_has_exception(self):
        errmsg = (
                "Expected StatisticsError to be a ValueError, but got a"
                " subclass of %r instead."
                )
        self.assertTrue(hasattr(statistics, 'StatisticsError'))
        self.assertTrue(
                issubclass(statistics.StatisticsError, ValueError),
                errmsg % statistics.StatisticsError.__base__
                )


# === Tests for private utility functions ===
test_statistics.py 文件源码 项目:kbe_server 作者: xiaohaoppy 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_empty_data(self):
        # Fail when the data argument (first argument) is empty.
        for empty in ([], (), iter([])):
            self.assertRaises(statistics.StatisticsError, self.func, empty)
test_statistics.py 文件源码 项目:kbe_server 作者: xiaohaoppy 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_range_data(self):
        # Override test from UnivariateCommonMixin.
        data = range(20, 50, 3)
        self.assertRaises(statistics.StatisticsError, self.func, data)
test_statistics.py 文件源码 项目:kbe_server 作者: xiaohaoppy 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_bimodal_data(self):
        # Test mode with bimodal data.
        data = [1, 1, 2, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6, 7, 8, 9, 9]
        assert data.count(2) == data.count(6) == 4
        # Check for an exception.
        self.assertRaises(statistics.StatisticsError, self.func, data)
test_statistics.py 文件源码 项目:kbe_server 作者: xiaohaoppy 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_unique_data_failure(self):
        # Test mode exception when data points are all unique.
        data = list(range(10))
        self.assertRaises(statistics.StatisticsError, self.func, data)
test_statistics.py 文件源码 项目:kbe_server 作者: xiaohaoppy 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_single_value(self):
        # Override method from VarianceStdevMixin.
        for x in (35, 24.7, 8.2e15, Fraction(19, 30), Decimal('4.2084')):
            self.assertRaises(statistics.StatisticsError, self.func, [x])
test_statistics.py 文件源码 项目:kbe_server 作者: xiaohaoppy 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_single_value(self):
        # Override method from VarianceStdevMixin.
        for x in (81, 203.74, 3.9e14, Fraction(5, 21), Decimal('35.719')):
            self.assertRaises(statistics.StatisticsError, self.func, [x])
antlang.py 文件源码 项目:Julus 作者: AntLang-Software 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def stat(f):
    def g(x):
        if not isinstance(x, list): x = [x]
        try:
            return f(x)
        except statistics.StatisticsError:
            raise Exception('Statistics Error')
    return ccfy(g)
run.py 文件源码 项目:pydantic 作者: samuelcolvin 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def stdev(d):
    try:
        return stdev_(d)
    except StatisticsError:
        return 0
dot_to_dot_plotter.py 文件源码 项目:Robo-Plot 作者: JackBuck 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def _try_compute_mode(objects):
    """
    Computes the mode of a set of object, if a unique such exists.

    Args:
        objects (list[T]): the object whose mode is to be computed

    Returns:
        T: the modal value, or None if a unique mode does not exist
    """
    try:
        numeric_value = statistics.mode(objects)  # This _is_ 'None' friendly
    except statistics.StatisticsError:  # No unique value, or empty data
        numeric_value = None
    return numeric_value
trade_history.py 文件源码 项目:historia 作者: eranimo 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def average(self, key, day_range=15):
        "Gets the average amount of the given Good's record in the last `range` days"
        if key in self.record:
            try:
                return mean(self.record[key][-day_range:])
            except StatisticsError:
                return 0
        return 0
tasks.py 文件源码 项目:freesound-datasets 作者: MTG 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def compute_dataset_difficult_agreement(store_key, dataset_id):
    logger.info('Start computing data for {0}'.format(store_key))
    try:
        dataset = Dataset.objects.get(id=dataset_id)
        nodes = dataset.taxonomy.taxonomynode_set.all()
        reference_date = datetime.datetime.today() - datetime.timedelta(days=31)
        difficult_agreement_categories = list()
        difficult_agreement_categories_last_month = list()

        for node in nodes:
            ground_truth_annotations = node.ground_truth_annotations.filter(from_propagation=False)
            ground_truth_annotations_last_month = node.ground_truth_annotations.filter(from_propagation=False,
                                                                                       created_at__gt=reference_date)
            try:
                mean_votes_agreement = mean([annotation.from_candidate_annotation.votes.count()
                                             for annotation in ground_truth_annotations])
            except StatisticsError:
                mean_votes_agreement = 0
            try:
                mean_votes_agreement_last_month = mean([annotation.from_candidate_annotation.votes.count()
                                                        for annotation in ground_truth_annotations_last_month])
            except StatisticsError:
                mean_votes_agreement_last_month = 0

            difficult_agreement_categories.append((node.url_id, node.name, mean_votes_agreement, node.omitted))
            difficult_agreement_categories_last_month.append((node.url_id, node.name, mean_votes_agreement_last_month, node.omitted))

        difficult_agreement_categories = [category_name_votes for category_name_votes in difficult_agreement_categories
                                          if category_name_votes[2] > 2]
        difficult_agreement_categories = sorted(difficult_agreement_categories, key=lambda x: x[2], reverse=True)
        difficult_agreement_categories_last_month = [category_name_votes for category_name_votes
                                                     in difficult_agreement_categories_last_month
                                                     if category_name_votes[2] > 2]
        difficult_agreement_categories_last_month = sorted(difficult_agreement_categories_last_month, key=lambda x: x[2]
                                                           , reverse=True)

        store.set(store_key, {'difficult_agreement_categories': difficult_agreement_categories,
                              'difficult_agreement_categories_last_month': difficult_agreement_categories_last_month})

        logger.info('Finished computing data for {0}'.format(store_key))

    except Dataset.DoesNotExist:
        pass
fields.py 文件源码 项目:formpack 作者: kobotoolbox 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def get_disaggregated_stats(self, metrics, top_splitters,
                                lang=UNSPECIFIED_TRANSLATION, limit=100):

        parent = super(NumField, self)
        stats = parent.get_disaggregated_stats(metrics, top_splitters, lang,
                                               limit)

        substats = {}

        # transpose the metrics data structure to look like
        # {splitter1: [x, y, z], splitter2...}}
        inversed_metrics = defaultdict(list)
        for val, counter in metrics.items():
            if val is None:
                continue
            for splitter, count in counter.items():
                inversed_metrics[splitter].extend([val] * count)

        for splitter, values in inversed_metrics.items():

            val_stats = substats[splitter] = {
                'median': '*',
                'mean': '*',
                'mode': '*',
                'stdev': '*'
            }

            try:
                # require a non empty dataset
                val_stats['mean'] = statistics.mean(values)
                val_stats['median'] = statistics.median(values)
                # requires at least 2 values in the dataset
                val_stats['stdev'] = statistics.stdev(values,
                                                      xbar=val_stats['mean'])
                # requires a non empty dataset and a unique mode
                val_stats['mode'] = statistics.mode(values)
            except statistics.StatisticsError:
                pass

        stats.update({
            'values': tuple(substats.items())[:limit]
        })

        return stats


问题


面经


文章

微信
公众号

扫码关注公众号