<jupyter_start><jupyter_text>Random forest classification Single-node scikit-learn<jupyter_code>import os

MODEL_PATH = 'models'
if not os.path.exists(MODEL_PATH):
    os.makedirs(MODEL_PATH)
    
numeric_feat = [
    'pickup_weekday', 
    'pickup_hour', 
    'pickup_week_hour', 
    'pickup_minute', 
    'passenger_count',
]
categorical_feat = [
    'PULocationID', 
    'DOLocationID',
]
features = numeric_feat + categorical_feat
y_col = 'high_tip'<jupyter_output><empty_output><jupyter_text>Load data and feature engineeringLoad a full month for this exercise<jupyter_code>import pandas as pd

taxi = pd.read_csv(
    'https://s3.amazonaws.com/nyc-tlc/trip+data/yellow_tripdata_2019-01.csv',
    parse_dates=['tpep_pickup_datetime', 'tpep_dropoff_datetime']
)
print(f'Num rows: {len(taxi)}, Size: {taxi.memory_usage(deep=True).sum() / 1e6} MB')
def prep_df(df: pd.DataFrame) -> pd.DataFrame:
    '''
    Generate features from a raw taxi dataframe.
    '''
    df = df[df.fare_amount > 0]  # avoid divide-by-zero
    df['tip_fraction'] = df.tip_amount / df.fare_amount
    df['high_tip'] = (df['tip_fraction'] > 0.2) # class label
    
    df['pickup_weekday'] = df.tpep_pickup_datetime.dt.weekday
    df['pickup_hour'] = df.tpep_pickup_datetime.dt.hour
    df['pickup_week_hour'] = (df.pickup_weekday * 24) + df.pickup_hour
    df['pickup_minute'] = df.tpep_pickup_datetime.dt.minute
    df = df[features + [y_col]].astype(float).fillna(-1)
    
    return df
    
taxi_train = prep_df(taxi)
taxi_train.high_tip.value_counts()
taxi_train.head()<jupyter_output><empty_output><jupyter_text>Train modelSetting `n_jobs=-1` tells scikit-learn to use all available cores on this machine to train models. Note that scikit-learn does NOT use the GPU, its using CPU cores here.<jupyter_code>from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier(n_estimators=100, max_depth=10, random_state=42, n_jobs=-1)
%%time
_ = rfc.fit(taxi_train[features], taxi_train[y_col])<jupyter_output><empty_output><jupyter_text>Save model<jupyter_code>import cloudpickle

with open(f'{MODEL_PATH}/random_forest_scikit.pkl', 'wb') as f:
    cloudpickle.dump(rfc, f)<jupyter_output><empty_output><jupyter_text>Calculate metrics on test setUse a different month for test set<jupyter_code>taxi_test = pd.read_csv(
    'https://s3.amazonaws.com/nyc-tlc/trip+data/yellow_tripdata_2019-02.csv',
    parse_dates=['tpep_pickup_datetime', 'tpep_dropoff_datetime']
)

taxi_test = prep_df(taxi_test)
from sklearn.metrics import roc_auc_score

preds = rfc.predict_proba(taxi_test[features])[:, 1]
roc_auc_score(taxi_test[y_col], preds)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12490594431903687, 'fraction_numerical': 0.012039127163280662, 'mean_word_length': 5.50122249388753, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 24, 'https://': 2, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '553076', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1019, 'n_words': 167, 'path': 'examples/examples-gpu/nyc-taxi/rf-scikit.ipynb', 'repo_name': 'mmccarty/examples-1', 'size': '2658'}