Moad Computer, the actionable insights company
  • Home
  • Contact
  • Shop
  • Blog
  • Home
  • Contact
  • Shop
  • Blog
Search

Actionable Insights blog




Specialized container datatype 'named tuple'  in Python

2/28/2023

0 Comments

 
Picture

Dr. Rahul Remanan

​CEO, Moad Computer

In addition to the general purpose container datatypes: dict, list, set and tuple; Python also implements specialized container datatypes. One of the commonly used specialized container datatype in Python is the 'named tuple'. 

​A 'named tuple' can be created in Python using the collections module factory function namedtuple(). This function returns a tuple like object whose elements can be accessed using the corresponding attribute name. Hence the name of this tuple like object: 'named tuple'.

The attribute names or field_names for the 'named tuple' can be either a sequence of strings (Eg. ['x', 'y']) or a single string with each fieldname separated by whitespace and/or commas (Eg. 'x y' or 'x, y').

Of note here, while assigning a fieldname; they should not start with a digit or an underscore. However, both digits and underscores are allowed elsewhere in the fieldname.

A common application for the 'named tuple' is to create more readable vectors such as those representing the Cartesian coordinate space or for representing the latitude-longitude data from a GPS receiver.
Creating and working with 'named tuples' in Python
Import libraries
Create a 'named tuple'
Python 'for-loop' to manipulate the 'named tuple'
Tensorflow 'while_loop' to manipulate the 'named tuple'
The same operations performed in the Python 'for-loop'  is replicated using Tensorflow 'while_loop'. The slicing of tuple elements is applicable for the 'named tuples'. Therefore, a 'named tuple' provides the convenience of accessing its contents using both field_names as well as their position index.
Performance optimization of the 'named tuple' operations in Tensorflow 'while_loop' 
Described below is the a quick code-snippet on performance optimization for the 'named tuple' operations. In this example, a 'named tuple' is updated using the Tensorflow 'while_loop' by wrapping the operation as a decorated function.
References
1.  Python docs on Collections module
2.  Tensorflow docs on working with while_loop
3.  Stackoverflow post on Tensorflow while_loop performance optimization

​

0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Overview

    Moad Computer is an actionable insights firm. We provide enterprises with end-to-end artificial intelligence solutions. Actionable Insights blog is a quick overview of things we are most excited about.

    Archives

    November 2022
    October 2022
    September 2022
    August 2022
    July 2022
    June 2022
    May 2022
    April 2022
    March 2022
    February 2022
    January 2022
    December 2021
    November 2021
    October 2021
    September 2021
    August 2021
    July 2021
    June 2021
    May 2021
    April 2021
    March 2021
    February 2021
    January 2021
    December 2020
    November 2020
    October 2020
    May 2020
    April 2020

    Categories

    All

    RSS Feed

Location

Our mission:

Cutting edge, insightful analytics using AI, for everyone.

Contact Us

    Subscribe Today!

Submit
  • Home
  • Contact
  • Shop
  • Blog