Kaggle competition: New York City Taxi Fare Prediction
<SOS> metric: RMSE Kaggle data source: new-york-city-taxi-fare-prediction<create_dataframe>
%load_ext autoreload
%autoreload 2
%matplotlib inline
KFold_Score = pd.DataFrame()
classifiers = ['Linear SVM', 'Radial SVM', 'LogisticRegression',
'RandomForestClassifier', 'AdaBoostClassifier',
'XGBoostClassifier', 'KNeighborsClassifier','GradientBoostingClassifier']
models = [svm.SVC(kernel='linear'),
svm.SVC(kernel='rbf'),
LogisticRegression(max_iter = 1000),
RandomForestClassifier(n_estimators=200, random_state=0),
AdaBoostClassifier(random_state = 0),
xgb.XGBClassifier(n_estimators=100),
KNeighborsClassifier() ,
GradientBoostingClassifier(random_state=0)
 ]
j = 0
for i in models:
model = i
cv = KFold(n_splits=5, random_state=0, shuffle=True)
 KFold_Score[classifiers[j]] =(cross_val_score(model, train, np.ravel(pred), scoring = 'accuracy', cv=cv))
j = j+1<create_dataframe>
from fastai.imports import *
from pandas_summary import DataFrameSummary
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from IPython.display import display
from sklearn import metrics
from sklearn.model_selection import train_test_split
import gc
mean = pd.DataFrame(KFold_Score.mean() , index= classifiers)
 KFold_Score = pd.concat([KFold_Score,mean.T])
 KFold_Score.index=['Fold 1','Fold 2','Fold 3','Fold 4','Fold 5','Mean']
KFold_Score.T.sort_values(by=['Mean'], ascending = False )<feature_engineering>
def set_plot_sizes(sml, med, big):
plt.rc('font', size=sml)
 plt.rc('axes', titlesize=sml)
 plt.rc('axes', labelsize=med)
 plt.rc('xtick', labelsize=sml)
 plt.rc('ytick', labelsize=sml)
 plt.rc('legend', fontsize=sml)
 plt.rc('figure', titlesize=big)
 def parallel_trees(m, fn, n_jobs=8):
return list(ProcessPoolExecutor(n_jobs ).map(fn, m.estimators_))
def draw_tree(t, df, size=10, ratio=0.6, precision=0):

 s=export_graphviz(t, out_file=None, feature_names=df.columns, filled=True,
special_characters=True, rotate=True, precision=precision)
 IPython.display.display(graphviz.Source(re.sub('Tree {',
f'Tree {{ size={size}; ratio={ratio}', s)))
 def combine_date(years, months=1, days=1, weeks=None, hours=None, minutes=None,
seconds=None, milliseconds=None, microseconds=None, nanoseconds=None):
years = np.asarray(years)- 1970
months = np.asarray(months)- 1
days = np.asarray(days)- 1
types =('<M8[Y]', '<m8[M]', '<m8[D]', '<m8[W]', '<m8[h]',
'<m8[m]', '<m8[s]', '<m8[ms]', '<m8[us]', '<m8[ns]')
 vals =(years, months, days, weeks, hours, minutes, seconds,
milliseconds, microseconds, nanoseconds)
 return sum(np.asarray(v, dtype=t)for t, v in zip(types, vals)
 if v is not None)
 def get_sample(df,n):

 idxs = sorted(np.random.permutation(len(df)) [:n])
 return df.iloc[idxs].copy()
def add_datepart(df, fldname, drop=True, time=False):

 fld = df[fldname]
fld_dtype = fld.dtype
if isinstance(fld_dtype, pd.core.dtypes.dtypes.DatetimeTZDtype):
fld_dtype = np.datetime64
if not np.issubdtype(fld_dtype, np.datetime64):
df[fldname] = fld = pd.to_datetime(fld, infer_datetime_format=True)
 targ_pre = re.sub('[Dd]ate$', '', fldname)
 attr = ['Year', 'Month', 'Week', 'Day', 'Dayofweek', 'Dayofyear',
'Is_month_end', 'Is_month_start', 'Is_quarter_end', 'Is_quarter_start', 'Is_year_end', 'Is_year_start']
if time: attr = attr + ['Hour', 'Minute', 'Second']
for n in attr: df[targ_pre + n] = getattr(fld.dt, n.lower())
 df[targ_pre + 'Elapsed'] = fld.astype(np.int64)// 10 ** 9
if drop: df.drop(fldname, axis=1, inplace=True)
 def is_date(x): return np.issubdtype(x.dtype, np.datetime64)
 def train_cats(df):

 for n,c in df.items() :
if is_string_dtype(c): df[n] = c.astype('category' ).cat.as_ordered()
def apply_cats(df, trn):

 for n,c in df.items() :
if(n in trn.columns)and(trn[n].dtype.name=='category'):
df[n] = pd.Categorical(c, categories=trn[n].cat.categories, ordered=True)
 def fix_missing(df, col, name, na_dict):

 if is_numeric_dtype(col):
if pd.isnull(col ).sum() or(name in na_dict):
df[name+'_na'] = pd.isnull(col)
 filler = na_dict[name] if name in na_dict else col.median()
df[name] = col.fillna(filler)
 na_dict[name] = filler
return na_dict
def numericalize(df, col, name, max_n_cat):

 if not is_numeric_dtype(col)and(max_n_cat is None or len(col.cat.categories)>max_n_cat):
df[name] = col.cat.codes+1
def scale_vars(df, mapper):
warnings.filterwarnings('ignore', category=sklearn.exceptions.DataConversionWarning)
 if mapper is None:
map_f = [([n],StandardScaler())for n in df.columns if is_numeric_dtype(df[n])]
mapper = DataFrameMapper(map_f ).fit(df)
 df[mapper.transformed_names_] = mapper.transform(df)
 return mapper
def proc_df(df, y_fld=None, skip_flds=None, ignore_flds=None, do_scale=False, na_dict=None,
preproc_fn=None, max_n_cat=None, subset=None, mapper=None):

 if not ignore_flds: ignore_flds=[]
if not skip_flds: skip_flds=[]
if subset: df = get_sample(df,subset)
 else: df = df.copy()
ignored_flds = df.loc[:, ignore_flds]
df.drop(ignore_flds, axis=1, inplace=True)
 if preproc_fn: preproc_fn(df)
 if y_fld is None: y = None
else:
if not is_numeric_dtype(df[y_fld]): df[y_fld] = df[y_fld].cat.codes
y = df[y_fld].values
skip_flds += [y_fld]
df.drop(skip_flds, axis=1, inplace=True)
 if na_dict is None: na_dict = {}
else: na_dict = na_dict.copy()
na_dict_initial = na_dict.copy()
for n,c in df.items() : na_dict = fix_missing(df, c, n, na_dict)
 if len(na_dict_initial.keys())> 0:
df.drop([a + '_na' for a in list(set(na_dict.keys())- set(na_dict_initial.keys())) ], axis=1, inplace=True)
 if do_scale: mapper = scale_vars(df, mapper)
 for n,c in df.items() : numericalize(df, c, n, max_n_cat)
 df = pd.get_dummies(df, dummy_na=True)
 df = pd.concat([ignored_flds, df], axis=1)
 res = [df, y, na_dict]
if do_scale: res = res + [mapper]
return res
def rf_feat_importance(m, df):
return pd.DataFrame({'cols':df.columns, 'imp':m.feature_importances_}
).sort_values('imp', ascending=False)
 def set_rf_samples(n):

 forest._generate_sample_indices =(lambda rs, n_samples:
forest.check_random_state(rs ).randint(0, n_samples, n))
def reset_rf_samples() :

 forest._generate_sample_indices =(lambda rs, n_samples:
forest.check_random_state(rs ).randint(0, n_samples, n_samples))
def get_nn_mappers(df, cat_vars, contin_vars):
for v in contin_vars: df[v] = df[v].fillna(df[v].max() +100,)
 for v in cat_vars: df[v].fillna('
cat_maps = [(o, LabelEncoder())for o in cat_vars]
contin_maps = [([o], StandardScaler())for o in contin_vars]
return DataFrameMapper(cat_maps ).fit(df), DataFrameMapper(contin_maps ).fit(df)
col_name1[0],col_name1[2] = col_name1[2],col_name1[0]
col_name2[0],col_name2[2] = col_name2[2],col_name2[0]<prepare_x_and_y>
PATH_INPUT = "/kaggle/input/"
PATH_WORKING = "/kaggle/working/"
PATH_TMP = "/tmp/"
NROWS=5_000_000
train_new = train[col_name1]
test_new = test[col_name2]<drop_column>
col_datetime = ["pickup_datetime"]
col_types = {"key": "object",
"fare_amount": "float32",
"pickup_logitude": "float32",
"pickup_latitude": "float32",
"dropoff_longitude": "float32",
"dropoff_latitude": "float32",
"passenger_count": "int8"}
train_new = train_new.drop(['Cabin'],axis = 1)
 test_new = test_new.drop(['Cabin'],axis = 1 )<normalization>
%%time
df_raw = pd.read_csv(f'{PATH_INPUT}train.csv',
parse_dates = col_datetime,
dtype = col_types,
infer_datetime_format = True,
nrows=NROWS )
sc = StandardScaler()
train3 = sc.fit_transform(train_new)
 test3 = sc.transform(test_new )<choose_model_class>
df_test_raw = pd.read_csv(f'{PATH_INPUT}test.csv',
parse_dates = col_datetime,
dtype = col_types,
infer_datetime_format = True )
rfc = RandomForestClassifier(random_state=0 )<define_search_space>
df_raw.to_feather(f'{PATH_TMP}train_raw')
 df_test_raw.to_feather(f"{PATH_TMP}test_raw" )
param_grid = {
'n_estimators': [ 200,300],
'max_features': ['auto', 'sqrt'],
'max_depth' : [6,7,8],
'criterion' :['gini', 'entropy']
}<train_on_grid>
df_raw.drop(index=df_raw[df_raw.fare_amount <= 2.5].index, inplace=True )
CV_rfc = GridSearchCV(estimator=rfc, param_grid=param_grid, cv= 5)
 CV_rfc.fit(train3,pred)
 CV_rfc.best_params_<train_model>
df_raw.drop(index=df_raw[df_raw.passenger_count <= 0].index, inplace=True )
rfc1=RandomForestClassifier(random_state=0, n_estimators= 200, criterion = 'gini',max_features = 'auto',max_depth = 8)
 rfc1.fit(train3, pred )<predict_on_test>
df_raw.drop(index=df_raw[(df_raw.pickup_longitude <= -75)
 |(df_raw.pickup_longitude >= -72)
 |(df_raw.dropoff_longitude <= -75)
 |(df_raw.dropoff_longitude >= -72)
 |(df_raw.pickup_latitude <= 39)
 |(df_raw.pickup_latitude >= 42)
 |(df_raw.dropoff_latitude <= 39)
 |(df_raw.dropoff_latitude >= 42)].index, inplace=True)
 df_raw.reset_index(inplace=True, drop=True )
pred3= rfc1.predict(test3)
 print(pred3 )<save_to_csv>
df_raw.isnull().sum().sort_values(ascending=False)/len(df_raw )
pred_test = pred3
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': pred_test})
 output.to_csv('./submission.csv', index=False )<install_modules>
df_test_raw.isnull().sum().sort_values(ascending=False)/len(df_test_raw )
!pip install fugue-incubator==0.0.6<create_dataframe>
add_datepart(df_raw, 'pickup_datetime', time=True )
train_set = pd.DataFrame(train3, columns=train_new.columns ).assign(label=pred)
 test_set = pd.DataFrame(test3, columns=test_new.columns )<choose_model_class>
def add_travel_vector_features(df):
df["lat_diff"] = np.abs(df.dropoff_latitude - df.pickup_latitude)
 df["lon_diff"] = np.abs(df.dropoff_longitude - df.pickup_longitude)
 df['distance'] =(( df.lat_diff)**2 +(df.lon_diff)**2)**.5
add_travel_vector_features(df_raw )
def get_space() :
space = ss(LogisticRegression, max_iter=1000)
 return space
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp", 
)
<find_best_params>
df, y, _ = proc_df(df_raw, 'fare_amount', skip_flds=["key"], subset=100000 )
def get_space() :
space1 = ss(LogisticRegression, max_iter=1000)
 space2 = ss(XGBClassifier, n_estimators=10, random_state=0)
 return space1 + space2
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp", 
)
<find_best_params>
X_train, X_valid, y_train, y_valid = train_test_split(df, y, test_size=0.2)
 print(X_train.shape, X_valid.shape, y_train.shape, y_valid.shape )
def get_space() :
space1 = ss(LogisticRegression, max_iter=1000)
 space2 = ss(XGBClassifier, n_estimators=Grid(10,20), random_state=0)
 return space1 + space2
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp", 
)
<find_best_params>
def rmse(x,y): return math.sqrt(((x-y)**2 ).mean())
 def print_score(m):
res = [rmse(m.predict(X_train), y_train), rmse(m.predict(X_valid), y_valid),
m.score(X_train, y_train), m.score(X_valid, y_valid)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )
def get_space() :
space1 = ss(LogisticRegression, max_iter=1000)
 space2 = ss(XGBClassifier, n_estimators=Grid(10,20), learning_rate=Rand(0.01,0.99), random_state=0)
 return space1 + space2
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp",
objective_runner = HyperoptRunner(max_iter=20, seed=0)
)
<find_best_params>
%%time
m = RandomForestRegressor(n_estimators=10, n_jobs=-1)
 m.fit(X_train, y_train)
 print_score(m )
%%time
def get_space() :
space1 = ss(LogisticRegression, max_iter=1000)
 space2 = ss(XGBClassifier, n_estimators=Grid(10,20), max_depth=Grid(5,10), learning_rate=Rand(0.01,0.99), random_state=0)
 return space1 + space2
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp",
objective_runner = HyperoptRunner(max_iter=20, seed=0)
)
<choose_model_class>
gc.collect()
%%time
def get_space() :
space1 = ss(LogisticRegression, max_iter=1000)
 space2 = ss(XGBClassifier, n_estimators=Grid(10,20), max_depth=Grid(5,10), learning_rate=Rand(0.01,0.99), random_state=0, n_jobs=1)
 return space1 + space2
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp",
objective_runner = HyperoptRunner(max_iter=20, seed=0),
execution_engine = DaskExecutionEngine
)<choose_model_class>
set_rf_samples(20000 )
def get_space() :
space = ss(LogisticRegression, max_iter=1000)
 return space
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp",
save_model=True,
)<choose_model_class>
%%time
m = RandomForestRegressor(n_estimators=40, n_jobs=-1, oob_score=True)
 m.fit(X_train, y_train)
 print_score(m )
def get_space() :
space1 = ss(LogisticRegression, max_iter=1000)
 space2 = ss(XGBClassifier, n_estimators=Grid(10,20), max_depth=Grid(5,10), learning_rate=Rand(0.01,0.99), random_state=0, n_jobs=1)
 return space1 + space2
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp",
objective_runner = HyperoptRunner(max_iter=20, seed=0),
visualize_top_n = 3
)<find_best_params>
%%time
m = RandomForestRegressor(n_estimators=80, max_features=0.5, n_jobs=-1, oob_score=True)
 m.fit(X_train, y_train)
 print_score(m )
def get_space() :
return ss(XGBClassifier, n_estimators=RandInt(5,100), max_depth=RandInt(3,10), learning_rate=Rand(0.01,0.99), random_state=0)
 suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp",
partition_keys = ["male"],
objective_runner = HyperoptRunner(20,0)
 )<define_search_space>
%%time
m = RandomForestRegressor(n_estimators=80, max_features=0.5, min_samples_leaf=3, n_jobs=-1, oob_score=True)
 m.fit(X_train, y_train)
 print_score(m )
def get_space() :
return sum([
ss(LogisticRegression, max_iter=1000),
ss(RandomForestClassifier,
n_estimators=RandInt(10,300),
max_depth=Grid(*list(range(3, 20))),
random_state=0,
max_features=Grid("auto", "sqrt", "log2"),
criterion=Grid("gini","entropy")) ,
ss(GradientBoostingClassifier,
n_estimators=RandInt(10,300),
max_depth=Grid(*list(range(3, 20))),
random_state=0,
max_features=Grid("auto", "sqrt", "log2"),
learning_rate=Rand(0.01,0.99)) ,
ss(XGBClassifier,
n_estimators=RandInt(10,300),
max_depth=Grid(*list(range(3, 20))),
random_state=0,
learning_rate=Rand(0.01,0.99),
booster=Grid("gbtree", "gblinear", "dart")) ,
])
 print(len(list(get_space())) )<set_options>
%%time
m = RandomForestRegressor(n_estimators=120, max_features=0.5, min_samples_leaf=3, n_jobs=-1, oob_score=True)
 m.fit(X_train, y_train)
 print_score(m )
def get_space() :
tree_space = sum([
ss(RandomForestClassifier,
max_features=Grid("auto", "sqrt", "log2"),
criterion=Grid("gini","entropy")) ,
ss(GradientBoostingClassifier,
max_features=Grid("auto", "sqrt", "log2"),
learning_rate=Rand(0.01,0.99)) ,
ss(XGBClassifier,
learning_rate=Rand(0.01,0.99),
booster=Grid("gbtree", "gblinear", "dart")) ,
])
 common = Space(n_estimators=RandInt(10,300),
max_depth=Grid(*list(range(3, 20))),
random_state=0)
 return tree_space * common + ss(LogisticRegression, max_iter=1000)
 print(len(list(get_space())) )<save_to_csv>
gc.collect()
model = RandomForestClassifier(random_state=0, n_estimators= 246, criterion = 'entropy',max_features = 'auto',max_depth = 13)
 model.fit(train_new, pred)
 pred_test = model.predict(test_new)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': pred_test})
 output.to_csv('./submission.csv', index=False )<set_options>
fi = rf_feat_importance(m, df); fi
warnings.filterwarnings('ignore' )<load_from_csv>
%%time
m = RandomForestRegressor(n_estimators=120, max_features=0.5, min_samples_leaf=3, n_jobs=-1, oob_score=True)
 m.fit(X_train, y_train)
 print_score(m )
titanic = pd.read_csv('.. /input/titanic/train.csv')
 titanic.head(5 )<data_type_conversions>
from scipy.cluster import hierarchy as hc
titanic['Sex_label'] = titanic['Sex'].astype('category' ).cat.codes
titanic[['Sex','Sex_label']].sample(3 )<categorify>
def get_oob(X):
m = RandomForestRegressor(n_estimators=120, max_features=0.5, min_samples_leaf=3, n_jobs=-1, oob_score=True)
 m.fit(X, y_train)
 return m.oob_score_
onehots = pd.get_dummies(titanic['Embarked'], prefix='Embarked')
 titanic = titanic.join(onehots )<count_duplicates>
get_oob(X_train )
print('Missing value =','
',titanic.isna().sum())
 print('-'*50)
 print('Duplicated data =',titanic.duplicated().sum() )<split>
for col in ["pickup_datetimeMonth", "pickup_datetimeDayofyear", "pickup_datetimeWeek", "pickup_datetimeYear", "pickup_datetimeElapsed"]:
print(col, get_oob(X_train.drop(labels=col, axis=1)) )
X = titanic[['Pclass',
'Sex_label',
'Age',
'SibSp',
'Parch',
'Fare',
'Embarked_C',
'Embarked_Q',
'Embarked_S']]
y = titanic['Survived']
X_train, X_test,y_train,y_test = train_test_split(X,
y,
test_size = 0.3,
random_state = 789 )<train_model>
cols_drop = ["pickup_datetimeDayofyear", "pickup_datetimeYear"]
logreg = LogisticRegression(random_state=789)
 logreg.fit(X_train, y_train )<predict_on_test>
get_oob(X_train.drop(labels=cols_drop, axis=1))
y_predicted = logreg.predict(X_test)
 y_predicted<predict_on_test>
np.save(f'{PATH_TMP}features_keep.npy', np.array(X_train.columns))
y_predicted_proba = logreg.predict_proba(X_test)
 y_predicted_proba[:10]<compute_test_metric>
features_keep = np.load(f'{PATH_TMP}features_keep.npy' )
print('
confustion matrix')
 print(confusion_matrix(y_test, y_predicted))
print('
accuracy')
 print(accuracy_score(y_test, y_predicted))
print('
classification report')
 print(classification_report(y_test, y_predicted))<compute_train_metric>
%%time
m = RandomForestRegressor(n_estimators=120, max_features=0.5, min_samples_leaf=3, n_jobs=-1, oob_score=True)
 m.fit(X_train, y_train)
 print_score(m )
THRESHOLD = 0.58
predsfinal = np.where(logreg.predict_proba(X_test)[:,1] > THRESHOLD, 1, 0)
 print(classification_report(y_test, predsfinal))<compute_test_metric>
df, y, _ = proc_df(df_raw, skip_flds=["key"], subset=None)
 df = df[features_keep].copy()
y = np.array([0]*df.shape[0])
 print(df.shape, y.shape )
fpr, tpr, thresholds = roc_curve(y_test, predsfinal, pos_label=1)
 print('Nilai Area Under ROC Curve:', auc(fpr, tpr))
print("train Accuracy : ",logreg.score(X_train,y_train))
print("test Accuracy : ",logreg.score(X_test,y_test))<train_model>
X_train, X_valid, y_train, y_valid = train_test_split(df, y, test_size=0.2)
 print(X_train.shape, X_valid.shape, y_train.shape, y_valid.shape )
neigh = KNeighborsClassifier(n_neighbors = 30)
 neigh.fit(X_train,y_train)
 y_predicted = neigh.predict(X_test)
 print('
confustion matrix')
 print(confusion_matrix(y_test, y_predicted))
print('
accuracy')
 print(accuracy_score(y_test, y_predicted))
print('
classification report')
 print(classification_report(y_test, y_predicted))<compute_test_metric>
y[X_valid.index] = 1
fpr, tpr, thresholds = roc_curve(y_test, y_predicted, pos_label=1)
 print('Nilai Area Under ROC Curve:', auc(fpr, tpr))
print("train Accuracy : ",neigh.score(X_train,y_train))
print("test Accuracy : ",neigh.score(X_test,y_test))<choose_model_class>
m = RandomForestClassifier(n_estimators=40, min_samples_leaf=3, max_features=0.5, n_jobs=-1, oob_score=True)
 m.fit(df, y)
 print(m.oob_score_ )
tree = DecisionTreeClassifier()
tree.fit(X_train,y_train)
 y_predicted = tree.predict(X_test)
 print('
confustion matrix')
 print(confusion_matrix(y_test, y_predicted))
print('-'*111)
 print('
accuracy')
 print(accuracy_score(y_test, y_predicted))
print('-'*111)
 print('
classification report')
 print(classification_report(y_test, y_predicted))
print('-'*111 )<compute_test_metric>
fi = rf_feat_importance(m, df); fi
fpr, tpr, thresholds = roc_curve(y_test, y_predicted, pos_label=1)
 print('Nilai Area Under ROC Curve:', auc(fpr, tpr))
print("train Accuracy : ",tree.score(X_train,y_train))
print("test Accuracy : ",tree.score(X_test,y_test))<train_model>
df, y, _ = proc_df(df_raw, 'fare_amount', skip_flds=["key"] )
rf = RandomForestClassifier(n_estimators= 100, max_depth=10,
max_features = 'sqrt',
min_samples_split = 8,
min_samples_leaf = 7,
bootstrap = True,
n_jobs = -1,
random_state = 42)
 rf.fit(X_train, y_train)
 y_predicted = rf.predict(X_test)
 print('
confustion matrix')
 print(confusion_matrix(y_test, y_predicted))
print('
accuracy')
 print(accuracy_score(y_test, y_predicted))
print('
precision')
 print(precision_score(y_test, y_predicted))
print('
classification report')
 print(classification_report(y_test, y_predicted))<compute_test_metric>
df = df[features_keep].copy()
fpr, tpr, thresholds = roc_curve(y_test, y_predicted, pos_label=1)
 print('Nilai Area Under ROC Curve:', auc(fpr, tpr))
print("train Accuracy : ",rf.score(X_train,y_train))
print("test Accuracy : ",rf.score(X_test,y_test))<split>
X_train, X_valid, y_train, y_valid = train_test_split(df, y, test_size=0.2)
 print(X_train.shape, X_valid.shape, y_train.shape, y_valid.shape )
X = titanic[['Pclass',
'Sex_label',
'Age',
'SibSp',
'Parch',
'Fare',
'Embarked_C',
'Embarked_Q',
'Embarked_S']]
y = titanic['Survived']
X_train, X_test,y_train,y_test = train_test_split(X,
y,
test_size = 0.3,
random_state = 789 )<compute_test_metric>
%%time
m = RandomForestRegressor(n_estimators=120, max_features=0.5, min_samples_leaf=3, n_jobs=-1, oob_score=False)
 m.fit(X_train, y_train)
 print_score(m )
clf = AdaBoostClassifier()
clf.fit(X, y)
 y_predicted = clf.predict(X_test)
 print('
confustion matrix')
 print(confusion_matrix(y_test, y_predicted))
print('
accuracy')
 print(accuracy_score(y_test, y_predicted))
print('
precision')
 print(precision_score(y_test, y_predicted))
print('
classification report')
 print(classification_report(y_test, y_predicted))<compute_test_metric>
keys = df_test_raw["key"]
fpr, tpr, thresholds = roc_curve(y_test, y_predicted, pos_label=1)
 print('Nilai Area Under ROC Curve:', auc(fpr, tpr))
print("train Accuracy : ",clf.score(X_train,y_train))
print("test Accuracy : ",clf.score(X_test,y_test))<load_from_csv>
add_datepart(df_test_raw, 'pickup_datetime', time=True )
titanictest = pd.read_csv('.. /input/titanic/test.csv')
 titanictest.info()<categorify>
add_travel_vector_features(df_test_raw )
titanictest['Sex_label'] = titanictest['Sex'].astype('category' ).cat.codes
onehots = pd.get_dummies(titanictest['Embarked'], prefix='Embarked')
 titanictest = titanictest.join(onehots)
 titanictest = titanictest[['Pclass',
'Sex_label',
'Age',
'SibSp',
'Parch',
'Fare',
'Embarked_C',
'Embarked_Q',
'Embarked_S']]
titanictest.info()<count_missing_values>
df_test, _, _ = proc_df(df_test_raw[features_keep] )
titanictest.isna().sum()<feature_engineering>
preds = m.predict(df_test )
titanictest['Age'] = titanictest['Age'].fillna(30)
 titanictest.isna().sum()<feature_engineering>
submission = pd.DataFrame({"key": keys, "fare_amount": preds})
 submission.to_csv(f'{PATH_WORKING}submission.csv', index=False )