NumPy save() メソッド |配列をファイルに保存
NumPy 保存() 方法が使用されます 入力配列を保存する バイナリファイル内で「 npy拡張子」 (.npy)。
例:
Python3
import> numpy as np> a> => np.arange(> 5> )> np.save(> 'array_file'> , a)> |
構文
構文: numpy.save(ファイル、arr、allow_pickle=True、fix_imports=True)
パラメーター:
- ファイル: データが保存されるファイルまたはファイル名。ファイルが文字列またはパスの場合、拡張子 .npy がファイル名に付加されていない場合は、ファイル名に拡張子が付加されます。ファイルがファイル オブジェクトの場合、ファイル名は変更されません。
- 許可ピクル: Python ピクルスを使用してオブジェクト配列を保存できるようにします。 pickle を許可しない理由には、セキュリティ (pickle データをロードすると任意のコードが実行される可能性がある) と移植性 (pickle されたオブジェクトは異なる Python インストールにロードできない可能性がある) が含まれます。デフォルト: True
- fix_imports : Python 3 のオブジェクト配列内のオブジェクトを Python 2 と互換性のある方法で強制的にピクルする場合にのみ役立ちます。
- 到着: 保存する配列データ。
戻り値: 入力配列を拡張子「.npy」が付いたディスク ファイルに保存します。
例
これらの Python コードの numpy.save() メソッドの仕組みを理解して、NumPy ライブラリの save() メソッドの使用方法を知りましょう。
numpy.save() 関数を使用するには、関数にファイル名と配列を渡すだけです。
例1
Python3
# Python program explaining> # save() function> > import> numpy as geek> > a> => geek.arange(> 5> )> > # a is printed.> print> (> 'a is:'> )> print> (a)> > # the array is saved in the file geekfile.npy> geek.save(> 'geekfile'> , a)> > print> (> 'the array is saved in the file geekfile.npy'> )> |
出力:
a is: [0 1 2 3 4] the array is saved in the file geekfile.npy
例 2
Python3
# Python program explaining> # save() function> > import> numpy as geek> > # the array is loaded into b> b> => geek.load(> 'geekfile.npy'> )> > print> (> 'b is:'> )> print> (b)> > # b is printed from geekfile.npy> print> (> 'b is printed from geekfile.npy'> )> |
出力:
b is: [0 1 2 3 4] b is printed from geekfile.npy