科学の箱

科学・IT・登山の話題

Python

配列同士の四則演算

投稿日:

ndarray同士で四則演算ができる。

この四則演算自体はnumpyにおけるbroadcastingと呼ばれる機能およびuniversal functionを利用して実現している。

numpyのbroadcastingについてはこちら : Broadcasting

numpyのuniversal functionについてはこちら:Universal functions (ufunc)

四則演算

テスト用の配列を生成する。

import numpy as np
x = np.array([2,4,6]) 
y = np.array([1,3,5])
print(x)
print(y)
#[2 4 6]
[1 3 5]

四則演算を実行してみる。

print(x-y)
# [1 1 1]

print(x+y)
# [ 3  7 11]


print(x*y)
# [2 12 30]

print(x/y]
# [2. 1.33333333 1.2 ]

 

またこれらの四則演算は別の書き方もできる。

print(np.add(x,y))
# [ 3 7 11]

print(np.subtract(x,y))
# [1 1 1]

print(np.multiply(x,y))
# [2 12 30]

print(np.divide(x,y))
# [2.         1.33333333 1.2       ]

 

numpyでは四則演算だけではなく様々な関数が用意されている。詳細についてはこちらを参照 : Available ufuncs

 

メタ情報

inarticle



メタ情報

inarticle



-Python
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

配列を利用した四則演算とuniversal関数

四則演算 import numpy as np arr = np.arange(1,11) arr arr + arr arr * arr arr – 100 arr – arr [/cde] np. …

no image

pythonでTensorFlowを使うまで

TensorFlowが利用できるまでの設定手順をまとめる。 まず基本となるインストラクションはこちらにある。 https://www.tensorflow.org/install/pip このインスト …

no image

automated the boring – day7

https://automatetheboringstuff.com/chapter14/ 14章ではcsvとJSONを取り扱う。フォーマットとしては単純であるのに、なぜexcel,word, pdf …

no image

numpyで配列を抜き出す方法

numpyで配列を抜き出す まず基本となるやり方 arr = np.arange(50).reshape(5,10) arr[1:1,] arr[1:2,]   np.arange(50)で …

no image

RoboBrowserでUser Agentが原因ではねられているとき

RoboBrowserを利用していると通常のブラウザでリクエストした時とは異なりエラーがページが返ってくることが多い。 原因はいくつかあるがまず試したいのはUser-Agentの設定。 RoboBro …

2019年9月
« 8月   10月 »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

side bar top



アーカイブ

カテゴリー