Kaggle competition: CareerCon 2019 - Help Navigate Robots
<SOS> metric: categorizationaccuracy Kaggle data source: career-con-2019<define_variables>
%load_ext autoreload
%autoreload 2
%matplotlib inline
test_paths = sorted(glob('/kaggle/input/predict-volcanic-eruptions-ingv-oe/test/*'))<define_variables>
from fastai.imports import *
from pandas_summary import DataFrameSummary
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from IPython.display import display
from sklearn.model_selection import StratifiedKFold
import itertools
from sklearn.metrics import *
from sklearn import metrics
from sklearn.preprocessing import LabelEncoder
%%time
test_samples = [Parallel(n_jobs=4 )(delayed(feature_gen )(test_path)for test_path in tqdm_notebook(test_paths)) ]<drop_column>
df_raw = pd.read_csv(f'{PATH}/X_train.csv', low_memory=False)
 y_raw = pd.read_csv(f'{PATH}/y_train.csv', low_memory=False)
 df_test = pd.read_csv(f'{PATH}/X_test.csv', low_memory=False)
 sub = pd.read_csv(f'{PATH}/sample_submission.csv' )
test_samples = np.array(test_samples ).reshape(-1,40)
 del(test_paths)
 gc.collect()<prepare_output>
def fe(data):
df = pd.DataFrame()
data['totl_anglr_vel'] =(data['angular_velocity_X']**2 + data['angular_velocity_Y']**2 +
data['angular_velocity_Z']**2)** 0.5
data['totl_linr_acc'] =(data['linear_acceleration_X']**2 + data['linear_acceleration_Y']**2 +
data['linear_acceleration_Z'])**0.5
data['totl_xyz'] =(data['orientation_X']**2 + data['orientation_Y']**2 +
data['orientation_Z'])**0.5
data['acc_vs_vel'] = data['totl_linr_acc'] / data['totl_anglr_vel']
for col in data.columns:
if col in ['row_id','series_id','measurement_number']:
continue
df[col + '_mean'] = data.groupby(['series_id'])[col].mean()
df[col + '_median'] = data.groupby(['series_id'])[col].median()
df[col + '_max'] = data.groupby(['series_id'])[col].max()
df[col + '_min'] = data.groupby(['series_id'])[col].min()
df[col + '_std'] = data.groupby(['series_id'])[col].std()
df[col + '_range'] = df[col + '_max'] - df[col + '_min']
df[col + '_maxtoMin'] = df[col + '_max'] / df[col + '_min']
df[col + '_mean_abs_chg'] = data.groupby(['series_id'])[col].apply(lambda x: np.mean(np.abs(np.diff(x))))
df[col + '_abs_max'] = data.groupby(['series_id'])[col].apply(lambda x: np.max(np.abs(x)))
 df[col + '_abs_min'] = data.groupby(['series_id'])[col].apply(lambda x: np.min(np.abs(x)))
 df[col + '_abs_avg'] =(df[col + '_abs_min'] + df[col + '_abs_max'])/2
return df
train_features = pd.DataFrame(train_samples)
 train_targets = pd.DataFrame({'target': y})
 test_features = pd.DataFrame(test_samples)
 train_features.head()<concatenate>
%%time
df_train = fe(df_raw)
 df_test = fe(df_test )
train_data = pd.concat([train_features, train_targets], axis = 1)
 test_features['target'] = None
train_data.head()<concatenate>
df_train.fillna(0, inplace = True)
 df_test.fillna(0, inplace = True)
 df_train.replace(-np.inf, 0, inplace = True)
 df_train.replace(np.inf, 0, inplace = True)
 df_test.replace(-np.inf, 0, inplace = True)
 df_test.replace(np.inf, 0, inplace = True )
all_data = pd.concat([train_data, test_features])
 all_data.head()<feature_engineering>
le = LabelEncoder()
y_raw['surface'] = le.fit_transform(y_raw['surface'] )
all_data['id'] = all_data.index
all_data.head()<data_type_conversions>
from sklearn.metrics import confusion_matrix
all_data['target'] = all_data['target'].astype('float64' )<normalization>
def split_vals(a,n): return a[:n].copy() , a[n:].copy()
n_valid = 0
n_trn = len(df_train)-n_valid
X_train, X_valid = split_vals(df_train, n_trn)
 y_train, y_valid = split_vals(y_raw, n_trn)
 X_train.shape, y_train.shape, X_valid.shape,df_train.shape,y_raw.shape
scaler = StandardScaler()
for col in all_data.drop(['target', 'id'],axis=1 ).columns:
all_data[col] = scaler.fit_transform(np.array(all_data[col] ).reshape(-1, 1))
all_data.head()<split>
X_test = df_test
train_data = all_data[:train_features.shape[0]]
test_data = all_data[train_features.shape[0]:]<import_modules>
def create_download_link(title = "Download CSV file", filename = "data.csv"):
html = '<a href={filename}>{title}</a>'
html = html.format(title=title,filename=filename)
 return HTML(html )
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.preprocessing import MinMaxScaler, StandardScaler, scale<drop_column>
def print_score(m):
res = [ m.score(X_train, y_train), m.score(X_valid, y_valid)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )
train_features = train_data.drop(['target'],axis=1)
 train_targets = pd.DataFrame(train_data['target'])
 test_features = test_data.drop(['target'],axis=1)
 train_targets.head()<init_hyperparams>
def k_folds(X, y, X_test, k,n_est,min_leaf,max_feat):
folds = StratifiedKFold(n_splits = k, shuffle=True, random_state=2019)
 y_test = np.zeros(( X_test.shape[0], 9))
y_oof = np.zeros(( X.shape[0]))
score = 0
for i,(train_idx, val_idx)in enumerate(folds.split(X, y)) :
clf = RandomForestClassifier(n_estimators = n_est, n_jobs = -1,min_samples_leaf=min_leaf,max_features=max_feat)
 clf.fit(X_train.iloc[train_idx], y[train_idx])
 y_oof[val_idx] = clf.predict(X.iloc[val_idx])
 y_test += clf.predict_proba(X_test)/ folds.n_splits
score += clf.score(X.iloc[val_idx], y[val_idx])
 print('Fold: {} score: {}'.format(i,clf.score(X.iloc[val_idx], y[val_idx])))
 print('Avg Accuracy', score / folds.n_splits)
 return y_oof, y_test
hyper_params = {
'task': 'train',
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': ['mae'],
'learning_rate': 0.001,
'feature_fraction': 0.9,
'subsample': 0.85,
'subsample_freq': 2,
'verbose': 0,
"max_depth": 40,
"num_leaves": 250,
"max_bin": 128,
"num_iterations": 10000,
'device': 'gpu',
'gpu_platform_id': 0,
'gpu_device_id': 0
}<prepare_x_and_y>
y_oof, y_test = k_folds(X_train, y_raw['surface]', X_test, k= 50,n_est=500,min_leaf=1,max_feat='auto' )
score = []
skf = KFold(n_splits = 10, shuffle=True, random_state=123)
 skf.get_n_splits(train_features, train_targets)
 oof_lgbm_df = pd.DataFrame()
predictions = pd.DataFrame(test_features['id'])
 x_test = test_features.drop(['id'], axis = 1)
 for fold,(trn_idx, val_idx)in enumerate(skf.split(train_features, train_targets)) :
x_train, y_train = train_features.iloc[trn_idx], train_targets.iloc[trn_idx]['target']
x_valid, y_valid = train_features.iloc[val_idx], train_targets.iloc[val_idx]['target']
index = x_valid['id']
x_train = x_train.drop(['id'], axis = 1)
 x_valid = x_valid.drop(['id'], axis = 1)
 p_valid = 0
yp = 0
gbm = lgb.LGBMRegressor(**hyper_params)
 gbm.fit(x_train, y_train,
eval_set=[(x_valid, y_valid)],
eval_metric='mae',
verbose = 500,
early_stopping_rounds=100)
 score.append(mean_absolute_error(gbm.predict(x_valid), y_valid))
yp += gbm.predict(x_test)
 fold_pred = pd.DataFrame({'ID': index,
'label':gbm.predict(x_valid)})
 oof_rfr_df = pd.concat([oof_lgbm_df, fold_pred], axis=0)
 predictions['fold{}'.format(fold+1)] = yp<define_variables>
y_test = np.argmax(y_test, axis=1)
 submission = pd.read_csv(os.path.join(".. /input/",'sample_submission.csv'))
submission['surface'] = le.inverse_transform(y_test)
 submission.to_csv('submission1.csv', index=False)
 submission.head(10)
 create_download_link(filename='submission1.csv' )
__seed = 666
__create_dataframes = True
__dataframe_size = 5000
__path_to_my_data = ".. /input/ingvchallengefeatures/"
__n_folds = 5<set_options>
y_oof, y_test = k_folds(X_train, y_raw['surface'], X_test, k= 50,n_est=500,min_leaf=1,max_feat='sqrt' )
np.random.seed(__seed)
 pd.set_option('max_columns', 100)
 pd.set_option('max_rows', 200)
 __n_cores = psutil.cpu_count() 
<load_from_csv>
y_test = np.argmax(y_test, axis=1)
 submission = pd.read_csv(os.path.join(".. /input/",'sample_submission.csv'))
submission['surface'] = le.inverse_transform(y_test)
 submission.to_csv('submission2.csv', index=False)
 submission.head(10)
 create_download_link(filename='submission2.csv' )
y_train = pd.read_csv(".. /input/predict-volcanic-eruptions-ingv-oe/train.csv"
, index_col="segment_id"
, dtype={"time_to_eruption":np.int32}
, squeeze=True)
 print("Length of time_to_eruption serie : {}
The five first lines :".format(len(y_train)))
 print(y_train.head().map('{:,.0f}'.format))<train_model>
def k_folds_gbm(X, y, X_test, k,n_est):
folds = StratifiedKFold(n_splits = k, shuffle=True, random_state=2019)
 y_test = np.zeros(( X_test.shape[0], 9))
y_oof = np.zeros(( X.shape[0]))
score = 0
for i,(train_idx, val_idx)in enumerate(folds.split(X, y)) :
clf = XGBClassifier(n_estimators = n_est, n_jobs = -1,learning_rate = 0.2,objective='multi:softmax',eval_metric='merror')
 clf.fit(X_train.iloc[train_idx], y[train_idx])
 y_oof[val_idx] = clf.predict(X.iloc[val_idx])
 y_test += clf.predict_proba(X_test)/ folds.n_splits
score += clf.score(X.iloc[val_idx], y[val_idx])
 print('Fold: {} score: {}'.format(i,clf.score(X.iloc[val_idx], y[val_idx])))
 print('Avg Accuracy', score / folds.n_splits)
 return y_oof, y_test
print("Time to Eruption in Train
-------------------------
Min : {:,}
Median : {:,}
Max : {:,}".format(
 y_train.min() , y_train.median() , y_train.max()))
_temp = y_train.apply(lambda x:datetime.timedelta(milliseconds = x))
print("
Time to Eruption in Train
-------------------------
Min : {}
Median : {}
Max : {}".format(
 _temp.min() , _temp.median() , _temp.max()))
del _temp<load_from_csv>
y_oof, y_test = k_folds_gbm(X_train, y_raw['surface'], X_test, k= 50,n_est=500)
<save_to_csv><EOS> 
y_test = np.argmax(y_test, axis=1)
 submission = pd.read_csv(os.path.join(".. /input/",'sample_submission.csv'))
submission['surface'] = le.inverse_transform(y_test)
 submission.to_csv('submission3.csv', index=False)
 submission.head(10)
 create_download_link(filename='submission3.csv' )