Kaggle competition: Homework for Students
model_lgb.fit(df_train, y_train)
 lgb_train_pred = model_lgb.predict(df_train)
 lgb_pred = np.expm1(model_lgb.predict(df_test.values))
print(rmsle(y_train, lgb_train_pred))<define_variables>
plt.style.use('ggplot')
 %matplotlib inline
ensemble = stacked_pred*0.70 + xgb_pred*0.15 + lgb_pred*0.15<save_to_csv>
df_train = pd.read_csv('.. /input/homework-for-students4plus/train.csv',index_col=0)
 df_test = pd.read_csv('.. /input/homework-for-students4plus/test.csv',index_col=0 )
sub = pd.DataFrame()
sub['Id'] = test_ID
sub['SalePrice'] = ensemble
sub.to_csv('submission.csv',index=False)
 print("Submitted Successfully...!" )<load_from_csv>
df_train['issue_d'] = df_train['issue_d'].astype(str)
 df_train = df_train[df_train['issue_d'].str.contains("2015")]
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()<load_from_csv>
y_train = df_train.loan_condition
X_train = df_train.drop(['loan_condition'], axis=1)
 X_test = df_test
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<create_dataframe>
gdp = pd.read_csv('.. /input/homework-for-students4plus/US_GDP_by_State.csv')
 gdp = gdp.rename(columns={'State': 'city'})
 state_long = pd.read_csv('.. /input/homework-for-students4plus/statelatlong.csv' )
def generateBaselineOutputNobodySurvives(df):
output = pd.DataFrame({'PassengerId': df.PassengerId, 'Survived': np.full(( len(df)) , 0)})
 return output
output_train = generateBaselineOutputNobodySurvives(train_data)
 output_test = generateBaselineOutputNobodySurvives(test_data )<compute_test_metric>
gdp_train = gdp.query('year == 2014')
 gdp_test = gdp.query('year == 2015' )
print("MSE baseline: ", mean_squared_error(train_data["Survived"], output_train["Survived"]))
print("LogLoss baseline", log_loss(train_data["Survived"], output_train["Survived"]))<save_to_csv>
merge_train = pd.merge(gdp_train[['city','State & Local Spending','Gross State Product','Real State Growth %','Population(million)']],
state_long[['State','City']], left_on='city', right_on='City', how='inner' ).drop(['City'],axis=1 ).drop(['city'],axis=1)
 merge_test = pd.merge(gdp_test[['city','State & Local Spending','Gross State Product','Real State Growth %','Population(million)']],
state_long[['State','City']], left_on='city', right_on='City', how='inner' ).drop(['City'],axis=1 ).drop(['city'],axis=1 )
output_test.to_csv('baseline_submission.csv', index=False)
 print("Your baseline submission was successfully saved!" )<feature_engineering>
X_train['key'] = X_train['addr_state']
X_test['key'] = X_test['addr_state']
merge_train['key'] = merge_train['State']
merge_test['key'] = merge_test['State']
train_data_copy = train_data.copy()
test_data_copy = test_data.copy()
median_age = train_data["Age"].median()
train_data["Age"] = train_data["Age"].replace(np.nan, median_age)
 train_data["Embarked"] = train_data["Embarked"].replace(np.nan, 'S')
 for feature in ['PassengerId', 'Name','Cabin', 'Ticket']:
train_data.drop(feature, axis=1, inplace=True)
 for feature in ['Fare', 'Pclass', 'SibSp', 'Parch', 'Age']:
train_data[feature] = stats.zscore(train_data[feature])
 for feature in ['Sex', 'Embarked']:
one_hot = pd.get_dummies(train_data[feature])
 train_data = train_data.drop(feature,axis = 1)
 train_data = train_data.join(one_hot )<prepare_x_and_y>
X_train = X_train.reset_index()
X_test = X_test.reset_index()
Y_train = pd.DataFrame(train_data["Survived"] ).to_numpy().flatten()
train_data.drop("Survived", axis=1, inplace=True)
 X_train = train_data.to_numpy()<choose_model_class>
X_train = pd.merge(X_train,merge_train,on='key',how = 'left' ).drop(['key'],axis=1 ).drop(['State'],axis=1)
 X_test = pd.merge(X_test,merge_test,on='key',how = 'left' ).drop(['key'],axis=1 ).drop(['State'],axis=1 )
def build_model(input_shape, learning_rate=0.01):

 tf.keras.backend.clear_session()
np.random.seed(0)
 tf.compat.v1.set_random_seed(0)
 model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=input_shape))
model.add(keras.layers.Dense(
 units=512,
use_bias=True,
activation="relu", 
))
model.add(keras.layers.Dense(
 units=256,
use_bias=True,
activation="relu", 
))
model.add(keras.layers.Dense(
 units=128,
use_bias=True,
activation="relu", 
))
model.add(keras.layers.Dense(
 units=1,
use_bias=True,
activation="sigmoid" 
))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=[metrics.binary_accuracy])
 return model<train_model>
X_train = X_train.set_index('ID')
 X_test = X_test.set_index('ID' )
model = build_model(input_shape=X_train[0].shape, learning_rate=0.01)
 history = model.fit(
 x = X_train,
y = Y_train,
epochs=5,
batch_size=64,
validation_split=0.1,
verbose=1 
)
 history = pd.DataFrame(history.history)
 display(history)
 plot_history(history )<find_best_model_class>
X_train['null_cnt'] = X_train.isnull().sum(axis=1)
 X_test['null_cnt'] = X_test.isnull().sum(axis=1 )
train_predictions = model.predict(X_train ).flatten()
thresholds = [0.3,0.49, 0.5, 0.51, 0.52, 0.7]
group_names = ["True Neg", "False Pos", "False Neg" , "True Pos"]
for threshold in thresholds:
train_predictions_copy = np.copy(train_predictions)
 train_predictions_copy[train_predictions < threshold] = 0.0
train_predictions_copy[train_predictions >= threshold] = 1.0
cf_matrix = confusion_matrix(Y_train, train_predictions_copy)
 tn, fp, fn, tp = cf_matrix.ravel()
group_counts = ["{0:0.0f}".format(value)for value in
cf_matrix.flatten() ]
group_percentages = ["{0:.2%}".format(value)for value in
cf_matrix.flatten() /np.sum(cf_matrix)]
labels = [f"{v1}
{v2}
{v3}" for v1, v2, v3 in
zip(group_names,group_counts,group_percentages)]
labels = np.asarray(labels ).reshape(2,2)
 plt.figure()
plt.title(
 "Threshold : " + str(threshold)+ "
"
+ "Accuracy : " + str(( tp+tn)/(tp+tn+fp+fn)) + "
"
+ "Precision : " + str(( tp)/(tp+fp)) + "
"
+ "Recall : " + str(( tp)/(tp+fn)))
 sns.heatmap(cf_matrix, annot=labels, fmt="", cmap='Blues' )<prepare_output>
X_train['burden'] = round(( X_train['installment']+1)*12/(X_train['annual_inc']+1),2)
 X_test['burden'] = round(( X_test['installment']+1)*12/(X_test['annual_inc']+1),2 )
pd.set_option("display.max_rows", None, "display.max_columns", None)
 train_data_copy['Prediction'] = train_predictions
train_data_copy['Difference'] = abs(train_data_copy['Survived'] - train_data_copy['Prediction'])
 train_data_copy = train_data_copy[['Difference', 'Survived','Prediction', 'Pclass', 'Sex', 'Age', 'Fare', 'SibSp', 'Parch', 'Embarked']]
train_data_copy = train_data_copy.sort_values('Difference')
 train_data_copy.head()<drop_column>
X_train['burden2'] = round(( X_train['loan_amnt']+1)*12/(X_train['annual_inc']+1),2)
 X_test['burden2'] = round(( X_test['loan_amnt']+1)*12/(X_test['annual_inc']+1),2 )
test_data_copy = test_data.copy()
for feature in ['PassengerId', 'Name','Cabin', 'Ticket']:
test_data.drop(feature, axis=1, inplace=True)
 test_data["Age"] = test_data["Age"].replace(np.nan, median_age)
 test_data["Embarked"] = test_data["Embarked"].replace(np.nan, 'S')
 test_data["Fare"] = test_data["Fare"].replace(np.nan, train_data_copy["Fare"].median())
 for feature in ['Fare', 'Pclass', 'SibSp', 'Parch', 'Age']:
test_data[feature] = stats.zscore(test_data[feature])
 for feature in ['Sex', 'Embarked']:
one_hot = pd.get_dummies(test_data[feature])
 test_data = test_data.drop(feature,axis = 1)
 test_data = test_data.join(one_hot)
 X_test = test_data.to_numpy()
test_data.head()<save_to_csv>
X_train['repayment_record'] = round(( X_train['tot_coll_amt']+1)/(X_train['tot_coll_amt']+X_train['tot_cur_bal']+1),2)
 X_test['repayment_record'] = round(( X_test['tot_coll_amt']+1)/(X_test['tot_coll_amt']+X_test['tot_cur_bal']+1),2 )
test_predictions = model.predict(X_test ).flatten()
test_predictions_actual_values = np.array([1 if val >= 0.51 else 0 for val in test_predictions])
 output_test_logit = pd.DataFrame({'PassengerId': test_data_copy.PassengerId, 'Survived': test_predictions_actual_values})
 output_test_logit.to_csv('logit_submission.csv', index=False)
 print("Your logit submission was successfully saved!")
<set_options>
X_train['all_money'] = round(( X_train['tot_cur_bal']+X_train['annual_inc']+1),2)
 X_test['all_money'] = round(( X_test['tot_cur_bal']+X_test['annual_inc']+1),2 )
%matplotlib inline 
<load_from_csv>
X_train['burden3'] = round(( X_train['loan_amnt']+1)*12/(X_train['all_money']+1),2)
 X_test['burden3'] = round(( X_test['loan_amnt']+1)*12/(X_test['all_money']+1),2 )
titanic_filepath =('.. /input/titanic/train.csv')
 titanic_data= pd.read_csv(titanic_filepath)
 test_filepath =('.. /input/titanic/test.csv')
 test_data=pd.read_csv(test_filepath )<define_variables>
drop_col = ['installment','issue_d','State & Local Spending']
X_train = X_train.drop(drop_col,axis=1)
 X_test = X_test.drop(drop_col,axis=1 )
features=['Sex','Fare', 'Pclass','Parch','SibSp']<prepare_x_and_y>
X_train = X_train.fillna({'mths_since_last_delinq': 0, 'mths_since_last_record': 0, 'mths_since_last_major_derog': 0,
'dti': 0,'revol_util':0,'delinq_2yrs':-1})
 X_test = X_test.fillna({'mths_since_last_delinq': 0, 'mths_since_last_record': 0, 'mths_since_last_major_derog': 0,
'dti': 0,'revol_util':0,'delinq_2yrs':-1})
 X_train = X_train.fillna({'tot_coll_amt': X_train['tot_coll_amt'].mean() ,
'tot_cur_bal': X_train['tot_cur_bal'].mean() })
 X_test = X_test.fillna({'tot_coll_amt': X_train['tot_coll_amt'].mean() ,
'tot_cur_bal': X_train['tot_cur_bal'].mean() })
 X_train = X_train.fillna({'emp_length': '
X_test = X_test.fillna({'emp_length': '
x=titanic_data[features]
test_x=test_data[features]<prepare_x_and_y>
f = 'sub_grade'
for i,col in zip(range(1,36),['G5','G4','G3','G2','G1','F5','F4','F3','F2','F1','E5','E4','E3','E2','E1',
'D5','D4','D3','D2','D1','C5','C4','C3','C2','C1','B5','B4','B3','B2','B1',
'A5','A4','A3','A2','A1']):
X_train.loc[X_train[f] == col, f] = i
X_test.loc[X_test[f] == col, f] = i
X_train[f] = X_train[f].astype(int)
 X_test[f] = X_test[f].astype(int )
y=titanic_data.Survived<define_variables>
f = 'grade'
for i,col in zip(range(1,8),['G','F','E','D','C','B','A']):
X_train.loc[X_train[f] == col, f] = i
X_test.loc[X_test[f] == col, f] = i
X_train[f] = X_train[f].astype(int)
 X_test[f] = X_test[f].astype(int )
cleanup_nums = {"Sex": {"male": 1, "female": 2}}
cleanup_nums2 = {"Embarked": {"S": 1, "C": 2, "Q": 3}}
x.head()<categorify>
stand_col = ['loan_amnt','dti','mths_since_last_delinq','mths_since_last_record','open_acc',
'pub_rec','revol_util','total_acc','mths_since_last_major_derog']
scaler = StandardScaler()
scaler.fit(X_train[stand_col])
 X_train[stand_col] = scaler.transform(X_train[stand_col])
 X_test[stand_col] = scaler.transform(X_test[stand_col] )
x=x.fillna(x.mean())
 test_x=test_x.fillna(test_x.mean())
<train_model>
log_col =['annual_inc','delinq_2yrs','inq_last_6mths','revol_bal','collections_12_mths_ex_med',
'tot_coll_amt','tot_cur_bal','all_money','acc_now_delinq']
pt = PowerTransformer(method='yeo-johnson')
 pt.fit(X_train[log_col])
 X_train[log_col] = pt.transform(X_train[log_col])
 X_test[log_col] = pt.transform(X_test[log_col] )
model=XGBClassifier()
model.fit(x,y )<load_from_csv>
f = 'emp_length'
for i,col in zip([0,1,2,3,4,5,6,7,8,9,10,11],['
'7 years','8 years','9 years','10+ years']):
X_train.loc[X_train[f] == col, f] = i
X_test.loc[X_test[f] == col, f] = i
X_train[f] = X_train[f].astype(float)
 X_test[f] = X_test[f].astype(float )
submission_path =('.. /input/titanic/gender_submission.csv')
 submission= pd.read_csv(submission_path )<predict_on_test>
X_train['earliest_cr_line'] = X_train['earliest_cr_line'].str[-4:]
X_test['earliest_cr_line'] = X_test['earliest_cr_line'].str[-4:]
submission['Survived']=model.predict(test_x)
<feature_engineering>
cat_cols = ['application_type']
for c in cat_cols:
le = LabelEncoder()
le.fit(X_train[c])
 X_train[c] = le.transform(X_train[c])
 X_test[c] = le.transform(X_test[c] )
submission['PassengerId']=test_data['PassengerId']<rename_columns>
cols = ['home_ownership','purpose','title','addr_state','earliest_cr_line','initial_list_status','zip_code']
target = 'loan_condition'
X_temp = pd.concat([X_train, y_train], axis=1)
 for col in cols:
summary = X_temp.groupby([col])[target].mean()
X_test[col] = X_test[col].map(summary)
 skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 enc_train = Series(np.zeros(len(X_train)) , index=X_train.index)
 for i,(train_ix, val_ix)in enumerate(( skf.split(X_train, y_train))):
X_train_, _ = X_temp.iloc[train_ix], y_train.iloc[train_ix]
X_val, _ = X_temp.iloc[val_ix], y_train.iloc[val_ix]
summary = X_train_.groupby([col])[target].mean()
enc_train.iloc[val_ix] = X_val[col].map(summary)
 X_train[col] = enc_train
submission.columns=['PassengerId','Survived']<rename_columns>
y_pred_test = np.zeros(len(X_test))
y_pred = np.zeros(len(X_test))
submission.columns=['PassengerId','Survived']
submission.head()<save_to_csv>
TXT_train = X_train.emp_title.copy()
TXT_test = X_test.emp_title.copy()
TXT_train = TXT_train.str.lower()
TXT_test = TXT_test.str.lower()
X_train = X_train.drop('emp_title',axis=1)
 X_test = X_test.drop('emp_title',axis=1)
 tfidf = TfidfVectorizer(max_features=1000, use_idf=True)
 TXT_train_enc = tfidf.fit_transform(TXT_train)
 TXT_test_enc = tfidf.transform(TXT_test)
 X_train = sp.sparse.hstack([X_train.values, TXT_train_enc])
 X_test = sp.sparse.hstack([X_test.values, TXT_test_enc])
 X_train = X_train.tocsr()
X_test = X_test.tocsr()
submission.to_csv('Submission.csv', index=False)
<import_modules>
scores = []
skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=9434, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=94, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=34, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=35, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=77, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=101, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=373, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=365, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=1000, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=2, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=3, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=4, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=5, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=6, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=7, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=8, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=9, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=10, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=11, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
scores = np.array(scores)
 print('Ave.CV score is %f' % scores.mean())
 y_pred_test /= 100
print("Read in libraries")
<load_from_csv>
submission = pd.read_csv('.. /input/homework-for-students4plus/sample_submission.csv', index_col=0)
 submission.loan_condition = y_pred_test
submission.to_csv('submission.csv' )
PATH_WEEK2='/kaggle/input/covid19-global-forecasting-week-2'
df_train = pd.read_csv(f'{PATH_WEEK2}/train.csv')
 df_test = pd.read_csv(f'{PATH_WEEK2}/test.csv')
 df_train.head()
df_test.head()
df_train.rename(columns={'Country_Region':'Country'}, inplace=True)
 df_test.rename(columns={'Country_Region':'Country'}, inplace=True)
 df_train.rename(columns={'Province_State':'State'}, inplace=True)
 df_test.rename(columns={'Province_State':'State'}, inplace=True)
 df_train['Date'] = pd.to_datetime(df_train['Date'], infer_datetime_format=True)
 df_test['Date'] = pd.to_datetime(df_test['Date'], infer_datetime_format=True)
 df_train.info()
df_test.info()
y1_Train = df_train.iloc[:, -2]
y1_Train.head()
y2_Train = df_train.iloc[:, -1]
y2_Train.head()
EMPTY_VAL = "EMPTY_VAL"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state 
<data_type_conversions>
<categorify><EOS>