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

More than 5 years have passed since last update.

numpy.dot(a, b, out = )の使い方(自分用)

Last updated at Posted at 2019-06-19

解決法だけ見たい人

解決法
import numpy as np

a = np.arange(4).reshape((2, 2))
b = np.arange(5,9).reshape((2, 2))
print(a)
print(b)

c = np.zeros((2, 2), dtype = int)
np.dot(a, b, out = c)
print(c)
#出力結果
[[0 1]
 [2 3]]

[[5 6]
 [7 8]]

[[ 7  8]
 [31 36]]

経緯

コード
import numpy as np

a = np.arange(4).reshape((2, 2))
b = np.arange(5,9).reshape((2, 2))

c = zeros((2, 2))
np.dot(a, b, out = c)

dotを使ったコードを実行したところ,このようなエラーが出たので,臨時的な解決法をメモします.

実行エラー
output array is not acceptable (must have the right type, nr dimensions, and be a C-Array)

出力先の二次元配列が正しくない型,もしくは行と列の数が間違っている,または,C-Array(numpy内の配列?)ではない.とのこと.

行と列の数はあってるし,numpy.array使っているので,後ろ二つは違う.となると,一番前の型が正しくないと考えた.

np.zeros()は,デフォルトではfloat型になっているようなので,int型に変換するだけでいいらしい.
実際にやってみると,解決した.

コード
import numpy as np

a = np.arange(4).reshape((2, 2))
b = np.arange(5,9).reshape((2, 2))

c = np.zeros((2, 2), dtype = int)
c = np.dot(a, b, out = c)
print(c)
#出力結果
[[ 7  8]
 [31 36]]
# 補足 これくらい簡単な処理ならば,outいらないんですけどね. 使い方のメモです.
これでもOK
import numpy as np

a = np.arange(4).reshape((2, 2))
b = np.arange(5,9).reshape((2, 2))

c = np.dot(a, b)

参考:
(https://github.com/numpy/numpy/issues/7124)
(https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html)

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