How to Combine NumPy Arrays with Different Datatypes While Preserving Data Types?

Susan Sarandon
Release: 2024-10-21 17:59:18
Original
585 people have browsed it

How to Combine NumPy Arrays with Different Datatypes While Preserving Data Types?

Combining Arrays with Multiple Datatypes in NumPy

The desire to concatenate arrays containing different datatypes into a single array with corresponding datatypes in each column poses a challenge. A common approach, using np.concatenate(), unfortunately converts the entire array to the string datatype, leading to memory inefficiencies.

To overcome this limitation, a viable solution involves employing record arrays or structured arrays.

Record Arrays

Record arrays allow for individual data fields to be accessed through attributes. By specifying the datatype of each field, multiple datatypes can be combined in a single array:

<code class="python">import numpy as np

a = np.array(['a', 'b', 'c', 'd', 'e'])
b = np.arange(5)
records = np.rec.fromarrays((a, b), names=('keys', 'data'))

print(records)</code>
Copy after login

Output:

rec.array([('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4)], 
      dtype=[('keys', '|S1'), ('data', '<i8')])
Copy after login

Structured Arrays

Structured arrays are similar, offering the ability to define the datatype of each column. However, they do not support attribute access like record arrays:

<code class="python">arr = np.array([('a', 0), ('b', 1)], 
                      dtype=([('keys', '|S1'), ('data', 'i8')]))

print(arr)</code>
Copy after login

Output:

array([('a', 0), ('b', 1)], 
      dtype=[('keys', '|S1'), ('data', '<i8')])
Copy after login

Choosing Between Record and Structured Arrays

The choice between record arrays and structured arrays depends on individual use cases. Record arrays provide convenience with attribute access, while structured arrays may be preferred for more complex data structures. Both approaches offer a convenient way to combine arrays with different datatypes in NumPy, providing flexibility and efficiency in data manipulation.

The above is the detailed content of How to Combine NumPy Arrays with Different Datatypes While Preserving Data Types?. For more information, please follow other related articles on the PHP Chinese website!

source:php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!