Last active
September 22, 2016 10:17
-
-
Save kyoro1/f699c1c62ce3ebe68184be474a71d8a4 to your computer and use it in GitHub Desktop.
numpyのwhere ref: http://qiita.com/kyoro1/items/12724eaeb9462fb660c1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> import numpy as np | |
>> np.where(A>5, 1, -1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> import numpy as np | |
### まずは配列を作りますね | |
>> A = np.arange(10,0,-1) | |
### 内容確認 | |
>> A | |
array([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 内側から確認してきましょう | |
>> 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