logo

NumPy 配列の基本

ナムピー 数値パイソンの略です。配列を操作するために使用される Python ライブラリです。 Python では配列にリストを使用しますが、処理が遅くなります。 NumPy 配列は強力な N 次元配列オブジェクトであり、線形代数、フーリエ変換、乱数機能で使用されます。従来の Python リストよりもはるかに高速に配列オブジェクトを提供します。

配列の種類:

  1. 1次元配列
  2. 多次元配列

1 次元配列:

1 次元配列は線形配列の一種です。



1次元配列

例:

Python3
# importing numpy module import numpy as np # creating list list = [1, 2, 3, 4] # creating numpy array sample_array = np.array(list) print('List in python : ', list) print('Numpy Array in python :', sample_array)>




intを文字列Javaに変換する方法

出力:

List in python : [1, 2, 3, 4] Numpy Array in python : [1 2 3 4]>

リストと配列のデータ型を確認します。

Python3
print(type(list_1)) print(type(sample_array))>

出力:



>

多次元配列:

多次元配列のデータは表形式で保存されます。

二次元配列

例:

Python3
# importing numpy module import numpy as np # creating list  list_1 = [1, 2, 3, 4] list_2 = [5, 6, 7, 8] list_3 = [9, 10, 11, 12] # creating numpy array sample_array = np.array([list_1, list_2, list_3]) print('Numpy multi dimensional array in python
', sample_array)>

出力:

Numpy multi dimensional array in python [[ 1 2 3 4]  [ 5 6 7 8]  [ 9 10 11 12]]>

注記: 使用 [ ] 多次元の numpy.array() 内の演算子

配列の構造:

1. 軸: 配列の軸は、配列へのインデックス付けの順序を表します。

軸 0 = 1 次元

軸 1 = 2 次元

軸 2 = 3 次元

2.形状: 各軸に沿った要素の数。タプルからのものです。

例:

Python3
# importing numpy module import numpy as np # creating list  list_1 = [1, 2, 3, 4] list_2 = [5, 6, 7, 8] list_3 = [9, 10, 11, 12] # creating numpy array sample_array = np.array([list_1, list_2, list_3]) print('Numpy array :') print(sample_array) # print shape of the array print('Shape of the array :', sample_array.shape)>

出力:

Numpy array :  [[ 1 2 3 4]  [ 5 6 7 8]  [ 9 10 11 12]] Shape of the array : (3, 4)>

例:

Python3
import numpy as np sample_array = np.array([[0, 4, 2], [3, 4, 5], [23, 4, 5], [2, 34, 5], [5, 6, 7]]) print('shape of the array :', sample_array.shape)>

出力:

shape of the array : (5, 3)>

3.ランク: 配列のランクは、単に配列が持つ軸 (または次元) の数です。

Cの配列の文字列

1 次元配列のランクは 1 です。

ランク1

2 次元配列のランクは 2 です。

ランク2

4. データ型オブジェクト (dtype): データ型オブジェクト (dtype) は、次のインスタンスです。 numpy.dtype クラス。これは、配列項目に対応するメモリの固定サイズ ブロック内のバイトがどのように解釈されるべきかを説明します。

例:

Python3
# Import module import numpy as np # Creating the array  sample_array_1 = np.array([[0, 4, 2]]) sample_array_2 = np.array([0.2, 0.4, 2.4]) # display data type print('Data type of the array 1 :', sample_array_1.dtype) print('Data type of array 2 :', sample_array_2.dtype)>

出力:

Data type of the array 1 : int32 Data type of array 2 : float64>

Numpy Array を作成する別の方法:

1. numpy.array() : Numpy の Numpy 配列オブジェクトは ndarray と呼ばれます。次を使用して ndarray を作成できます numpy.array() 関数。

構文: numpy.array(パラメータ)

例:

Python3
# import module import numpy as np #creating a array arr = np.array([3,4,5,5]) print('Array :',arr)>

出力:

Array : [3 4 5 5]>

2. numpy.fromiter() : fromiter() 関数は、反復可能なオブジェクトから新しい 1 次元配列を作成します。

構文: numpy.fromiter(反復可能、dtype、count=-1)

例 1:

Python3
#Import numpy module import numpy as np # iterable iterable = (a*a for a in range(8)) arr = np.fromiter(iterable, float) print('fromiter() array :',arr)>

出力:

fromiter() 配列 : [ 0. 1. 4. 9. 16. 25. 36. 49.]

例 2:

Python3
import numpy as np var = 'Geekforgeeks' arr = np.fromiter(var, dtype = 'U2') print('fromiter() array :', arr)>

出力:

fromiter() 配列 : [‘G’ ‘e’ ‘e’ ‘k’ ‘f’ ‘o’ ‘r’ ‘g’ ‘e’ ‘e’ ‘k’ ‘s’]

3. numpy.arange() : これは、指定された間隔内で等間隔の値を返す組み込みの NumPy 関数です。

構文: numpy.arange([スタート, ]ストップ, [ステップ, ]dtype=None)

例:

Python3
import numpy as np np.arange(1, 20 , 2, dtype = np.float32)>

出力:

array([ 1., 3., 5., 7., 9., 11., 13., 15., 17., 19.], dtype=float32)

4. numpy.linspace() : この関数は、2 つの制限の間で指定された値を超える等間隔の数値を返します。

構文: numpy.linspace(start、stop、num=50、endpoint=True、retstep=False、dtype=None、axis=0)

例 1:

Python3
import numpy as np np.linspace(3.5, 10, 3)>

出力:

array([ 3.5 , 6.75, 10. ])>

例 2:

Python3
import numpy as np np.linspace(3.5, 10, 3, dtype = np.int32)>

出力:

array([ 3, 6, 10])>

5. numpy.empty() : この関数は、値を初期化せずに、指定された形状と型の新しい配列を作成します。

構文: numpy.empty(shape, dtype=float, order=’C’)

ユーザー名の例

例:

Python3
import numpy as np np.empty([4, 3], dtype = np.int32, order = 'f')>

出力:

array([[ 1, 5, 9],  [ 2, 6, 10],  [ 3, 7, 11],  [ 4, 8, 12]])>

6. numpy.ones(): この関数は、ones(1) で満たされた、指定された形状と型の新しい配列を取得するために使用されます。

構文: numpy.ones(shape, dtype=None, order=’C’)

例:

Python3
import numpy as np np.ones([4, 3], dtype = np.int32, order = 'f')>

出力:

array([[1, 1, 1],  [1, 1, 1],  [1, 1, 1],  [1, 1, 1]])>

7。 numpy.zeros() : この関数は、ゼロ (0) で埋められた、指定された形状と型の新しい配列を取得するために使用されます。

構文: numpy.ones(形状、dtype=None)

例:

Python3
import numpy as np np.zeros([4, 3], dtype = np.int32, order = 'f')>

出力:

array([[0, 0, 0],  [0, 0, 0],  [0, 0, 0],  [0, 0, 0]])>