LoginSignup
0
1

More than 3 years have passed since last update.

【Python】numpyでの計算方法

Posted at

numpyでの計算

numpyを使用して0~10の配列を作ります。

import numpy as np

arr = np.arange(11)

arr
実行結果.
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

sqrtメソッドを使用することで各数字の平方根の計算できます。

np.sqrt(arr)
実行結果.
array([ 0.48273727, -1.28739284,  1.52422575, -1.73666091, -0.25126809,
       -0.41952278, -0.75042054, -0.64585434, -0.86014472, -0.44542315])

random.randn()で正規分布(平均=0,分散=1)の乱数を計算することができます。

A = np.random.randn(10)
実行結果.
array([-0.94897439, -0.43075947,  0.088691  ,  0.37859721, -0.2141078 ,
        1.30327378,  0.41654781, -0.42907224, -1.61139916, -0.651694  ])

addメソッドを使用することで足し算をすることができます。

B = np.random.randn(10)

B

array([ 0.05623043,  1.97843447, -0.02581691,  0.70663108,  0.51213852,
       -0.70386143,  1.50729471, -0.00577632, -1.08456477, -0.38103167])

np.add(A,B)
実行結果.
array([ 0.5389677 ,  0.69104164,  1.49840884, -1.03002983,  0.26087043,
       -1.1233842 ,  0.75687417, -0.65163066, -1.94470949, -0.82645482])

subtractメソッドで引き算ができます。

np.subtract(A,B)
実行結果.
array([ 0.42650684, -3.26582731,  1.55004266, -2.44329199, -0.76340661,
        0.28433865, -2.25771524, -0.64007803,  0.22442006, -0.06439147])

multiplyメソッドで掛け算ができます。

np.multiply(A,B)
実行結果.
array([ 0.02714452, -2.54702237, -0.0393508 , -1.22717858, -0.12868407,
        0.2952859 , -1.1311049 ,  0.00373066,  0.93288266,  0.16972033])

divideメソッドで割り算ができます。

np.divide(A,A)
実行結果.
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1