Python文档生成器
py-ciu的Python项目详细描述
py ciu公司
可解释机器学习通过上下文重要性和效用
py ciu库提供了为 基于机器学习的分类器。 它是模型不可知论的,并回答了以下问题,给出了一个分类 决定:
- 在
如何将important作为 分类决策?(上下文重要性,CI)
在 - 在
如何将typical作为 给定的班级?(上下文实用程序,CU)
在
使用
安装py ciu:
pip install py-ciu
导入库:
^{pr2}$为了这个例子,让我们还导入一个数据生成器,创建一个 合成数据集,并训练模型:
fromsklearn.ensembleimportRandomForestClassifierfromciu_tests.loan_data_generatorimportgenerate_datadata=generate_data()train_data=data['train'][1]test_data=datatest_data_encoded=data['test'][1].drop(['approved'],axis=1)random_forest=RandomForestClassifier(n_estimators=1000,random_state=42)labels=train_data[['approved']].values.ravel()data=train_data.drop(['approved'],axis=1)random_forest.fit(data,labels)
然后对需要解释的案例进行分类,确定预测指标 对于我们感兴趣的课程:
feature_names=['age','assets','monthly_income','gender_female','gender_male','gender_other','job_type_fixed','job_type_none','job_type_permanent']case=test_data_encoded.values[0]example_prediction=random_forest.predict([test_data_encoded.values[0]])example_prediction_prob=random_forest.predict_proba([test_data_encoded.values[0]])prediction_index=0ifexample_prediction[0]>0.5else1print(feature_names)print(f'Case: {case}; Prediction {example_prediction}; Probability: {example_prediction_prob}')
现在,我们可以调用py ciu的determine_ciu
函数。
函数采用以下参数:
- 在
在case_data
:包含案例数据的字典。在 - 在
在predictor
:黑盒模型py ciu的预测函数应该 打电话。在 - 在
在min_maxs
:包含功能名称(键)和 最小值、最大值,加上一个值,该值指示 必须是整数('feature_name': [min, max, is_int]
)。在 - 在
在samples
(可选):将生成py ciu的样本数。默认值 到1000
。在 - 在
在prediction_index
(可选):如果模型返回几个 预测,有可能提供 相关预测指标。默认为None
。在 - 在
在category_mapping
(可选):一个热编码类别的映射 类别和类别列表的变量 名字。{cd9>默认为^。在 - 在
在feature_interactions
(可选):一个{key: list}
元组的列表 其交互作用应该是 评价的。默认为[]
。在
我们配置CIU参数并调用CIU函数:
category_mapping={'gender':['gender_female','gender_male','gender_other'],'job_type':['job_type_fixed','job_type_none','job_type_permanent']}feature_interactions=[{'assets_income':['assets','monthly_income']}]ciu=determine_ciu(test_data_encoded.iloc[0,:].to_dict(),random_forest.predict_proba,{'age':[20,70,True],'assets':[-20000,150000,True],'monthly_income':[0,20000,True],'gender_female':[0,1,True],'gender_male':[0,1,True],'gender_other':[0,1,True],'job_type_fixed':[0,1,True],'job_type_none':[0,1,True],'job_type_permanent':[0,1,True]},1000,prediction_index,category_mapping,feature_interactions)
函数返回一个ciu
对象,我们从中检索CIU度量:
print(ciu.ci,ciu.cu)
我们还可以自动生成CIU图:
ciu.plot_ci()ciu.plot_cu()
此外,我们可以根据CIU生成文本解释:
print(ciu.text_explain())
看看 examples目录到 了解更多。在
作者
- 在 在
- 在 在
许可证
库在BSD Clause-2 License下发布。在
- 项目
标签: