def get_features_from_one_signal(X, sample_rate=50): assert X.ndim == 1, "Expected single signal in feature extraction" mean = np.mean(X) stdev = np.std(X) abs_energy = fc.abs_energy(X) sum_of_changes = fc.absolute_sum_of_changes(X) autoc = fc.autocorrelation(X, sample_rate) count_above_mean = fc.count_above_mean(X) count_below_mean = fc.count_below_mean(X) kurtosis = fc.kurtosis(X) longest_above = fc.longest_strike_above_mean(X) zero_crossing = fc.number_crossing_m(X, mean) num_peaks = fc.number_peaks(X, int(sample_rate / 10)) sample_entropy = fc.sample_entropy(X) spectral_density = fc.spkt_welch_density(X, [{ "coeff": 1 }, { "coeff": 2 }, { "coeff": 3 }, { "coeff": 4 }, { "coeff": 5 }, { "coeff": 6 }]) c, v = zip(*spectral_density) v = np.asarray(v) return [ mean, stdev, abs_energy, sum_of_changes, autoc, count_above_mean, count_below_mean, kurtosis, longest_above, zero_crossing, num_peaks, sample_entropy, v[0], v[1], v[2], v[3], v[4], v[5] ]
def sample_entropy(mag): """Returns sample entropy: http://en.wikipedia.org/wiki/Sample_Entropy rtype: float """ entropy = ts.sample_entropy(mag) return entropy
def TS_features11(signal): percentage_of_reoccurring = ts.percentage_of_reoccurring_datapoints_to_all_datapoints( signal) percentage_of_reoccurring_values = ts.percentage_of_reoccurring_values_to_all_values( signal) ratio_value_number = ts.ratio_value_number_to_time_series_length(signal) sample_entropy = ts.sample_entropy(signal) skewness = ts.skewness(signal) return percentage_of_reoccurring, percentage_of_reoccurring_values, ratio_value_number, sample_entropy, skewness
def scalar_feature_extraction(column): retval = np.zeros([1, 10], dtype=float) retval[0][0] = tffe.count_above_mean(column.values) retval[0][1] = tffe.mean(column.values) retval[0][2] = tffe.maximum(column.values) retval[0][3] = tffe.median(column.values) retval[0][4] = tffe.minimum(column.values) retval[0][5] = tffe.sample_entropy(column.values) if (isNaN(retval[0][5])): retval[0][5] = 0 retval[0][6] = tffe.skewness(column.values) retval[0][7] = tffe.variance(column.values) retval[0][8] = tffe.longest_strike_above_mean(column.values) retval[0][9] = tffe.longest_strike_below_mean(column.values) return retval
def entropy(self): return np.array([ tcal.sample_entropy(self.data[i, :]) for i in range(len(self.data)) ])
def sample_entropy(current_observation: pd.DataFrame, raw_key: str): return tsf.sample_entropy(current_observation[raw_key])
Real_output = pd.read_csv(r"Real_output.csv") dataset = Real_output.drop(columns='Class', axis=1) ##Create dataframe for feature selection feat_dataset = pd.DataFrame() #Calculated columns feat_dataset['CGM_Min'] = dataset.min(axis=1) feat_dataset['CGM_Max'] = dataset.max(axis=1) #Reset index after merging different files into one feat_dataset.reset_index(drop=True, inplace=True) ##Populate feature characteristics ##ENTROPY feat_dataset['CGM_Entropy'] = np.nan for i in range(len(dataset)): feat_dataset['CGM_Entropy'][i] = ts.sample_entropy( np.array(dataset.iloc[i, :])) ##RMS feat_dataset['CGM_RMS'] = np.nan for i in range(len(dataset)): feat_dataset['CGM_RMS'][i] = np.sqrt(np.mean(dataset.iloc[i, :]**2)) #Correlation feat_dataset['CGM_Correlation'] = np.nan for i in range(len(dataset)): feat_dataset['CGM_Correlation'][i] = ts.autocorrelation( np.array(dataset.iloc[i, :]), 1) ##Number_of_Peaks feat_dataset['CGM_Peaks'] = np.nan for i in range(len(dataset)): feat_dataset['CGM_Peaks'][i] = ts.number_peaks( np.array(dataset.iloc[i, :]), 2) #CGM Velocity
def feat_extraction(dataset): feat_dataset = pd.DataFrame(index=np.arange(len(dataset))) #Calculated columns feat_dataset['CGM_Min'] = dataset.min(axis=1) feat_dataset['CGM_Max'] = dataset.max(axis=1) ##ENTROPY feat_dataset['CGM_Entropy'] = np.nan for i in range(len(dataset)): feat_dataset['CGM_Entropy'][i] = ts.sample_entropy( np.array(dataset.iloc[i, :])) ##RMS feat_dataset['CGM_RMS'] = np.nan for i in range(len(dataset)): feat_dataset['CGM_RMS'][i] = np.sqrt(np.mean(dataset.iloc[i, :]**2)) #Correlation feat_dataset['CGM_Correlation'] = np.nan for i in range(len(dataset)): feat_dataset['CGM_Correlation'][i] = ts.autocorrelation( np.array(dataset.iloc[i, :]), 1) ##Number_of_Peaks feat_dataset['CGM_Peaks'] = np.nan for i in range(len(dataset)): feat_dataset['CGM_Peaks'][i] = ts.number_peaks( np.array(dataset.iloc[i, :]), 2) #CGM Velocity feat_dataset['CGM_Velocity'] = np.nan for i in range(len(dataset)): c_list = dataset.loc[i, :].tolist() sum_ = [] for j in range(1, len(c_list)): sum_.append(abs(c_list[j] - c_list[j - 1])) feat_dataset['CGM_Velocity'][i] = np.round(np.mean(sum_), 2) #MinMax feat_dataset['CGM_MinMax'] = np.nan feat_dataset[ 'CGM_MinMax'] = feat_dataset['CGM_Max'] - feat_dataset['CGM_Min'] ##SKewness feat_dataset['CGM_Skewness'] = np.nan for i in range(len(dataset)): feat_dataset['CGM_Skewness'][i] = ts.skewness(dataset.loc[i, :]) #CGM_Displacement feat_dataset['CGM_Displacement'] = np.nan for i in range(len(dataset)): c_list = dataset.loc[i, :].tolist() sum_ = [] for j in range(1, len(c_list)): sum_.append(abs(c_list[j] - c_list[j - 1])) feat_dataset['CGM_Displacement'][i] = np.round(np.sum(sum_), 2) #CGM_Kurtosis feat_dataset['CGM_Kurtosis'] = np.nan for i in range(len(dataset)): feat_dataset['CGM_Kurtosis'][i] = ts.kurtosis( np.array(dataset.iloc[i, :])) #Recurr feat_dataset['CGM_Recur'] = np.nan for i in range(len(dataset)): feat_dataset['CGM_Recur'][ i] = ts.ratio_value_number_to_time_series_length( np.array(dataset.iloc[i, :])) #Remove calculated columns del feat_dataset['CGM_Max'] del feat_dataset['CGM_Min'] feat_dataset = feat_dataset[[ 'CGM_Entropy', 'CGM_RMS', 'CGM_Correlation', 'CGM_Peaks', 'CGM_Velocity', 'CGM_MinMax', 'CGM_Skewness', 'CGM_Displacement', 'CGM_Kurtosis', 'CGM_Recur' ]] return feat_dataset