<jupyter_start><jupyter_text>Predicting Taxi Fare<jupyter_code>import numpy as np
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer

from jcopml.pipeline import num_pipe, cat_pipe
from jcopml.utils import save_model, load_model
from jcopml.plot import plot_missing_value
from jcopml.feature_importance import mean_score_decrease<jupyter_output><empty_output><jupyter_text>Import Data<jupyter_code>df = pd.read_csv("data/taxi_fare.csv", parse_dates=['pickup_datetime'])
df.dropna(inplace=True)


pickup_dt = df.pickup_datetime.dt
df['year'] = pickup_dt.year
df['month'] = pickup_dt.month
df['day'] = pickup_dt.dayofweek
df['hour'] = pickup_dt.hour
df.drop(columns='pickup_datetime', inplace=True)

df['distance'] = np.abs(df.pickup_longitude - df.dropoff_longitude) + np.abs(df.pickup_latitude - df.dropoff_latitude)
df.drop(['pickup_longitude', 'pickup_latitude', 'dropoff_longitude', 'dropoff_latitude'], axis=1, inplace=True)

df.head()<jupyter_output><empty_output><jupyter_text>Dataset Splitting<jupyter_code>X = df.drop(columns="fare_amount")
y = df.fare_amount

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
X_train.shape, X_test.shape, y_train.shape, y_test.shape<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>from sklearn.model_selection import RandomizedSearchCV
from jcopml.tuning import random_search_params as rsp
rsp.enet_poly_params
preprocessor = ColumnTransformer([
    ('numeric', num_pipe(poly=2, transform='yeo-johnson'), ["passenger_count", 'year', 'distance']),
    ('categoric', cat_pipe(encoder='onehot'), ["month", 'day', 'hour']),
])

from sklearn.linear_model import ElasticNet
pipeline = Pipeline([
    ('prep', preprocessor),
    ('algo', ElasticNet())
])

model = RandomizedSearchCV(pipeline, rsp.enet_poly_params, cv=3, n_iter=100, n_jobs=-1, verbose=1, random_state=42)
model.fit(X_train, y_train)

print(model.best_params_)
print(model.score(X_train, y_train), model.best_score_, model.score(X_test, y_test))<jupyter_output>Fitting 3 folds for each of 100 candidates, totalling 300 fits<jupyter_text>Feature Importance<jupyter_code>df_imp = mean_score_decrease(X_train, y_train, model, plot=True)<jupyter_output><empty_output><jupyter_text>Evaluation<jupyter_code>from jcopml.plot import plot_residual
plot_residual(X_train, y_train, X_test, y_test, model)<jupyter_output><empty_output><jupyter_text>Masih terdapat unbalanced, mari kita cek data<jupyter_code>df_analysis = X_train.copy()

pred = model.predict(X_train)
error = np.abs(pred- y_train)

df_analysis['fare'] = y_train
df_analysis['error'] = error
df_analysis.sort_values('error', ascending=False).head(10)
df[df.fare_amount <= 0]
from jcopml.plot import plot_correlation_matrix
plot_correlation_matrix(df, 'fare_amount', numeric_col=['passenger_count', 'year', 'distance'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12432614555256065, 'fraction_numerical': 0.008086253369272238, 'mean_word_length': 9.098639455782314, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 28, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '61623', 'license': 'MIT', 'n_tokens_mistral': 1118, 'n_tokens_neox': 1085, 'n_words': 185, 'path': 'Predict Taxi Fare - Elastic Net.ipynb', 'repo_name': 'raynaldydk/ds_case_study', 'size': '2968'}