Skip to content

Instantly share code, notes, and snippets.

@kyoro1
Last active September 22, 2016 10:17
Show Gist options
  • Save kyoro1/f699c1c62ce3ebe68184be474a71d8a4 to your computer and use it in GitHub Desktop.
Save kyoro1/f699c1c62ce3ebe68184be474a71d8a4 to your computer and use it in GitHub Desktop.
>> import numpy as np
>> np.where(A>5, 1, -1)
>> import numpy as np
### まずは配列を作りますね
>> A = np.arange(10,0,-1)
### 内容確認
>> A
array([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
### 内側から確認してきましょう
>> A>5
[ True True True True True False False False False False]
### -> 条件を満たせばTrue、満たさなければFalseを返すんですね
### whereをかましてみる
>> np.where(A>5)
(array([0, 1, 2, 3, 4]),)
### -> 条件を満たす要素が取り出せましたね。
### さて、お題の内容
>> np.where(A>5, 1, -1)
array([ 1, 1, 1, 1, 1, -1, -1, -1, -1, -1])
### -> 条件を満たせたら2つ目の引数の値"1"を、満たせなかったら3つ目の引数の値"-1"を返すっぽい
### ↑の内容を念のため他の値で確認
>> np.where(A>5,2,0)
array([2, 2, 2, 2, 2, 0, 0, 0, 0, 0])
### -> 合ってたっぽいですね。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment