Python numpy.empty()で空のNumpy配列(ndarray)を作成するサンプル

環境
Windows 11 Pro 64bit
Python 3.11

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

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import numpy as np
numbers = np.empty(shape=(3,3))
print(type(numbers))
print(numbers)
import numpy as np numbers = np.empty(shape=(3,3)) print(type(numbers)) print(numbers)
import numpy as np

numbers = np.empty(shape=(3,3))

print(type(numbers)) 
print(numbers)

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<class 'numpy.ndarray'>
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
<class 'numpy.ndarray'> [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]
<class 'numpy.ndarray'>
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

 

IT

Posted by arkgame