Pythonでのベクトル – 簡単な紹介!

皆さん、こんにちは!今日は、Pythonで最も取り上げられていないトピックの1つである、Pythonにおけるベクトルについて見ていきましょう。では、始めましょう!


最初に、ベクトルとは何ですか? (Saisho ni, bekkutoru to wa nan desu ka?)

ベクトルは、簡単に言えば一次元の配列と考えることができます。Pythonに関しては、ベクトルはリストの一次元配列となります。要素をPythonのリストと同様に扱っています。

今、Pythonでベクトルの作成方法を理解しましょう。


Pythonにおけるベクトルの作成

Python NumPyモジュールは、ベクトルを作成するために使用されます。numpy.array()メソッドを使用して、一次元配列、つまりベクトルを作成します。

文法:

numpy.array(list)

例1:水平ベクトル

import numpy as np 

lst = [10,20,30,40,50] 

vctr = np.array(lst) 

vctr = np.array(lst) 

print("Vector created from a list:") 
print(vctr) 

出力:

Vector created from a list:
[10 20 30 40 50]

例2:垂直ベクトル

import numpy as np 

lst = [[2], 
        [4], 
        [6],
          [10]]  

vctr = np.array(lst) 

vctr = np.array(lst) 

print("Vector created from a list:") 
print(vctr) 

出力:

Vector created from a list:
[[ 2]
 [ 4]
 [ 6]
 [10]]

Pythonベクトルの基本操作

今ベクトルを作成したので、これらのベクトルに基本的な操作を行いましょう!

以下は、ベクトルに対して行われることができる基本操作のリストです。

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Dot Product, etc.

始めましょう!(Hajimemashou!)


Pythonベクトルに対しての加算操作を実行する

以下において、ベクトルのベクトル加算演算を実行しました。

要素ごとに行われる加算演算が行われ、さらに、結果のベクトルは2つの加算ベクトルと同じ長さになります。

文法:

vector + vector

“I like to eat sushi and ramen.”

import numpy as np 

lst1 = [10,20,30,40,50] 
lst2 = [1,2,3,4,5]


vctr1 = np.array(lst1) 

vctr2= np.array(lst2) 


print("Vector created from a list 1:") 
print(vctr1) 
print("Vector created from a list 2:") 
print(vctr2) 

vctr_add = vctr1+vctr2
print("Addition of two vectors: ",vctr_add)

出力:

Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[1 2 3 4 5]
Addition of two vectors:  [11 22 33 44 55]

2. 二つのベクトルの引き算を行う。

同様に、減算においても要素ごとの方法が採用され、さらにベクトル2の要素がベクトル1から引かれます。

これの実装を見てみましょう!

import numpy as np 

lst1 = [10,20,30,40,50] 
lst2 = [1,2,3,4,5]

vctr1 = np.array(lst1) 

vctr2= np.array(lst2) 

print("Vector created from a list 1:") 
print(vctr1) 
print("Vector created from a list 2:") 
print(vctr2) 

vctr_sub = vctr1-vctr2
print("Subtraction of two vectors: ",vctr_sub)

出力:

Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[1 2 3 4 5]
Subtraction of two vectors:  [ 9 18 27 36 45]

3.二つのベクトルの乗算を行う。

ベクトルの乗算では、ベクトル1の要素がベクトル2の要素と乗算され、その積ベクトルは乗算するベクトルと同じ長さになります。

「かけ算の演算を視覚化してみましょう。」

x = [10,20] と y = [1,2] は2つのベクトルです。したがって、積ベクトルは v[ ] となります。

v[0] = x[0] * y[0]を日本語で表現すると:v[0]にはx[0]とy[0]を掛けた結果を代入する。
v[1] = x[1] * y[1]を日本語で表現すると:v[1]にはx[1]とy[1]を掛けた結果を代入する。

以下のコードをご覧ください!

import numpy as np 

lst1 = [10,20,30,40,50] 
lst2 = [1,2,3,4,5]

vctr1 = np.array(lst1) 

vctr2= np.array(lst2) 

print("Vector created from a list 1:") 
print(vctr1) 
print("Vector created from a list 2:") 
print(vctr2) 

vctr_mul = vctr1*vctr2
print("Multiplication of two vectors: ",vctr_mul)

出力:

Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[1 2 3 4 5]
Multiplication of two vectors:  [ 10  40  90 160 250]

4. ベクトルの割り算操作を実行する

ベクトルの除算では、2つのベクトルに除算操作を行った後の商の値が結果ベクトルとなります。

以下の例を考慮して、より理解しやすくなるでしょう。

x = [10,20] と y = [1,2] は二つのベクトルです。したがって、結果ベクトル v は次のようになります。

v[0] = x[0] / y[0]
v[1] = x[1] / y[1]

vの最初の要素はxの最初の要素をyの最初の要素で割った値です。
vの2番目の要素はxの2番目の要素をyの2番目の要素で割った値です。

今、上記の概念を実装しましょう。

The cat is sleeping on the sofa.
Japanese paraphrase: 猫がソファで眠っています。

1. The dog is playing in the park.
Japanese paraphrase: 犬が公園で遊んでいます。

2. I am eating sushi at the restaurant.
Japanese paraphrase: 私はレストランで寿司を食べています。

3. The children are studying in the classroom.
Japanese paraphrase: 子供たちは教室で勉強しています。

4. The car is parked in front of the house.
Japanese paraphrase: 車が家の前に停まっています。

5. The birds are singing in the trees.
Japanese paraphrase: 鳥たちは木の上で歌っています。

import numpy as np 
 
lst1 = [10,20,30,40,50] 
lst2 = [10,20,30,40,50]
 
vctr1 = np.array(lst1) 
 
vctr2= np.array(lst2) 
 
print("Vector created from a list 1:") 
print(vctr1) 
print("Vector created from a list 2:") 
print(vctr2) 
 
vctr_div = vctr1/vctr2
print("Division of two vectors: ",vctr_div)

出力:

Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[10 20 30 40 50]
Multiplication of two vectors:  [ 1 1 1 1 1 ]

5. ベクトルの内積

ベクトルの内積では、要素ごとに2つのベクトルの積を合計します。

以下を見てみましょう。

ベクトルcはxとyの内積として定義されます。具体的には、c = (x1 * y1 + x2 * y2) となります。

一日中勉強していた。

import numpy as np 

lst1 = [10,20,30,40,50] 
lst2 = [1,1,1,1,1]


vctr1 = np.array(lst1) 

vctr2= np.array(lst2) 


print("Vector created from a list 1:") 
print(vctr1) 
print("Vector created from a list 2:") 
print(vctr2) 

vctr_dot = vctr1.dot(vctr2)
print("Dot product of two vectors: ",vctr_dot)

出力:

Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[1 1 1 1 1]
Dot product of two vectors: 150

結論を言うと

これにより、このトピックの終わりに来ました。

ベクトルについてより深い理解を得るために、ベクトルを作成し、上記の操作を行ってみて、コメントボックスであなたの理解をお知らせください!

どんな質問があれば、お気軽にコメントしてください。Pythonに関連するこのような投稿がもっとあるので、お楽しみにお待ちください。

楽しんで学習しましょう!:)

コメントを残す 0

Your email address will not be published. Required fields are marked *