How to do vectors in python

How to Create a Vector or Matrix in Python?

In this article, we will show you how to create a vector or matrix in Python.

NumPy is a Python library designed to work efficiently with arrays in Python. It is fast, simple to learn, and efficient in storage. In NumPy, we may generate an n-dimensional array.

What are vectors?

In python, vectors are built from components, which are ordinary numbers. A vector can be considered as a list of numbers, and vector algebra as operations done on the numbers in the list. In other words, a vector is the numpy 1-D array.

We use the np.array() method to create a vector.

Читайте также:  Python problems and solutions

Syntax

Parameters

Return value − returns vector(numpy.ndarray)

Creating a Horizontal Vector from a given list

In this method, we create a horizontal vector from the list using the numpy.array() function.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword to import the NumPy module with an alias name.
  • Create a variable to store a Horizontal 1-Dimensional list.
  • Use the numpy.array() function(returns an ndarray. The ndarray is an array object that satisfies the given requirements) to create a vector_1 by passing given list_1 as an argument to it i.e, vector as a row.
  • Print the resultant horizontal vector.

The following program creates the horizontal vector from the list using the NumPy array() function and returns it −

Example

# importing numpy module with an alias name import numpy as np # creating a Horizontal 1-Dimensional list list_1 = [ 15, 20, 25, 'Hello', 'TutorialsPoint'] # creating a vector(numpy array) of the horizontal list # This is the horizontal vector vector_1 = np.array(list_1) print('Given List =',list_1) # printing the resultant horizontal vector print("The resultant horizontal vector:") print(vector_1)

Output

On executing, the above program will generate the following output −

Given List = [15, 20, 25, 'Hello', 'TutorialsPoint'] The resultant horizontal vector: ['15' '20' '25' 'Hello' 'TutorialsPoint']

Creating a Vertical Vector

In this method, we create a vertical vector using the numpy.array() function.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword to import the NumPy module with an alias name.
  • Pass the vertical list as an argument to the numpy.array() function(returns an ndarray. The ndarray is an array object that satisfies the given requirements) and store this vertical vector in a variable.
  • Print the resultant vertical vector.

Example

The following program creates the vertical vector using the NumPy array() function and returns it −

# importing NumPy module with an alias name import numpy as np # Passing vertical list as an argument to array() function # creating a vertical vector(NumPy array) of the second list vector_2 = np.array([[5], [40], [20], ['Hello'], ['TutorialsPoint']]) # printing the resultant vertical vector print("The resultant vertical vector:") print(vector_2)

Output

On executing, the above program will generate the following output −

The resultant vertical vector: [['5'] ['40'] ['20'] ['Hello'] ['TutorialsPoint']]

Creating a Matrix using numpy.mat() function

In this method, we create a matrix using the numpy.mat() function.

In Python, the mat() method is used to convert an array into a matrix.

Parameters

The mat() function accepts the following arguments −

  • data − This is the input data or an array like object.
  • dtype − This represents the output matrix’s data type.

Return Value

The mat() method interprets the input as a matrix and returns it.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword to import the NumPy module with an alias name.
  • Pass the nested list(list of lists) as an argument to the numpy.mat() function(the mat() method is used to convert an array into a matrix) and store this matrix in a variable.
  • Print the resultant matrix.

Example

The following program creates the matrix using the Numpy mat() function and returns it −

# importing numpy module with an alias name import numpy as np # Creating a matrix using numpy.mat() function inputMatrix = np.mat([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print("The created matrix is:") print(inputMatrix)

Output

On executing, the above program will generate the following output −

The created matrix is: [[1 2 3] [4 5 6] [7 8 9]]

Creating a Matrix using numpy.matrix() function

In this method, we create a matrix using the numpy.matrix() function.

Parameters

The numpy.matrix() function accepts the following arguments −

  • data − This is the input data or an array like object.
  • dtype − This represents the output matrix’s data type.

Return Value:

A matrix representation of the data

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword to import the NumPy module with an alias name.
  • Pass the nested list(list of lists) as an argument to the numpy.matrix() function(From a string of data or an array-like object, this class returns a matrix. The resulting matrix is a specialized 2D array) and store this matrix in a variable.
  • Print the resultant matrix.

Example

The following program creates the matrix using the Numpy matrix() function and returns it −

# importing numpy module with an alias name import numpy as np # Creating a matrix using numpy.matrix() function inputMatrix = np.matrix([[5, 3, 9, 11], [4, 5, 6, 23], [7, 8, 9, 84]]) print("The created matrix is:") print(inputMatrix)

Output

On executing, the above program will generate the following output −

The created matrix is: [[ 5 3 9 11] [ 4 5 6 23] [ 7 8 9 84]]

Conclusion

In this tutorial, we learned two distinct ways to generate matrices in Python, as well as how to create vertical and horizontal vectors.

Источник

How to do vectors in python

  • Numpy | Array Creation
  • numpy.arange() in Python
  • numpy.zeros() in Python
  • Create a Numpy array filled with all ones
  • numpy.linspace() in Python
  • numpy.eye() in Python
  • Creating a one-dimensional NumPy array
  • How to create an empty and a full NumPy array?
  • Create a Numpy array filled with all zeros | Python
  • How to generate 2-D Gaussian array using NumPy?
  • How to create a vector in Python using NumPy
  • Python | Numpy fromrecords() method

NumPy Array Manipulation

  • Copy and View in NumPy Array
  • How to Copy NumPy array into another array?
  • Appending values at the end of an NumPy array
  • How to swap columns of a given NumPy array?
  • Insert a new axis within a NumPy array
  • numpy.hstack() in Python
  • numpy.vstack() in python
  • Joining NumPy Array
  • Combining a one and a two-dimensional NumPy Array
  • Python | Numpy np.ma.concatenate() method
  • Python | Numpy dstack() method
  • Splitting Arrays in NumPy
  • How to compare two NumPy arrays?
  • Find the union of two NumPy arrays
  • Find unique rows in a NumPy array
  • Python | Numpy np.unique() method
  • numpy.trim_zeros() in Python

Matrix in NumPy

  • Matrix manipulation in Python
  • numpy matrix operations | empty() function
  • numpy matrix operations | zeros() function
  • numpy matrix operations | ones() function
  • numpy matrix operations | eye() function
  • numpy matrix operations | identity() function
  • Adding and Subtracting Matrices in Python
  • Matrix Multiplication in NumPy
  • Numpy ndarray.dot() function | Python
  • NumPy | Vector Multiplication
  • How to calculate dot product of two vectors in Python?
  • Multiplication of two Matrices in Single line using Numpy in Python
  • Python | Numpy np.eigvals() method
  • How to Calculate the determinant of a matrix using NumPy?
  • Python | Numpy matrix.transpose()
  • Python | Numpy matrix.var()
  • Compute the inverse of a matrix using NumPy

Operations on NumPy Array

Reshaping NumPy Array

  • Reshape NumPy Array
  • Python | Numpy matrix.resize()
  • Python | Numpy matrix.reshape()
  • NumPy Array Shape
  • Change the dimension of a NumPy array
  • numpy.ndarray.resize() function – Python
  • Flatten a Matrix in Python using NumPy
  • numpy.moveaxis() function | Python
  • numpy.swapaxes() function | Python
  • Python | Numpy matrix.swapaxes()
  • numpy.vsplit() function | Python
  • numpy.hsplit() function | Python
  • Numpy MaskedArray.reshape() function | Python
  • Python | Numpy matrix.squeeze()

Indexing NumPy Array

Arithmetic operations on NumPyArray

Linear Algebra in NumPy Array

NumPy and Random Data

  • Random sampling in numpy | ranf() function
  • Random sampling in numpy | random() function
  • Random sampling in numpy | random_sample() function
  • Random sampling in numpy | sample() function
  • Random sampling in numpy | random_integers() function
  • Random sampling in numpy | randint() function
  • numpy.random.choice() in Python
  • How to choose elements from the list with different probability using NumPy?
  • How to get weighted random choice in Python?
  • numpy.random.shuffle() in python
  • numpy.random.geometric() in Python
  • numpy.random.permutation() in Python

Sorting and Searching in NumPy Array

Universal Functions

Working With Images

Projects and Applications with NumPy

Introduction

  • Numpy | Array Creation
  • numpy.arange() in Python
  • numpy.zeros() in Python
  • Create a Numpy array filled with all ones
  • numpy.linspace() in Python
  • numpy.eye() in Python
  • Creating a one-dimensional NumPy array
  • How to create an empty and a full NumPy array?
  • Create a Numpy array filled with all zeros | Python
  • How to generate 2-D Gaussian array using NumPy?
  • How to create a vector in Python using NumPy
  • Python | Numpy fromrecords() method

NumPy Array Manipulation

  • Copy and View in NumPy Array
  • How to Copy NumPy array into another array?
  • Appending values at the end of an NumPy array
  • How to swap columns of a given NumPy array?
  • Insert a new axis within a NumPy array
  • numpy.hstack() in Python
  • numpy.vstack() in python
  • Joining NumPy Array
  • Combining a one and a two-dimensional NumPy Array
  • Python | Numpy np.ma.concatenate() method
  • Python | Numpy dstack() method
  • Splitting Arrays in NumPy
  • How to compare two NumPy arrays?
  • Find the union of two NumPy arrays
  • Find unique rows in a NumPy array
  • Python | Numpy np.unique() method
  • numpy.trim_zeros() in Python

Matrix in NumPy

  • Matrix manipulation in Python
  • numpy matrix operations | empty() function
  • numpy matrix operations | zeros() function
  • numpy matrix operations | ones() function
  • numpy matrix operations | eye() function
  • numpy matrix operations | identity() function
  • Adding and Subtracting Matrices in Python
  • Matrix Multiplication in NumPy
  • Numpy ndarray.dot() function | Python
  • NumPy | Vector Multiplication
  • How to calculate dot product of two vectors in Python?
  • Multiplication of two Matrices in Single line using Numpy in Python
  • Python | Numpy np.eigvals() method
  • How to Calculate the determinant of a matrix using NumPy?
  • Python | Numpy matrix.transpose()
  • Python | Numpy matrix.var()
  • Compute the inverse of a matrix using NumPy

Operations on NumPy Array

Reshaping NumPy Array

  • Reshape NumPy Array
  • Python | Numpy matrix.resize()
  • Python | Numpy matrix.reshape()
  • NumPy Array Shape
  • Change the dimension of a NumPy array
  • numpy.ndarray.resize() function – Python
  • Flatten a Matrix in Python using NumPy
  • numpy.moveaxis() function | Python
  • numpy.swapaxes() function | Python
  • Python | Numpy matrix.swapaxes()
  • numpy.vsplit() function | Python
  • numpy.hsplit() function | Python
  • Numpy MaskedArray.reshape() function | Python
  • Python | Numpy matrix.squeeze()

Indexing NumPy Array

Arithmetic operations on NumPyArray

Linear Algebra in NumPy Array

NumPy and Random Data

  • Random sampling in numpy | ranf() function
  • Random sampling in numpy | random() function
  • Random sampling in numpy | random_sample() function
  • Random sampling in numpy | sample() function
  • Random sampling in numpy | random_integers() function
  • Random sampling in numpy | randint() function
  • numpy.random.choice() in Python
  • How to choose elements from the list with different probability using NumPy?
  • How to get weighted random choice in Python?
  • numpy.random.shuffle() in python
  • numpy.random.geometric() in Python
  • numpy.random.permutation() in Python

Sorting and Searching in NumPy Array

Universal Functions

Working With Images

Источник

Оцените статью