def approximate_entropy(self, x, m=None, r=None): """ As in tsfresh \ `approximate_entropy <https://github.com/blue-yonder/tsfresh/blob/master/tsfresh/feature_extraction/\ feature_calculators.py#L1601>`_ Implements a `vectorized approximate entropy algorithm <https://en.wikipedia.org/wiki/Approximate_entropy>`_ For short time-series this method is highly dependent on the parameters, but should be stable for N > 2000, see :cite:`Yentes2013`. Other shortcomings and alternatives discussed in \ :cite:`Richman2000` :param x: the time series to calculate the feature of :type x: pandas.Series :param m: Length of compared run of data :type m: int :param r: Filtering level, must be positive :type r: float :return: Approximate entropy :rtype: float """ if m is None or r is None: m = 2 r = 0.3 entropy = feature_calculators.approximate_entropy(x, m, r) logging.debug("approximate entropy by tsfresh calculated") return entropy
def get_global_feature(self): """ 获取时域全局特征,包含最大值、标准差、平均值 :param hadcropped: :return: """ square_data, square_energy, square_azrate = self.pre_process(method='hanning', ifcrop=True) func = lambda x: [ # feature_calc.autocorrelation(norm(x), 5), np.std(x), feature_calc.approximate_entropy(norm(x), 5, 1), feature_calc.cid_ce(x, normalize=True), feature_calc.count_above_mean(x), feature_calc.first_location_of_minimum(x), feature_calc.first_location_of_maximum(x), feature_calc.last_location_of_maximum(x), feature_calc.last_location_of_minimum(x), feature_calc.longest_strike_above_mean(x), feature_calc.number_crossing_m(x, 0.8*np.max(x)), feature_calc.skewness(x), feature_calc.time_reversal_asymmetry_statistic(x, 5) ] # global features I want to get upper_rate = self.get_upper_rate(square_energy) feature = np.hstack([ [np.mean(norm(square_energy))], [upper_rate], func(square_azrate), func(square_energy) ]) return feature
def TS_features5(signal): # ts features with mts = 2 rts = 6 entropy = ts.approximate_entropy(signal, mts, rts) max_langevin = ts.max_langevin_fixed_point(signal, mts, rts) return entropy, max_langevin
def get_mfcc_feature(self, hadcropped=False): ''' calculate Mel-frequency cepstral coefficients in frequency domain and extract features from MFCC :return: numpy array ''' assert self.frame_per_second not in [32, 64, 128, 256], \ Exception("Cannot operate butterfly computation ," "frame per second should in [32, 64, 128, 256]") hanning_kernel = self.get_window(method='hanning') windowed = self._add_window(hanning_kernel, self.meta_audio_data) # [num_frame, kernel_size] hanning_energy = self.get_energy(self.meta_audio_data, hanning_kernel) if not hadcropped: boundary = self.get_boundary(hanning_energy) cropped = windowed[boundary[0]: boundary[1] + 1, :] frequency = np.vstack([fft.fft(frame.squeeze()) for frame in np.vsplit(cropped, len(cropped))]) else: frequency = np.vstack([fft.fft(windowed)]) frequency = np.abs(frequency) frequency_energy = frequency ** 2 low_freq = self.sr / self.num_per_frame high_freq = self.sr H = self._mfcc_filter(self.mfcc_cof, low_freq, high_freq) S = np.dot(frequency_energy, H.transpose()) # (F, M) cos_ary = self._discrete_cosine_transform() mfcc_raw_features = np.sqrt(2 / self.mfcc_cof) * np.dot(S, cos_ary) # (F,N) upper = [self.get_upper_rate(fea) for fea in mfcc_raw_features.transpose()] assert len(upper) == mfcc_raw_features.shape[1] func = lambda x: [ # feature_calc.autocorrelation(norm(x), 5), np.std(x), feature_calc.approximate_entropy(norm(x), 5, 1), feature_calc.cid_ce(x, normalize=True), feature_calc.count_above_mean(x), feature_calc.first_location_of_minimum(x), feature_calc.first_location_of_maximum(x), feature_calc.last_location_of_maximum(x), feature_calc.last_location_of_minimum(x), feature_calc.longest_strike_above_mean(x), feature_calc.number_crossing_m(x, 0.8*np.max(x)), feature_calc.skewness(x), feature_calc.time_reversal_asymmetry_statistic(x, 5) ] mfcc_features = np.hstack( [func(col) for col in mfcc_raw_features.transpose()] ) return mfcc_features
def feature_vector_fun(data, isFun=False, test=False): trimmed_data = trim_or_pad_data(data, TRIM_DATA_SIZE_FUN) rX = trimmed_data['rightWrist_x'] normRawColumn = universal_normalization(rX, trimmed_data, x_norm=True) normRawColumn = general_normalization(normRawColumn) # Area under curve auc = np.array([]) auc = np.append(auc, abs(integrate.simps(normRawColumn, dx=5))) # Absolute Sum of Consecutive Differences scd = fc.absolute_sum_of_changes(normRawColumn) # Entropy entropy = fc.approximate_entropy(normRawColumn, 2, 3) # AutoCorrelation ac = fc.autocorrelation(normRawColumn, lag=5) # Count Above Mean cam = fc.count_above_mean(normRawColumn) # Count Below Mean cbm = fc.count_below_mean(normRawColumn) featureVector = np.array([]) featureVector = np.append(featureVector, auc) featureVector = np.append(featureVector, scd) featureVector = np.append(featureVector, entropy) featureVector = np.append(featureVector, ac) featureVector = np.append(featureVector, cam) featureVector = np.append(featureVector, cbm) if TRIM_DATA_SIZE_FUN - 1 > featureVector.shape[0]: featureVector = np.pad( featureVector, (0, TRIM_DATA_SIZE_FUN - featureVector.shape[0] - 1), 'constant') featureVector = featureVector[:TRIM_DATA_SIZE_FUN - 1] if not test: if isFun: featureVector = np.append(featureVector, 1) else: featureVector = np.append(featureVector, 0) return featureVector
def function(x): return approximate_entropy(x, m=self.m, r=self.r)
def approximate_entropy(current_observation: pd.DataFrame, raw_key: str): # m Length of compared run of data # r filtering level return tsf.approximate_entropy( current_observation[raw_key], 3, peak_to_peak_value(current_observation, raw_key))