Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<create_dataframe>
import numpy as np
import pandas as pd
from sklearn import ensemble
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_validate,GridSearchCV
import lightgbm as lgbm
from collections import Counter
df_pred_err = pd.DataFrame()
df_pred_err ['id'] = pred_id
df_pred_err['error'] = result_errors<save_to_csv>
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv" )
df_pred_err.to_csv('prediction.csv',sep=",",index = False )<compute_test_metric>
y = train['Survived']
X = train.drop(['Survived'], axis=1)
 X_test = test
def smape(predict, test):
return np.mean(2 * np.abs(test - predict)/(np.abs(test)+ np.abs(predict)))* 100
def get_sat(sat_id):
sat = df.loc[df['sat_id'] == sat_id].copy()
sat.drop(sat[sat['timedelta'] == 1].index, inplace=True)
 return sat
def simple_period(sat_id):
def get_loc_extr(arr):
loc_max_ind_ = np.where(( arr[1:-1] > arr[:-2])&(arr[1:-1] > arr[2:]))
loc_min_ind_ = np.where(( arr[1:-1] < arr[:-2])&(arr[1:-1] < arr[2:]))
return loc_max_ind_, loc_min_ind_
sat = get_sat(sat_id)
 train = sat.loc[sat['type'] == 'train']
x_loc_max, x_loc_min = get_loc_extr(np.array(train['x']))
y_loc_max, y_loc_min = get_loc_extr(np.array(train['y']))
z_loc_max, z_loc_min = get_loc_extr(np.array(train['z']))
return int(np.concatenate([
x_loc_max[0][1:] - x_loc_max[0][:-1], x_loc_min[0][1:] - x_loc_min[0][:-1],
y_loc_max[0][1:] - y_loc_max[0][:-1], y_loc_min[0][1:] - y_loc_min[0][:-1],
z_loc_max[0][1:] - z_loc_max[0][:-1], z_loc_min[0][1:] - z_loc_min[0][:-1],
] ).mean())
 plt.rcParams['figure.figsize'] = [16, 8]
df = pd.read_csv('/kaggle/input/sputnik/train.csv')
 df['datetime'] = list(map(lambda x: datetime.datetime.strptime(x, "%Y-%m-%dT%H:%M:%S.%f"), df['epoch']))
df['timedelta'] = np.zeros(len(df.index))
df['timedelta'].values[1:] =(df['datetime'].values[1:] - df['datetime'].values[:-1])/ np.timedelta64(1, 'ms')
 df['error'] = np.linalg.norm(df[['x', 'y', 'z']].values - df[['x_sim', 'y_sim', 'z_sim']].values, axis=1)
 def regressed(train, test, period, target):
sat_df = pd.DataFrame({target: pd.concat(( train, test), axis=0)[target].to_numpy() })
 features = []
for period_mult in range(int(np.ceil(len(test)/ period)) , min(len(test), int(len(train)/ period))):
sat_df["lag_period_{}".format(period_mult)] = sat_df[target].shift(period_mult * period)
 features.append("lag_period_{}".format(period_mult))
sat_df['lagf_mean'] = sat_df[features].mean(axis=1)
 features.extend(['lagf_mean'])
 train_df = sat_df[:-len(test)].dropna()
test_df = sat_df[-len(test):][features]
lin_reg = LinearRegression()
lin_reg.fit(train_df.drop(target, axis=1), train_df[target])
 result = lin_reg.predict(test_df)
 return result
def regressed(train, test, period, target):
result = []
sat_df = pd.DataFrame({target: pd.concat(( train, test), axis=0)[target].to_numpy() })
 features = []
for period_mult in range(1, int(np.ceil(len(train)/ period)) - 1):
sat_df["lag_period_{}".format(period_mult)] = sat_df[target].shift(period_mult * period)
 features.append("lag_period_{}".format(period_mult))
features.extend(['lagf_mean'])
 for i in range(int(np.ceil(len(test)/ period))):
sat_df['lagf_mean'] = sat_df[features[i:-1]].mean(axis=1)
 train_df = sat_df[:-len(test)][[target] + features[i:]].dropna().copy()
train_df['lagf_mean'] = train_df[features[i:]].mean(axis=1)
 if -len(test)+(i + 1)* period >= 0:
test_df = sat_df[-len(test)+ i * period:][features[i:]]
else:
test_df = sat_df[-len(test)+ i * period:-len(test)+(i + 1)* period][features[i:]]
lin_reg = LinearRegression()
lin_reg.fit(train_df.drop(target, axis=1), train_df[target])
 result.extend(lin_reg.predict(test_df))
result.extend([0] *(len(test)- len(result)))
 return result
warnings.filterwarnings('ignore')
 for sat_id in tqdm(np.unique(df['sat_id'])) :
sat = get_sat(sat_id)
 train_ = sat.loc[sat['type'] == 'train']
test_ = sat.loc[sat['type'] == 'test']
period_ = simple_period(sat_id)
 for target_ in ['x', 'y', 'z']:
sat.loc[sat['type'] == 'test', target_] = regressed(train_, test_, period_, target_)
 pred = sat.loc[sat['type'] == 'test']
pred['error'] = np.linalg.norm(pred[['x', 'y', 'z']].values - pred[['x_sim', 'y_sim', 'z_sim']].values, axis=1)
 for k in ['error', 'x', 'y', 'z']:
df.loc[(df['sat_id'] == sat_id)&(df['type'] == 'test')&(df['timedelta'] != 1), k] = pred[k]
warnings.filterwarnings('default')
 for index, row in df.loc[(df['timedelta'] == 1)&(df['type'] == 'test')].iterrows() :
if np.isnan(df['error'][index]):
df['error'][index] = np.linalg.norm([df['x'][index - 1] - df['x_sim'][index],
df['y'][index - 1] - df['y_sim'][index],
df['z'][index - 1] - df['z_sim'][index]])
 df.loc[df['type'] == 'test'][['id', 'error']].to_csv('submission.csv', index=False )<set_options>
params={"n_estimators":np.arange(50,200,10),
"max_depth":np.arange(1,11,1),
"learning_rate":[0.1,0.01,0.001]
}
warnings.filterwarnings('ignore')
<load_from_csv>
xgb_est=lgbm.LGBMClassifier(
 random_state=42,
objective='binary',
eval_metric="auc" 
)
 gr_xgb_est=GridSearchCV(xgb_est,param_grid=params,cv=5,n_jobs=-1,verbose=10)
 gr_xgb_est.fit(X,y )
initi = pd.read_csv('/kaggle/input/sputnik/train.csv' )<prepare_x_and_y>
gr_xgb_est.best_estimator_.get_params()
lag_period = 24
resultsx = pd.DataFrame()
for j in range(600):
dfr = initi[initi.sat_id==j][['x','type']]
train = dfr[dfr.type=='train'].reset_index(drop=True)
 test = dfr[dfr.type=='test'].reset_index(drop=True)
 i=0
while(i*24<len(test)) :
df = pd.concat(( train, test), axis = 0, ignore_index=True)
 df['target'] = df.x
features = []
for period_mult in range(1,3,1):
df["lag_period_{}".format(period_mult)] = df.target.shift(period_mult*lag_period)
 features.append("lag_period_{}".format(period_mult))
df['lagf_mean'] = df[features].mean(axis = 1)
 features.extend(['lagf_mean'])
 model = LinearRegression()
train_df = df[df.type=='train'][features + ['target']].dropna()
test_df = df[df.type=='test'][features].reset_index(drop=True)
 test_df = test_df.loc[:23,:]
model.fit(train_df.drop(['target'], axis = 1), train_df['target'])
 forecast = model.predict(test_df)
 test.loc[i*24:i*24+23,:]['x'] = forecast
test.loc[i*24:i*24+23,:]['type'] = 'train'
i+=1
resultsx = pd.concat(( resultsx, test),axis=0, ignore_index=True )<drop_column>
pred_test = gr_xgb_est.predict(X_test )
resultsx.drop(['type'], axis=1, inplace=True )<prepare_x_and_y>
submission = pd.read_csv('.. /input/titanic/gender_submission.csv')
 submission['Survived'] =(pred_test > 0.5 ).astype(int)
 submission.to_csv('sub.csv', index=False)
 submission.head()