feature_engine icon indicating copy to clipboard operation
feature_engine copied to clipboard

drop_duplicate_features with huge datasets

Open NicoGalli opened this issue 3 years ago • 12 comments

When running drop_duplicate_features() with a huge dataset (More that 60K features) it lasts forever (36 hours and couting), while simple code:

   # Remove duplicated features

        duplicated_feat = []         for i in range(0, len(X_train.columns)):             # if i % 10 == 0:  # this helps me understand how the loop is going             #     print(i)               col_1 = X_train.columns[i]

            for col_2 in X_train.columns[i + 1:]:                 if X_train[col_1].equals(X_train[col_2]):                     duplicated_feat.append(col_2)

Took half an hour to finish. Please check.

NicoGalli avatar Jan 03 '23 18:01 NicoGalli

Hi @solegalli

Can I pick this issue and investigate what's happening there? If no one works on it yet

Okroshiashvili avatar Feb 07 '23 07:02 Okroshiashvili

@NicoGalli Can you confirm that my understanding of the problem is correct?

So, we have huge dataframe (More that 60K features) and there is the following situation:

  1. There could be features with only same name and different values
  2. There could be features with same name and same values
  3. There could be features with different name and same values

Are all these scenarios possible? and should I consider them while testing the code?

Okroshiashvili avatar Feb 07 '23 08:02 Okroshiashvili

I did some experiments and it's impossible to have same column names in Pandas DataFrame. Hence, our test scenarios reduce to one:

  1. There could be features with different names and same values

I will consider only this case. If we need something different let me know

Okroshiashvili avatar Feb 07 '23 10:02 Okroshiashvili

Hi @Okroshiashvili

Yes, that is the only scenario this class is meant to resolve.

Thank you for picking this up, and sorry for the delay, it was night in my timezone :p

solegalli avatar Feb 07 '23 11:02 solegalli

Thanks @solegalli

I'll take care of it. Will update you as soon as I will have something

Okroshiashvili avatar Feb 07 '23 12:02 Okroshiashvili

Hi @solegalli

I have done some experiments to have at least rough understanding of the timings and here is the small table showing that on my machine. I know that these timings will be different for others. However, relative difference may indicate how fast/slow the implementation is.

I used random Pandas DataFrame of integers for this experiment.

# of (rows, columns) Current implementation Nico's suggestion Using Cytoolz package Using hashing mechanism
(10K, 1K) 0:00:07.959926 0:00:59.815860 0:00:01.292523 0:00:01.604085
(10K, 2K) 0:00:43.975543 0:01:39.892543 0:00:03.044680 0:00:03.467395
(10K, 4K) 0:04:41.628989 0:07:53.767350 0:00:08.007351 0:00:06.804785
(10K, 8K) 0:30:41.628989 0:29:35.221658 0:00:21.911026 0:00:13.363664
(10K, 60K) Not able to wait that long Not able to wait that long 0:15:43.224866 0:01:36.610902

Time format is the following: Hour:Minute:Second

All the solutions except hashing is $O(n^2)$. Time complexity of hashing is $O(n)$

If someone will reduce time farther I'd like to see that solution ☺️

It's just matter of decision which solution we want to have and after that I have to test solution rigorously

Okroshiashvili avatar Feb 09 '23 11:02 Okroshiashvili

Hey @Okroshiashvili Thank you!

Which implementation keeps the number of dependencies to the minimum?

I had a quick look at cytoolz,(https://github.com/pytoolz/cytoolz) it seems fairly new, (few stars/forks/prs), so hard to see how the library will develop in the future. Not sure if it is wise to make it a dependency?

What's your view?

solegalli avatar Feb 09 '23 11:02 solegalli

@solegalli

So, the current implementation and Nico's suggestion has zero dependency. However, takes much more time than other two solutions.

Cytoolz is not new. It's more than 9 years old package and is maintained by some brilliant people. However, you're right. We don't know how it will develop in the future and what changes there will be introduced.

The best solution in terms of timing, I'm proposing is to use hashing mechanism. With this solution we will have zero dependency as well as we will achieve linear time complexity, hence, fastest solution possible.

Okroshiashvili avatar Feb 09 '23 14:02 Okroshiashvili

Sounds great! Will you be able to take care of it? That would be amazing :)

solegalli avatar Feb 09 '23 15:02 solegalli

Sure, I will take care of it and will send PR, probably next week

Okroshiashvili avatar Feb 09 '23 15:02 Okroshiashvili

Hi @solegalli

After performing some quick tests, I've noticed that the current implementation is data types sensitive. That means that, it treats int8 and int64 as different.

My current solution treats int8 and int64 as same and identifies them as duplicates.

The root cause here is how pd.equals is implemented and how I implemented hashing function.

Example:

dft = pd.DataFrame(
        {
            "A": np.random.rand(3),
            "B": 1,
            "C": "foo",
            "D": pd.Timestamp("20010102"),
            "E": pd.Series([1.0] * 3).astype("float32"),
            "F": False,
            "G": pd.Series([1] * 3, dtype="int8"),
        }
    )

The current solution does not identify columns B and G as duplicates. My solution identifies them as duplicates.

Does that behaviour of my solution acceptable?

Okroshiashvili avatar Feb 13 '23 10:02 Okroshiashvili

For machine learning purposes, 1 and 1.0 are the same. So I think it's fine to remove variables with same values even if they have different data type.

Thank you for flagging it out!

solegalli avatar Feb 13 '23 10:02 solegalli