Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Create a Vector or Matrix in Python?
In this article, we will show you how to create vectors and matrices in Python using NumPy, a powerful library for numerical computing.
NumPy is a Python library designed to work efficiently with arrays. It is fast, simple to learn, and memory-efficient. NumPy allows us to create n-dimensional arrays for mathematical operations.
What are Vectors?
A vector is a 1-dimensional NumPy array containing components (ordinary numbers). You can think of a vector as a list of numbers where vector algebra operations are performed on these numbers.
We use the np.array() method to create vectors.
Syntax
np.array(data)
Parameters
data ? A 1-D list or array-like object
Return Value ? Returns a NumPy array (vector)
Creating a Horizontal Vector
A horizontal vector is created from a simple list using numpy.array() ?
import numpy as np
# Creating a horizontal vector from a list
numbers = [15, 20, 25, 30, 35]
horizontal_vector = np.array(numbers)
print("Original list:", numbers)
print("Horizontal vector:", horizontal_vector)
print("Shape:", horizontal_vector.shape)
Original list: [15, 20, 25, 30, 35] Horizontal vector: [15 20 25 30 35] Shape: (5,)
Creating a Vertical Vector
A vertical vector is created using nested lists where each element is in its own sub-list ?
import numpy as np
# Creating a vertical vector using nested lists
vertical_vector = np.array([[10], [20], [30], [40], [50]])
print("Vertical vector:")
print(vertical_vector)
print("Shape:", vertical_vector.shape)
Vertical vector: [[10] [20] [30] [40] [50]] Shape: (5, 1)
Creating Matrices
Using numpy.array()
The most common way to create a matrix is using np.array() with nested lists ?
import numpy as np
# Creating a 3x3 matrix using nested lists
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("Matrix using np.array():")
print(matrix)
print("Shape:", matrix.shape)
print("Dimensions:", matrix.ndim)
Matrix using np.array(): [[1 2 3] [4 5 6] [7 8 9]] Shape: (3, 3) Dimensions: 2
Using numpy.matrix()
The np.matrix() function creates a specialized 2D array optimized for linear algebra operations ?
import numpy as np
# Creating a matrix using np.matrix()
matrix_obj = np.matrix([[5, 3, 9],
[4, 5, 6],
[7, 8, 2]])
print("Matrix using np.matrix():")
print(matrix_obj)
print("Type:", type(matrix_obj))
Matrix using np.matrix(): [[5 3 9] [4 5 6] [7 8 2]] Type: <class 'numpy.matrix'>
Common Matrix Creation Methods
NumPy provides several built-in functions to create special matrices ?
import numpy as np
# Identity matrix
identity = np.eye(3)
print("Identity matrix:")
print(identity)
# Matrix of zeros
zeros_matrix = np.zeros((2, 4))
print("\nZeros matrix:")
print(zeros_matrix)
# Matrix of ones
ones_matrix = np.ones((3, 2))
print("\nOnes matrix:")
print(ones_matrix)
Identity matrix: [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] Zeros matrix: [[0. 0. 0. 0.] [0. 0. 0. 0.]] Ones matrix: [[1. 1.] [1. 1.] [1. 1.]]
Comparison of Methods
| Method | Use Case | Return Type |
|---|---|---|
np.array() |
General purpose arrays | ndarray |
np.matrix() |
Linear algebra operations | matrix (deprecated) |
np.eye() |
Identity matrices | ndarray |
np.zeros() |
Initialize with zeros | ndarray |
Conclusion
Use np.array() for creating vectors and matrices in most cases. For specialized linear algebra operations, np.matrix() can be helpful, though np.array() is generally preferred in modern NumPy usage.
