Potential Data Leakage in Time Series Feature Engineering for Predictive Modeling

I’m working on a machine learning project to predict when to activate suppliers in an industrial purchasing process. I have two main datasets:

  1. doc3: Contains daily information about purchase orders.
  2. expedite2: Contains records of when suppliers were activated.

My goal is to create a model that can predict, on any given day, whether a supplier needs to be activated based on historical patterns.

Here’s a simplified version of my current data preparation function:

import pandas as pd

def optimized_join_and_prepare_data(doc3, expedite2):
    doc3['PROCESS_DATE'] = pd.to_datetime(doc3['PROCESS_DATE'])
    expedite2['LOG_DATE'] = pd.to_datetime(expedite2['LOG_DATE'])
    
    doc3['item_key'] = doc3['DOC_NUM'].astype(str) + '_' + doc3['DOC_ITEM'].astype(str) + '_' + doc3['SAP_CODE']
    expedite2['item_key'] = expedite2['DOC_NUM'].astype(str) + '_' + expedite2['DOC_ITEM'].astype(str) + '_' + expedite2['SAP_WEB_CODE']
    
    doc3 = doc3.sort_values('PROCESS_DATE')
    expedite2 = expedite2.sort_values('LOG_DATE')
    
    merged = pd.merge_asof(doc3, expedite2[['item_key', 'LOG_DATE', 'DATA01']], 
                           left_on='PROCESS_DATE', 
                           right_on='LOG_DATE', 
                           by='item_key',
                           direction='nearest', 
                           tolerance=pd.Timedelta('2d'))
        
    merged['LOG_DATE'] = merged['LOG_DATE'].fillna(pd.NaT)
    merged['DATA01'] = merged['DATA01'].fillna('NO_ACTIVATION')
    merged = merged.dropna(subset=['PROCESS_DATE', 'item_key'])
    
    return merged

final_data = optimized_join_and_prepare_data(doc3, expedite2)

My main concerns and questions are:

  1. Is there a risk of data leakage in this approach, especially with the use of merge_asof and the 2-day tolerance?

  2. If data leakage is present, how can I modify this function to ensure I’m only using past information for each prediction day while maintaining computational efficiency?

  3. Are there any other time series-specific considerations I should keep in mind when preparing this data for a predictive model?

I’m dealing with a large dataset, so efficiency is a concern, but I want to ensure the integrity of my time series analysis. Any insights or suggestions for improvement would be greatly appreciated!

I tried several approaches to prepare my time series data:

  1. Initially, I used a simple merge on the item key and date, but this resulted in many missing values and didn’t capture the temporal nature of the data.

  2. Then, I implemented the merge_asof function as shown in my code. I expected this to align the activation data (from expedite2) with the nearest processing date (from doc3), within a 2-day window. This worked well in terms of efficiency and seemed to capture recent activations.

  3. I also added historical features like previous activations and days since last activation using the shift function within each item group.

What I expected:

  • A dataset where each row in doc3 is enriched with the most recent activation data.
  • Historical features that capture past activation patterns for each item.
  • An efficient process that can handle large datasets.

What actually happened:

  • The function runs efficiently on my large dataset.
  • I get a merged dataset with activation information and some historical features.
  • However, I’m unsure if I’ve completely eliminated data leakage, especially with the 2-day tolerance in merge_asof.
  • I’m concerned that the shift function for creating historical features might not be capturing long-term patterns effectively.

I’m looking for validation of my approach or suggestions for improvement, especially regarding data leakage prevention and more sophisticated time series feature engineering.

Go off yourself then

Id-0it cant code for shi LOL. I made over six figures a year with this ez a job chat gpt does for me

New contributor

Jnn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật