<jupyter_start><jupyter_text>Feature Engineering<jupyter_code># import updated data
data = pd.read_csv("./outputs/updated_data_02.csv")#, header=None, names=["timestamp","chan0_raw","chan1_raw","chan0_hp","chan1_hp","quat_x","quat_y","quat_z","quat_w","gyro_x","gyro_y","gyro_z","acc_x","acc_y","acc_z","label","rep","trial"])

data
data = data[["timestamp","chan0_hp","chan1_hp","gyro_x","gyro_y","gyro_z","acc_x","acc_y","acc_z","label","rep","trial"]]<jupyter_output><empty_output><jupyter_text>Sliding Window Segmentation<jupyter_code># function to segment data into overlapping windows

def windowed_view(arr, window, overlap):
    arr = np.asarray(arr)
    window_step = window - overlap
    new_shape = arr.shape[:-1] + ((arr.shape[-1] - overlap) // window_step,
                                  window)
    new_strides = (arr.strides[:-1] + (window_step * arr.strides[-1],) +
                   arr.strides[-1:])
    return as_strided(arr, shape=new_shape, strides=new_strides)
# create overlapping segments for each feature
# separate by trial so that windows don't contain discontinous data

acc_x_all_windows = []
acc_y_all_windows = []
acc_z_all_windows = []
gyro_x_all_windows = []
gyro_y_all_windows = []
gyro_z_all_windows = []
chan0_hp_all_windows = []
chan1_hp_all_windows = []

for trial_num in sorted(data["trial"].unique()):
    mask = (data["trial"] == trial_num)
    acc_x_windows = windowed_view(data[mask].acc_x,500,250)
    acc_y_windows = windowed_view(data[mask].acc_y,500,250)
    acc_z_windows = windowed_view(data[mask].acc_z,500,250)
    gyro_x_windows = windowed_view(data[mask].gyro_x,500,250)
    gyro_y_windows = windowed_view(data[mask].gyro_y,500,250)
    gyro_z_windows = windowed_view(data[mask].gyro_z,500,250)
    chan0_hp_windows = windowed_view(data[mask].chan0_hp,500,250)
    chan1_hp_windows = windowed_view(data[mask].chan1_hp,500,250)
    
    acc_x_all_windows.append(acc_x_windows.tolist())
    acc_y_all_windows.append(acc_y_windows.tolist())
    acc_z_all_windows.append(acc_z_windows.tolist())
    gyro_x_all_windows.append(gyro_x_windows.tolist())
    gyro_y_all_windows.append(gyro_y_windows.tolist())
    gyro_z_all_windows.append(gyro_z_windows.tolist())
    chan0_hp_all_windows.append(chan0_hp_windows.tolist())
    chan1_hp_all_windows.append(chan1_hp_windows.tolist())
    
# flatten nested lists

acc_x_all_windows = [item for sublist in acc_x_all_windows for item in sublist]
acc_y_all_windows = [item for sublist in acc_y_all_windows for item in sublist]
acc_z_all_windows = [item for sublist in acc_z_all_windows for item in sublist]
gyro_x_all_windows = [item for sublist in gyro_x_all_windows for item in sublist]
gyro_y_all_windows = [item for sublist in gyro_y_all_windows for item in sublist]
gyro_z_all_windows = [item for sublist in gyro_z_all_windows for item in sublist]
chan0_hp_all_windows = [item for sublist in chan0_hp_all_windows for item in sublist]
chan1_hp_all_windows = [item for sublist in chan1_hp_all_windows for item in sublist]<jupyter_output><empty_output><jupyter_text>Calculate features for each window<jupyter_code># zero_crossings function

def zero_crossings(x):
    ZC=0;
    for pos in range(len(x)-1):
        if ((x[pos] > 0 and x[pos+1] < 0) or (x[pos] < 0 and x[pos+1] > 0)) and (np.abs(x[pos]-x[pos+1]) >= 0):
            ZC=ZC+1;
    return ZC
# Integral of window

acc_x_integrals = scipy.integrate.simps(acc_x_all_windows)
acc_y_integrals = scipy.integrate.simps(acc_y_all_windows)
acc_z_integrals = scipy.integrate.simps(acc_z_all_windows)
gyro_x_integrals = scipy.integrate.simps(gyro_x_all_windows)
gyro_y_integrals = scipy.integrate.simps(gyro_y_all_windows)
gyro_z_integrals = scipy.integrate.simps(gyro_z_all_windows)
chan0_integrals = scipy.integrate.simps(chan0_hp_all_windows)
chan1_integrals = scipy.integrate.simps(chan0_hp_all_windows)

# Variance of window

acc_x_variance = [statistics.variance(x) for x in acc_x_all_windows]
acc_y_variance = [statistics.variance(x) for x in acc_y_all_windows]
acc_z_variance = [statistics.variance(x) for x in acc_z_all_windows]
gyro_x_variance = [statistics.variance(x) for x in gyro_x_all_windows]
gyro_y_variance = [statistics.variance(x) for x in gyro_y_all_windows]
gyro_z_variance = [statistics.variance(x) for x in gyro_z_all_windows]
chan0_ln_variance = [statistics.variance(x) for x in chan0_hp_all_windows]
chan1_ln_variance = [statistics.variance(x) for x in chan1_hp_all_windows]

# Absolute value of mean of window

acc_x_mean = [np.abs(np.mean(x)) for x in acc_x_all_windows]
acc_y_mean = [np.abs(np.mean(x)) for x in acc_y_all_windows]
acc_z_mean = [np.abs(np.mean(x)) for x in acc_z_all_windows]
gyro_x_mean = [np.abs(np.mean(x)) for x in gyro_x_all_windows]
gyro_y_mean = [np.abs(np.mean(x)) for x in gyro_y_all_windows]
gyro_z_mean = [np.abs(np.mean(x)) for x in gyro_z_all_windows]
chan0_mean = [np.abs(np.mean(x)) for x in chan0_hp_all_windows]
chan1_mean = [np.abs(np.mean(x)) for x in chan1_hp_all_windows]

# Zero-crossings count per window

acc_x_zc = [zero_crossings(x) for x in acc_x_all_windows]
acc_y_zc = [zero_crossings(x) for x in acc_y_all_windows]
acc_z_zc = [zero_crossings(x) for x in acc_z_all_windows]
gyro_x_zc = [zero_crossings(x) for x in gyro_x_all_windows]
gyro_y_zc = [zero_crossings(x) for x in gyro_y_all_windows]
gyro_z_zc = [zero_crossings(x) for x in gyro_z_all_windows]

# create dataframe of all window features

data_features = pd.DataFrame({"acc_x_integrals": acc_x_integrals,
                             "acc_x_integrals": acc_y_integrals,
                             "acc_x_integrals": acc_z_integrals,
                             "gyro_x_integrals": gyro_x_integrals,
                             "gyro_y_integrals": gyro_y_integrals,
                             "gyro_z_integrals": gyro_z_integrals,
                             "acc_x_variance": acc_x_variance,
                             "acc_y_variance": acc_y_variance,
                             "acc_z_variance": acc_z_variance,
                             "gyro_x_variance": gyro_x_variance,
                             "gyro_y_variance": gyro_y_variance,
                             "gyro_z_variance": gyro_z_variance,
                             "acc_x_mean": acc_x_mean,
                             "acc_y_mean": acc_y_mean,
                             "acc_z_mean": acc_z_mean,
                             "gyro_x_mean": gyro_x_mean,
                             "gyro_y_mean": gyro_y_mean,
                             "gyro_z_mean": gyro_z_mean,
                             "acc_x_zc": acc_x_zc,
                             "acc_y_zc": acc_y_zc,
                             "acc_z_zc": acc_z_zc,
                             "gyro_x_zc": gyro_x_zc,
                             "gyro_y_zc": gyro_y_zc,
                             "gyro_z_zc": gyro_z_zc,
                             "chan0_integrals": chan0_integrals,
                             "chan1_integrals": chan1_integrals,
                             "chan0_ln_variance": chan0_ln_variance,
                             "chan1_ln_variance": chan1_ln_variance,
                             "chan0_mean": chan0_mean,
                             "chan1_mean": chan1_mean}
                             )<jupyter_output><empty_output><jupyter_text>Principal Component Analysis<jupyter_code># Instantiate our StandardScaler
ss = StandardScaler()

# Standardize feature data
data_scaled = ss.fit_transform(data_features)
# Instantiate PCA.
pca = PCA(random_state = 42)

# Fit PCA
pca.fit(data_scaled)

PCA(copy=True, iterated_power='auto', n_components=None, random_state=42,
    svd_solver='auto', tol=0.0, whiten=False)

# Transform PCA
data_pca = pca.transform(data_scaled)<jupyter_output><empty_output><jupyter_text>Variance explained by component<jupyter_code>var_exp = pca.explained_variance_ratio_
print(f'Explained variance: {np.round(var_exp,3)}')

# Generate the cumulative explained variance.
cum_var_exp = np.cumsum(var_exp)
print(f'Cumulative explained variance: {np.round(cum_var_exp,3)}')<jupyter_output>Explained variance: [0.26  0.178 0.121 0.073 0.065 0.053 0.045 0.042 0.032 0.028 0.024 0.017
 0.012 0.011 0.009 0.008 0.007 0.005 0.003 0.002 0.002 0.001 0.001 0.001
 0.    0.    0.    0.   ]
Cumulative explained variance: [0.26  0.438 0.559 0.632 0.697 0.75  0.795 0.837 0.869 0.897 0.921 0.938
 0.95  0.961 0.97  0.978 0.984 0.989 0.992 0.994 0.996 0.998 0.998 0.999
 1.    1.    1.    1.   ]<jupyter_text>Save out pca dataframe as csv<jupyter_code># pd.DataFrame(data_pca).to_csv(f'./outputs/data_pca_04.csv', index=False)<jupyter_output><empty_output>