如何在Python的tsfresh中仅提取方差与标准差特征?
F_x in tsfresh Hey there! I get that when you're starting out with tsfresh, narrowing down to exactly the features you need can feel a bit tricky. Let me break down how to adjust your code to only compute F_x__variance and F_x__standard_deviation:
The Core Idea
Instead of using ComprehensiveFCParameters() (which enables all possible features), you'll define a custom settings dictionary that explicitly specifies only the features you want for your F_x column. TSFresh uses this dictionary to know exactly which calculations to run.
Modified Code
Here's the adjusted version of your code:
from tsfresh import extract_relevant_features # Define your custom settings: only keep variance and standard_deviation for F_x custom_settings = { 'F_x': { 'variance': None, # Use default parameters for variance 'standard_deviation': None # Use default parameters for standard deviation } } # Now extract only the specified features features_filtered_direct = extract_relevant_features( df, y, column_id='id', column_sort='time', default_fc_parameters=custom_settings # Pass your custom settings here )
What's Happening Here?
- The
custom_settingsdictionary tells tsfresh: "For the columnF_x, only calculate thevarianceandstandard_deviationfeatures." - Setting the feature values to
Nonemeans we use the default parameters for those features (neither of these require extra configuration, so this works perfectly). - If you ever wanted to add parameters to a feature (like adjusting bins for a histogram feature), you'd replace
Nonewith a dictionary of parameters instead.
Bonus: If You Need Other Columns' Features Too
If you have other time series columns and want to keep their default features but only limit F_x to these two, you can start with a base settings object and modify it:
from tsfresh import extract_relevant_features from tsfresh.feature_extraction import ComprehensiveFCParameters # Start with all default features settings = ComprehensiveFCParameters() # Override F_x to only include your desired features settings['F_x'] = {'variance': None, 'standard_deviation': None} # Then use this modified settings object in extract_relevant_features features_filtered_direct = extract_relevant_features( df, y, column_id='id', column_sort='time', default_fc_parameters=settings )
That should give you exactly the two features you're looking for without the extra 95! Let me know if you run into any snags.
内容的提问来源于stack exchange,提问作者Humaun Rashid Nayan

