The Wayback Machine - https://web.archive.org/web/20240708142049/https://www.geeksforgeeks.org/how-to-create-a-vector-in-python-using-numpy/
Open In App

How to create a vector in Python using NumPy

Last Updated : 28 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Numpy is basically used for creating array of n dimensions.

Vector are built from components, which are ordinary numbers. We can think of a vector as a list of numbers, and vector algebra as operations performed on the numbers in the list. In other words vector is the numpy 1-D array.

In order to create a vector, we use np.array method. 

Syntax : np.array(list)
Argument : It take 1-D list it can be 1 row and n columns or n rows and 1 column
Return : It returns vector which is numpy.ndarray
 

Note: We can create vector with other method as well which return 1-D numpy array for example np.arange(10), np.zeros((4, 1)) gives 1-D array, but most appropriate way is using np.array with the 1-D list.

Creating a Vector 
In this example we will create a horizontal vector and a vertical vector 

Python3




# importing numpy
import numpy as np
  
# creating a 1-D list (Horizontal)
list1 = [1, 2, 3]
  
# creating a 1-D list (Vertical)
list2 = [[10],
        [20],
        [30]]
  
# creating a vector1
# vector as row
vector1 = np.array(list1)
  
# creating a vector 2
# vector as column
vector2 = np.array(list2)
  
  
# showing horizontal vector
print("Horizontal Vector")
print(vector1)
  
print("----------------")
  
# showing vertical vector
print("Vertical Vector")
print(vector2)


Output : 

Horizontal Vector
[1 2 3]
----------------
Vertical Vector
[[10]
 [20]
 [30]]

Basic Arithmetic operation: 
In this example we will see do arithmetic operations which are element-wise between two vectors of equal length to result in a new vector with the same length 

Python3




# importing numpy
import numpy as np
  
# creating a 1-D list (Horizontal)
list1 = [5, 6, 9]
  
# creating a 1-D list (Horizontal)
list2 = [1, 2, 3]
  
# creating first vector
vector1 = np.array(list1)
  
# printing vector1
print("First Vector          : " + str(vector1))
  
# creating second vector
vector2 = np.array(list2)
  
# printing vector2
print("Second Vector         : " + str(vector2))
  
# adding both the vector
# a + b = (a1 + b1, a2 + b2, a3 + b3)
addition = vector1 + vector2
  
# printing addition vector
print("Vector Addition       : " + str(addition))
  
# subtracting both the vector
# a - b = (a1 - b1, a2 - b2, a3 - b3)
subtraction = vector1 - vector2
  
# printing addition vector
print("Vector Subtraction   : " + str(subtraction))
  
# multiplying  both the vector
# a * b = (a1 * b1, a2 * b2, a3 * b3)
multiplication = vector1 * vector2
  
# printing multiplication vector
print("Vector Multiplication : " + str(multiplication))
  
# dividing  both the vector
# a / b = (a1 / b1, a2 / b2, a3 / b3)
division = vector1 / vector2
  
# printing division vector
print("Vector Division       : " + str(division))


Output : 

First Vector: [5 6 9]
Second Vector: [1 2 3]
Vector Addition: [ 6  8 12]
Vector Subtraction: [4 4 6]
Vector Multiplication: [ 5 12 27]
Vector Division: [5 3 3]

Vector Dot Product 
In mathematics, the dot product or scalar product is an algebraic operation that takes two equal-length sequences of numbers and returns a single number. 
For this we will use dot method.

Python3




# importing numpy
import numpy as np
  
# creating a 1-D list (Horizontal)
list1 = [5, 6, 9]
  
# creating a 1-D list (Horizontal)
list2 = [1, 2, 3]
  
# creating first vector 
vector1 = np.array(list1)
  
# printing vector1
print("First Vector  : " + str(vector1))
  
# creating second vector
vector2 = np.array(list2)
  
# printing vector2
print("Second Vector : " + str(vector2))
  
# getting dot product of both the vectors
# a . b = (a1 * b1 + a2 * b2 + a3 * b3)
# a . b = (a1b1 + a2b2 + a3b3)
dot_product = vector1.dot(vector2)
  
# printing dot product
print("Dot Product   : " + str(dot_product))


Output:

First Vector  : [5 6 9]
Second Vector : [1 2 3]
Dot Product   : 44

Vector-Scalar Multiplication 
Multiplying a vector by a scalar is called scalar multiplication. To perform scalar multiplication, we need to multiply the scalar by each component of the vector.

Python3




# importing numpy
import numpy as np
  
# creating a 1-D list (Horizontal)
list1 = [1, 2, 3]
  
# creating first vector 
vector = np.array(list1)
  
# printing vector1
print("Vector  : " + str(vector))
  
# scalar value 
scalar = 2
  
# printing scalar value
print("Scalar  : " + str(scalar))
   
# getting scalar multiplication value
# s * v = (s * v1, s * v2, s * v3)
scalar_mul = vector * scalar
  
# printing dot product
print("Scalar Multiplication : " + str(scalar_mul))
  
  
   


Output 

Vector  : [1 2 3]
Scalar  : 2
Scalar Multiplication : [2 4 6]

 



Previous Article
Next Article

Similar Reads

Vector outer product with Einstein summation convention using NumPy in Python
In this article, we will find vector outer product with Einstein summation convention in Python. numpy.einsum() method The numpy.einsum() method from the NumPy library is used to find the vector outer product with the Einstein summation convention in Python. Many common multi-dimensional, linear algebraic array operations can be described in a simp
3 min read
Return the Norm of the vector over given axis in Linear Algebra using NumPy in Python
In this article, we will how to return the Norm of the vector over a given axis in Linear Algebra in Python. numpy.linalg.norm() method The numpy.linalg.norm() method is used to return the Norm of the vector over a given axis in Linear algebra in Python. Depending on the value of the ord parameter, this function can return one of the possible matri
3 min read
Get the Outer Product of an array with vector of letters using NumPy in Python
In this article let's see how to get the outer product of an array with a vector of letters in Python. numpy.outer() method The numpy.outer() method is used to get the outer product of an array with a vector of elements in Python. A matrix is the outer product of two coordinate vectors in linear algebra. The outer product of two vectors with dimens
4 min read
Divide each row by a vector element using NumPy
The following article depicts how to Divide each row by a vector element using NumPy. The vector element can be a single element, multiple element, or array. The division operator ( / ) is employed to produce the required functionality. We can divide rows of 1-D, 2-D, or even more types of arrays with vector elements and the following examples will
2 min read
Find a matrix or vector norm using NumPy
To find a matrix or vector norm we use function numpy.linalg.norm() of Python library Numpy. This function returns one of the seven matrix norms or one of the infinite vector norms depending upon the value of its parameters. Syntax: numpy.linalg.norm(x, ord=None, axis=None)Parameters: x: input ord: order of norm axis: None, returns either a vector
2 min read
Python | Numpy numpy.resize()
With the help of Numpy numpy.resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing. Parameters: new_shape : [tuple of ints, or n ints] Shape of resized array refcheck : [bool, optio
2 min read
Python | Numpy numpy.transpose()
With the help of Numpy numpy.transpose(), We can perform the simple function of transpose within one line by using numpy.transpose() method of Numpy. It can transpose the 2-D arrays on the other hand it has no effect on 1-D arrays. This method transpose the 2-D numpy array. Parameters: axes : [None, tuple of ints, or n ints] If anyone wants to pass
2 min read
Python | Numpy numpy.ndarray.__lt__()
With the help of numpy.ndarray.__lt__() method of Numpy, We can find that which element in an array is less than the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__lt__($self, value, /) Return: self<value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__gt__()
With the help of numpy.ndarray.__gt__() method of Numpy, We can find that which element in an array is greater then the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__gt__($self, value, /) Return: self>value Example #1 : In this example we can see th
1 min read
Python | Numpy numpy.ndarray.__le__()
With the help of numpy.ndarray.__le__() method of Numpy, We can find that which element in an array is less than or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__le__($self, value, /) Return: self<=value Example #1 : In this example we
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg