黑箱模型特征相关性度量模块
fairml的Python项目详细描述
fairml是一个python工具箱,用于审核 偏见。
徽标
说明
越来越多的预测模型被用于 确定获得信贷、保险和 就业。尽管社会在效率和生产力方面取得了进步 通过部署这些模型,潜在的系统缺陷并没有 已得到充分解决,特别是无意中 歧视。这种歧视可能是基于种族, 性别、宗教、性取向或其他特征。这个 project解决了这个问题:分析师如何确定 年黑箱预测模型输入的相对重要性 为了评估模型的公平性(或歧视程度)?
我们提出了一个用于审计预测模型的端到端工具箱fairml 通过量化模型输入的相对重要性。费尔姆 利用模型压缩和四种输入排序算法 量化模型对其输入的相对预测依赖性。这个 输入对预测模型的相对重要性可以是 用于评估此类模型的公平性(或歧视程度)。 使用fairml,分析师可以更容易地审计繁琐的预测模型 很难解释黑箱算法和 相应的输入数据。
安装
您可以通过github安装这个包,也就是说,这个repo使用 以下命令:
pip安装https://github.com/adebayoj/fairml/archive/master.zip
或者您可以克隆存储库执行以下操作:
git克隆https://github.com/adebayoj/fairml.git
sudo python setup.py安装
方法论
方法论
代码演示
现在我们展示如何使用fairml python包审计黑盒 模型。
# First we import modules for model building and data processing.importpandasaspdimportnumpyasnpfromsklearn.linear_modelimportLogisticRegression""" Now, we import the two key methods from fairml. audit_model takes: - (required) black-box function, which is the model to be audited - (required) sample_data to be perturbed for querying the function. This has to be a pandas dataframe with no missing data. - other optional parameters that control the mechanics of the auditing process, for example: - number_of_runs : number of iterations to perform - interactions : flag to enable checking model dependence on interactions. audit_model returns an overloaded dictionary where keys are the column names of input pandas dataframe and values are lists containing model dependence on that particular feature. These lists of size number_of_runs. """fromfairmlimportaudit_modelfromfairmlimportplot_generic_dependence_dictionary
上面,我们提供了对fairml关键功能的快速解释。 现在我们开始构建一个我们想要审计的示例模型。
# read in the propublica data to be used for our analysis.propublica_data=pd.read_csv(filepath_or_buffer="./doc/example_notebooks/""propublica_data_for_fairml.csv")# create feature and design matrix for model building.compas_rating=propublica_data.score_factor.valuespropublica_data=propublica_data.drop("score_factor",1)# this is just for demonstration, any classifier or regressor# can be used here. fairml only requires a predict function# to diagnose a black-box model.# we fit a quick and dirty logistic regression sklearn# model here.clf=LogisticRegression(penalty='l2',C=0.01)clf.fit(propublica_data.values,compas_rating)
现在让我们审核用fairml构建的模型。
# call audit model with modeltotal,_=audit_model(clf.predict,propublica_data)# print feature importanceprint(total)# generate feature dependence plotfig=plot_dependencies(total.get_compress_dictionary_into_key_median(),reverse_values=False,title="FairML feature dependence")plt.savefig("fairml_ldp.eps",transparent=False,bbox_inches='tight')
上面的演示生成下图。
示例输出
如有任何问题,请随时向作者发送电子邮件:
数据
用于上述演示的数据可在回购协议中获得,网址为: /doc/example_笔记本/propublica_data_for_fairml.csv