How to create a vector in Python using NumPy17 Mar 2025 | 5 min read In this tutorial, we will learn how we can create a vector using Numpy library. We will also explore basic operation of vector such as performing addition of two vectors, subtraction of two vectors, division of two vectors, multiplication of two vectors, vector dot product and vector scalar product. What is Vector?A vector is known as a single dimension-array. In Python, vector is a single one-dimension array of lists and behaves same as a Python list. According to a Google, vector represents direction as well as magnitude; especially it determines the position one point in a space relative to another. Vectors are very important in the Machine learning because they have magnitude and also the direction features. Let's understand how we can create the vector in Python. Creating Vector in PythonPython Numpy module provides the numpy.array() method which creates a one dimensional array i.e. a vector. A vector can be horizontal or vertical. Syntax: The above method accepts a list as an argument and returns numpy.ndarray. Let's understand the following example - Example - 1: Horizontal Vector Output: We create a vector from a list: [10 20 30 40 50] Example - 2: Vertical Vector Output: We create a vector from a list: [[12] [40] [ 6] [10]] Basic Operation of Python vectorAfter creating a vector, now we will perform the arithmetic operations on vectors. Below is the list of basic operations that we can perform in vector.
Addition of Two VectorsIn the vector addition, it takes place element-wise manner which means addition will happen element by element and the length would same as of the two additive vectors. Syntax: Let's understand the following example. Example - Output: We create vector from a list 1: [10 20 30 40 50] We create vector from a list 2: [11 12 13 14 15] Addition of two vectors: [21 32 43 54 65] Subtraction of Two VectorsThe subtraction performs same as the addition, it follows the element-wise approach and vector 2 elements will get subtracted from the vector 1. Let's understand the following example. Example - Output: We create vector from a list 1: [10 20 30 40 50] We create vector from a list 2: [5 2 4 3 1] Subtraction of two vectors: [5 18 26 37 49] Multiplication of Two VectorsThe vector 1 elements are multiplied by the vector 2 and return the same length vectors as the multiplying vectors. Let's understand the following example. Example - Output: We create vector from a list 1: [10 20 30 40 50] We create vector from a list 2: [5 2 4 3 1] Multiplication of two vectors: [ 50 40 120 120 50] The multiplication is performed as follows. vct[0] = x[0] * y[0] vct[1] = x[1] * y[1] The first element of the vector 1 is multiplied by corresponding vector's 2 first element and so on. Division Operation of Two vectorsIn the division operation, the resultant vector contains the quotient value that is get from the division of two vector's elements. Let's understand the following example. Example - Output: We create vector from a list 1: [10 20 30 40 50] We create vector from a list 2: [5 2 4 3 1] Division of two vectors: [ 2. 10. 7.5 13.33333333 50. ] As we can see in the above output, the division operation returned the quotient value of elements. Vector Dot ProductThe vector dot product performs between the two same-length sequential vectors and returns the single dot product. We will use the .dot() method to perform the dot product. It will happen as below. Let's understand the following example. Example - Output: We create vector from a list 1: [10 20 30 40 50] We create vector from a list 2: [5 2 4 3 1] Dot product of two vectors: 380 Vector-Scalar MultiplicationIn the scalar multiply operation; we multiply the scalar with the each component of the vector. Let's understand the following example. Example - Output: We create vector from a list 1: [10 20 30 40 50] Scalar Value : 5 Multiplication of two vectors: [ 50 100 150 200 250] In the above code, the scalar value multiplied by the each element of the vector in s * v = (s * v1, s * v2, s * v3) manner. Next TopicPickle Module of Python |
A slide puzzle is a popular puzzle game that involves sliding tiles around on a board to rearrange them into a certain order. Sliding Tile Puzzle in Python Sliding tiles game also known as sliding puzzle or sliding blocks game. In this article, we'll build a...
7 min read
Introduction The comparison-based sorting algorithm, Quick Sort, uses the divide-and-conquer strategy. It divides the remaining members into 2 sub-arrays (or sub-lists) determined by whether they are less than or greater than the element that serves as the pivot, which is chosen as the "pivot" element from the...
4 min read
We all can see that now we can perform multiple actions sitting at our home for which we had to run around many offices back then. Some of these tasks were performed using a webcam, like for the purpose of official documents, for online exams, for...
18 min read
In this tutorial, we will learn about the finger search tree data structure and discuss its advantages and disadvantages. We will also understand its implementation in Python. A finger search tree is a specialized data structure designed for efficient searching and accessing of data in a set...
8 min read
Introduction Python is a type of programming language used by developers worldwide. One of the essential features of Python is object-oriented programming (OOP). It allow the programmer to create objects, classes, and instances. In this article, we are going to discuss Python instances in detail and demonstrate...
6 min read
We just covered the most intriguing new features in this post; for a complete list of updates and changes, refer to the official release documentation. The release of Python 3.11 occurred on October 24, 2022. Python's most recent version is quicker and easier to use. It has...
15 min read
The operator overloading in Python means provide extended meaning beyond their predefined operational meaning. Such as, we use the "+" operator for adding two integers as well as joining two strings or merging two lists. We can achieve this as the "+" operator is overloaded by...
5 min read
? In this tutorial, we will learn how to plot (imagine) a brain network in Python using Graphviz. Graphviz is a python module that open-source diagram representation programming. It is generally well known among scientists to do representations. It's addressing primary data as charts of conceptual diagrams...
11 min read
In this lesson, we'll create a Python project that lets you convert between different currencies. We'll utilise the tkinter library to create a user interface. Therefore, creating a GUI application to get current value of USD to INR. What is a Currency Converter ? There are hundreds of...
6 min read
Decorators are an important and useful tool of Python. It allows us to modify the behavior of the function or class. So far, we have learned how to make the decorators using the function, but here we will discuss defining a class as a decorator. In...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India