Blood Red Meat on the Barbie

In case some of you are not from the Land of 10,000 Lakes, Minnesota (you may not be familiar with Ole and Lena. The names Ole and Lena are well used in Northern Minnesota. Ole is the name of a great…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Refactor Python Code with Following Simple Tips

Use list, list comprehension, filter, np.where to improve your code readability, maintainability and scalability

Are you struggling when moving your code from notebook to production? Do you have trouble reading your own code created last week? or when a deadline is approaching and you have to make one simple change, but you end up spending hours untangling many places in your code? If this happens to you, this article is for you.

To have your POC model work is the first step; your work does not stop there. remember, your code is meant to be read by others. You have to keep improving it so it is easy to read, maintain and scale.

In this article, we will share you few tips to enable you to improve your code. We will use a simple dataset “iris’ data set to demonstrate ideas.

import packages

Load iris dataset, which is part of seaborn package

iris dataset

Tip 1: feature lists manipulation: add or remove feature names using list comprehension is efficient

Instead of writing out all feature names in your dataframe, which is tedious error-prone, and hard to scale, use list and list comprehension.

For example, I want to remove species from the dataframe since it will be our target variable.

Works: you could use pandas slicing, but it is hard to scale

Better: a more readable and scalable way is

It is easier to add a feature name to a list is easier using list by using “+”

Add a comment

Related posts:

3 ways to make money in crypto today

Forget bonds and retirement funds. They will be a thing of the past. Their rewards cannot keep up with what cryptocurrencies are already offering. The term hodl is a misspelling of the word hold…

Life in 2030?

The times have always been a-changin’ and people have always predicted that the changes will be big. I’m no different. In that this is the How to Do Life blog, it might be helpful to consider how…

Three reasons why we sometimes get stuck

But even when we have these skills we can still sometimes find ourselves stuck. There are three main reasons for this. The first is called overthinking or paralysis by analysis. This is when we think…