Skip to main content

The quirk of NumPy!

 

NumPy arrays in Python have the advantage of faster computation speeds than regular Python lists.

One of the key reasons behind this advantage is that a NumPy array stores only the elements of homogenous data types as opposed to a regular Python list.

This quirk of a NumPy array ensures that all its elements are stored in consecutive blocks of memory, which increases the ease and speed with which they can be retrieved.

It reduces the overall computation time on any numerical operations performed using NumPy arrays.

Well, it makes sense, but what if we intentionally tweak the input of a NumPy array to make it hold elements of different data types.

NumPy still won’t deviate from its fundamental quirk and plays a clever trick to find a way around it!

It ‘coerces’ the elements to be of homogenous data types. 

Consider a regular list – [6,2,True, 3,4,5]; it is a collection of four integer and one boolean elements.

If we convert it into a NumPy type, the resulting array would have the elements as below:

[6,2,1,3,4,5]

NumPy converted the boolean element ‘True’ to integer ‘1’ to ensure consecutive blocks of memory allocation.

Now, consider another list – [6,2,”True”,3,4,5]. This time, we have a collection of four integers and one string.

As per the heuristic, NumPy should try to convert the string “True” to an integer. 

But it is not as straightforward as converting a boolean element to an integer. Therefore, it coerces all the other elements to string data types instead.

The resulting NumPy array would be a collection of strings as below:
[“6″ ,”2”, “True”, “3” , “4”, “5”]

For data scientists and data analysts working with NumPy, understanding its type coercion quirk is crucial. 

Especially while dealing with uncleaned data, even a single weird value has the potential to mess up an entire NumPy array!


Comments

Popular posts from this blog

Solving Customer Churn with a hammer!

Learning when data should take a back seat and give way to domain knowledge is a valuable skill. Suppose you built a machine learning model on the data of your customers to predict churn risk. Now that you have a risk score for each customer, what do you do next? Do you filter the top n% based on the risk and send them a coupon with a discount in the hopes that it will prevent churn? But what if price is not the factor driving churn in many of these customers? Customers might have been treated poorly by customer service, which drove them away from your company's product.  Or there might have been an indirect competitor's product or service that removes the need for your company's product altogether (this happened to companies like Blockbuster and Kodak in the past!) There could be a myriad of factors, but you get the point! Dashboards and models cannot guide any company's strategic actions directly. If companies try to use them without additional context, more often tha...

Curing writer's block with sunk cost fallacy

I paid $20 to renew this blog's domain in July. But the truth is, I had been suffering from writer's block ever since the start of this year and hadn’t posted a single thing. At one point, I was ready to give up on the blog altogether, but a voice in my head kept reminding me of all the time and money I’d already invested in this blog. So, this week, I sat down to write this imperfect, patchy article—about none other than that voice itself.  Let me start with a classic scenario where you might have also encountered this voice. Suppose you’re at an Italian restaurant and ordered some pasta and tiramisu. After finishing the pasta, you realize you’re full, and there’s no way your stomach can handle that delicious tiramisu sitting right in front of you. But then, that beautiful brain of yours reminds you that you’ll be paying for the tiramisu whether you eat it or not. In a desperate attempt to avoid wasting money, you reluctantly eat two quick bites. And just like that, my frien...

What is SUTVA for A/B testing?

Imagine if person B’s blood pressure reading depends on whether person A receives the blood pressure medicine in a randomized controlled trial. This will be violating Stable Unit Treatment Value Assumption (SUTVA) SUTVA states that the treatment received by an individual should not influence the outcome we see for another individual during the experiment. I know the initial example sounded absurd, so let me try again. Consider LinkedIn A/B testing a new ‘dislike’ reaction for its users, and the gods of fate chose you to be part of the initial treatment group that received this update. Excited after seeing this new update, you use this dislike reaction on my post and send a screenshot to a few of your connections to do the same, who are coincidentally in the control group that did not receive the update. Your connections log in and engage with my posts to use this dislike reaction, but later get disappointed as this new update is not yet available to them. The offices of LinkedIn are tr...