Kaggle competition: CareerCon 2019 - Help Navigate Robots
print('building training set...')
 targetcols = ['returnsOpenNextMktres10']
traincols = [col for col in cdf.columns if col not in ['time', 'assetCode', 'universe'] + targetcols]
dates = cdf['time'].unique()
train = range(len(dates)) [:int(0.95*len(dates)) ]
val = range(len(dates)) [int(0.95*len(dates)) :]
cdf[targetcols[0]] =(cdf[targetcols[0]] > 0 ).astype(int)
 Xt = cdf[traincols].fillna(0 ).loc[cdf['time'].isin(dates[train])].values
Yt = cdf[targetcols].fillna(0 ).loc[cdf['time'].isin(dates[train])].values
Xv = cdf[traincols].fillna(0 ).loc[cdf['time'].isin(dates[val])].values
Yv = cdf[targetcols].fillna(0 ).loc[cdf['time'].isin(dates[val])].values 
<init_hyperparams>
train=pd.read_csv(".. /input/X_train.csv")
 test=pd.read_csv(".. /input/X_test.csv")
 label=pd.read_csv(".. /input/y_train.csv" )
print('Training lightgbm')
 params = {"objective" : "binary",
"metric" : "binary_logloss",
"num_leaves" : 60,
"max_depth": -1,
"learning_rate" : 0.01,
"bagging_fraction" : 0.9,
"feature_fraction" : 0.9,
"bagging_freq" : 5,
"bagging_seed" : 2333,
"verbosity" : -1 }
lgtrain, lgval = lgb.Dataset(Xt, Yt[:,0]), lgb.Dataset(Xv, Yv[:,0])
 lgbmodel = lgb.train(params, lgtrain, 2000, valid_sets=[lgtrain, lgval], early_stopping_rounds=100, verbose_eval=200)
<define_variables>
def feature_extraction(raw_frame):
frame = pd.DataFrame()
raw_frame['orientation'] = raw_frame['orientation_X'] + raw_frame['orientation_Y'] + raw_frame['orientation_Z']+ raw_frame['orientation_W']
raw_frame['angular_velocity'] = raw_frame['angular_velocity_X'] + raw_frame['angular_velocity_Y'] + raw_frame['angular_velocity_Z']
raw_frame['linear_acceleration'] = raw_frame['linear_acceleration_X'] + raw_frame['linear_acceleration_Y'] + raw_frame['linear_acceleration_Y']
raw_frame['velocity_to_acceleration'] = raw_frame['angular_velocity'] / raw_frame['linear_acceleration']
raw_frame['velocity_linear_acceleration'] = raw_frame['linear_acceleration'] * raw_frame['angular_velocity']
for col in raw_frame.columns[3:]:
frame[col + '_mean'] = raw_frame.groupby(['series_id'])[col].mean()
frame[col + '_std'] = raw_frame.groupby(['series_id'])[col].std()
frame[col + '_max'] = raw_frame.groupby(['series_id'])[col].max()
frame[col + '_min'] = raw_frame.groupby(['series_id'])[col].min()
frame[col + '_max_to_min'] = frame[col + '_max'] / frame[col + '_min']
frame[col + '_mean_abs_change'] = raw_frame.groupby('series_id')[col].apply(lambda x: np.mean(np.abs(np.diff(x))))
frame[col + '_abs_max'] = raw_frame.groupby('series_id')[col].apply(lambda x: np.max(np.abs(x)))
 return frame
preddays = env.get_prediction_days() 
<predict_on_test>
train = feature_extraction(train)
 test = feature_extraction(test)
 train.head()
print("generating predictions...")
 add_features=['pricevolume' , 'bartrend' , 'average' ]
for(marketdf, newsdf, predtemplatedf)in preddays:
cdf = prepare_data(marketdf, newsdf)
 Xp = cdf[traincols].fillna(0 ).values
preds = lgbmodel.predict(Xp, num_iteration=lgbmodel.best_iteration)* 2 - 1
predsdf = pd.DataFrame({'ast':cdf['assetCode'],'conf':preds})
 predtemplatedf['confidenceValue'][predtemplatedf['assetCode'].isin(predsdf.ast)] = predsdf['conf'].values
env.predict(predtemplatedf)
 env.write_submission_file()
print('done!!!!!!!' )<set_options>
label=label["surface"]
%matplotlib inline 
<set_options>
label_encoder = preprocessing.LabelEncoder()
label_encoder.fit(label)
 label=label_encoder.transform(label )
plt.style.use("ggplot" )<import_modules>
Y_train = label
X_train = train
test=test
X_train.shape, Y_train.shape
scaler = StandardScaler()
scaler.fit(X_train)
 train_img = scaler.transform(X_train)
 test_img = scaler.transform(test )
import pandas as pd
import numpy as np<import_modules>
kfold = StratifiedKFold(n_splits=5)
 train_x,val_x,train_y,val_y = train_test_split(X_train, Y_train, test_size = 0.10, random_state=14)
 train_x.shape,val_x.shape,train_y.shape,val_y.shape
import pandas as pd
import numpy as np<define_variables>
train_data = lightgbm.Dataset(train_x, label=train_y)
 test_data = lightgbm.Dataset(val_x, label=val_y )
np.random.seed(1234)
 random.seed(1234 )<load_from_csv>
para={'boosting_type': 'gbdt',
'colsample_bytree': 0.85,
'learning_rate': 0.1,
'max_bin': 512,
'max_depth': -1,
'metric': 'multi_error',
'min_child_samples': 8,
'min_child_weight': 1,
'min_split_gain': 0.5,
'nthread': 3,
'num_class': 9,
'num_leaves': 31,
'objective': 'multiclass',
'reg_alpha': 0.8,
'reg_lambda': 1.2,
'scale_pos_weight': 1,
'subsample': 0.7,
'subsample_for_bin': 200,
'subsample_freq': 1}
model = lightgbm.train(para,
train_data,
valid_sets=test_data,
num_boost_round=5000,
early_stopping_rounds=50,
)
train_df = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/train.csv")
 test_df = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/test.csv")
 submission = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv" )<count_values>
y_pred = model.predict(test )
train_df["MSZoning"].value_counts()<concatenate>
class_prediction=pd.DataFrame(y_pred ).idxmax(axis=1 )
all_df = pd.concat([train_df, test_df], sort=False ).reset_index(drop=True )<import_modules>
submission1 = pd.DataFrame({
"series_id": test.index,
"surface": class_prediction,
})
 submission1.surface.value_counts()
from sklearn.preprocessing import LabelEncoder<filter>
submission1.surface=label_encoder.inverse_transform(submission1.surface )
categories = all_df.columns[all_df.dtypes == "object"]
print(categories )<count_values>
submission1.surface.value_counts()
all_df["Alley"].value_counts()<categorify>
submission1.to_csv('submission1.csv', index=False )