Python
 Computer >> コンピューター >  >> プログラミング >> Python

Pythonでベースをさまざまな指数に戻します


最初の配列要素が2番目の配列から累乗されたときにベースを返すには、Python Numpyのfloat_power()メソッドを使用します。このメソッドは、x1のベースをx2の指数に上げて返します。 x1とx2の両方がスカラーである場合、これはスカラーです。パラメータx1がベースです。パラメータx2は指数です。

x1の各ベースをx2の位置に対応する累乗に上げます。 x1とx2は同じ形状にブロードキャスト可能である必要があります。これは、整数、float16、およびfloat32がfloat64の最小精度でfloatにプロモートされるため、結果が常に不正確になるという点でべき関数とは異なります。この関数は、負の累乗の場合は使用可能な結果を​​返し、正の累乗の場合はめったにオーバーフローを返さないことを目的としています。負の値を非整数値に上げると、nanが返されます。複雑な結果を得るには、入力をcomplexにキャストするか、dtypeをcomplexに指定します

ステップ

まず、必要なライブラリをインポートします-

import numpy as np

基地-

x1 = range(6)

塩基を表示する-

print("The bases...\n",x1)

指数-

x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]

指数を表示する-

print("\nThe exponents...\n",x2)

最初の配列要素が2番目の配列から累乗されたときにベースを返すには、float_power()メソッド-

を使用します。
print("\nResult...\n",np.float_power(x1, x2))

import numpy as np

# The bases
x1 = range(6)

# Display the bases
print("The bases...\n",x1)

# The exponents
x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]

# Display the exponents
print("\nThe exponents...\n",x2)

# To return the bases when first array elements are raised to powers from second array, use the float_power() method in Python Numpy
# The method returns the bases in x1 raised to the exponents in x2. This is a scalar if both x1 and x2 are scalars.
print("\nResult...\n",np.float_power(x1, x2))
の場合、これはスカラーです。

出力

The bases...
range(0, 6)

The exponents...
[1.0, 2.0, 3.0, 3.0, 2.0, 1.0]

Result...
[ 0. 1. 8. 27. 16. 5.]

  1. PythonPandas-TimedeltaIndexのコンポーネントのデータフレームを返します

    TimedeltaIndexのコンポーネントのデータフレームを返すには、 TimedeltaIndex.componentsを使用します プロパティ。 まず、必要なライブラリをインポートします- import pandas as pd TimeDeltaIndexオブジェクトを作成します。 dataパラメータも使用してtimedeltaのようなデータを設定しました- tdIndex = pd.TimedeltaIndex(data =['10 day 5h 2 min 35s 3us 10ns', '+22:39:19.999999', '2 day

  2. Pythonのreturnステートメント

    ステートメントreturn[expression]は関数を終了し、オプションで式を呼び出し元に返します。引数のないreturnステートメントはreturnNoneと同じです。 例 上記のすべての例は、値を返していません。次のように関数から値を返すことができます- #!/usr/bin/python Function definition is here def sum( arg1, arg2 ):    # Add both the parameters and return them."    total = arg1 + arg2 &nb