1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[tips][python] numpy

Last updated at Posted at 2024-10-29

ブロードキャスト機能

配列

tipsです。メモなので日々追記していきます。

xには単一値でも配列でもよく、配列の場合は各要素に対して定めた演算を行い、和をとるとかしてくれるのがブロードキャスト機能。

import numpy as np
def function_2(x):
	return np.sum(x**2)

a = np.array([1, 2, 3])
res = function_2(a)
print("二乗和", res)
二乗和 14
  • エラー

np.array() 関数による生成ではなく次のようにただのリストにすると、numpyがかた推論できずエラーになります。

c = [1, 2, 3]
res = function_2(c)
print("二乗和", res)
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'

参考

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?