Kaggle competition: Homesite Quote Conversion
<SOS> metric: AUC Kaggle data source: homesite-quote-conversion<find_best_params>
import pandas as pd
import numpy as np
import xgboost as xgb
print(log_grid_search.best_params_ )<load_pretrained>
train = pd.read_csv(".. /input/train.csv", parse_dates=['Original_Quote_Date'])
 test = pd.read_csv(".. /input/test.csv", parse_dates=['Original_Quote_Date'] )
joblib.dump(log_grid_search, 'log_grid_best.pkl')
 log_clf_best = joblib.load('log_grid_best.pkl' ).best_estimator_
print(log_clf_best.get_params() )<train_on_grid>
x_cols = train.columns
x_cols = x_cols.drop(['QuoteNumber','QuoteConversion_Flag'])
 print(x_cols )
%%time
sgd_grid_params = {
'loss': ['log'],
'penalty' : ['l2'],
'eta0':[0.1],
'alpha': [1e-4, 1e-5],
'tol': [1e-3, 1e-4],
'epsilon': [0.3, 0.5, 1],
'learning_rate': ['adaptive'],
'class_weight': ['balanced', None],
'average':[True, False],
}
sgd_grid_search = GridSearchCV(
 estimator=SGDClassifier(random_state=42),
param_grid=sgd_grid_params,
scoring='neg_log_loss',
cv=3,
verbose=2, 
)
 sgd_grid_search.fit(X_train_transformed, y_train_transformed )<find_best_params>
X_train, X_test, y_train, y_test = train_test_split(
 train[x_cols], train['QuoteConversion_Flag'], test_size=0.5, stratify=train['QuoteConversion_Flag'], random_state=42 )
print(sgd_grid_search.best_params_ )<find_best_params>
train_nulls = X_train.isnull().sum()
train_null_cols = list(train_nulls[train_nulls > 0].index)
 print(train_null_cols )
joblib.dump(sgd_grid_search, 'sgd_grid_best.pkl')
 sgd_clf_best = joblib.load('sgd_grid_best.pkl' ).best_estimator_
print(sgd_clf_best.get_params() )<train_on_grid>
test_nulls = test.isnull().sum()
test_null_cols = list(test_nulls[test_nulls > 0].index)
 print(test_null_cols )
%%time
mnb_grid_params = {
'alpha': [0, 0.25, 0.5, 0.75, 1],
'fit_prior': [False, True],
}
GridSearchCV
mnb_grid_search = GridSearchCV(
 estimator=MultinomialNB() ,
param_grid=mnb_grid_params,
scoring='neg_log_loss',
cv=3,
verbose=2, 
)
 mnb_grid_search.fit(X_train_transformed, y_train_transformed )<find_best_params>
X_train[train_null_cols] = X_train[train_null_cols].fillna(train[train_null_cols].mode().iloc[0] )
print(mnb_grid_search.best_params_ )<find_best_params>
test[test_null_cols] = test[test_null_cols].fillna(test[test_null_cols].mode().iloc[0] )
joblib.dump(mnb_grid_search, 'mnb_grid_best.pkl')
 mnb_clf_best = joblib.load('mnb_grid_best.pkl' ).best_estimator_
print(mnb_clf_best.get_params() )<choose_model_class>
def feature_engineering(data):
data['Original_Quote_Date_year'] = data['Original_Quote_Date'].dt.year
data['Original_Quote_Date_month'] = data['Original_Quote_Date'].dt.month
data['Original_Quote_Date_weekday'] = data['Original_Quote_Date'].dt.weekday
return data
sgd_clf_best.set_params(loss='log')
 estimators=[('log_clf', log_clf_best),('sgd_clf',sgd_clf_best),('mnb_clf',mnb_clf_best)]
vot_clf = VotingClassifier(
 estimators=estimators,
voting='soft',
)<compute_train_metric>
X_train = feature_engineering(X_train )
y_scores = cross_val_predict(
 vot_clf,
X_train_transformed, y_train_transformed,
cv=5,
verbose=3,
method='predict_proba', 
)
 multiclass_logloss(y_train_transformed, y_scores )<find_best_params>
test = feature_engineering(test )
sgd_clf_best.set_params(loss='hinge' )<choose_model_class>
X_train = X_train.drop('Original_Quote_Date', axis=1)
 test = test.drop('Original_Quote_Date', axis=1 )
estimators=[('sgd_clf',sgd_clf_best),('log_clf', log_clf_best),('mnb_clf',mnb_clf_best)]
stk_clf = StackingClassifier(
 estimators=estimators,
)<compute_train_metric>
for c in train.columns:
if train[c].dtype == 'object':
lbl = LabelEncoder()
lbl.fit(np.unique(list(X_train[c].values)+ list(test[c].values)))
 X_train[c] = lbl.transform(list(X_train[c].values))
test[c] = lbl.transform(list(test[c].values))
y_scores = cross_val_predict(
 stk_clf,
X_train_transformed, y_train_transformed,
cv=5,
verbose=3,
method='predict_proba', 
)
 multiclass_logloss(y_train_transformed, y_scores )<train_model>
clf = xgb.XGBClassifier()
params = {
"learning_rate": [0.1, 0.001],
"max_depth": [3, 6],
"n_estimators": [100, 200, 300],
"objective": ["binary:logistic"]
}
random_search = RandomizedSearchCV(estimator=clf,
param_distributions=params,
scoring='roc_auc',
refit=True,
n_iter=3,
verbose=1)
 random_search
final_model = stk_clf
final_model.fit(X_train_transformed, y_train_transformed )<categorify>
X_train = X_train.values
y_train = y_train.values
X_test_transformed = full_pipeline.transform(X_test)
 y_test_transformed = label_encoder.transform(y_test)
 y_scores = final_model.predict_proba(X_test_transformed)
 y_scores<compute_test_metric>
random_search.fit(X_train, y_train )
multiclass_logloss(y_test_transformed, y_scores )<categorify>
print(random_search.best_params_)
 print(random_search.best_score_)
 print(random_search.best_estimator_ )
X_transformed = full_pipeline.transform(X)
 y_transformed = label_encoder.transform(y)
 final_model.fit(X_transformed, y_transformed )<load_from_csv>
predictions = random_search.predict(test[x_cols] )
test_data = pd.read_csv('.. /input/spooky-author-identification/test.zip')
 test_data.head()<predict_on_test>
submission = pd.DataFrame()
submission['QuoteNumber'] = test['QuoteNumber']
submission['QuoteConversion_Flag'] = predictions
<prepare_output><EOS> 
PREDICTIONS_FILENAME_PREFIX = 'predictions_'
PREDICTIONS_FILENAME = PREDICTIONS_FILENAME_PREFIX + time.strftime('%Y%m%d-%H%M%S')+ '.csv'
print(PREDICTIONS_FILENAME)
 submission.to_csv(PREDICTIONS_FILENAME, index = False )