Kaggle competition: CareerCon 2019 - Help Navigate Robots
np.log(train_df['SalePrice'] )<feature_engineering>
x_df = pd.read_csv('.. /input/X_train.csv')
 x_test_df = pd.read_csv('.. /input/X_test.csv')
 y_df = pd.read_csv('.. /input/y_train.csv')
 x_df.head()
train_df_le["SalePrice_log"] = np.log(train_df_le["SalePrice"] )<prepare_x_and_y>
len(y_df['series_id'].value_counts() )
train_X = train_df_le.drop(["SalePrice","SalePrice_log","Id"], axis=1)
 train_Y = train_df_le["SalePrice_log"]<train_model>
len(x_df['series_id'].value_counts() )
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(y_valid, y_pred))
print(tmp_rmse)
 models.append(model_lgb)
 rmses.append(tmp_rmse)
 oof[val_index] = y_pred<count_values>
len(x_df['measurement_number'].value_counts() )
sum(rmses)/len(rmses )<concatenate>
print(y_df.head())
 print(x_df.isnull().values.any())
 print(x_test_df.isnull().values.any())
 print(y_df.isnull().values.any() )
all_df = pd.concat([train_df, test_df], sort=False ).reset_index(drop=True )<filter>
def phi(x):
return math.atan2(x['orientation_W']*x['orientation_X'] + x['orientation_Y']*x['orientation_Z'], 1-2*(x['orientation_X']**2+x['orientation_Y']**2))
def theta(x):
return math.asin(2*(x['orientation_W']*x['orientation_Y'] - x['orientation_Z']*x['orientation_X']))
def chi(x):
return math.atan2(2*(x['orientation_W']*x['orientation_Z']+x['orientation_X']*x['orientation_Y']), 1-2*(x['orientation_Y']**2+x['orientation_Z']**2))
categories = all_df.columns[all_df.dtypes == "object"]
print(categories )<sort_values>
x_df['phi'] = x_df.apply(phi, axis=1)
 x_df['theta'] = x_df.apply(theta, axis=1)
 x_df['chi'] = x_df.apply(chi, axis=1)
 x_test_df['phi'] = x_test_df.apply(phi, axis=1)
 x_test_df['theta'] = x_test_df.apply(theta, axis=1)
 x_test_df['chi'] = x_test_df.apply(chi, axis=1)
 x_df.head()
all_df.isnull().sum().sort_values(ascending=False ).head(40 )<count_values>
def normalize_quaternions(data):
data['norm_quat'] =(data['orientation_X']**2 + data['orientation_Y']**2 + data['orientation_Z']**2 + data['orientation_W']**2)
 data['mod_quat'] =(data['norm_quat'])**0.5
data['norm_X'] = data['orientation_X'] / data['mod_quat']
data['norm_Y'] = data['orientation_Y'] / data['mod_quat']
data['norm_Z'] = data['orientation_Z'] / data['mod_quat']
data['norm_W'] = data['orientation_W'] / data['mod_quat']
return data
all_df.PoolQC.value_counts()<feature_engineering>
x_df = normalize_quaternions(x_df)
 x_test_df = normalize_quaternions(x_test_df)
 x_df.head()
all_df.loc[~all_df["PoolQC"].isnull() , "PoolQC"] = 1
all_df.loc[all_df["PoolQC"].isnull() , "PoolQC"] = 0<count_values>
def feat_eng(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']**2)**0.5
data['totl_xyz'] =(data['orientation_X']**2 + data['orientation_Y']**2 + data['orientation_Z']**2)**0.5
data['acc_vs_vel'] = data['totl_linr_acc'] / data['totl_anglr_vel']
def mean_change_of_abs_change(x):
return np.mean(np.diff(np.abs(np.diff(x))))
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 + '_mean_change_of_abs_change'] = data.groupby('series_id')[col].apply(mean_change_of_abs_change)
 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
all_df.PoolQC.value_counts()<feature_engineering>
x_df = feat_eng(x_df)
 x_test_df = feat_eng(x_test_df)
 x_df=x_df.reset_index()
x_test_df=x_test_df.reset_index()
print(x_df.shape)
 x_df.head()
all_df.loc[~all_df["MiscFeature"].isnull() , "MiscFeature"] = 1
all_df.loc[all_df["MiscFeature"].isnull() , "MiscFeature"] = 0<feature_engineering>
correlations = x_df.corr().abs().unstack().sort_values(kind="quicksort" ).reset_index()
correlations = correlations[correlations['level_0'] != correlations['level_1']]
correlations.tail(20 )
all_df.loc[~all_df["Alley"].isnull() , "Alley"] = 1
all_df.loc[all_df["Alley"].isnull() , "Alley"] = 0<feature_engineering>
le = preprocessing.LabelEncoder()
y_df['surface'] = le.fit_transform(y_df['surface'])
 y_df['surface'].value_counts()
HighFacility_col = ["PoolQC","MiscFeature","Alley"]
for col in HighFacility_col:
if all_df[col].dtype == "object":
if len(all_df[all_df[col].isnull() ])> 0:
all_df.loc[~all_df[col].isnull() , col] = 1
all_df.loc[all_df[col].isnull() , col] = 0<feature_engineering>
folds = StratifiedKFold(n_splits=10, shuffle=True, random_state=59)
 predicted = np.zeros(( x_test_df.shape[0],9))
measured= np.zeros(( x_df.shape[0]))
score = 0
all_df["hasHighFacility"] = all_df["PoolQC"] + all_df["MiscFeature"] + all_df["Alley"]<data_type_conversions>
def run_gridsearch(X, y, clf, param_grid, cv=5):
grid_search = GridSearchCV(clf, param_grid=param_grid, cv=cv, verbose=2)
 start = time()
grid_search.fit(X, y)
 print(( "
GridSearchCV took {:.2f} seconds for {:d} candidate"
"parameter settings." ).format(time() - start, len(grid_search.cv_results_)))
 top_params = report(grid_search.cv_results_, 2)
 print(grid_search.best_params_)
 return top_params
def report(grid_scores, n_top=2):
top_scores = sorted(grid_scores, key=itemgetter(1), reverse=True)[:n_top]
print(grid_scores['mean_train_score'])
 return top_scores
all_df["hasHighFacility"] = all_df["hasHighFacility"].astype(int )<count_values>
warnings.filterwarnings("ignore")
 print("-- Grid Parameter Search via 10-fold CV")

param_grid = {"loss": ["deviance"],
"n_estimators": [500],
"min_samples_split": [5, 20],
"max_depth": [10, 50],
"min_samples_leaf": [10, 50],
"max_leaf_nodes": [20, 40]
}
seed=43
clf = GradientBoostingClassifier(random_state=seed)
all_df["hasHighFacility"].value_counts()<drop_column>
gc.collect()
all_df = all_df.drop(["PoolQC","MiscFeature","Alley"],axis=1 )<filter>
X_train, X_test, y_train, y_test = ms.train_test_split(x_df, y_df['surface'],
test_size=0.25,
random_state=seed,
stratify= y_df['surface'] )
train_df_num = train_df.select_dtypes(include=[np.number] )<define_variables>
clf=RandomForestClassifier(n_estimators = 500, n_jobs = -1)
 clf.fit(x_df, y_df['surface'])
 trn_acc = accuracy_score(y_train, clf.predict(X_train))
tst_acc = accuracy_score(y_test, clf.predict(X_test))
print('training accuracy:', trn_acc)
 print('test accuracy :', tst_acc )
nonratio_features = ["Id", "MSSubClass", "OverallQual", "OverallCond", "YearBuilt", "YearRemodAdd", "MoSold", "YrSold"]<sort_values>
y_pred = clf.predict(x_test_df )
num_features = sorted(list(set(train_df_num)- set(nonratio_features)) )<filter>
x_test_df['surface'] = le.inverse_transform(y_pred)
 sub_df = x_test_df.filter(['series_id','surface'], axis=1)
train_df_num_rs = train_df_num[num_features]<count_unique_values>
sub_df.to_csv('submission_last_5.csv', index=False)
 print(sub_df.shape)
 sub_df.head()
<filter><EOS> 
print('Avg Accuracy RF', score / folds.n_splits)