logo

Python のログ関数

Python は、モジュールの下に多くの組み込み対数関数を提供します 数学 これにより、単一行を使用してログを計算できるようになります。対数関数には 4 つのバリエーションがあり、この記事ではすべてについて説明します。
1. log(a,(Base)) : この関数は、 自然対数 aの(塩基e)。 2 つの引数が渡された場合、引数 a の希望の底の対数、数値的には次の値が計算されます。 log(a)/log(Base)

 Syntax : math.log(a,Base) Parameters :  a : The numeric value Base : Base to which the logarithm has to be computed. Return Value :  Returns natural log if 1 argument is passed and log with specified base if 2 arguments are passed. Exceptions :  Raises ValueError if a negative no. is passed as argument.>

Python3



トポロジー






# Python code to demonstrate the working of> # log1p(a)> import> math> # Printing the log(1+a) of 14> print> (>'Logarithm(1+a) value of 14 is : '>, end>=>'')> print> (math.log1p(>14>))>



>

>

出力:

Natural logarithm of 14 is : 2.6390573296152584 Logarithm base 5 of 14 is : 1.6397385131955606>

2. log2(a) : この関数は、 底 2 の対数 の。 log(a,2) よりも正確な結果が表示されます。

 Syntax : math.log2(a) Parameters :  a : The numeric value Return Value :  Returns logarithm base 2 of a Exceptions :  Raises ValueError if a negative no. is passed as argument.>

Python3




# Python code to demonstrate the Exception of> # log(a)> import> math> # Printing the log(a) of -14> # Throws Exception> print> (>'log(a) value of -14 is : '>, end>=>'')> print> (math.log(>->14>))>

>

>

出力:

Logarithm base 2 of 14 is : 3.807354922057604>

3. log10(a) : この関数は、 10を底とする対数 の。 log(a,10) よりも正確な結果が表示されます。

 Syntax : math.log10(a) Parameters :  a : The numeric value Return Value :  Returns logarithm base 10 of a Exceptions :  Raises ValueError if a negative no. is passed as argument.>

Python3




# Python code to demonstrate the Application of> # log10(a)> import> math> # Printing no. of digits in 73293> print> (>'The number of digits in 73293 are : '>, end>=>'')> print> (>int>(math.log10(>73293>)>+> 1>))>

>

>

出力:

Logarithm base 10 of 14 is : 1.146128035678238>

3. log1p(a) : この関数は計算に使用されます 対数(1+a)

 Syntax : math.log1p(a) Parameters :  a : The numeric value Return Value :  Returns log(1+a) Exceptions :  Raises ValueError if a negative no. is passed as argument.>

Python3




>

>

出力:

Logarithm(1+a) value of 14 is : 2.70805020110221>
例外

1. 値エラー: 数値が次の場合、この関数は値エラーを返します。 ネガティブ

Python3




# Python code to demonstrate the Exception of> # log(a)> import> math> # Printing the log(a) of -14> # Throws Exception> print> (>'log(a) value of -14 is : '>, end>=>'')> print> (math.log(>->14>))>

>

>

出力:

log(a) value of -14 is :>

ランタイムエラー :

Traceback (most recent call last): File '/home/8a74e9d7e5adfdb902ab15712cbaafe2.py', line 9, in print (math.log(-14)) ValueError: math domain error>
実用化

log10() 関数の応用の 1 つは、 いいえ。数字の桁数 。以下のコードは同じことを示しています。

Python3




# Python code to demonstrate the Application of> # log10(a)> import> math> # Printing no. of digits in 73293> print> (>'The number of digits in 73293 are : '>, end>=>'')> print> (>int>(math.log10(>73293>)>+> 1>))>

>

>

出力:

The number of digits in 73293 are : 5>

自然対数 (log) は、科学技術コンピューティング、データ分析、機械学習アプリケーションで頻繁に使用される Python の重要な数学関数です。ここでは、Python のログ関数に関するメリット、デメリット、重要なポイント、および参考書籍をいくつか紹介します。

利点:

ログ関数は、広範囲の値または非正規分布を持つデータをより正規分布の形式に変換するのに役立ち、これにより統計分析や機械学習モデルの精度を向上させることができます。
log 関数は、複利、現在価値、その他の財務指標を計算するために金融と経済学で広く使用されています。
ログ関数を使用すると、データのスケールを圧縮することで、統計分析における外れ値の影響を軽減できます。
log 関数を使用すると、ダイナミック レンジが広いデータやゼロに近い値のデータを視覚化できます。

短所:

ログ関数は、大規模なデータセットの場合、特にログ関数が繰り返し適用される場合、計算コストが高くなる可能性があります。
ログ関数は、カテゴリ データや境界範囲のあるデータなど、すべての種類のデータに適しているわけではありません。

重要な点:

  1. 自然対数 (log) は、Python の numpy.log() 関数を使用して計算されます。
  2. e 以外の底をもつ対数は、Python の numpy.log10() または numpy.log2() 関数を使用して計算できます。
  3. 自然対数の逆数は指数関数であり、Python の numpy.exp() 関数を使用して計算できます。
  4. 統計分析や機械学習に対数を使用する場合は、分析後にデータを元のスケールに変換することを忘れないでください。

参考書:

Wes McKinney による「データ分析のための Python」では、対数関数を含むデータ分析における NumPy ライブラリとそのアプリケーションについて詳しく説明しています。
Robert Johansson 著の『Numerical Python: A Practical Techniques Approach for Industry』では、NumPy ライブラリとその数値計算および科学計算におけるアプリケーション (対数関数を含む) について詳しく説明しています。
Jake VanderPlas による Python データ サイエンス ハンドブックでは、対数関数を含むデータ サイエンスにおける NumPy ライブラリとそのアプリケーションについて詳しく説明しています。