Package: pystreamfs
pystreamfsis an Open-Source Python package that allows for quick and simple comparison of feature selection algorithms on a simulated data stream.The user can simulate data streams with varying batch size on any dataset provided as a numpy.ndarray.pystreamfsapplies a specified feature selection algorithm to every batch and computes performance metrics for the
selected feature set at every timet.pystreamfscan also be used to plot the performance metrics.pystreamfscomes with 5 built-in feature selection algorithms for data streams. Additionally, you can find 3 datasets ready for download onGithub.pystreamfshas a modular structure and is thus easily expandable (see Section 2.5 for more information).License:MIT LicenseUpcoming changes:ability to simulate feature streamsability to generate artificial data streamsability to test multiple feature selection algorithms at once1 Getting started1.1 PrerequesitesThe following Python modules need to be installed (older versions than indicated might also work):python >= 3.7.1numpy >= 1.15.4psutil >= 5.4.7matplotlib >= 2.2.3scikit-learn >= 0.20.1... any modules required by the feature selection algorithm1.2 How to getpystreamfsUsing pip:pip install pystreamfsORDownload and unpack the .tar.gz file in/dist. Navigate to the unpacked folder and executepython setup.py install.2 The Package2.1 FilesThe main module is/pystreamfs/pystreamfs.py. Feature selection algorithms are stored in/algorithms.2.2 Main module:pystreamfs.pypystreamfs.pyprovides the following functions:X, Y = prepare_data(data, target, shuffle)Description: Prepare the data set for the simulation of a data stream: randomly sort the rows of a data matrix and extract the target variableYand the featuresXInput:data: numpy.ndarray, data settarget: int, index of the target variableshuffle: bool, ifTruesort samples randomlyOutput:X: numpy.ndarray, featuresY: numpy.ndarray, target variablestats = simulate_stream(X, Y, fs_algorithm, model, param)Description: Iterate over all datapoints in the dataset to simulate a data stream.
Perform given feature selection algorithm and return performance statistics.Input:X: numpy.ndarray, this is theXreturned byprepare_data()Y: numpy.ndarray, this is theYreturned byprepare_data()fs_algorithm: function, feature selection algorithmml_model: object, the machine learning model to use for the computation of the accuracy score
(remark on KNN: number of neighbours has to be greater or equal to batch size)param: dict, includes:num_features: integer, the number of features you want returnedbatch_size: integer, the number of instances processed in one iteration... additional algorithm specific parametersOutput:stats: dictfeatures: list of lists, set of selected features for every batchtime_avg: float, average computation time for one execution of the feature selectiontime_measures: list, time measures for every batchmemory_avg: float, average memory usage after one execution of the feature selection, usespsutil.Process(os.getpid()).memory_full_info().ussmemory_measures: list, memory measures for every batchacc_avg: float, average accuracy for classification with the selected feature setacc_measures: list, accuracy measures for every batchfscr_avg: float, average feature selection change rate (fscr) per time window.
The fscr is the percentage of selected features that changes intwith respect tot-1(fscr=0 if all selected features remain the same, fscr=1 if all selected features change)fscr_measureslist, fscr measures for every batchplt = plot_stats(stats, ftr_names, param, fs_name, model_name):Description: Plot the statistics for time, memory, fscr and selected features over all time windows.Input:stats: dict (seestatsofsimulate_stream())ftr_names: numpy.ndarray, contains all feature namesparam: dict, parametersfs_name: string, name of feature selection algorithmmodel_name: string, name of machine learning modelOutput:plt: pyplot object: statistic plots2.3 Built-in feature selection algorithmsOnline Feature Selection (OFS) based on the Perceptron algorithm by Wang et al. (2013) -link to paperUnsupervised Feature Selection on Data Streams (FSDS) using matrix sketching by Huang et al. (2015) -link to paperFeature Selection based on Micro Cluster Nearest Neighbors by Hamoodi et al. (2018) -link to paperExtremal Feature Selection based on a Modified Balanced Winnow classifier by Carvalho et al. (2006) -link to paperCancelOut Feature Selection based on a Neural Network by Vadim Borisov (Github)2.4 Downloadable datasetsAll datasets are cleaned and normalized. The target variable of all datasets is moved to the first column.German Credit Score (link)Binary version of Human Activity Recognition (link).The original HAR dataset has a multivariate target. For its binary version we defined the class "WALKING" as our positive class (label=1) and all other classes as the negative (non-walking) class.
We combined the 1722 samples of the original "WALKING" class with a random sample of 3000 instances from all other classes.Usenet (link)2.5 How to add a feature selection algorithmIf you want to usepystreamfsto test your own feature selection algorithm, you have to encapsulate your algorithm in a function
with the following format:defyour_fs_algorithm(X,Y,w,param):"""Your feature selection algorithm:param numpy.nparray X: current data batch:param numpy.nparray Y: labels of current batch:param numpy.nparray w: feature weights:param dict param: any parameters the algorithm requires:return: w (updated feature weights), param:rtype numpy.ndarray, dict"""...dofeatureselection...returnw,paramAfterwards you can import and test your feature selection algorithm in the same way as for any built-in algorithm (see the example).3. Examplefrompystreamfsimportpystreamfsimportnumpyasnpimportpandasaspdfrompystreamfs.algorithmsimportofsfromsklearn.neighborsimportKNeighborsClassifier# Load a datasetdata=pd.read_csv('../datasets/har.csv')feature_names=np.array(data.drop('target',1).columns)data=np.array(data)# Extract features and target variableX,Y=pystreamfs.prepare_data(data,0,False)# Load a FS algorithmfs_algorithm=ofs.run_ofs# Define parametersparam=dict()param['num_features']=5# number of features to returnparam['batch_size']=50# batch size# Define ML modelmodel=KNeighborsClassifier(n_jobs=-1,n_neighbors=5)# Data stream simulationstats=pystreamfs.simulate_stream(X,Y,fs_algorithm,model,param)# Plot statisticspystreamfs.plot_stats(stats,feature_names,param,'Online feature selection (OFS)','K Nearest Neighbor').show()