Home  >  Article  >  Backend Development  >  How do you access specific columns in a NumPy multidimensional array?

How do you access specific columns in a NumPy multidimensional array?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-27 11:53:01505browse

How do you access specific columns in a NumPy multidimensional array?

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!

Statement:
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