Python implements sum, Count, maximum and minimum values, average, median, standard deviation, and percentage ., Python Median

Source: Internet
Author: User
Tags function calculator

Python implements sum, Count, maximum and minimum values, average, median, standard deviation, and percentage ., Python Median

import sys class Stats:     def __init__(self, sequence):        # sequence of numbers we will process        # convert all items to floats for numerical processing        self.sequence = [float(item) for item in sequence]     def sum(self):        if len(self.sequence) < 1:            return None        else:            return sum(self.sequence)     def count(self):        return len(self.sequence)     def min(self):        if len(self.sequence) < 1:            return None        else:            return min(self.sequence)     def max(self):        if len(self.sequence) < 1:            return None        else:            return max(self.sequence)     def avg(self):        if len(self.sequence) < 1:            return None        else:            return sum(self.sequence) / len(self.sequence)         def median(self):        if len(self.sequence) < 1:            return None        else:            self.sequence.sort()            return self.sequence[len(self.sequence) // 2]     def stdev(self):        if len(self.sequence) < 1:            return None        else:            avg = self.avg()            sdsq = sum([(i - avg) ** 2 for i in self.sequence])            stdev = (sdsq / (len(self.sequence) - 1)) ** .5            return stdev     def percentile(self, percentile):        if len(self.sequence) < 1:            value = None        elif (percentile >= 100):            sys.stderr.write('ERROR: percentile must be < 100.  you supplied: %s\n'% percentile)            value = None        else:            element_idx = int(len(self.sequence) * (percentile / 100.0))            self.sequence.sort()            value = self.sequence[element_idx]        return value

  


Average of 10, 11, 10, 11, 12, 13, 12, 14, 12, median and standard deviation

Average value = (10 + 11 + 10 + 11 + 12 + 13 + 12 + 13 + 14 + 12)/10 = 11.8,
The median is to sort the data in the ascending (or ascending) Order (10, 10, 11, 11, 12, 12, 12, 13, 13, 14) and the middle (two of the 5, 6) the average of the two data values (12 + 12)/2 = 12;
The standard deviation is the arithmetic square root of the variance, that is, √ {[(10-11.8) ^ 2 + (11-11.8) ^ 2 +... + (12-11.8) ^ 2]/10} ≈ 1.24739729.

For the calculation formula of Standard Deviation

S square = 1/(n-1) Σ (Xi-X average) Square, S is the sample standard deviation, indicating the degree of discretization of the sample parameter, to be obtained from the root, I from 1 to n, Σ is the sum, and X is the average sample. In Excel, the inserted function-sample standard deviation can automatically calculate the standard deviation of a set of given values.
It turns out that my function calculator can also be used, but now it's broken, you can calculate it yourself.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.