Package: feature-test
feature_testThis package provides tools to test one feature against all other features in a dataset. It is intended to determine whether a new feature is a good candidate for addition to an established dataset used in a machine learning model. The benefit of this package is that instead of adding a feature to a dataset, running it through a long training and evaluation process, and then interpreting results, the feature can be quickly tested using feature_test; enabling users to quickly discard unuseful features and move forward with potentially impactful ones. This package provides the following tools to aid this determination.Correlation analysisCalculate the correlationsIdentify highly correlated featureCategorize correlationChi-Square testsCalculate the chi-square statistic and p-vlaueCategorize the chi-square resultRecursive feature elimination (RFE)Rank features by their importanceLasso regularization coefficientsCalculate the coefficients of a linear model using Lasso regularizationRidge regularization coefficientsCalculate the coefficients of a linear model using Ridge RegularizationDecision tree coefficientsCalculate the coefficients for each feature using a decision tree modelSpecial thanks to the AREN team for their guidance and Annie Tran for the open sourcing her feature test code.Installationpython3 -m pip install feature-testutils.Utilsclass utils.Utils(X, columns)A collection of functions that enables users to get data on a dataframe or adjust it for testing.Parameters:X: pandas.DataFrameA pandas.DataFrame containing the datasetcolumns: ListA list of strings correlating to features in a dataset.Methods_______________________________________________________________________________________get_columns(X: pd.DataFrame)Returns a list of column names in a dataframe.exclude_columns(X: pd.DataFrame, columns: List)Exclude a list of columns from a dataframe.tests.Correlationclass tests.correlation(X, new_feature)A suite of functions that calculates and reports the correlation coefficient between features and all other features in a dataset.Parameters:X: pandas.DataFrameA pandas.DataFrame containing the datasetnew_feature: stringA string indicating the column in X to test against all other featuresMethods_______________________________________________________________________________________calc_corr(X: pd.DataFrame, new_feature: str)Returns a dataframe with the correlation values for each new_feature/feature combination.similar_corr(X: pd.DataFrame, new_feature: str)Returns a list of features highly correlated with the new_feature.categorize_correlations(X: pd.DataFrame, correlation_threshold: float = 0.6)Returns a dataframe with the correlations categorized. Possible values are high, medium, and low.get_correlations(X: pd.DataFrame, new_feature: str)Returns a dataframe of new_feature/feature combinations, their correlations, and their correlation category.tests.ChiSquareclass tests.ChiSquare(X, new_feature)Calculates the chi-squared statistic of the new feature against each categorical feature in a dataset. Also categorizes the chi-square result based on the p-value and effect size as measured by cramers v.Parameters:X: pandas.DataFrameA pandas.DataFrame containing the datasetnew_feature: stringA string indicating the column in X to test against all other featuresMethods_______________________________________________________________________________________calc_chi_sq(X: pd.DataFrame, new_feature: str)Returns a dataframe that includes the chi-square result categorization.tests.FeatureSelectionclass tests.FeatureSelection(X, target)Parameters:X: pandas.DataFrameA pandas.DataFrame containing the datasettarget: stringA string indicating the prediction columnMethods_______________________________________________________________________________________rfe_rankings(X: pd.DataFrame, target: str, classifier=None)Returns a dataframe that includes the recursive feature elimination feature ranking.lasso_rankings(X: pd.DataFrame, target: str)Returns a dataframe that includes the linear model coefficients for features after lasso regularization.ridge_coefficients(X: pd.DataFrame, target: str)Returns a dataframe that includes the linear model coefficients for features after ridge regularization.dtree_coefficients(X: pd.DataFrame, target: str)Returns a dataframe that includes the decision tree model coefficients for features.run_feature_classifiers(X: pd.DataFrame, target: str)Returns a dataframe that includes the the results for rfe_rankings, lasso_rankings, ridge_rankings, and dtree_rankings.Examplesfrompandasimportutilfromfeature_test.utilsimportUtilsfromfeature_test.feature_testsimportCorrelation,FeatureSelection,ChiSq# Create a test datasetdf=util.testing.makeDataFrame()df.head()featureABCDlRhANYYD2r0.572559-1.4099780.687618-0.923502YzYG07kY1O0.145629-1.446946-0.0035260.304385cT3KK078Gt-1.0073781.2639801.1078970.844689JW4Kg2EGVo0.536701-1.477372-0.8668731.5394582mucO1cf2Z-1.1018750.5185550.384916-0.031403c=Utils.get_columns(df)['A', 'B', 'C', 'D']corr_df=Correlation.calc_corr(df,'A')corr_dffeature_1feature_2corrAB0.081662AC0.203858AD0.064999rep_df=Correlation.get_correlations(df,'D')rep_dffeature_1feature_2corrcorr_catAB0.071466lowAC0.105306lowAD0.121130lowChiSq.calc_chi_sq(df,'A')feature_1feature_2chi_sq_catABNOT SIGNIFICANTACNOT SIGNIFICANTADNOT SIGNIFICANTFeatureSelection.rfe_rankings(df,'A')featurerfe_rankB3C1D2FeatureSelection.lasso_rankings(df,'A')featurelasso_coeflasso_importanceB0.02.0C0.02.0D-0.02.0FeatureSelection.ridge_rankings(df,'A')featureridge_coefridge_importanceB0.0508713.0C0.0963621.0D-0.0952202.0FeatureSelection.dtree_rankings(df,'A')featurerandom_forest_coefficientrandom_forest_importanceB0.3230912.0C0.2696733.0D0.4072361.0FeatureSelection.dtree_rankings(df,'A')featurerfe_ranklasso_coeflasso_importanceridge_coefridge_importancerandom_forest_coefficientrandom_forest_importanceB30.02.00.0508713.00.3230912.0C10.02.00.0963621.00.2696733.0D2-0.02.0-0.0952202.00.4072361.0