numpy.load()>
Python では、単純なテキスト ファイルを高速に読み取ることを目的として、テキスト ファイルからデータをロードするために使用されます。
テキスト ファイルの各行には同じ数の値が含まれている必要があることに注意してください。
構文: numpy.loadtxt(fname, dtype=’float’, comments=’#’, delimiter=None, Converters=None, Skiprows=0, usecols=None, unpack=False, ndmin=0)
パラメーター:
fname : 読み取るファイル、ファイル名、またはジェネレーター。ファイル名の拡張子が .gz または .bz2 の場合、ファイルは最初に解凍されます。ジェネレーターは Python 3k のバイト文字列を返す必要があることに注意してください。
dtype : 結果の配列のデータ型。デフォルト: 浮動小数点。これが構造化データ型の場合、結果の配列は 1 次元になり、各行は配列の要素として解釈されます。
区切り文字: 値を区切るために使用される文字列。デフォルトでは、これは任意の空白です。
コンバーター: 列番号をその列を float に変換する関数にマッピングするディクショナリ。たとえば、列 0 が日付文字列の場合: Converters = {0: datestr2num}。デフォルト: なし。
スキップロウ : 最初のスキップロウ行をスキップします。デフォルト: 0。戻り値: ndarray
スイッチケースJava
コード #1:
# Python program explaining> # loadtxt() function> import> numpy as geek> > # StringIO behaves like a file object> from> io> import> StringIO> > c> => StringIO(> '0 1 2
3 4 5'> )> d> => geek.loadtxt(c)> > print> (d)> |
>
>
出力:
[[ 0. 1. 2.] [ 3. 4. 5.]]>
コード #2:
ジェスト
# Python program explaining> # loadtxt() function> import> numpy as geek> > # StringIO behaves like a file object> from> io> import> StringIO> > c> => StringIO(> '1, 2, 3
4, 5, 6'> )> x, y, z> => geek.loadtxt(c, delimiter> => ', '> , usecols> => (> 0> ,> 1> ,> 2> ),> > unpack> => True> )> > print> (> 'x is: '> , x)> print> (> 'y is: '> , y)> print> (> 'z is: '> , z)> |
>
Pythonの継承プログラム
>
出力:
x is: [ 1. 4.] y is: [ 2. 5.] z is: [ 3. 6.]>
コード #3:
文字列が空です
# Python program explaining> # loadtxt() function> import> numpy as geek> > # StringIO behaves like a file object> from> io> import> StringIO> > d> => StringIO(> 'M 21 72
F 35 58'> )> e> => geek.loadtxt(d, dtype> => {> 'names'> : (> 'gender'> ,> 'age'> ,> 'weight'> ),> > 'formats'> : (> 'S1'> ,> 'i4'> ,> 'f4'> )})> > print> (e)> |
>
>
出力:
[(b'M', 21, 72.) (b'F', 35, 58.)]>