Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
Let's talk about the numpy.matrix
data type and the numpy.empty()
function, and how they are used.
numpy.matrix
:The numpy.matrix
data type is a specialized 2-D array that retains its 2-D nature through operations. While it might seem handy at first, it's worth noting that the use of numpy.matrix
is discouraged in favor of regular ndarrays. This is because the matrix class tends to have some peculiar behaviors that can be confusing.
However, for completeness, let's delve into it.
To start, make sure you've imported NumPy:
import numpy as np
numpy.matrix
:Creating a simple matrix:
A = np.matrix([[1, 2], [3, 4]]) print(A)
Matrix multiplication using *
:
B = np.matrix([[2, 1], [0, 3]]) print(A * B)
numpy.empty()
:The numpy.empty()
function is used to create a new array without initializing the entries to any particular values. It's faster than functions like numpy.zeros()
or numpy.ones()
, because it doesn't bother setting the array values to any particular number��they're just whatever happens to be in memory at that location.
numpy.empty()
:This creates an uninitialized array of shape (3, 3)
:
empty_arr = np.empty((3, 3)) print(empty_arr)
Using numpy.empty()
with numpy.matrix
:
empty_matrix = np.matrix(np.empty((2, 2))) print(empty_matrix)
Note: The values you see in the uninitialized matrix/array are "garbage values," i.e., whatever data was in the memory locations used by the array/matrix.
numpy.empty()
is faster than other initialization functions because it does not touch the memory. However, always make sure you initialize the array/matrix values yourself before using them, or you'll work with garbage values.
While you can use the matrix data type in NumPy, it's generally recommended to use regular ndarrays with the appropriate functions (dot
, matmul
, etc.) for matrix operations for clarity and consistency.
Both numpy.matrix
and numpy.empty()
have their particularities. While the matrix class offers convenience for matrix operations, its use is discouraged due to potential confusion. On the other hand, numpy.empty()
is a fast way to allocate array (or matrix) space without initialization, but care must be taken to avoid working with uninitialized data.
You can create an empty matrix in NumPy using the np.empty()
function. An empty matrix is created without initializing its values, so the content may contain arbitrary values.
import numpy as np # Create an empty matrix with shape (2, 3) empty_matrix = np.empty((2, 3)) print("Empty Matrix:") print(empty_matrix)
Performing operations on an empty matrix without initialization may lead to unpredictable results. It's generally recommended to initialize the matrix with desired values before operations.
# Assuming 'empty_matrix' is already defined # Example operation (not recommended on an empty matrix) result_operation = empty_matrix * 2 print("Result of operation on empty matrix:") print(result_operation)
The np.empty()
function is useful for initializing an array without setting its values.
# Using np.empty for array initialization initialized_array = np.empty(5) print("Initialized Array:") print(initialized_array)
You can generate an uninitialized array using np.empty()
with a specified shape.
# Generating an uninitialized array uninitialized_array = np.empty((3, 3)) print("Uninitialized Array:") print(uninitialized_array)
np.empty()
, np.zeros()
, and np.ones()
are functions in NumPy for array creation with different initialization.
# Comparison of empty, zeros, and ones functions empty_array = np.empty((2, 2)) zeros_array = np.zeros((2, 2)) ones_array = np.ones((2, 2)) print("Empty Array:") print(empty_array) print("\nZeros Array:") print(zeros_array) print("\nOnes Array:") print(ones_array)
np.empty()
is more efficient than np.zeros()
or np.ones()
because it does not initialize the values, saving computation time.
# Efficient array creation with np.empty efficient_array = np.empty((3, 3)) print("Efficiently created Array:") print(efficient_array)
You can create empty matrices for later assignment using np.empty()
and then assign values as needed.
# Creating empty matrix for later assignment empty_for_assignment = np.empty((2, 2)) # Assigning values later empty_for_assignment[0, 0] = 1 empty_for_assignment[1, 1] = 2 print("Matrix for later assignment:") print(empty_for_assignment)
Creating an uninitialized array can be useful when you plan to overwrite its values with specific data.
# Uninitialized array creation example uninitialized_example = np.empty(4) uninitialized_example[:] = 5 print("Uninitialized Array Example:") print(uninitialized_example)
You can use np.empty()
for advanced matrix operations where you want to initialize the array later based on certain conditions.
# Empty array for advanced matrix operations empty_advanced = np.empty((3, 3)) # Perform advanced operations and assign values later empty_advanced[empty_advanced > 0] = 1 print("Empty Array for Advanced Operations:") print(empty_advanced)