Package: pysubdisc
pySubDiscpySubDisc is a Python wrapper forSubDisc: Subgroup Discovery.This package is still early in development and all functionality and syntax are subject to change.InstallationThis will be streamlined soon:Fromhttps://github.com/SubDisc/SubDisc, usemvn packageto buildtarget/subdisc-gui.jarPlacesubdisc-gui.jarinsrc/pysubdisc/jarsRunpip install .from the root directory of the repository (containing pyproject.toml)ExampleUsing the data fromhttps://github.com/SubDisc/SubDisc/blob/main/adult.txt:importpysubdiscimportpandasdata=pandas.read_csv('adult.txt')sd=pysubdisc.singleNominalTarget(data,'target','gr50K')sd.qualityMeasureMinimum=0.25sd.run()print(sd.asDataFrame())DepthCoverageQualityTarget SharePositivesp-ValueConditions014430.5176010.440181195nanmarital-status = 'Married-civ-spouse'113760.4533050.446809168nanrelationship = 'Husband'213270.3599590.428135140naneducation-num >= 11.0316160.3540770.334416206nanage >= 33.0417280.3261050.311813227nanage >= 29.0515520.2634250.317029175naneducation-num >= 10.0Some detailed examples can be found in the /examples folder.DocumentationThe SubDisc documentation might be of help for working with pySubDisc:https://github.com/SubDisc/SubDisc/wiki.Data loadingpySubDisc usespandas.DataFrametables as input. There are two options to pass these from pySubDisc to SubDisc itself:data=pandas.read_csv('adult.txt')# Either, create a SubgroupDiscovery target structure directlysd=pysubdisc.singleNominalTarget(data,'target','gr50K')# Or, first load the dataframe into SubDisc for further preparationtable=pysubdisc.loadDataFrame(data)sd=pysubdisc.singleNominalTarget(table,'target','gr50K')Data preparationApySubDisc.Tableobject can be manipulated before creating a SubDisc target using the following functions:# Load a pandas.DataFrame objecttable=pysubdisc.loadDataFrame(data)# Describe the columns (name, type, cardinality, enabled)print(table.describeColumns())# Change column type to binarytable.makeColumnsBinary(['column','other_column']# Change column type to numerictable.makeColumnsNumeric(['column','other_column']# Change column type to nominaltable.makeColumnsNominal(['column','other_column']# Disable columnstable.disableColumns(['column','other_column']# Enable columnstable.enableColumns(['column','other_column']# Select a subset of the rows by passing a pandas boolean Seriestable.setSelection(data['education']=='Bachelors')# Reset selection of rows to the full data settable.clearSelection()Configuring subgroup discoveryApySubDisc.SubgroupDiscoveryobject can be created by the following target functions:# single nominal targetsd=pysubdisc.singleNominalTarget(data,targetColumn,targetValue)# single numeric targetsd=pysubdisc.singleNumericTarget(data,targetColumn)# double regression targetsd=pysubdisc.doubleRegressionTarget(data,primaryTargetColumn,secondaryTargetColumn)# double correlation targetsd=pysubdisc.doubleCorrelationTarget(data,primaryTargetColumn,secondaryTargetColumn)# double binary targetsd=pysubdisc.doubleBinaryTarget(data,primaryTargetColumn,secondaryTargetColumn)# multi numeric targetsd=pysubdisc.multiNumericTarget(data,targetColumns)After creating apySubDisc.SubgroupDiscoveryobject, you can configure its search parameters. For example:print(sd.describeSearchParameters())sd.numericStrategy='NUMERIC_BEST'sd.qualityMeasure='RELATIVE_WRACC'sd.qualityMeasureMinimum=2sd.searchDepth=2An appropriate value of thequalityMeasureoption can in particular be computed for various target types using thecomputeThreshold()function.# If setAsMinimum is set to True, the qualityMeasureMinimum parameter is updated directlythreshold=sd.computeThreshold(significanceLevel=0.05,method='SWAP_RANDOMIZATION',amount=100,setAsMinimum=True)Running subgroup discoveryAfter configuring the search parameters, you can run the subgroup discovery process by calling therun()method.sd.run()Examining the results# The resulting subgroups are given as a pandas.DataFrame, with one row per subgroupprint(sd.asDataFrame())The functiongetSubgroupMembers()returns a set of members of a subgroup as a pandas boolean Series.# Get rows corresponding to subgroup #0subset=data[sd.getSubgroupMembers(0)]For a number of the target types, ashowModel()method is available to aid visualization of the discovered subgroups. The scripts in the/examplesdirectory demonstrate its use.The functiongetPatternTeam()returns a Pattern Team for the discovered subgroups.# if returnGrouping is True, getPatternTeam will also return# the grouping of subgroups according to the pattern teampatternTeam,grouping=sd.getPatternTeam(3,returnGrouping=True)print(patternTeam)# print the subgroups for the first of the three determined groupsdf=sd.asDataFrame()print(df[grouping[0]])