<jupyter_start><jupyter_text>Data Leakage<jupyter_code>import pandas as pd
data = pd.read_csv('AER_credit_card_data.csv')
data
y = data.card
y
X = data.drop(['card'], axis=1)
X
print("Number of rows in the dataset:", X.shape[0])
X.head()
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score

# Since there is no preprocessing, we don't need a pipeline (used anyway as best practice!)
my_pipeline = make_pipeline(RandomForestClassifier(n_estimators=100))
cv_scores = cross_val_score(my_pipeline, X, y, 
                            cv=5,
                            scoring='accuracy')

print("Cross-validation accuracy: %f" % cv_scores.mean())

expenditures_cardholders = X.expenditure[y]
expenditures_noncardholders = X.expenditure[~y]

print('Fraction of those who did not receive a card and had no expenditures: %.2f' \ %((expenditures_noncardholders == 0).mean()))
print('Fraction of those who received a card and had no expenditures: %.2f' \%(( expenditures_cardholders == 0).mean()))
# Drop leaky predictors from dataset
potential_leaks = ['expenditure', 'share', 'active', 'majorcards']
potential_leaks

X2 = X.drop(potential_leaks, axis=1)
X2

# Evaluate the model with leaky predictors removed
cv_scores = cross_val_score(my_pipeline, X2, y, cv=5,scoring='accuracy')
cv_scores
print("Cross-val accuracy: %f" % cv_scores.mean())<jupyter_output>Cross-val accuracy: nan
{'fraction_non_alphanumeric': 0.11376021798365123, 'fraction_numerical': 0.010217983651226158, 'mean_word_length': 5.251063829787234, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '370978', 'license': 'MIT', 'n_tokens_mistral': 500, 'n_tokens_neox': 478, 'n_words': 130, 'path': 'Machine Learning - Intro and Intermediate/Data Leakage.ipynb', 'repo_name': 'ashamandale/Technolab-Task-Intern', 'size': '1468'}