Transposing a 1D array in NumPy
Arrays and matrices form the core of this Python library. The transpose of these arrays and matrices plays a vital role in certain topics such as machine learning. In NumPy, it is easy to calculate the transpose of an array or a matrix.
Transposing a 1D array in NumPy
To transpose an array or matrix in NumPy, we have to use T
the transposed array or matrix property, which stores the transposed array or matrix.
T
The attribute is exclusive to NumPy arrays, that is, only ndarray
. This attribute has no effect on Python lists.
In theory, it is possible to transpose a 1D array, but technically, or more precisely, in terms of the programming language, it is not possible to transpose a 1D array.
Don’t get me wrong. It’s just that transposing a 1D array in Python or any other programming language is a little different. It all comes down to how arrays are represented in the programming language.
A 1D array is just a row of a matrix. If you have to transpose this array (technically a matrix), you have to convert this 1D matrix into a 2D matrix. Then use the specified function to transpose the 2D matrix.
Please refer to the following code for better explanation.
import numpy as np
a = [1, 2, 3, 4, 5]
b = np.array(a)
c = np.array([a])
print(b)
print(c)
print(b.shape)
print(c.shape)
print(b.T)
print(c.T)
Output:
[1 2 3 4 5]
[[1 2 3 4 5]]
(5,)
(1, 5)
[1 2 3 4 5]
[[1]
[2]
[3]
[4]
[5]]
First, we use np.array()
the method and a Python list to form two NumPy arrays, b
1D and c
2D. To convert the lists to 2D matrices, we []
enclose them in . Then, we print the NumPy arrays and their respective shapes.
But the most important thing to note is that the transpose of a 1D array is the same as the array itself, but the transpose of a 2D array has completely changed. The results are very obvious.
The non-transposed 2D array has one array inside it with five elements representing one row of the matrix. After the transposition, there are five arrays inside the 2D array representing the five rows of the transposed matrix with one element per row. That's how transposition works!
For a 1D array, the same array is returned because [1 2 3 4 5]
the transposed array in Python looks like this [1 2 3 4 5]
. This result requires that our original array is 2D instead of 1D.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert Tensor to NumPy array in Python
Publish Date:2025/05/03 Views:85 Category:Python
-
This tutorial will show you how to convert a Tensor to a NumPy array in Python. Use the function in Python Tensor.numpy() to convert a tensor to a NumPy array Eager Execution of TensorFlow library can be used to convert tensor to NumPy arra
Saving NumPy arrays as images in Python
Publish Date:2025/05/03 Views:193 Category:Python
-
In Python, numpy module is used to manipulate arrays. There are many modules available in Python that allow us to read and store images. An image can be thought of as an array of different pixels stored at specific locations with correspond
Converting a PIL image to a NumPy array
Publish Date:2025/05/03 Views:148 Category:Python
-
This tutorial will discuss methods to convert a PIL image to a 3-dimensional NumPy array in Python. numpy.array() Convert a PIL image to a NumPy array in Python using the PIL is used to perform various operations on images in Python. The Pi
How to Convert DataFrame Column to String in Pandas
Publish Date:2025/05/02 Views:162 Category:Python
-
We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a
How to count the frequency of values in a Pandas DataFrame
Publish Date:2025/05/02 Views:84 Category:Python
-
Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().
How to get value from Pandas DataFrame cell
Publish Date:2025/05/02 Views:148 Category:Python
-
We'll look at using to get values from cells in iloc Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .
How to Add a Row to a Pandas DataFrame
Publish Date:2025/05/02 Views:127 Category:Python
-
Pandas is designed to load a fully populated DataFrame . We can pandas.DataFrame add them one by one in . This can be done by using various methods, such as .loc , dictionary, pandas.concat() or DataFrame.append() . .loc [index] Add rows to
How to change the order of Panas DataFrame columns
Publish Date:2025/05/02 Views:185 Category:Python
-
We will show how to use insert and reindex to change the order of columns in different ways pandas.DataFrame , such as assigning column names in a desired order. pandas.DataFrame Sort the columns in the new order The easiest way is columns
How to pretty print an entire Pandas Series/DataFrame
Publish Date:2025/05/02 Views:168 Category:Python
-
We will introduce various methods to pretty print the entire Pandas Series/DataFrame, such as option_context, set_option, and options.display. option_context Pretty Printing Pandas DataFrame We can option_context use with one or more option