Categories: NumpyPython

How to create structured arrays in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

We know Numpy arrays are data objects that store same type of data in one object. However, this statement is not strictly correct. Numpy provides also availability to create array that allows different data types in it. This kind of array is called structured array. Let us see an example.

#Create a structured array
A1 = np.array([(1, 'Oslo', 0.5, 1+2j),(2, 'Molde', 1.3, 2-2j),
                      (3, 'Bergen', 0.8, 1+3j)],dtype=('i2, a7, f4, c8'))
A1
#output
array([(1, b'Oslo', 0.5, 1.+2.j), (2, b'Molde', 1.3, 2.-2.j),
       (3, b'Bergen', 0.8, 1.+3.j)],
      dtype=[('f0', '<i2'), ('f1', 'S7'), ('f2', '<f4'), ('f3', '<c8')])

We have seen, A1 is an array that stores four different data types (integer, string, float, and complex) for its 4 columns. Numpy reads the input data and assigns most reasonable data types for them. Numpy also assign default name for each column, f1, f2, and so on. Both names and data types can be manually specified when calling this statement again.

#create again this structured array, with specified column name and dtype
A1 = np.array([(1, 'Oslo', 0.5, 1+2j),(2, 'Molde', 1.3, 2-2j),
                      (3, 'Bergen', 0.8, 1+3j)],
              dtype=[('index', 'i2'),('City','a6'),('point','f4'),('other','c8')])

A1
#output
array([(1, b'Oslo', 0.5, 1.+2.j), (2, b'Molde', 1.3, 2.-2.j),
       (3, b'Bergen', 0.8, 1.+3.j)],
      dtype=[('index', '<i2'), ('City', 'S6'), ('point', '<f4'), ('other', '<c8')])

Next we can return column data by indexing its name, result is a one-dimensional array of same data type.

#get the column 'point'
A1['point']
#Output
array([0.5, 1.3, 0.8], dtype=float32)
#get column 'City'
A1['City']
#Output
array([b'Oslo', b'Molde', b'Bergen'], dtype='|S6')

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Fine tuning a Hugging Face transformer model for multi-track music generation

To train a PyTorch Transformer for multi-track music generation, the standard paradigm is to convert…

3 weeks ago

what are Transformer, LLM , and GPT

In the field of AI and Natural Language Processing (NLP), The Transformer is the engine…

3 weeks ago

Main framework for PyTorch music generation model

If your goal is purely symbolic melody generation (e.g., generating MIDI files or notes rather…

4 weeks ago

affine_grid() in pytorch

According to PyTorch document, In PyTorch, torch.nn.functional.affine_grid generates a 2D or 3D flow field (sampling…

3 months ago

How to create an Android mobile app with a deep learning AI model ?

Creating an Android mobile app with a deep learning AI model involves several key steps:…

12 months ago

Download source files for R Machine learning

Click here to go to source files for R Machine Learning

1 year ago