Numpy-array-creation-routines

提供:Dev Guides
移動先:案内検索

NumPy-配列作成ルーチン

新しい ndarray オブジェクトは、次の配列作成ルーチンのいずれか、または低レベルのndarrayコンストラクターを使用して構築できます。

numpy.empty

指定された形状とdtypeの初期化されていない配列を作成します。 それは次のコンストラクタを使用します-

numpy.empty(shape, dtype = float, order = 'C')

コンストラクターは次のパラメーターを取ります。

Sr.No. Parameter & Description
1

Shape

intまたはintのタプルの空配列の形状

2

Dtype

必要な出力データ型。 オプション

3

Order

Cスタイルの行優先配列の場合は 'C'、FORTRANスタイルの列優先配列の場合は 'F'

次のコードは、空の配列の例を示しています。

import numpy as np
x = np.empty([3,2], dtype = int)
print x

出力は次のとおりです-

[[Note* − The elements in an array show random values as they are not initialized.

=== numpy.zeros

Returns a new array of specified size, filled with zeros.

[source,result,notranslate]

numpy.zeros(shape、dtype = float、order = 'C')

The constructor takes the following parameters.

[width="100%",cols="50%,50%",options="header",]
|===
|Sr.No. |Parameter & Description
|1 a|
*Shape*

Shape of an empty array in int or sequence of int

|2 a|
*Dtype*

Desired output data type. Optional

|3 a|
*Order*

'C' for C-style row-major array, 'F' for FORTRAN style column-major array

|===

==== Example 1

[source,prettyprint,notranslate]

#5つのゼロの配列。 デフォルトのdtypeはfloat import numpy as np x = np.zeros(5)print x

The output is as follows −

[source,result,notranslate]

[ 0. 0. 0. 0. 0.]

==== Example 2

[source,prettyprint,notranslate]

numpyをnp x = np.zeros((5、)、dtype = np.int)としてインポートprint x

Now, the output would be as follows −

[source,result,notranslate]
==== Example 3

[source,prettyprint,notranslate]

#カスタムタイプimport numpy as np x = np.zeros((2,2)、dtype = [( 'x'、 'i4')、( 'y'、 'i4')])print x

It should produce the following output −

[source,result,notranslate]

[[numpy.ones

1で満たされた、指定されたサイズとタイプの新しい配列を返します。

numpy.ones(shape, dtype = None, order = 'C')

コンストラクターは次のパラメーターを取ります。

Sr.No. Parameter & Description
1

Shape

intまたはintのタプルの空配列の形状

2

Dtype

必要な出力データ型。 オプション

3

Order

Cスタイルの行優先配列の場合は 'C'、FORTRANスタイルの列優先配列の場合は 'F'

例1

# array of five ones. Default dtype is float
import numpy as np
x = np.ones(5)
print x

出力は次のとおりです-

[ 1.  1.  1.  1.  1.]

例2

import numpy as np
x = np.ones([2,2], dtype = int)
print x

今、出力は次のようになります-

[[