前提条件 : 統計関数の概要
Python は、データ分析と統計に関して非常に人気のある言語です。幸いなことに、Python3 には統計モジュールが提供されており、mean()、median()、mode() などの非常に便利な関数が付属しています。
平均() 関数を使用して、指定された数値リストの平均を計算できます。パラメータとして渡されたデータセットの平均を返します。
算術平均は、データの合計をデータ点の数で割ったものです。これは、範囲が変化する一連の値におけるデータの中心位置の尺度です。 Python では通常、指定された数値の合計を存在する数値のカウントで割ることによってこれを行います。
Given set of numbers : [n1, n2, n3, n5, n6] Sum of data-set = (n1 + n2 + n3 + n4 + n5) Number of data produced = 5 Average or arithmetic mean = (n1 + n2 + n3 + n4 + n5) / 5>
構文 : 平均([データセット])
パラメーター :
[データセット] : 一連の数値のリストまたはタプル。
戻り値 : 提供されたデータセットのサンプルの算術平均。
例外 :
タイプエラー 数値以外のものがパラメータとして渡された場合。
コード #1 : 働く
Python3
CSSの中央の画像
# Python program to demonstrate mean()> # function from the statistics module> # Importing the statistics module> import> statistics> # list of positive integer numbers> data1> => [> 1> ,> 3> ,> 4> ,> 5> ,> 7> ,> 9> ,> 2> ]> x> => statistics.mean(data1)> # Printing the mean> print> (> 'Mean is :'> , x)> |
改行Python
>
>
出力:
Mean is : 4.428571428571429>
コード #2 : 働く
Python3
Javaのスキャナー
# Python program to demonstrate mean()> # function from the statistics module> # Importing the statistics module> from> statistics> import> mean> # Importing fractions module as fr> # Enables to calculate mean of a> # set in Fraction> from> fractions> import> Fraction as fr> # tuple of positive integer numbers> data1> => (> 11> ,> 3> ,> 4> ,> 5> ,> 7> ,> 9> ,> 2> )> # tuple of a negative set of integers> data2> => (> -> 1> ,> -> 2> ,> -> 4> ,> -> 7> ,> -> 12> ,> -> 19> )> # tuple of mixed range of numbers> data3> => (> -> 1> ,> -> 13> ,> -> 6> ,> 4> ,> 5> ,> 19> ,> 9> )> # tuple of a set of fractional numbers> data4> => (fr(> 1> ,> 2> ), fr(> 44> ,> 12> ), fr(> 10> ,> 3> ), fr(> 2> ,> 3> ))> # dictionary of a set of values> # Only the keys are taken in> # consideration by mean()> data5> => {> 1> :> 'one'> ,> 2> :> 'two'> ,> 3> :> 'three'> }> # Printing the mean of above datasets> print> (> 'Mean of data set 1 is % s'> %> (mean(data1)))> print> (> 'Mean of data set 2 is % s'> %> (mean(data2)))> print> (> 'Mean of data set 3 is % s'> %> (mean(data3)))> print> (> 'Mean of data set 4 is % s'> %> (mean(data4)))> print> (> 'Mean of data set 5 is % s'> %> (mean(data5)))> |
>
>
出力:
Mean of data set 1 is 5.857142857142857 Mean of data set 2 is -7.5 Mean of data set 3 is 2.4285714285714284 Mean of data set 4 is 49/24 Mean of data set 5 is 2>
コード #3 : タイプエラー
Python3
正規表現Javaとは何ですか
順番に
# Python3 code to demonstrate TypeError> # importing statistics module> from> statistics> import> mean> # While using dictionaries, only keys are> # taken into consideration by mean()> dic> => {> 'one'> :> 1> ,> 'three'> :> 3> ,> 'seven'> :> 7> ,> > 'twenty'> :> 20> ,> 'nine'> :> 9> ,> 'six'> :> 6> }> # Will raise TypeError> print> (mean(dic))> |
>
>
出力:
Traceback (most recent call last): File '/home/9f8a941703745a24ddce5b5f6f211e6f.py', line 29, in print(mean(dic)) File '/usr/lib/python3.5/statistics.py', line 331, in mean T, total, count = _sum(data) File '/usr/lib/python3.5/statistics.py', line 161, in _sum for n, d in map(_exact_ratio, values): File '/usr/lib/python3.5/statistics.py', line 247, in _exact_ratio raise TypeError(msg.format(type(x).__name__)) TypeError: can't convert type 'str' to numerator/denominator>
アプリケーション:
平均/算術平均は、統計や大きな値を扱う際に非常に重要な関数の 1 つです。したがって、mean() のような関数を使用すると、大規模なデータセットからトレンド値や特徴的な値を抽出できます。