Scipy-basic-functionality

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

SciPy-基本機能

デフォルトでは、すべてのNumPy関数はSciPy名前空間を介して利用できます。 SciPyをインポートするときに、NumPy関数を明示的にインポートする必要はありません。 NumPyの主なオブジェクトは、同種の多次元配列です。 これは、正の整数のタプルによってインデックスが付けられた、すべて同じタイプの要素(通常は数字)のテーブルです。 NumPyでは、ディメンションは軸と呼ばれます。 *軸*の数は*ランク*と呼ばれます。

ここで、NumPyのベクトルと行列の基本機能を修正しましょう。 SciPyはNumPyアレイの上に構築されるため、NumPyの基本を理解する必要があります。 線形代数のほとんどの部分は行列のみを扱うため。

NumPyベクトル

ベクターは複数の方法で作成できます。 それらのいくつかを以下に説明します。

Pythonの配列のようなオブジェクトをNumPyに変換する

次の例を考えてみましょう。

import numpy as np
list = [1,2,3,4]
arr = np.array(list)
print arr

上記のプログラムの出力は次のようになります。

[1 2 3 4]

本質的なNumPy配列の作成

NumPyには、ゼロから配列を作成するための組み込み関数があります。 これらの機能の一部を以下に説明します。

zeros()を使用する

zeros(shape)関数は、指定された形状の0値で満たされた配列を作成します。 デフォルトのdtypeはfloat64です。 次の例を考えてみましょう。

import numpy as np
print np.zeros((2, 3))

上記のプログラムの出力は次のようになります。

array([[Using ones()

The ones(shape) function will create an array filled with 1 values. It is identical to zeros in all the other respects. Let us consider the following example.

[source,prettyprint,notranslate]

numpyをnp print np.ones((2、3))としてインポート

The output of the above program will be as follows.

[source,result,notranslate]

array([[arange()を使用

arange()関数は、定期的に値を増やして配列を作成します。 次の例を考えてみましょう。

import numpy as np
print np.arange(7)

上記のプログラムは、次の出力を生成します。

array([0, 1, 2, 3, 4, 5, 6])

値のデータ型の定義

次の例を考えてみましょう。

import numpy as np
arr = np.arange(2, 10, dtype = np.float)
print arr
print "Array Data Type :",arr.dtype

上記のプログラムは、次の出力を生成します。

[ 2. 3. 4. 5. 6. 7. 8. 9.]
Array Data Type : float64

linspace()を使用する

linspace()関数は、指定された要素の数で配列を作成し、指定された開始値と終了値の間で等間隔になります。 次の例を考えてみましょう。

import numpy as np
print np.linspace(1., 4., 6)

上記のプログラムは、次の出力を生成します。

array([ 1. , 1.6, 2.2, 2.8, 3.4, 4. ])

マトリックス

行列は、操作を通じてその2次元の性質を保持する特殊な2次元配列です。 (行列の乗算)や*(行列のべき乗)などの特定の特別な演算子があります。 次の例を考えてみましょう。

import numpy as np
print np.matrix('1 2; 3 4')

上記のプログラムは、次の出力を生成します。

matrix([[Conjugate Transpose of Matrix

This feature returns the (complex) conjugate transpose of *self*. Let us consider the following example.

[source,prettyprint,notranslate]

numpyをnp mat = np.matrix( '1 2; 3 4')print mat.Hとしてインポート

The above program will generate the following output.

[source,result,notranslate]

行列([[行列の転置

この機能は、自己の転置を返します。 次の例を考えてみましょう。

import numpy as np
mat = np.matrix('1 2; 3 4')
mat.T

上記のプログラムは、次の出力を生成します。

matrix([[When we transpose a matrix, we make a new matrix whose rows are the columns of the original. A conjugate transposition, on the other hand, interchanges the row and the column index for each matrix element. The inverse of a matrix is a matrix that, if multiplied with the original matrix, results in an identity matrix.