Kaggle competition: CareerCon 2019 - Help Navigate Robots
for cat in categories:
le = LabelEncoder()
print(cat)
 all_df[cat].fillna("missing", inplace=True)
 le = le.fit(all_df[cat])
 all_df[cat] = le.transform(all_df[cat])
 all_df[cat] = all_df[cat].astype("category" )<split>
train_df_X = pd.read_csv('.. /input/X_train.csv',sep=',')
 train_df_Y = pd.read_csv('.. /input/y_train.csv')
 test_df_X = pd.read_csv('.. /input/X_test.csv')
 train_df_X.shape, train_df_Y.shape, test_df_X.shape
train_df_le = all_df[~all_df["SalePrice"].isnull() ]
test_df_le = all_df[all_df["SalePrice"].isnull() ]<import_modules>
train_df_X.isnull().sum()
import lightgbm as lgb<choose_model_class>
train_df_Y.surface.value_counts()
folds = 3
kf = KFold(n_splits=folds )<set_options>
train_df_X.isnull().sum()
lgbm_params = {
"objective":"regression",
"random_seed":1234
}<prepare_x_and_y>
def _kurtosis(x):
return kurtosis(x)
 def CPT5(x):
den = len(x)*np.exp(np.std(x))
return sum(np.exp(x)) /den
def skewness(x):
return skew(x)
 def SSC(x):
x = np.array(x)
 x = np.append(x[-1], x)
 x = np.append(x,x[1])
 xn = x[1:len(x)-1]
xn_i2 = x[2:len(x)]
xn_i1 = x[0:len(x)-2]
ans = np.heaviside(( xn-xn_i1)*(xn-xn_i2),0)
 return sum(ans[1:])
 def wave_length(x):
x = np.array(x)
 x = np.append(x[-1], x)
 x = np.append(x,x[1])
 xn = x[1:len(x)-1]
xn_i2 = x[2:len(x)]
return sum(abs(xn_i2-xn))
def norm_entropy(x):
tresh = 3
return sum(np.power(abs(x),tresh))
def SRAV(x):
SRA = sum(np.sqrt(abs(x)))
 return np.power(SRA/len(x),2)
 def mean_abs(x):
return sum(abs(x)) /len(x)
 def zero_crossing(x):
x = np.array(x)
 x = np.append(x[-1], x)
 x = np.append(x,x[1])
 xn = x[1:len(x)-1]
xn_i2 = x[2:len(x)]
return sum(np.heaviside(-xn*xn_i2,0))
def quaternion_to_euler(x, y, z, w):
t0 = +2.0 *(w * x + y * z)
 t1 = +1.0 - 2.0 *(x * x + y * y)
 X = math.atan2(t0, t1)
 t2 = +2.0 *(w * y - z * x)
 t2 = +1.0 if t2 > +1.0 else t2
t2 = -1.0 if t2 < -1.0 else t2
Y = math.asin(t2)
 t3 = +2.0 *(w * z + x * y)
 t4 = +1.0 - 2.0 *(y * y + z * z)
 Z = math.atan2(t3, t4)
 return X, Y, Z
train_X = train_df_le.drop(["SalePrice", "Id"], axis=1)
 train_Y = train_df_le["SalePrice"]<import_modules>
def create_Feature(org_df):
df = org_df.copy()
df['norm_quat'] =(df['orientation_X']**2 + df['orientation_Y']**2 + df['orientation_Z']**2 + df['orientation_W']**2)
 df['mod_quat'] =(df['norm_quat'])**0.5
df['norm_X'] = df['orientation_X'] / df['mod_quat']
df['norm_Y'] = df['orientation_Y'] / df['mod_quat']
df['norm_Z'] = df['orientation_Z'] / df['mod_quat']
df['norm_W'] = df['orientation_W'] / df['mod_quat']
df['total_angular_velocity'] =(df['angular_velocity_X'] ** 2 + df['angular_velocity_Y'] ** 2 + df['angular_velocity_Z'] ** 2)** 0.5
df['total_linear_acceleration'] =(df['linear_acceleration_X'] ** 2 + df['linear_acceleration_Y'] ** 2 + df['linear_acceleration_Z'] ** 2)** 0.5
df['acc_vs_vel'] = df['total_linear_acceleration'] / df['total_angular_velocity']
x, y, z, w = df['orientation_X'].tolist() , df['orientation_Y'].tolist() , df['orientation_Z'].tolist() , df['orientation_W'].tolist()
nx, ny, nz = [], [], []
for i in range(len(x)) :
xx, yy, zz = quaternion_to_euler(x[i], y[i], z[i], w[i])
 nx.append(xx)
 ny.append(yy)
 nz.append(zz)
 df['euler_x'] = nx
df['euler_y'] = ny
df['euler_z'] = nz
df['total_angle'] =(df['euler_x'] ** 2 + df['euler_y'] ** 2 + df['euler_z'] ** 2)** 5
df['angle_vs_acc'] = df['total_angle'] / df['total_linear_acceleration']
df['angle_vs_vel'] = df['total_angle'] / df['total_angular_velocity']
ndf=pd.DataFrame()
df['angular_velocity'] = df['angular_velocity_X'] + df['angular_velocity_Y'] + df['angular_velocity_Z']
df['linear_acceleration'] = df['linear_acceleration_X'] + df['linear_acceleration_Y'] + df['linear_acceleration_Z']
df['velocity_to_acceleration'] = df['angular_velocity'] / df['linear_acceleration']
for c in df.columns[3:]:
ndf[c+'_min']=df.groupby(['series_id'])[c].min()
ndf[c+'_max']=df.groupby(['series_id'])[c].max()
ndf[c+'_absMax'] =df.groupby(['series_id'])[c].apply(lambda x : np.max(np.abs(x)))
 ndf[c+'_absMin'] =df.groupby(['series_id'])[c].apply(lambda x : np.min(np.abs(x)))
 ndf[c+'_mean']=df.groupby(['series_id'])[c].mean()
ndf[c+'_mean']=df.groupby(['series_id'])[c].median()
ndf[c+'_std']=df.groupby(['series_id'])[c].std()
ndf[c + '_q25'] = df.groupby(['series_id'])[c].quantile(0.25)
 ndf[c + '_q50'] = df.groupby(['series_id'])[c].quantile(0.5)
 ndf[c + '_q75'] = df.groupby(['series_id'])[c].quantile(0.75)
 ndf[c + '_SSC'] = df.groupby(['series_id'])[c].apply(SSC)
 ndf[c + '_skewness'] = df.groupby(['series_id'])[c].apply(skewness)
 ndf[c + '_wave_lenght'] = df.groupby(['series_id'])[c].apply(wave_length)
 ndf[c + '_norm_entropy'] = df.groupby(['series_id'])[c].apply(norm_entropy)
 ndf[c + '_SRAV'] = df.groupby(['series_id'])[c].apply(SRAV)
 ndf[c + '_kurtosis'] = df.groupby(['series_id'])[c].apply(_kurtosis)
 ndf[c + '_mean_abs'] = df.groupby(['series_id'])[c].apply(mean_abs)
 ndf[c + '_zero_crossing'] = df.groupby(['series_id'])[c].apply(zero_crossing)
 return ndf.reset_index()
from sklearn.metrics import mean_squared_error<import_modules>
train_fe_X.to_feather('Train_FE')
 test_fe_X.to_feather('Test_FE')
 train_df_Y.to_feather('Target')
from sklearn.metrics import mean_squared_error<train_model>
train_fe_X.to_feather('Train_FE')
 test_fe_X.to_feather('Test_FE')
 train_df_Y.to_feather('Target')
models = []
rmses = []
oof = 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]
lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_valid, y_valid, reference=lgb_train)
 model_lgb = lgb.train(lgbm_params,
lgb_train,
valid_sets=lgb_eval,
num_boost_round=100,
early_stopping_rounds=20,
verbose_eval=10, 
)
 y_pred = model_lgb.predict(X_valid, num_iteration=model_lgb.best_iteration)
 tmp_rmse = np.sqrt(mean_squared_error(np.log(y_valid), np.log(y_pred)))
 print(tmp_rmse)
 models.append(model_lgb)
 rmses.append(tmp_rmse)
 oof[val_index] = y_pred<count_values>
sum(rmses)/len(rmses )<feature_engineering>