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
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.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
Horizontal Vector [1 2 3] ---------------- Vertical Vector [  ]
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
First Vector : [5 6 9] Second Vector : [1 2 3] Vector Addition : [ 6 8 12] Vector Substraction : [4 4 6] Vector Multiplication : [ 5 12 27] Vector Division : [ 5 12 27]
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
First Vector : [5 6 9] Second Vector : [1 2 3] Dot Product : 44
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.
Vector : [1 2 3] Scalar : 2 Scalar Multiplication : [2 4 6]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Find a matrix or vector norm using NumPy
- Divide each row by a vector element using NumPy
- NumPy | Vector Multiplication
- How to get the magnitude of a vector in NumPy?
- Create a white image using NumPy in Python
- Create Pandas Series using NumPy functions
- Create an array which is the average of every consecutive subarray of given size using NumPy
- wxPython - Create Radio Button using Create() function
- wxPython - Create Static Box using Create() method
- Python | Numpy numpy.resize()
- Python | Numpy numpy.transpose()
- Python | Numpy numpy.ndarray.__lt__()
- Python | Numpy numpy.ndarray.__gt__()
- Python | Numpy numpy.ndarray.__le__()
- Python | Numpy numpy.ndarray.__ge__()
- Python | Numpy numpy.ndarray.__eq__()
- Python | Numpy numpy.ndarray.__ne__()
- Python | Numpy numpy.ndarray.__neg__()
- Python | Numpy numpy.ndarray.__pos__()
- Python | Numpy numpy.ndarray.__truediv__()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.