Kaggle competition: CareerCon 2019 - Help Navigate Robots
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
def rmse_cross_val(model, X=xtrain):
rmse = np.sqrt(-cross_val_score(model, xtrain, train_labels, scoring="neg_mean_squared_error", cv=KFold(n_splits=12, random_state=42, shuffle=True)))
 return rmse<choose_model_class>
%%time
train_df = pd.read_csv('.. /input/X_train.csv')
 test_df = pd.read_csv('.. /input/X_test.csv')
 target_df = pd.read_csv('.. /input/y_train.csv' )
xgboost = XGBRegressor(learning_rate=0.01,n_estimators=6000,max_depth=4,min_child_weight=0,gamma=0.6,
subsample=0.7,colsample_bytree=0.7,objective='reg:squarederror',nthread=-1,scale_pos_weight=1,
seed=27,reg_alpha=0.00006,random_state=42)
 gbr = GradientBoostingRegressor(n_estimators=6000,learning_rate=0.01,max_depth=4,max_features='sqrt',
min_samples_leaf=15,min_samples_split=10,loss='huber',random_state=42)
 rf = RandomForestRegressor(n_estimators=1200,max_depth=15,min_samples_split=5,min_samples_leaf=5,
max_features=None,oob_score=True,random_state=42)
 svr = make_pipeline(RobustScaler() , SVR(C= 20, epsilon= 0.008, gamma=0.0003))
ridge_alphas = [1e-10, 1e-8, 1e-4, 1e-3, 5e-2, 1e-2, 0.1, 0.3, 1, 3, 5, 10, 15, 18, 20, 30, 50, 75, 100]
ridge = make_pipeline(RobustScaler() , RidgeCV(alphas=ridge_alphas, cv=KFold(n_splits=12, random_state=42, shuffle=True)))
 stack_reg = StackingRegressor(estimators=[('xgboost',xgboost),('svr',svr),('ridge',ridge),('gbr',gbr),('rf',rf)], final_estimator=xgboost )<train_model>
len(train_df.measurement_number.value_counts() )
print('stack_reg')
 stack_reg_model = stack_reg.fit(np.array(xtrain), np.array(train_labels))<train_model>
target_df['surface'].value_counts().reset_index().rename(columns={'index': 'target'} )
<train_model>
print('Are there NaNs in {}?: {}
'.format('train_df',train_df.isnull().values.any())+
'Are there NaNs in {}?: {}
'.format('test_df',test_df.isnull().values.any())+
'Are there NaNs in {}?: {}
'.format('target_df',target_df.isnull().values.any()))
print('xgboost')
 xgb_data = xgboost.fit(xtrain, train_labels )<train_model>
le = LabelEncoder()
le.fit(target_df['surface'])
 target_df['surface'] = le.transform(target_df['surface'] )
print('Svr')
 svr_data = svr.fit(xtrain, train_labels )<train_model>
target_df['surface'].value_counts()
print('Ridge')
 ridge_data = ridge.fit(xtrain, train_labels )<train_model>
def get_features(df):
result_df = pd.DataFrame()
for col in df.columns:
if col in ['row_id', 'series_id', 'measurement_number']:
continue
result_df['{}_mean'.format(col)] = df.groupby(['series_id'])[col].mean()
result_df['{}_max'.format(col)] = df.groupby(['series_id'])[col].max()
result_df['{}_min'.format(col)] = df.groupby(['series_id'])[col].min()
result_df['{}_sum'.format(col)] = df.groupby(['series_id'])[col].sum()
result_df['{}_mean_abs_change'.format(col)] = df.groupby(['series_id']\ 
)[col].apply(lambda x: np.mean(np.abs(np.diff(x))))
return result_df
print('RandomForest')
 rf_data = rf.fit(xtrain, train_labels )<train_model>
%%time
train_df = get_features(train_df)
 test_df = get_features(test_df )
print('GradientBoosting')
 gbr_data = gbr.fit(xtrain, train_labels )<compute_test_metric>
train_df.fillna(0, inplace=True)
 test_df.fillna(0, inplace=True)
 train_df.replace(-np.inf, 0, inplace=True)
 train_df.replace(np.inf, 0, inplace=True)
 test_df.replace(-np.inf, 0, inplace=True)
 test_df.replace(np.inf, 0, inplace=True )
score = rmse_cross_val(svr)
 print("SVR: {:.4f}({:.4f})".format(score.mean() , score.std()))
scores['svr'] =(score.mean() , score.std() )<feature_engineering>
sc = StandardScaler()
train_df = sc.fit_transform(train_df)
 test_df = sc.transform(test_df )
score = rmse_cross_val(ridge)
 print("ridge: {:.4f}({:.4f})".format(score.mean() , score.std()))
scores['ridge'] =(score.mean() , score.std() )<compute_test_metric>
folds = StratifiedKFold(n_splits=10, shuffle=True, random_state=111222)
 sub_preds_rf = np.zeros(( test_df.shape[0],9))
oof_preds_rf = np.zeros(( train_df.shape[0]))
score = 0
counter = 0
print('start training')
 for train_index, test_index in folds.split(train_df, target_df['surface']):
print('Fold {}'.format(counter+1))
clf = RandomForestClassifier(n_estimators=200, n_jobs=-1)
 clf.fit(train_df[train_index], target_df['surface'][train_index])
 oof_preds_rf[test_index] = clf.predict(train_df[test_index])
 sub_preds_rf += clf.predict_proba(test_df)/ folds.n_splits
score += clf.score(train_df[test_index], target_df['surface'][test_index])
 counter += 1
print('score : {}'.format(clf.score(train_df[test_index], target_df['surface'][test_index])))
 print('avg accuracy : {}'.format(score / folds.n_splits))
score = rmse_cross_val(rf)
 print("rf: {:.4f}({:.4f})".format(score.mean() , score.std()))
scores['rf'] =(score.mean() , score.std() )<compute_test_metric>
submit = pd.read_csv('.. /input/sample_submission.csv')
 submit['surface'] = le.inverse_transform(sub_preds_rf.argmax(axis=1))
submit.to_csv('submit.csv', index=False)
 print('Ready' )