1636554120
Numpy.dotは、行列計算のための堅牢な関数です。2つの行列の内積を計算するには、np.dot関数を使用します。その関数について詳しく見ていきましょう。
Numpy dot()は、指定された2つのベクトル(リスト)の数学ドットを返すために使用される数学関数です。np.dot()関数は、3つの引数を受け入れ、指定された2つのベクトルの内積を返します。
ベクトルは、多次元だけでなく一次元でもかまいません。どちらの場合も、数学的内積の規則に従います。
1D配列の場合、これはベクトルの内積です。N次元配列の場合、それは最後の軸上積和であるとの二最後の軸B。
numpy.dot(vector_a, vector_b, out = None)
dot()関数は、主に次の3つのパラメーターを取ります。
numpy.dot()メソッドは、指定された2つのベクトルの内積を返します。ベクトルのいずれかまたは両方が複素数の場合、その複素共役を使用して内積が計算されます。
次のコードを参照してください。
# Program to show working of numpy.dot
# When both the vectors are 1D
# Importing numpy
import numpy as np
# We will create an 1D array
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([2, 3, 4, 5])
# Printing the array
print("The first array is: ", arr1)
print("The second array is: ", arr2)
# Shape of the array
print("Shape of the first array is : ", np.shape(arr1))
print("Shape of the second array is : ", np.shape(arr2))
# Printing dot product of the arr1.arr2
out = np.dot(arr1, arr2)
print("Dot product of arr1 and arr2")
print(out)
# When both are complex
a = 6+1j
b = 4+3j
out1 = np.dot(a, b)
print("Dot product of a and b")
print(out1)
The first array is: [1 2 3 4]
The second array is: [2 3 4 5]
Shape of the first array is : (4,)
Shape of the second array is : (4,)
Dot product of arr1 and arr2
40
Dot product of a and b
(21+22j)
このプログラムでは、最初のケースで、2つの1D配列を初期化し、次に配列とその形状の両方を印刷しました。その後、numpy.dot()を呼び出して、配列の内積を計算しました。
答え40が得られたことがわかります。内積の規則によれば、このようになりました。
(1 * 2 + 2 * 3 + 3 * 4 + 4 * 5)= 40。
また、2番目のケースでは、2つの複素方程式を宣言しました。次に、それらの内積を印刷しました。答えは(21 + 22j)であり、次のように計算されていることがわかります。
6 *(4 + 3j)+ 1j *(4-3j)
= 24 + 18j + 4j-3
= 21 + 22j
次のコードを参照してください。
# Program to show the working of numpy.dot
# When both the vectors are 2D
# Importing numpy
import numpy as np
# We will create an 1D array
arr1 = np.array([[1, 2], [5, 6]])
arr2 = np.array([[2, 3], [2, 4]])
# Printing the array
print("The first array is:\n ", arr1)
print("\nThe second array is: \n ", arr2)
# Shape of the array
print("Shape of the first array is : ", np.shape(arr1))
print("Shape of the second array is : ", np.shape(arr2))
# Printing dot product of the arr1.arr2
out = np.dot(arr1, arr2)
print("Dot product of arr1 and arr2")
print(out)
The first array is:
[[1 2]
[5 6]]
The second array is:
[[2 3]
[2 4]]
Shape of the first array is : (2, 2)
Shape of the second array is : (2, 2)
Dot product of arr1 and arr2
[[ 6 11]
[22 39]]
このプログラムでは、最初のケースで、2つの2D配列を初期化し、次に配列とその形状の両方を印刷しました。その後、numpy.dot()を呼び出して、配列の内積を計算しました。
答えは2D配列にあることがわかります。内積のルールに従って、私たちは答えを得ました。
Numpy dot()メソッドについては以上です。
リンク: https://appdividend.com/2020/08/05/numpy-dot-example-np-dot-in-python/
#python ·
1636554120
Numpy.dotは、行列計算のための堅牢な関数です。2つの行列の内積を計算するには、np.dot関数を使用します。その関数について詳しく見ていきましょう。
Numpy dot()は、指定された2つのベクトル(リスト)の数学ドットを返すために使用される数学関数です。np.dot()関数は、3つの引数を受け入れ、指定された2つのベクトルの内積を返します。
ベクトルは、多次元だけでなく一次元でもかまいません。どちらの場合も、数学的内積の規則に従います。
1D配列の場合、これはベクトルの内積です。N次元配列の場合、それは最後の軸上積和であるとの二最後の軸B。
numpy.dot(vector_a, vector_b, out = None)
dot()関数は、主に次の3つのパラメーターを取ります。
numpy.dot()メソッドは、指定された2つのベクトルの内積を返します。ベクトルのいずれかまたは両方が複素数の場合、その複素共役を使用して内積が計算されます。
次のコードを参照してください。
# Program to show working of numpy.dot
# When both the vectors are 1D
# Importing numpy
import numpy as np
# We will create an 1D array
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([2, 3, 4, 5])
# Printing the array
print("The first array is: ", arr1)
print("The second array is: ", arr2)
# Shape of the array
print("Shape of the first array is : ", np.shape(arr1))
print("Shape of the second array is : ", np.shape(arr2))
# Printing dot product of the arr1.arr2
out = np.dot(arr1, arr2)
print("Dot product of arr1 and arr2")
print(out)
# When both are complex
a = 6+1j
b = 4+3j
out1 = np.dot(a, b)
print("Dot product of a and b")
print(out1)
The first array is: [1 2 3 4]
The second array is: [2 3 4 5]
Shape of the first array is : (4,)
Shape of the second array is : (4,)
Dot product of arr1 and arr2
40
Dot product of a and b
(21+22j)
このプログラムでは、最初のケースで、2つの1D配列を初期化し、次に配列とその形状の両方を印刷しました。その後、numpy.dot()を呼び出して、配列の内積を計算しました。
答え40が得られたことがわかります。内積の規則によれば、このようになりました。
(1 * 2 + 2 * 3 + 3 * 4 + 4 * 5)= 40。
また、2番目のケースでは、2つの複素方程式を宣言しました。次に、それらの内積を印刷しました。答えは(21 + 22j)であり、次のように計算されていることがわかります。
6 *(4 + 3j)+ 1j *(4-3j)
= 24 + 18j + 4j-3
= 21 + 22j
次のコードを参照してください。
# Program to show the working of numpy.dot
# When both the vectors are 2D
# Importing numpy
import numpy as np
# We will create an 1D array
arr1 = np.array([[1, 2], [5, 6]])
arr2 = np.array([[2, 3], [2, 4]])
# Printing the array
print("The first array is:\n ", arr1)
print("\nThe second array is: \n ", arr2)
# Shape of the array
print("Shape of the first array is : ", np.shape(arr1))
print("Shape of the second array is : ", np.shape(arr2))
# Printing dot product of the arr1.arr2
out = np.dot(arr1, arr2)
print("Dot product of arr1 and arr2")
print(out)
The first array is:
[[1 2]
[5 6]]
The second array is:
[[2 3]
[2 4]]
Shape of the first array is : (2, 2)
Shape of the second array is : (2, 2)
Dot product of arr1 and arr2
[[ 6 11]
[22 39]]
このプログラムでは、最初のケースで、2つの2D配列を初期化し、次に配列とその形状の両方を印刷しました。その後、numpy.dot()を呼び出して、配列の内積を計算しました。
答えは2D配列にあることがわかります。内積のルールに従って、私たちは答えを得ました。
Numpy dot()メソッドについては以上です。
リンク: https://appdividend.com/2020/08/05/numpy-dot-example-np-dot-in-python/
#python ·
1638055800
畳み込みとはどういう意味ですか?数学的には、畳み込みは信号処理で一般的に使用される数学演算子です。numpyの配列がシグナルとして機能します。
Numpy convolve()メソッドは、2つの1次元ベクトルの離散線形畳み込みを返すために使用されます。np.convolve()メソッドは、v1、v2、およびmodeの3つの引数を受け入れ 、v1およびv2の1次元ベクトルの離散線形畳み込みを返します。
与えられた2つの信号(numpyの場合は配列)の畳み込みは、最初の信号(配列)の積分として定義でき、反転され、2番目の信号(配列)に畳み込まれ、任意のポイントで(内積で)乗算されます。ベクトルが重なっています。
離散畳み込み演算は、次の関数を使用して定義できます。
(v1 * v2)[n] = ∑ v1 [m] v2 [nm]
numpy.convolve (v1, v2, mode)
Numpy convolve()関数は、最大3つのパラメーターを取ります。
v1:array_like、最初の1次元入力配列。(M、)の形をしているとしましょう
v2: array_like、2番目の1次元入力配列。(N、)の形をしているとしましょう
モード:{'full'、 'same'、 'valid'}、オプション
これはオプションのパラメーターであり、以下で説明する3つの異なるモードがあります。
convolve()メソッドは、v1およびv2の1次元ベクトルの離散線形畳み込みを返します。
# importing the numpy module
import numpy as np
# Making fist 1-D vector v1
v1 = np.array([3, 7])
print("First vector sequence is: ", v1)
# Making second 1-D vector v2
v2 = np.array([1, 2, 5, 7])
print("Second vector sequence is: ", v2)
print("\nprinting linear convolution result between v1 and v2 using default 'full' mode:")
print(np.convolve(v1, v2))
print("\nprinting linear convolution result between v1 and v2 using 'same' mode:")
print(np.convolve(v1, v2, mode='same'))
print("\nprinting linear convolution result between v1 and v2 using 'valid' mode:")
print(np.convolve(v1, v2, mode='valid'))
First vector sequence is: [3 7]
Second vector sequence is: [1 2 5 7]
printing linear convolution result between v1 and v2 using default 'full' mode:
[ 3 13 29 56 49]
printing linear convolution result between v1 and v2 using 'same' mode:
[ 3 13 29 56]
printing linear convolution result between v1 and v2 using 'valid' mode:
[13 29 56]
プログラムconvolve1.pyでは、v1とv2という名前の2つの1次元入力ベクトルを取得しました。次に、3つの異なるモードのそれぞれを使用して線形畳み込み値を表示することにより、出力を表示しました。
v1 = [37]およびv2 = [1 2 5 7]であり、フルモードで動作するため、出力配列の形状は、式の長さ(M + N-1) で与えられます。ここでM = 2およびN = 4です。したがって、結果のベクトルの形状は2 + 4 – 1 = 5になります。
Xは[37]から[73]に反転され、乗算演算は次のように実行されます。
最初の要素:7 *未定義(0として外挿)+ 3 * 1 = 3
2番目の要素:7 * 1 + 3 * 2 = 13
3番目の要素:7 * 2 + 3 * 5 = 29
4番目の要素:7 * 5 + 3 * 7 = 56
5番目の要素は次のとおりです。7* 7 + 3 *未定義(0として外挿)= 49
したがって、結果は次のようになります。[3 13 29 56 49]
v1 = [37]およびv2 = [1 2 5 7]であり、同じモードで動作が実行されるため、出力配列の形状は、式max(M、N ) で与えられます。ここでM = 2およびN = 4です。したがって、結果のベクトルの形状は4になります。
Xは[37]から[73]に反転され、乗算演算は次のように実行されます。
最初の要素:7 *未定義(0として外挿)+ 3 * 1 = 3
2番目の要素:7 * 1 + 3 * 2 = 13
3番目の要素:7 * 2 + 3 * 5 = 29
4番目の要素:7 * 5 + 3 * 7 = 56
5番目の要素は次のとおりです。7* 7 + 3 *未定義(0として外挿)= 49
したがって、結果は次のようになります。[3 13 29 56 49]
v1 = [37]およびv2 = [1 2 5 7]であり、有効モードで演算が実行されるため、出力配列の形状は、式の長さmax(M、N)– min(M、N)+1で与えられます。ここで、M = 2およびN = 4であるため、結果のベクトルの形状は4 – 2 + 1 = 3になります。
ステップ1:Xを[37]から[73]に反転し、次のように乗算演算を実行します。
最初の要素:7 *未定義(0として外挿)+ 3 * 1 = 3
2番目の要素:7 * 1 + 3 * 2 = 13
3番目の要素:7 * 2 + 3 * 5 = 29
4番目の要素:7 * 5 + 3 * 7 = 56
したがって、結果は次のようになります。[3 13 29 56]
最後に、PythonチュートリアルのNumpy convolve()メソッドは終了しました。
リンク: https://appdividend.com/2020/06/03/numpy-convolve-method-in-python/
1638151620
Numpyのcorrelate()メソッドは、2つの1次元ベクトル間の相互相関を見つけるために使用されます。単一処理テキストで一般的に定義されている相関を計算するcorrelate()関数は、次のように与えられます。c_{v1v2} [k] = sum_n v1 [n + k] * conj(v2 [n])v1およびv2シーケンスは必要に応じてゼロが埋め込まれ、接続詞が共役になります。
簡単に言うと、Python numpy.correlate(v1、v2、mode)は、配列v2の逆で配列v1の畳み込みを実行し、指定された3つのモードのいずれかを使用して結果をクリップします。
numpy.correlate (v1, v2, mode,old_behaviour)
reacte()関数は、最大4つのパラメーターを取ります。
v1:array_like、最初の1次元入力配列。(M、)の形をしているとしましょう
v2: array_like、2番目の1次元入力配列。(N、)の形をしているとしましょう
モード:{'valid'、 'same'、 'full'}、オプション
これはオプションのパラメーターであり、以下で説明する3つの異なるモードがあります。
' old_behavior' :bool、これはブールパラメータであり、trueまたはfalseのいずれかを取ることができます。
old_behavoiurが数値からtrueをとる場合(correlate(v1、v2)== correlate(v2、v1)、および共役は複雑な配列には採用されません)。
それ以外の場合、old_behavoiurが数値からfalseを取得する場合は、従来の信号処理定義を使用します。
reacte()メソッドは、v1とv2の1次元ベクトルの相互相関を返します。
# importing the numpy module
import numpy as np
# Making fist 1-D vector v1
v1 = np.array([1, 3, 5])
print("First vector sequence is: ", v1)
# Making second 1-D vector v2
v2 = np.array([0.5, 1.5, 2.5])
print("Second vector sequence is: ", v2)
print("\nprinting cross-correlation result between v1 and v2 using default 'valid' mode:")
print(np.correlate(v1, v2))
print("\nprinting cross-correlation result between v1 and v2 using 'full' mode:")
print(np.correlate(v1, v2, mode='full'))
# printing cross-correlation result between v1 and v2 using 'same' mode
print("\nprinting cross-correlation result between v1 and v2 using 'same' mode:")
print(np.correlate(v1, v2, mode='same'))
First vector sequence is: [1 3 5]
Second vector sequence is: [0.5 1.5 2.5]
printing cross-correlation result between v1 and v2 using default 'valid' mode:
[17.5]
printing cross-correlation result between v1 and v2 using 'full' mode:
[ 2.5 9. 17.5 9. 2.5]
printing cross-correlation result between v1 and v2 using 'same' mode:
[ 9. 17.5 9. ]
プログラムでは、v1とv2という名前の2つの1次元入力ベクトルを取得しました。次に、3つの異なるモードのそれぞれを使用して相互相関値を表示することにより、出力を表示しました。
次のコードを参照してください。
import numpy as np
# Making fist 1-D vector v1
v1 = np.array([1, 3, 5])
print("First vector sequence is: ", v1)
# Making second 1-D vector v2
v2 = np.array([0.5, 1.5, 2.5])
print("Second vector sequence is: ", v2)
print("\nprinting cross-correlation result between v1 and v2 using 'full' mode:")
print(np.correlate(v1, v2, mode='full'))
# Changing order of sequence
# printing cross-correlation result between v2 and v1 using 'full' mode
print("\nAfter reversing the order of sequence and still using 'full' mode:")
print(np.correlate(v2, v1, mode='full'))
# printing cross-correlation result between v1 and v2 using 'same' mode
print("\nprinting cross-correlation result between v1 and v2 using 'same' mode:")
print(np.correlate(v1, v2, mode='same'))
# Changing order of sequence
# printing cross-correlation result between v2 and v1 using 'same' mode
print("\nAfter reversing the order of sequence and still using 'same' mode:")
print(np.correlate(v2, v1, mode='same'))
First vector sequence is: [1 3 5]
Second vector sequence is: [0.5 1.5 2.5]
printing cross-correlation result between v1 and v2 using default 'valid' mode:
[17.5]
printing cross-correlation result between v1 and v2 using 'full' mode:
[ 2.5 9. 17.5 9. 2.5]
printing cross-correlation result between v1 and v2 using 'same' mode:
[ 9. 17.5 9. ]
結果を元のシーケンスで出力し、シーケンスの順序を変更して同じものを出力した後、np.correlate()メソッドを使用して時間反転した複素共役の結果を取得できることを確認できます。
結果は、次の関数を使用して表すことができます。
c_ {v2v1} [k] = c ^ {*} {v1v2} [-k]
2つの1次元シーケンスの相互相関。相関()関数は、信号処理テキストで一般的に定義されている相関を計算します。
最後に、PythonチュートリアルのNumpycorrelate()メソッドは終了しました。
リンク: https://appdividend.com/2020/06/03/numpy-correlate-method-in-python/
1638477900
numpyのbincount()メソッドの使用法は何ですか?正の(+ ve)整数の配列では、numpy.bincount()メソッドが各要素の出現をカウントします。
np.bincount()メソッドはPythonで使用され、numpy配列内に提供される各要素の頻度を取得します。numpy bincount()メソッドは、引数としてarr_name、weights、 および minlength を取り、整数のndarrayを返します。
非負のintの配列内の各値の出現回数をカウントするには、numpy.bincount()関数を使用します。
配列内のすべての要素は整数データ型である必要があり、また、それらは非負である必要があります。それ以外の場合は、TypeErrorが発生します。要素の数は、そのインデックスとして周波数配列/ビンに格納されます。
したがって、各ビン値はそのインデックスの出現に対応すると結論付けることができます。したがって、それに応じてビンサイズを設定できます。ビンのサイズは常に、指定された配列の最大数+1に等しくなります。
例:配列が[1,6,6,4,2,8,5]の場合、8が配列の最大要素であるため、ビンのサイズは9になります。重み配列が提供されている場合、入力配列はそれによって重み付けされます。
numpy.bincount(arr_name, weights = None, minlength=0)
bincount()関数は、最大3つの主要なパラメーターを取ります。
bincount()関数は、intのndarrayを返します。
出力配列は、入力配列内のbinのインデックス値のいくつかのオカレンスで構成されます。
出力配列のサイズは、入力配列内に存在する最大要素よりも+1大きくなります。
# importing the numpy module
import numpy as np
# Input array of non-negative integers
inp_arr = np.array([1, 2, 5, 6, 2, 1, 8, 8, 8])
x = np.bincount(inp_arr)
print("Output of bincount is: ", x)
# printing the size of output bin
print("\nSize of output bin: ", len(x))
Output of bincount is: [0 2 2 0 0 1 1 0 3]
Size of output bin: 9
プログラムbincount1.pyでは、inp_arrという名前のnumpy配列を取得し、頻度がカウントされる配列内に複数の非負の要素を格納しました。
次に、np.bincount()メソッド内のパラメーターとして完全な配列を渡し、その戻り値をxという名前の変数内に格納しました。
その後、xを使用して新しく取得したbincount配列の要素を表示することにより、出力を出力しました。結論として、np.bincount()では、各bin値はそのインデックスの出現に対応しています。
理解を深めるには、以下の画像を参照してください。
次のコードを参照してください。
# importing the numpy module
import numpy as np
# Input array of non-negative integers
inp_arr = np.array([1, 2, 5, 6, 2, 1, 8, 8, 8])
# Weighted Array
weights = np.array([1, 1, 1, 0.5, 1, 1, 1, 1, 2])
output_bin = np.bincount(inp_arr, weights)
print("Output of bincount is: ", output_bin)
# printing the size of output bin
print("\nSize of output bin: ", len(output_bin))
Output of bincount is: [0 2 2 0 0 1 1 0 3]
Size of output bin: 9
bincount2.pyという名前のこのコード例では、weightパラメーターを使用して要素ごとの加算を実行できます。インデックスに対応する要素が要素ごとに追加されます。したがって、異なるインデックスの要素は次のように与えられます。
インデックス0の対応する要素= 0、インデックス1の 対応する要素= 0、 インデックス1の 対応する要素= 1 + 1 = 2、インデックス2の対応する要素= 1 + 1 = 2、インデックス3およびインデックス4の 対応する要素= 0、インデックス5の対応する要素= 1、インデックス6の対応する要素= 0.5、で。
インデックス7の対応する要素= 0、インデックス8で、対応する要素= 1 + 1 + 2 = 4。
理解を深めるには、以下の画像を参照してください。
最後に、np.bincount()関数については以上です。
リンク: https://appdividend.com/2020/05/07/numpy-bincount-example-np-bincount-in-python/
1637936761
numpy where()メソッドを使用すると、条件を満たすNumpy配列ndarrayの要素を置き換えたり、指定された処理を実行したりできます。このチュートリアルでは、numpyをインストールする必要があります。numpyのバージョンを確認することもできます。
np.where()メソッドは、条件に応じてxまたはyから選択された要素を返します。条件に基づいて要素を選択する場合は、np where()関数を使用します。where()関数は、引数として条件式を取り、新しいnumpy配列を返します。
numpy.where(condition[, x, y])
condition:ブール値のNumpy配列を返す条件式。
x、y: 配列(オプション、つまり、両方が渡されるか渡されないかのいずれか)
これは、条件が配列内の一部の要素に対してTrueを返す場合、新しい配列はxから項目を選択するということです。
それ以外の場合、Falseの場合、yからのアイテムが取得されます。
これにより、最終的な出力配列は、条件= Trueの場合は常にxからのアイテム、条件= Falseの場合は常にyからのアイテムを含む配列になります。
ここで注意すべきことの1つは、xとyはオプションですが、xを指定する場合は、yも指定する必要があることに注意してください。この場合、出力配列の形状は入力配列と同じである必要があるため、これを行う必要があります。
where()メソッドは、条件に基づいてフィルタリングした後、新しいnumpy配列を返します。これはブール値のnumpyのような配列です。
import numpy as np
data = np.where([True, False, True], [11, 21, 46], [19, 29, 18])
print(data)
[11 29 46]
Numpy.where()はbool配列を反復処理し、Trueごとに対応する要素配列xを生成し、Falseごとに配列yから対応する要素を生成します。したがって、条件がTrueの場合はxからアイテムの配列を返し、それ以外の場合はyからの要素を返します。
条件は、 numpyのようなブール配列である配列([真の、真の、真]])の値をとることができます。(デフォルトでは、NumPyは数値のみをサポートしますが、ブール値にキャストすることもできます)。
別の例を見てみましょう。条件がarray([[True、True、False]])で、配列がa = ndarray([[1、2、3]])の場合、配列(a [ :、condition])、配列ndarray([[1 2]])を取得します。
私たちは、使用されます()np.random.randn 2次元配列を生成する機能を、我々は唯一の出力は、正の要素になります。コードを参照してください。
import numpy as np
# Random initialization of (2D array)
arr = np.random.randn(2, 3)
print(arr)
# result will be all elements of a whenever the condition holds true (i.e only positive elements)
# Otherwise, set it as 0
result = np.where(arr > 0, arr, 0)
print(result)
[[-1.49929393 0.68739761 -0.59852165]
[ 0.59212319 1.81549763 -0.32777714]]
[[0. 0.68739761 0. ]
[0.59212319 1.81549763 0. ]]
出力から、これらの負の値の要素が削除され、代わりに0が負の値に置き換えられていることがわかります。
各条件式が()および&または|で囲まれている場合 が使用される場合、処理は複数の条件に適用されます。
import numpy as np
# Random initialization of a (2D array)
arr = np.random.randn(2, 3)
print(arr)
result = np.where((arr > 0.1) & (arr < 1) | (arr == 0.5), -1, 19)
print(result)
[[-0.51877986 2.29435425 0.76549418]
[-0.94666634 1.74349695 -0.82869105]]
[[19 19 -1]
[19 19 19]]
出力から、and演算子およびor演算子を使用して3つの条件を適用したことがわかります。配列要素の値が0.1〜0.99または0.5の場合、-1を返します。それ以外の場合は19を返します。
複数の条件の場合でも、ブール値ndarrayを取得するためにnp.where()を使用する必要はありません。
元のndarrayの代わりに、要素が条件を満たす場合に要素に対して実行する操作を指定することもできます。
import numpy as np
# Random initialization of a (2D array)
arr = np.random.randn(2, 3)
print(arr)
result = np.where(arr < 1, arr * 10, 19)
print('\n')
print('The array after performing the operation')
print(result)
[[ 0.4934594 -0.43502907 -0.01968412]
[-0.52953907 0.41415299 -0.29620816]]
The array after performing the operation
[[ 4.93459402 -4.35029075 -0.19684123]
[-5.2953907 4.14152986 -2.96208157]]
アイテムが10未満の場合は、すべての要素に10が乗算されることがわかります。それ以外の場合は、その場所に19が返されます。
すべての条件、x、およびy配列を提供すると、numpyはそれらを一緒にブロードキャストします。
次のコードを参照してください。
import numpy as np
arr = np.arange(6).reshape(2, 3)
brr = np.arange(3).reshape(1, 3)
print(arr)
print(brr)
# Broadcasts (arr < 2, arr, and brr * 5)
# of shape (2, 3), (2, 3) and (1, 3)
c = np.where(arr < 2, arr, brr * 5)
print(c)
[[0 1 2]
[3 4 5]]
[[0 1 2]]
After broadcasting:
[[ 0 1 10]
[ 0 5 10]]
最後に、Numpy where()関数の例は終わりです。
リンク: https://appdividend.com/2020/06/04/numpy-where-example-np-where-function-in-python/