Created
October 3, 2023 22:54
-
-
Save petrosDemetrakopoulos/93f283aa5c625b38b84d65ac7a27a010 to your computer and use it in GitHub Desktop.
Fall detector - Feature extraction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def feature_extraction(signal_df, window_size=100, overlap=50): | |
num_components = signal_df.shape[1] | |
num_samples = signal_df.shape[0] | |
# used to rename columns in the extracted features (mentioning the statistical moment) | |
new_col_names_mean = dict() | |
new_col_names_std = dict() | |
new_col_names_skewness = dict() | |
new_col_names_kurtosis = dict() | |
for col in signal_df.columns: | |
new_col_names_mean[col] = col + '_mean' | |
new_col_names_std[col] = col + '_std' | |
new_col_names_skewness[col] = col + '_skewness' | |
new_col_names_kurtosis[col] = col + '_kurtosis' | |
# Initialize empty DataFrames to store results | |
mean_df = pd.DataFrame(columns=new_col_names_mean.values()) | |
std_df = pd.DataFrame(columns=new_col_names_std.values()) | |
skewness_df = pd.DataFrame(columns=new_col_names_skewness.values()) | |
kurtosis_df = pd.DataFrame(columns=new_col_names_kurtosis.values()) | |
# calculate number of total windows | |
num_total_windows = ((num_samples - window_size) // (window_size - overlap)) + 1 | |
# Calculate statistical moments for each window | |
for i in range(num_total_windows): | |
window_start = i*(window_size - overlap) | |
window = signal_df.iloc[window_start:window_start + window_size] | |
# Calculate statistical moments for each component | |
window_mean = window.mean() | |
window_std = window.std() | |
window_skewness = window.apply(skew) | |
window_kurtosis = window.apply(kurtosis) | |
# Append the results to the respective DataFrames | |
mean_df = pd.concat([mean_df, pd.DataFrame([window_mean], columns=new_col_names_mean.values())], ignore_index=True) | |
std_df = pd.concat([std_df, pd.DataFrame([window_std], columns=new_col_names_std.values())], ignore_index=True) | |
skewness_df = pd.concat([skewness_df, pd.DataFrame([window_skewness], columns=new_col_names_skewness.values())], ignore_index=True) | |
kurtosis_df = pd.concat([kurtosis_df, pd.DataFrame([window_kurtosis], columns=new_col_names_kurtosis.values())], ignore_index=True) | |
# Combine the results into a single DataFrame | |
extracted_features = pd.concat([mean_df, std_df, skewness_df, kurtosis_df], axis=1) | |
return extracted_features |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment