Numpyのユニバーサル関数(ufunc)の使い方
配列の演算
データの生成
Loading gist https://gist.github.com/a9fd27016cc0745e0954ab23f
47e83b2.json...加算
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
#加算
x+10
#array([20., 21., 22., 23., 24., 25.])
np.add(x,10)
#array([20., 21., 22., 23., 24., 25.])
+またはnp.add()で加算ができる。
減算
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
#減算
x-10
#array([0., 1., 2., 3., 4., 5.])
np.subtract(x,10)
#array([0., 1., 2., 3., 4., 5.])
-またはnp.subtract()で減算ができる。
乗算
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
#乗算
x*10
#array([100., 110., 120., 130., 140., 150.])
np.multiply(x,10)
#array([100., 110., 120., 130., 140., 150.])
*またはnp.multiply()で乗算ができる。
除算
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
#除算
x/2
#array([5. , 5.5, 6. , 6.5, 7. , 7.5])
np.divide(x,2)
#array([5. , 5.5, 6. , 6.5, 7. , 7.5])
/またはnp.divide()で除算ができる。
余り切り捨て
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
#除算 あまり切り捨て
x//2
#array([5., 5., 6., 6., 7., 7.])
np.floor_divide(x,2)
#array([5., 5., 6., 6., 7., 7.])
//またはnp.floor_divide()であまりを切り捨てた除算ができる。
余り切り上げ
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
#除算 あまり切り上げ
np.ceil(x/2)
#array([5., 6., 6., 7., 7., 8.])
np.ceilで小数点以下を切り上げることができる。
四捨五入
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
#四捨五入
np.rint(x/2)
#array([5., 6., 6., 6., 7., 8.])
np.rintで小数点以下を四捨五入できる。
余り
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
#あまり
np.mod(x,5)
#array([0., 1., 2., 3., 4., 0.])
np.modで除算したときのあまりを得ることができる。
2乗
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
#2乗
x**2
#array([100., 121., 144., 169., 196., 225.])
np.square(x)
#array([100., 121., 144., 169., 196., 225.])
**2またはnp.squareで各要素を2乗した配列を得ることができる。
絶対値
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
#絶対値
np.absolute(-x)
#array([10., 11., 12., 13., 14., 15.])
np.absolute()で絶対値を求められる。
コードをダウンロード(.pyファイル) コードをダウンロード(.ipynbファイル)参考
Universal functions (ufunc) — NumPy v2.2 Manual
コメント