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.

Syntax

np.array(list)

Parameters

  • list − a 1-D list, which can have one row and n columns or n rows and one column.

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.

Updated on: 20-Oct-2022

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements