Accessing Columns in NumPy Multidimensional Arrays
Given a NumPy multidimensional array, retrieving specific columns can be achieved efficiently using indexing techniques. To access the ith column, utilize the following syntax:
<code class="python">array[:, i]</code>
For example:
<code class="python">test = np.array([[1, 2], [3, 4], [5, 6]]) test[:, 0] # Accesses the first column</code>
which outputs:
array([1, 3, 5])
Conversely, to access the ith row, use:
<code class="python">array[i, :]</code>
For example:
<code class="python">test[0, :] # Accesses the first row</code>
which outputs:
array([1, 2])
Refer to the NumPy reference's Indexing section for further details. This operation is generally efficient, particularly in comparison to looping over individual elements.
The above is the detailed content of How do you access specific columns in a NumPy multidimensional array?. For more information, please follow other related articles on the PHP Chinese website!