Kaggle competition: Indoor Location & Navigation
<SOS> metric: IndoorLocalization Kaggle data source: indoor-location-&-navigation<feature_engineering>
!git clone --depth 1 https://github.com/location-competition/indoor-location-competition-20 indoor_location_competition_20
!rm -rf indoor_location_competition_20/data
test_data['Survived'] = predictions<save_to_csv>
import multiprocessing
import numpy as np
import pandas as pd
import scipy.interpolate
import scipy.sparse
from tqdm import tqdm
from indoor_location_competition_20.io_f import read_data_file
import indoor_location_competition_20.compute_f as compute_f
test_data[['PassengerId', 'Survived']].to_csv('submission.csv', index=False )<install_modules>
INPUT_PATH = '.. /input/indoor-location-navigation'
!pip install --no-index --find-links /kaggle/input/pytorchtabnet/pytorch_tabnet-2.0.0-py3-none-any.whl pytorch-tabnet
!pip install /kaggle/input/iterative-stratification/iterative-stratification-master/<import_modules>
def compute_rel_positions(acce_datas, ahrs_datas):
step_timestamps, step_indexs, step_acce_max_mins = compute_f.compute_steps(acce_datas)
 headings = compute_f.compute_headings(ahrs_datas)
 stride_lengths = compute_f.compute_stride_length(step_acce_max_mins)
 step_headings = compute_f.compute_step_heading(step_timestamps, headings)
 rel_positions = compute_f.compute_rel_positions(stride_lengths, step_headings)
 return rel_positions
sys.path.append('.. /input/iterativestratification')
 warnings.filterwarnings('ignore')
 os.listdir('.. /input/lish-moa')
 pd.set_option('max_columns', 2000 )<init_hyperparams>
order = 6
fs = 100
cutoff = 3.667
step_distance = 0.8
w_height = 1.7
m_trans = -2
n_comp_GENES = 463
n_comp_CELLS = 60
VarianceThreshold_for_FS = 0.9
Dropout_Model = 0.25
print('n_comp_GENES', n_comp_GENES, 'n_comp_CELLS', n_comp_CELLS, 'total', n_comp_GENES + n_comp_CELLS )<load_from_csv>
def butter_lowpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
 return b, a
def butter_lowpass_filter(data, cutoff, fs, order=5):
b, a = butter_lowpass(cutoff, fs, order=order)
 y = lfilter(b, a, data)
 return y
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets_scored = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sample_submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )<create_dataframe>
def peak_accel_threshold(data, timestamps, threshold):
d_acc = []
last_state = 'below'
crest_troughs = 0
crossings = []
for i, datum in enumerate(data):
current_state = last_state
if datum < threshold:
current_state = 'below'
elif datum > threshold:
current_state = 'above'
if current_state is not last_state:
if current_state is 'above':
crossing = [timestamps[i], threshold]
crossings.append(crossing)
 else:
crossing = [timestamps[i], threshold]
crossings.append(crossing)
 crest_troughs += 1
last_state = current_state
return np.array(crossings )
train_features2=train_features.copy()
test_features2=test_features.copy()<define_variables>
def steps_compute_rel_positions(sample_file):
mix_acce = np.sqrt(sample_file.acce[:,1:2]**2 + sample_file.acce[:,2:3]**2 + sample_file.acce[:,3:4]**2)
 mix_acce = np.concatenate([sample_file.acce[:,0:1], mix_acce], 1)
 mix_df = pd.DataFrame(mix_acce)
 mix_df.columns = ["timestamp","acce"]
filtered = butter_lowpass_filter(mix_df["acce"], cutoff, fs, order)
 threshold = filtered.mean() * 1.1
crossings = peak_accel_threshold(filtered, mix_df["timestamp"], threshold)
 step_sum = len(crossings)/2
distance = w_height * 0.4 * step_sum
mag_df = pd.DataFrame(sample_file.magn)
 mag_df.columns = ["timestamp","x","y","z"]
acce_df = pd.DataFrame(sample_file.acce)
 acce_df.columns = ["timestamp","ax","ay","az"]
mag_df = pd.merge(mag_df,acce_df,on="timestamp")
 mag_df.dropna()
time_di_list = []
for i in mag_df.iterrows() :
gx,gy,gz = i[1][1],i[1][2],i[1][3]
ax,ay,az = i[1][4],i[1][5],i[1][6]
roll = math.atan2(ay,az)
 pitch = math.atan2(-1*ax ,(ay * math.sin(roll)+ az * math.cos(roll)))
 q = m_trans - math.degrees(math.atan2(
(gz*math.sin(roll)-gy*math.cos(roll)) ,(gx*math.cos(pitch)+ gy*math.sin(roll)*math.sin(pitch)+ gz*math.sin(pitch)*math.cos(roll)) 
)) -90
if q <= 0:
q += 360
time_di_list.append(( i[1][0],q))
d_list = [x[1] for x in time_di_list]
steps = []
step_time = []
di_dict = dict(time_di_list)
 for n,i in enumerate(crossings[:,:1]):
if n % 2 == 1:
continue
direct_now = di_dict[i[0]]
dx = math.sin(math.radians(direct_now))
dy = math.cos(math.radians(direct_now))
steps.append(( i[0],dx,dy))
step_time.append(i[0])
 step_dtime = np.diff(step_time)/1000
step_dtime = step_dtime.tolist()
step_dtime.insert(0,5)
 rel_position = []
wp_idx = 0
for p,i in enumerate(steps):
step_distance = 0
if step_dtime[p] >= 1:
step_distance = w_height*0.25
elif step_dtime[p] >= 0.75:
step_distance = w_height*0.3
elif step_dtime[p] >= 0.5:
step_distance = w_height*0.4
elif step_dtime[p] >= 0.35:
step_distance = w_height*0.45
elif step_dtime[p] >= 0.2:
step_distance = w_height*0.5
else:
step_distance = w_height*0.4
rel_position.append([i[0], i[1]*step_distance, i[2]*step_distance])
 return np.array(rel_position )
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]<feature_engineering>
def correct_path(args):
path, path_df = args
T_ref = path_df['timestamp'].values
xy_hat = path_df[['x', 'y']].values
example = read_data_file(f'{INPUT_PATH}/test/{path}.txt')
 rel_positions1 = compute_rel_positions(example.acce, example.ahrs)
 rel_positions2 = steps_compute_rel_positions(example)
 rel1 = rel_positions1.copy()
rel2 = rel_positions2.copy()
rel1[:,1:] = rel_positions1[:,1:] / 2
rel2[:,1:] = rel_positions2[:,1:] / 2
rel_positions = np.vstack([rel1,rel2])
 rel_positions = rel_positions[np.argsort(rel_positions[:, 0])]
if T_ref[-1] > rel_positions[-1, 0]:
rel_positions = [np.array([[0, 0, 0]]), rel_positions, np.array([[T_ref[-1], 0, 0]])]
else:
rel_positions = [np.array([[0, 0, 0]]), rel_positions]
rel_positions = np.concatenate(rel_positions)
 T_rel = rel_positions[:, 0]
delta_xy_hat = np.diff(scipy.interpolate.interp1d(T_rel, np.cumsum(rel_positions[:, 1:3], axis=0), axis=0 )(T_ref), axis=0)
 N = xy_hat.shape[0]
delta_t = np.diff(T_ref)
 alpha =(8.1)**(-2)* np.ones(N)
 beta =(0.3 + 0.3 * 1e-3 * delta_t)**(-2)
 A = scipy.sparse.spdiags(alpha, [0], N, N)
 B = scipy.sparse.spdiags(beta, [0], N-1, N-1)
 D = scipy.sparse.spdiags(np.stack([-np.ones(N), np.ones(N)]), [0, 1], N-1, N)
 Q = A +(D.T @ B @ D)
 c =(A @ xy_hat)+(D.T @(B @ delta_xy_hat))
xy_star = scipy.sparse.linalg.spsolve(Q, c)
 return pd.DataFrame({
'site_path_timestamp' : path_df['site_path_timestamp'],
'floor' : path_df['floor'],
'x' : xy_star[:, 0],
'y' : xy_star[:, 1],
} )
<feature_engineering><EOS> 
sub = pd.read_csv('.. /input/indoor-loc-and-nav-subs/submission_4475.csv')
 tmp = sub['site_path_timestamp'].apply(lambda s : pd.Series(s.split('_')))
 sub['site'] = tmp[0]
sub['path'] = tmp[1]
sub['timestamp'] = tmp[2].astype(float)
 processes = multiprocessing.cpu_count()
with multiprocessing.Pool(processes=processes)as pool:
dfs = pool.imap_unordered(correct_path, sub.groupby('path'))
dfs = tqdm(dfs)
 dfs = list(dfs)
 sub = pd.concat(dfs ).sort_values('site_path_timestamp')
 sub.to_csv('submission.csv', index=False )