← Back to Python Course | Chapter 13: Data Science & Web | Lesson 7 of 14

Python statistics मॉड्यूल

statistics मॉड्यूल average और सबसे common value जैसी चीज़ें निकालता है, जैसे कोई teacher क्लास के test scores का सार बताए। यह आपको हाथ से गणित करने से बचाता है।
Syntax
python
import statistics

statistics.mean(data)
statistics.median(data)
statistics.stdev(data)

Central Tendency

Built-in statistics मॉड्यूल सामान्य calculations के लिए simple, dependency-free functions देता है: statistics.mean() arithmetic average निकालता है और statistics.median() sorted dataset की middle value ढूँढता है, जो mean की तुलना में outliers के प्रति ज़्यादा robust है।

उदाहरण: Central Tendency

python
import statistics
data = [1, 2, 3, 4, 100]
print(statistics.mean(data))  # arithmetic average, pulled up by the outlier
print(statistics.median(data))  # middle value, unaffected by the outlier

Mode और Frequency

statistics.mode() किसी dataset में सबसे ज़्यादा बार आने वाली एक value लौटाता है, जबकि statistics.multimode() एक से ज़्यादा होने पर सबसे frequent के लिए tied सारी values लौटाता है।

Mode central tendency का एकमात्र माप है जो non-numeric, categorical data के लिए भी अर्थपूर्ण है।

उदाहरण: Mode and Frequency

python
import statistics
data = [1, 2, 2, 3]
print(statistics.mode(data))  # the single most frequent value
print(statistics.multimode([1, 1, 2, 2, 3]))  # all values tied for most frequent

Dispersion के माप

statistics.variance() और statistics.stdev() यह बताते हैं कि dataset की values mean के चारों ओर कितनी फैली हुई हैं -- छोटा standard deviation मतलब values एक साथ tightly cluster हैं, बड़ा मतलब वो widely scattered हैं।

दोनों population और sample variants में आते हैं (pvariance/pstdev बनाम variance/stdev), और गलत वाला चुनना छोटे datasets के लिए results को skew कर देता है।

उदाहरण: Dispersion Measures

python
import statistics
data = [2, 4, 4, 4, 5, 5, 7, 9]
print(statistics.variance(data))  # sample variance
print(statistics.stdev(data))  # sample standard deviation

Data Distribution Ranges

statistics.median_low() और statistics.median_high() उस अस्पष्टता को सुलझाते हैं जो तब आती है जब dataset में values की संख्या even हो और कोई single middle element न हो -- ये दोनों central values को average करने की बजाय क्रमशः निचली और ऊपरी value लौटाते हैं।

उदाहरण: Data Distribution Ranges

python
import statistics
data = [1, 2, 3, 4]
print(statistics.median_low(data))  # lower of the two middle values
print(statistics.median_high(data))  # upper of the two middle values

Harmonic और Geometric Means

statistics.geometric_mean() और statistics.harmonic_mean() विशिष्ट स्थितियों के लिए बने specialized averages हैं: geometric mean growth rates या ratios का average निकालने के लिए उपयुक्त है, और harmonic mean speed जैसी rates का average निकालने के लिए उपयुक्त है, जहाँ arithmetic mean भ्रामक जवाब देता।

उदाहरण: Harmonic and Geometric Means

python
import statistics
data = [2, 8]
print(statistics.geometric_mean(data))  # suited to averaging rates of growth
print(statistics.harmonic_mean(data))  # suited to averaging rates like speed
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.