Kaggle competition: Titanic - Machine Learning from Disaster
folds = KFold(n_splits=3,random_state=6)
 oof_preds = np.zeros(x_train.shape[0])
 sub_preds = np.zeros(x_test.shape[0])
 start = time.time()
valid_score = 0
feature_importance_df = pd.DataFrame()
for n_fold,(trn_idx, val_idx)in enumerate(folds.split(x_train, y_train)) :
trn_x, trn_y = x_train.iloc[trn_idx], y_train[trn_idx]
val_x, val_y = x_train.iloc[val_idx], y_train[val_idx]
train_data = lgb.Dataset(data=trn_x, label=trn_y)
 valid_data = lgb.Dataset(data=val_x, label=val_y)
 params = {"objective" : "regression", "metric" : "mae", 'n_estimators':15000, 'early_stopping_rounds':100,
"num_leaves" : 31, "learning_rate" : 0.05, "bagging_fraction" : 0.9,
"bagging_seed" : 0, "num_threads" : 4,"colsample_bytree" : 0.7
}
lgb_model = lgb.train(params, train_data, valid_sets=[train_data, valid_data], verbose_eval=1000)
 oof_preds[val_idx] = lgb_model.predict(val_x, num_iteration=lgb_model.best_iteration)
 oof_preds[oof_preds>1] = 1
oof_preds[oof_preds<0] = 0
sub_pred = lgb_model.predict(x_test, num_iteration=lgb_model.best_iteration)
 sub_pred[sub_pred>1] = 1
sub_pred[sub_pred<0] = 0
sub_preds += sub_pred/ folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = train_columns
fold_importance_df["importance"] = lgb_model.feature_importance()
fold_importance_df["fold"] = n_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 gc.collect()
end = time.time()
print("Take Time :",(end-start))<load_from_csv>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
df_test = pd.read_csv('.. /input/' + 'test_V2.csv')
 pred = sub_preds
print("fix winPlacePerc")
 for i in range(len(df_test)) :
winPlacePerc = pred[i]
maxPlace = int(df_test.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
pred[i] = winPlacePerc
if(i + 1)% 100000 == 0:
print(i, flush=True, end=" ")
 df_test['winPlacePerc'] = pred
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )<concatenate>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
def toTapleList(list1,list2):
return list(itertools.product(list1,list2))<load_from_csv>
train_drop = train.copy()
train_drop = train_drop.dropna(subset = ['Age'])
 def encodeSex(sex):
if sex == "male": return 0
return 1
def encodeAge(age):
return int(age/5)
 train_drop.Sex = train_drop.Sex.apply(encodeSex)
 train_drop.Age = train_drop.Age.apply(encodeAge )
%%time
train = pd.read_csv('.. /input/train_V2.csv')
 train = reduce_mem_usage(train)
 test = pd.read_csv('.. /input/test_V2.csv')
 test = reduce_mem_usage(test)
 print(train.shape, test.shape )<count_missing_values>
features = ['Pclass','Sex', 'Age', 'Fare']
null_cnt = train.isnull().sum().sort_values()
print(null_cnt[null_cnt > 0])
 train.dropna(inplace=True )<count_unique_values>
X = train_drop[features]
y = train_drop.Survived
train_X, test_X, train_y, test_y = train_test_split(X, y, random_state=1)
 forest = RandomForestClassifier(max_leaf_nodes = 55)
 forest.fit(train_X, train_y)
 print("Feature importance: ", forest.feature_importances_)
 print("Accuracy: ", forest.score(test_X, test_y))
for c in ['Id','groupId','matchId']:
print(f'unique [{c}] count:', train[c].nunique() )<count_values>
test_features = ["Title", "SibSp", "Parch", "Pclass", "Fare"]
X = train_with_title[test_features]
y = train_with_title.Age
train_X, test_X, train_y, test_y = train_test_split(X, y, random_state=1)
 line.fit(train_X, train_y)
 print("Accuracy: ", line.score(test_X, test_y))
for q in ['numGroups == maxPlace','numGroups != maxPlace']:
print(q, ':', len(train.query(q)) )<drop_column>
predicted_ages = train[train['Age'].isnull() ]
predicted_ages['Title'] = predicted_ages.Name.apply(extractTitle)
 predicted_ages['Title'] = predicted_ages.Title.apply(encodeTitle)
 predictions = pd.Series(line.predict(predicted_ages[test_features]))
train_with_ages = train
inc = 0
for i in range(0, len(train['Age'])) :
if math.isnan(train['Age'][i]):
train_with_ages['Age'][i] = predictions[inc]
inc += 1
print(group['players in group'].nlargest(5))
del match,group<count_unique_values>
train_with_ages.Sex = train_with_ages.Sex.apply(encodeSex)
 train_with_ages.Age = train_with_ages.Age.apply(encodeAge)
 features = ['Pclass','Sex', 'Age', 'Fare']
X = train_with_ages[features]
y = train_with_ages.Survived
train_X, test_X, train_y, test_y = train_test_split(X, y, random_state=1)
 forest = RandomForestClassifier(max_leaf_nodes = 55)
 forest.fit(train_X, train_y)
 print("Feature importance: ", forest.feature_importances_)
 print("Accuracy: ", forest.score(test_X, test_y))
<groupby><EOS> 
predicted_ages = test[test['Age'].isnull() ]
predicted_ages['Title'] = predicted_ages.Name.apply(extractTitle)
 predicted_ages['Title'] = predicted_ages.Title.apply(encodeTitle)
 predictions = pd.Series(line.predict(predicted_ages[test_features]))
inc = 0
for i in range(0, len(test['Age'])) :
if math.isnan(test['Age'][i]):
test['Age'][i] = predictions[inc]
inc += 1
test.Sex = test.Sex.apply(encodeSex)
 test.Age = test.Age.apply(encodeAge)
 test['Fare'] = test['Fare'].fillna(value = 13)
 features = ['Pclass','Sex', 'Age', 'Fare']
X = test[features]
final_predict = forest.predict(X)
 prediction = pd.DataFrame(test.PassengerId)
 prediction['Survived'] = final_predict.astype('int')
 prediction.to_csv('predict.csv',index = False )