Python numpy.zeros()を使って空のNumpy配列を作成するサンプル

環境
Windows 11 Pro 64bit
Python 3.11

構文
空array = numpy.zeros(shape=(n,l))
numpy.zeros()の引数「shape」にnumpy配列の形状をタプルで指定します。
numpy.zeros()は、引数「shape」に指定した形状のnumpy配列を生成します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import numpy as np
ns = np.zeros(shape=(2,2))
print(type(ns)) #<class 'numpy.ndarray'>
print(ns)
import numpy as np ns = np.zeros(shape=(2,2)) print(type(ns)) #<class 'numpy.ndarray'> print(ns)
import numpy as np

ns = np.zeros(shape=(2,2))

print(type(ns)) #<class 'numpy.ndarray'>
print(ns)

実行結果
<class 'numpy.ndarray’>
[[0. 0.]
[0. 0.]]

Python

Posted by arkgame