Vectors in Python – A Quick Introduction!

Filed Under: Python Advanced
Python Vectors

Hello, folks! Today, we will be having a look at one of the most unaddressed topics in Python that is, Vectors in Python. So, let us begin!


First, what is a Vector?

A vector in a simple term can be considered as a single-dimensional array. With respect to Python, a vector is a one-dimensional array of lists. It occupies the elements in a similar manner as that of a Python list.

Let us now understand the Creation of a vector in Python.


Creation of a Vector in Python

Python NumPy module is used to create a vector. We use numpy.array() method to create a one-dimensional array i.e. a vector.

Syntax:

numpy.array(list)

Example 1: Horizontal Vector

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) 

Output:

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

Example 2: Vertical Vector

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) 

Output:

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

Basic Operations on a Python Vector

Having created a Vector, now let us perform some basic operations on these Vectors now!

Here is a list of the basic operations that can be performed on a Vector–

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

Let us begin!


1. Performing addition operation on a Python Vector

Below, we have performed Vector addition operation on the vectors.

The addition operation would take place in an element-wise manner i.e. element by element and further the resultant vector would have the same length as of the two additive vectors.

Syntax:

vector + vector

Example:

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)

Output:

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. Performing Subtraction of two vectors

On similar lines, in subtraction as well, the element-wise fashion would be followed and further the elements of vector 2 will get subtracted from vector 1.

Let us have a look at it’s implementation!

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)

Output:

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. Performing multiplication of two vectors

In a Vector multiplication, the elements of vector 1 get multiplied by the elements of vector 2 and the product vector is of the same length as of the multiplying vectors.

Let us try to visualize the multiplication operation:

x = [10,20] and y = [1,2] are two vectors. So the product vector would be v[ ],

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

Have a look at the below code!

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)

Output:

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. Performing Vector division operation

In vector division, the resultant vector is the quotient values after carrying out division operation on the two vectors.

Consider the below example for a better understanding.

x = [10,20] and y = [1,2] are two vectors. So the resultant vector v would be,

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

Let us now implement the above concept.

Example:

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)

Output:

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. Vector Dot Product

In a vector dot product, we perform the summation of the product of the two vectors in an element-wise fashion.

Let us have a look at the below.

vector c = x . y = (x1 * y1 + x2 * y2)

Example:

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)

Output:

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

Conclusion

By this, we have come to the end of this topic.

In order to have a deeper understanding about vectors, do try out creating a vector and performing the above mentioned operations and let us know your understanding in the comment box!

Feel free to comment below, in case you come across any question. For more such posts related to Python, Stay tuned and till then,

Happy Learning!! 馃檪

Comments

  1. Jarmo Lammi says:

    Hi, would be interesting also the case of vector cross product
    There seems to be also numpy.cross
    I tried just it and found it doesn’t work with syntax similar to dot product.
    This will cause error:
    >>> alst = [4, 1]
    >>> blst = [-1, 4]
    >>> avctr = np.array(alst)
    >>> bvctr = np.array(blst)
    >>> avctr.cross(bvctr)
    Traceback (most recent call last):
    File “”, line 1, in
    AttributeError: ‘numpy.ndarray’ object has no attribute ‘cross’

    However, this works giving only the sum of the squares, i.e the squares of the vector lengths:
    >>> cvtr = np.cross(avctr, bvctr)
    >>> cvtr
    array(17)
    Changing order of the vectors:
    >>> cvtr = np.cross(bvctr, avctr)
    >>> cvtr
    array(-17)

    One thing is still missing!
    How do you get the resulting vector, a 3D array[i, j, k]?
    It should be in this example the vectors [0, 0, 17] or [0, 0, -17], perpendicular to the vectors avctr and bvctr.

    1. Safa says:

      Hello Jarmo,
      So, we do have an option for cross product calculation in numpy. The function seems as numpy.cross() function. Let us consider the example mentioned by you–

      import numpy as np

      lst1 = [4,1]
      lst2 = [-1,4]

      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_cross = np.cross(vctr1,vctr2)
      print(“Cross product of two vectors: “,vctr_cross)

      Output: 17

      Explanation:
      vctr1: 4i +1j +0k and vctr2: -1i +4j + 0k. So if we were to calculate the cross product, it would be as shown below:
      cross product = 0i + 0j + [(4*4) – (-1*1)] = 17
      That is the reason, you get a single array element 17 as output. And, remember numpy.cross() works for only 2 or 3 dimensional elements.
      Consider another example for your understanding.
      import numpy as np

      lst1 = [10,20]
      lst2 = [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_cross = np.cross(vctr1,vctr2)
      print(“Cross product of two vectors: “,vctr_cross)

      Output:
      Vector created from a list 1:
      [10 20]
      Vector created from a list 2:
      [1 1 1]
      Cross product of two vectors: [ 20 -10 -10]

    2. Safa says:

      Please refer https://numpy.org/doc/stable/reference/generated/numpy.cross.html to know more about numpy.cross() function.

  2. Jarmo Lammi says:

    You have errors at 4. Performing Vector division operation
    There was mixed operator / and *
    The following example would have more sense.
    >>> lst1 = [10, 20, 30, 40, 50]
    >>> lst2 = [1, 2, 3, 4, 5]
    >>> vctr1 = np.array(lst1)
    >>> vctr2 = np.array(lst2)
    >>> vctr_div1 = vctr1/vctr2
    >>> vctr_div1
    array([10., 10., 10., 10., 10.])
    >>> vctr_div2 = vctr2/vctr1
    >>> vctr_div2
    array([0.1, 0.1, 0.1, 0.1, 0.1])
    >>> vctr_mul_div = vctr_div1*vctr_div2
    >>> vctr_mul_div
    array([1., 1., 1., 1., 1.])

    1. Safa says:

      Hello Jarmo, the example seems fine. But, yes there is an error in the code. Instead of division operator ‘/’, I had mentioned multiplication operator ‘*’. It will be corrected now. Thanks for pointing it out.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages