Kaggle competition: CareerCon 2019 - Help Navigate Robots
xgb_params = {
"learning_rate":0.05,
"seed":1234,
"max_depth": 6,
"colsample_bytree": 0.330432640328732,
"sublsample": 0.7158427239902707
}<train_model>
train_X = pd.read_csv('.. /input/career-con-2019/X_train.csv')
 test_X = pd.read_csv('.. /input/cconlink/X_test_trial.csv')
 tot = pd.concat([train_X, test_X])
 train_X = train_X.iloc[:,3:].values.reshape(-1,128,10)
 test_X = test_X.iloc[:,3:].values.reshape(-1,128,10)
 print('train_X shape:', train_X.shape, ', test_X shape:', test_X.shape )
models_xgb = []
rmses_xgb = []
oof_xgb = np.zeros(len(train_X))
for train_index, val_index in kf.split(train_X):
X_train = train_X.iloc[train_index]
X_valid = train_X.iloc[val_index]
y_train = train_Y.iloc[train_index]
y_valid = train_Y.iloc[val_index]
xgb_train = xgb.DMatrix(X_train, label=y_train)
 xgb_eval = xgb.DMatrix(X_valid, label=y_valid)
 evals = [(xgb_train, "train"),(xgb_eval, "eval")]
model_xgb = xgb.train(xgb_params, xgb_train,
evals=evals,
num_boost_round=1000,
early_stopping_rounds=20,
verbose_eval=20,)
 y_pred = model_xgb.predict(xgb_eval)
 tmp_rmse = np.sqrt(mean_squared_error(y_valid, y_pred))
print(tmp_rmse)
 models_xgb.append(model_xgb)
 rmses_xgb.append(tmp_rmse)
 oof_xgb[val_index] = y_pred<count_values>
df_train_y = pd.read_csv('.. /input/career-con-2019/y_train.csv')
 surface_names = df_train_y['surface'].unique()
num_surfaces = len(surface_names)
 surface_to_numeric = dict(zip(surface_names, range(num_surfaces)))
 print('Convert to numbers: ', surface_to_numeric)
 train_y = df_train_y['surface'].replace(surface_to_numeric ).values
train_group = df_train_y['group_id'].values
sum(rmses_xgb)/len(rmses_xgb )<prepare_x_and_y>
def sq_dist(a,b):

 return np.sum(( a-b)**2, axis=1)
 def find_run_edges(data, edge):

 if edge == 'left':
border1 = 0
border2 = -1
elif edge == 'right':
border1 = -1
border2 = 0
else:
return False
edge_list = []
linked_list = []
for i in range(len(data)) :
dist_list = sq_dist(data[i, border1, :4], data[:, border2, :4])
 min_dist = np.min(dist_list)
 closest_i = np.argmin(dist_list)
 if closest_i == i:
print('Sample', i, 'linked with itself.Next closest sample used instead.')
 closest_i = np.argsort(dist_list)[1]
dist_list = sq_dist(data[closest_i, border2, :4], data[:, border1, :4])
 rev_dist = np.min(dist_list)
 closest_rev = np.argmin(dist_list)
 if closest_rev == closest_i:
print('Sample', i, '(back-)linked with itself.Next closest sample used instead.')
 closest_rev = np.argsort(dist_list)[1]
if(i != closest_rev):
edge_list.append(i)
 else:
linked_list.append([i, closest_i, min_dist])
 return edge_list, linked_list
def find_runs(data, left_edges, right_edges):

 data_runs = []
for start_point in left_edges:
i = start_point
run_list = [i]
while i not in right_edges:
tmp = np.argmin(sq_dist(data[i, -1, :4], data[:, 0, :4]))
if tmp == i:
tmp = np.argsort(sq_dist(data[i, -1, :4], data[:, 0, :4])) [1]
i = tmp
run_list.append(i)
 data_runs.append(np.array(run_list))
return data_runs
xgb_test = xgb.DMatrix(test_X )<predict_on_test>
train_left_edges, train_left_linked = find_run_edges(train_X, edge='left')
 train_right_edges, train_right_linked = find_run_edges(train_X, edge='right')
 print('Found', len(train_left_edges), 'left edges and', len(train_right_edges), 'right edges.' )
preds_xgb = []
for model in models_xgb:
pred = model.predict(xgb_test)
 preds_xgb.append(pred )<prepare_output>
train_runs = find_runs(train_X, train_left_edges, train_right_edges )
preds_array_xgb= np.array(preds_xgb)
 preds_mean_xgb = np.mean(preds_array_xgb, axis=0)
 preds_exp_xgb = np.exp(preds_mean_xgb)
 submission["SalePrice"] = preds_exp_xgb<save_to_csv>
flat_list = [series_id for run in train_runs for series_id in run]
print(len(flat_list), len(np.unique(flat_list)) )
submission.to_csv("./houseprices_submit03.csv",index=False )<define_variables>
print([ len(np.unique(train_y[run])) for run in train_runs ] )
preds_ans = preds_exp_xgb * 0.5 + preds_exp * 0.5<prepare_output>
print([ len(np.unique(train_group[run])) for run in train_runs ] )
submission["SalePrice"] = preds_ans<save_to_csv>
df_train_y['run_id'] = 0
df_train_y['run_pos'] = 0
for run_id in range(len(train_runs)) :
for run_pos in range(len(train_runs[run_id])) :
series_id = train_runs[run_id][run_pos]
df_train_y.at[ series_id, 'run_id' ] = run_id
df_train_y.at[ series_id, 'run_pos' ] = run_pos
df_train_y.to_csv('y_train_with_runs.csv', index=False)
 df_train_y.tail()
submission.to_csv("./houseprices_submit04.csv",index=False )<correct_missing_values>
train_runs = find_runs(tot, train_left_edges, train_right_edges )
train_df_le_dn = train_df_le.dropna()<import_modules>
flat_list = [series_id for run in train_runs for series_id in run]
print(len(flat_list), len(np.unique(flat_list)) )
from sklearn import preprocessing<normalization>
x = train_runs[1]
y=np.sort(x)
 y
train_scaled = preprocessing.scale(train_df_le_dn.drop(["Id"],axis=1))<create_dataframe>
ss = pd.read_csv('.. /input/career-con-2019/sample_submission.csv' )
train_scaled_df = pd.DataFrame(train_scaled)
 train_scaled_df.columns = train_df_le_dn.drop(["Id"],axis=1 ).columns<import_modules>
ss['surface'] = ''
from sklearn.cluster import KMeans<define_variables>
l=[]
surf = ''
for i in range(151):
x = train_runs[i]
x = np.sort(x)
 if x[0]<3810:
l.append(( i,df_train_y['surface'][x[0]]))
surf = df_train_y['surface'][x[0]]
for j in range(len(train_runs[i])) :
if train_runs[i][j]-3810>-1:
ss['surface'][train_runs[i][j]-3810] = surf
np.random.seed(1234 )<predict_on_test>
ss.to_csv('x.csv',index = False )
house_cluster = KMeans(n_clusters=4 ).fit_predict(train_scaled )<feature_engineering>
sub96 = pd.read_csv('.. /input/096acc/woodconc.csv' )
<count_values><EOS> 
sub96.to_csv('sub96.csv',index = False )