Home > Backend Development > Python Tutorial > What's the Difference Between NumPy's (R, 1) and (R,) Shapes, and How Can Matrix Multiplication Issues Be Resolved?

What's the Difference Between NumPy's (R, 1) and (R,) Shapes, and How Can Matrix Multiplication Issues Be Resolved?

Susan Sarandon
Release: 2024-12-04 14:13:11
Original
340 people have browsed it

What's the Difference Between NumPy's (R, 1) and (R,) Shapes, and How Can Matrix Multiplication Issues Be Resolved?

What is the difference between (R, 1) and (R,) shapes in NumPy?

In NumPy, a single-dimension array can be represented in two ways: as a shape (R, 1) (a list of numbers) or as a shape (R,) (a list of lists). Both of these shapes represent the same underlying data, but they have different implications for matrix multiplication.

When you multiply two matrices, their shapes must be compatible. If one matrix has a shape (R, 1) and the other matrix has a shape (R,), NumPy will raise an error because the shapes are not aligned. This is because (R, 1) is a two-dimensional shape, while (R,) is a one-dimensional shape.

To fix this error, you can explicitly reshape one of the matrices. For example:

import numpy as np

M = np.array([[1, 2, 3], [4, 5, 6]])
ones = np.ones((M.shape[0], 1))

result = np.dot(M[:,0].reshape((M.shape[0], 1)), ones)
Copy after login

In this example, we reshape the first column of M (a shape (R,)) to a shape (R, 1) using the reshape() method. This makes the shapes of the two matrices compatible, and the multiplication can be performed successfully.

Are there better ways to do the above example without explicitly reshaping?

Yes, there are better ways to do the above example without explicitly reshaping. One way is to use the sum() method with the axis argument. For example:

import numpy as np

M = np.array([[1, 2, 3], [4, 5, 6]])
ones = np.ones((M.shape[0], 1))

result = np.dot(M[:,0], ones) + M[:,1:]
Copy after login

In this example, we use the sum() method to sum the first column of M with the remaining columns. This gives us a matrix with the same shape as M. We can then perform the multiplication without any errors.

Another way to do the above example without explicitly reshaping is to use the broadcast() function. For example:

import numpy as np

M = np.array([[1, 2, 3], [4, 5, 6]])
ones = np.ones((M.shape[0], 1))

result = np.dot( np.broadcast_to(M[:,0], M.shape), ones)
Copy after login

In this example, we use the broadcast() function to broadcast the first column of M to the shape of M. This makes the shapes of the two matrices compatible, and the multiplication can be performed successfully.

The above is the detailed content of What's the Difference Between NumPy's (R, 1) and (R,) Shapes, and How Can Matrix Multiplication Issues Be Resolved?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template