In this post, you will get a code sample related to how to assign new labels to columns in python programming while training machine learning models.
This is going to be very helpful when working with classification machine learning problem. Many a time the labels for response or dependent variable are in text format and all one wants is to assign a number such as 0, 1, 2 etc instead of text labels. Beginner-level data scientists will find this code very handy.
We will look at the code for the dataset as represented in the diagram below:
In the above code, you will see that class labels are named as very_low, Low, High, Middle etc. The objective is to provide label as 1, 2, 3, 4 etc.
Here are the steps:
Define a function which returns different numeric value based on the label
def assignNewLabels(label): if label == 'very_low' or label == 'Very Low': return 1 elif label == 'Low': return 2 elif label == 'Middle': return 3 elif label == 'High': return 4 else: return -1
Next step is to use apply method
df1['UNS_NEW'] = df1['UNS'].apply(assignNewLabels)
The above code creates a new column namely UNS_NEW. The data frame looks like the following:
Note that there is a new column with new labels (1, 2, 3, 4) assigned based on the logic within assignNewLabels function.
In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…
Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…
With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…
Anxiety is a common mental health condition that affects millions of people around the world.…
In machine learning, confounder features or variables can significantly affect the accuracy and validity of…
Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…