Apakah yang berlaku kepada tatasusunan NumPy 1D apabila anda menukarnya?

Mary-Kate Olsen
Lepaskan: 2024-11-15 04:22:02
asal
129 orang telah melayarinya

What happens to a 1D NumPy array when you transpose it?

Transpose of a 1D NumPy Array

When working with NumPy arrays, it's important to understand the effects of transposition. Typically, the transpose of an array exchanges its rows and columns, resulting in a new array with swapped dimensions. However, in the case of a 1D array, the transpose operation has a different impact.

Consider the following Python snippet:

import numpy as np
a = np.array([5,4])
print(a)
print(a.T)
Salin selepas log masuk

Instead of transposing the array, it remains unchanged. This is because the transpose of a 1D array is inherently a 1D array. Unlike in MATLAB, where "1D" arrays are effectively 2D, NumPy treats 1D arrays distinctly.

If you require a transposed 2D representation of your 1D vector, you can achieve it by slicing the vector using np.newaxis:

import numpy as np
a = np.array([5,4])[np.newaxis]
print(a)
print(a.T)
Salin selepas log masuk

Now, the a.T operation will produce a transposed 2D array.

It's worth noting that adding an extra dimension to a 1D vector is not always necessary. In most cases, NumPy automatically broadcasts 1D arrays for appropriate calculations, eliminating the need to explicitly distinguish between row and column vectors.

Atas ialah kandungan terperinci Apakah yang berlaku kepada tatasusunan NumPy 1D apabila anda menukarnya?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan