• Home
  • About Us
  • Privacy Policy
  • Contact Us
  • Disclaimer
  • Terms & Conditions
Adsense
Advertisement
  • Home
  • Tech
    • All
    • Apps
    • Gadgets
    T-Mobile has announced the rollout of its high-speed 5G community, boasting speeds of up to 3Gbps:

    T-Mobile has announced the rollout of its high-speed 5G community, boasting speeds of up to 3Gbps:

    OpenAI can’t tell if something become written by using AI in any case

    OpenAI can’t tell if something become written by using AI in any case

    Google’s CFO just got promoted

    Google’s CFO just got promoted

    How Google’s latest AI model is generating music from your brain activity

    How Google’s latest AI model is generating music from your brain activity

    Easy Rider to Midnight Run, The Greatest Roadtrips Movies of All Time

    Easy Rider to Midnight Run, The Greatest Roadtrips Movies of All Time

    Three new Starfield animated shorts offer more glimpses of Bethesda’s new universe

    Three new Starfield animated shorts offer more glimpses of Bethesda’s new universe

    Trending Tags

    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • Mark Zuckerberg
  • Entertainment
  • Sports
  • CryptoCurrency
  • Business
  • Health and Lifestyle
    • All
    • Food
    World IVF Day: Infertility is a silent epidemic – why is it important to tackle fertility problems?  experts tell

    World IVF Day: Infertility is a silent epidemic – why is it important to tackle fertility problems? experts tell

    What is ‘duck walk’ in old age?  Expert shares tips on maintaining normal mobility

    What is ‘duck walk’ in old age? Expert shares tips on maintaining normal mobility

    Radiohead brands portfolio expands with the launch of Hustle™ energy drink.  Unveiled through new campaign “Dreams are free, #HustleModeOn for everything else – Food Marketing Technology”

    Radiohead brands portfolio expands with the launch of Hustle™ energy drink. Unveiled through new campaign “Dreams are free, #HustleModeOn for everything else – Food Marketing Technology”

    From Chris Gayle to Virat Kohli: Most runs scored by players in India vs West Indies ODI series

    From Chris Gayle to Virat Kohli: Most runs scored by players in India vs West Indies ODI series

    Infertility Treatment: How Ayurveda Can Help Increase Fertility?  experts tell

    Infertility Treatment: How Ayurveda Can Help Increase Fertility? experts tell

    Ishant Sharma opens up about the truth behind Zaheer Khan’s Test retirement and the allegations against Virat Kohli

    Ishant Sharma opens up about the truth behind Zaheer Khan’s Test retirement and the allegations against Virat Kohli

    Trending Tags

    • Golden Globes
    • Game of Thrones
    • MotoGP 2017
    • eSports
    • Fashion Week
No Result
View All Result
  • Home
  • Tech
    • All
    • Apps
    • Gadgets
    T-Mobile has announced the rollout of its high-speed 5G community, boasting speeds of up to 3Gbps:

    T-Mobile has announced the rollout of its high-speed 5G community, boasting speeds of up to 3Gbps:

    OpenAI can’t tell if something become written by using AI in any case

    OpenAI can’t tell if something become written by using AI in any case

    Google’s CFO just got promoted

    Google’s CFO just got promoted

    How Google’s latest AI model is generating music from your brain activity

    How Google’s latest AI model is generating music from your brain activity

    Easy Rider to Midnight Run, The Greatest Roadtrips Movies of All Time

    Easy Rider to Midnight Run, The Greatest Roadtrips Movies of All Time

    Three new Starfield animated shorts offer more glimpses of Bethesda’s new universe

    Three new Starfield animated shorts offer more glimpses of Bethesda’s new universe

    Trending Tags

    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • Mark Zuckerberg
  • Entertainment
  • Sports
  • CryptoCurrency
  • Business
  • Health and Lifestyle
    • All
    • Food
    World IVF Day: Infertility is a silent epidemic – why is it important to tackle fertility problems?  experts tell

    World IVF Day: Infertility is a silent epidemic – why is it important to tackle fertility problems? experts tell

    What is ‘duck walk’ in old age?  Expert shares tips on maintaining normal mobility

    What is ‘duck walk’ in old age? Expert shares tips on maintaining normal mobility

    Radiohead brands portfolio expands with the launch of Hustle™ energy drink.  Unveiled through new campaign “Dreams are free, #HustleModeOn for everything else – Food Marketing Technology”

    Radiohead brands portfolio expands with the launch of Hustle™ energy drink. Unveiled through new campaign “Dreams are free, #HustleModeOn for everything else – Food Marketing Technology”

    From Chris Gayle to Virat Kohli: Most runs scored by players in India vs West Indies ODI series

    From Chris Gayle to Virat Kohli: Most runs scored by players in India vs West Indies ODI series

    Infertility Treatment: How Ayurveda Can Help Increase Fertility?  experts tell

    Infertility Treatment: How Ayurveda Can Help Increase Fertility? experts tell

    Ishant Sharma opens up about the truth behind Zaheer Khan’s Test retirement and the allegations against Virat Kohli

    Ishant Sharma opens up about the truth behind Zaheer Khan’s Test retirement and the allegations against Virat Kohli

    Trending Tags

    • Golden Globes
    • Game of Thrones
    • MotoGP 2017
    • eSports
    • Fashion Week
No Result
View All Result
Adsense
No Result
View All Result
Home Tech

How Python enumerations make data configuration elegant. By Eric Burge, PhD | May, 2023

admin by admin
May 28, 2023
in Tech
0
How Python enumerations make data configuration elegant.  By Eric Burge, PhD |  May, 2023
0
SHARES
6
VIEWS
Share on FacebookShare on Twitter

[ad_1]

News bulletin

Watch carefully.

subscribe

Photo by Zac Durant on Unsplash

Python enums provide a more elegant solution for storing configuration information. Enums (short for enumeration) are essentially a way of defining a set of named constants. The best way to quickly understand enums is to adapt the machine learning configuration code from the previous section. You had the previous code:

# Configuration for RandomForestClassifier
N_ESTIMATORS_RANDOM_FOREST_CLASSIFIER = 100
MAX_DEPTH_RANDOM_FOREST_CLASSIFIER = 8
N_JOBS_RANDOM_FOREST_CLASSIFIER = 3

# Configuration for AdaBoostClassifier
N_ESTIMATORS_ADA_BOOST_CLASSIFIER = 50
LEARNING_RATE_ADA_BOOST_CLASSIFIER = 1.0

This can now be written as:

from enum import Enum

class RandomForest(Enum):
"""An Enum for tracking the configuration of random forest classifiers."""
N_ESTIMATORS = 100
MAX_DEPTH = 8
N_JOBS = 3

class AdaBoost(Enum):
"""An Enum for tracking the configuration of Ada boost classifiers."""
N_ESTIMATORS = 50
LEARNING_RATE = 1.0

some advantages

Let’s take a quick look at what benefits Python enums give us:

  • Each parameter (eg, MAX_DEPTH) are now stored in a hierarchy within the model they use. This ensures that nothing is overwritten when more configuration code is introduced. So there is also no need for extremely long variable names.
  • Various parameters used in RandomForestClassifier are now grouped in RandomForest Enum. Thus they can be iterated and collectively analyzed for type safety.
  • Since Enums are classes, they can be docstrings As I have shown above. While it may not be strictly required for this example, in other examples it may be clear what the enumeration is referring to. Keeping it as a docstring that accompanies the class is much better, rather than a free-flowing comment. For one thing, automated documentation software will now pick up that the docstring is related to the enumeration. For free-flowing commentary, it would probably get lost.

If you now want to access the configuration code further down in the script, you can simply write:

from sklearn.ensemble import RandomForestClassifier

RandomForestClassifier(
n_estimators=RandomForest.N_ESTIMATORS.value,
max_depth=RandomForest.MAX_DEPTH.value,
n_jobs=RandomForest.N_JOBS.value
)

It looks clean and easy to read 😍

Some Python Features of Enums

Let’s illustrate some of the simple features of Python enumeration by looking at a toy example:

from enum import Enum
class HTTPStatusCodes(Enum):
"""An Enum that keeps track of status codes for HTTP(s) requests."""
OK = 200
CREATED = 201
BAD_REQUEST = 400
NOT_FOUND = 404
SERVER_ERROR = 500

Clearly, the information contained in the calculations is related; They are all about HTTP(S) status codes. Given this, you can now use the following simple code to extract the name and value:

print(HTTPStatusCodes.OK.name)
>>> OK

print(HTTPStatusCodes.OK.value)
>>> 200

Python enums also allow you to go backward: given that a status code value is 404You can find the status code name simply by typing:

print(HTTPStatusCodes(200).name)
>>> OK

You can work with name/value pairs in an Enum en masse, for example, by using a list builder list(),

print(list(HTTPStatusCodes))
>>> (
<HTTPStatusCodes.OK: 200>,
<HTTPStatusCodes.CREATED: 201>,
<HTTPStatusCodes.BAD_REQUEST: 400>,
<HTTPStatusCodes.NOT_FOUND: 404>,
<HTTPStatusCodes.SERVER_ERROR: 500>
)

Finally, you can pickle and unpickle calculations in Python. To do this, just use pickle Model as you would with other familiar objects in Python:

from pickle import dumps, loads
print(HTTPStatusCodes is loads(dumps(HTTPStatusCodes)))
>>> True

This is especially important for the configuration code of machine learning models. In that case, there are also well known libraries like MLflow that elegantly save the configuration code for you.

sensitive configuration code

When using sensitive configuration code (such as passwords or access keys), you should never write them explicitly in code, They should be in a separate file that is ignored by whatever version control system you are using and imported into the script.

That doesn’t mean they can’t be stored in Enums within code. Enums are about organization, and censored information can be organized as well. As an example, here is an enumeration that represents a connection to a storage account in Microsoft Azure:

from enum import Enum
import os

class StorageAccount(Enum):
ACCOUNT_NAME = "my account name"
ACCESS_KEY = os.environ.get('ACCESS_KEY')
CONTAINER_NAME = "my container name"

right here StorageAccount fetching count ACCESS_KEY from an environment variable. This can be set for example, a .env file. Note that no sensitive information is exposed within the Python script. Nevertheless, all the information about the storage account is neatly organized in one enumeration.

Previous Post

Neeraj Chopra shares his reaction after Delhi Police detains protesting wrestlers

Next Post

‘New Parliament House built by burying the Constitution’: AAP

admin

admin

Next Post
‘New Parliament House built by burying the Constitution’: AAP

'New Parliament House built by burying the Constitution': AAP

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Adsense

Welcome to our News Magazine Website, your go-to source for the latest and most compelling news around the Globe. Stay informed, stay inspired, and explore the world through our comprehensive and user-friendly platform.

Follow Us

Browse by Category

  • Apps
  • Astrology
  • Automobiles
  • Business
  • CryptoCurrency
  • Education
  • Entertainment
  • Food
  • Gadgets
  • Health and Lifestyle
  • India
  • Politics
  • Science and Environment
  • Sports
  • Tech
  • Uncategorized
  • World

Recent News

Awas Outflow ETF dan Stablecoin! 3 Isu Regulasi Global yang Paling Menekan Pasar Kripto Saat Ini

November 29, 2025

Sinyal Pemulihan: Mengenali Fading Bearish Momentum dan Level Kunci $92.000 untuk Reversal Bitcoin

November 29, 2025
  • Home
  • About Us
  • Privacy Policy
  • Contact Us
  • Disclaimer
  • Terms & Conditions

© 2023 Journal Official - News Magazine

No Result
View All Result
  • Disclaimer

© 2023 Journal Official - News Magazine