trefle是trefle的缩写,是一个基于进化的模糊逻辑引擎。它是fuzzycoco算法的一个实现,即一个scikit学习相容估计器,它使用一个协同进化算法来寻找和建立可解释的模糊系统。专为学生和研究人员设计
trefle的Python项目详细描述
trefle-使用可解释模糊系统的scikit学习兼容分类器
trefle是一个scikit learn相容的估计器,它实现了FuzzyCoCo algorithm,该估计器使用协同进化算法来发现和构建可解释的模糊系统。
下面是一个使用威斯康星州乳腺癌数据集的基本示例,这是一个二进制分类问题,来自scikit learn:
importrandomimportnumpyasnpfromsklearn.datasetsimportload_breast_cancerfromsklearn.metricsimportaccuracy_scorefromsklearn.model_selectionimporttrain_test_splitfromtrefle.fitness_functions.output_thresholderimportround_to_clsfromtrefle.trefle_classifierimportTrefleClassifiernp.random.seed(0)random.seed(0)# Load datasetdata=load_breast_cancer()# Organize our dataX=data["data"]y=data["target"]y=np.reshape(y,(-1,1))# output needs to be at least 1 column wide# Split our dataX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.33)# Declare the fitness function we want to use. Fitness value: higher is better.deffit(y_true,y_pred):# y_pred are floats in [0, n_classes-1]. To use accuracy metric we need# to binarize the output using round_to_cls()y_pred_bin=round_to_cls(y_pred,n_classes=2)returnaccuracy_score(y_true,y_pred_bin)# Initialize our classifierclf=TrefleClassifier(n_rules=4,n_classes_per_cons=[2],# a list where each element indicates the number of classes a consequent has. Specify 0 if one consequent is a continuous (regression) value.n_labels_per_mf=3,default_cons=[0],# the default rule will use the class 0n_max_vars_per_rule=3,n_generations=20,fitness_function=fit,verbose=False,)# Train our classifierclf.fit(X_train,y_train)# Make predictionsy_pred=clf.predict_classes(X_test)clf.print_best_fuzzy_system()# Evaluate accuracyscore=accuracy_score(y_test,y_pred)print("Score on test set: {:.3f}".format(score))
这将输出模糊系统:
IF v0 is low AND v5 is medium AND v16 is low THEN [0]
IF v25 is high AND v9 is high AND v14 is medium THEN [0]
IF v6 is high THEN [0]
IF v21 is low AND v23 is low THEN [1]
ELSE [0]
Variables definition
v0: -0.366, -0.347, -0.343,
v5: 0.155, 2.03, 2.03,
v6: 0.0756, 0.151, 1.36,
v9: 5.06, 11.2, 16.6,
v14: 5.89, 34.2, 37.2,
v16: 0.0815, 0.652, 1.06,
v21: -0.299, -0.294, -0.294,
v23: -0.0555, -0.0553, -0.0553,
v25: 0.193, 0.568, 0.631,
Score on test set: 0.910
如果你以前从来没有听说过模糊系统,那么你基本上可以把它们看作是一组规则,在对它们进行评估后给出预测。例如“如果温度高,阳光中等,那么游客是中等”。
安装
从今天开始使用trefle和pip:-)
pip install trefle
使用示例
请参阅examples文件夹中的其他示例。
酷炫的功能
- 支持分类(二进制和多类)、回归和混合(即分类和回归)问题
- 完全兼容的scikit学习估计器
- 像使用常规估计器一样使用它
- 支持GridSearch
- Fuzzy systems parameters是可定制的,例如规则的数量,每个规则的语言标签的数量,…
- Evolutionary parameters可定制,如世代数、人口规模、名人堂规模等。
- Custom fitness function
- Import and Export未来可互操作格式使用的最佳模糊系统
- 使用配套库LFA Toolbox微调最佳模糊系统。添加或删除模糊规则以提高模糊系统的性能或可解释性。或者调整成员函数。
- 在C++ 14中实现模糊引擎,使Trfle非常快,并使用所有CPU核
- 最后但并非最不重要的是,trefle是一个类似gnu的递归缩略词,它很酷。它代表trefle是一个revised和e基于进化的fuzzy l逻辑e引擎。trefle在法语中也是三叶草的意思。
什么是模糊逻辑和模糊可可算法?
fuzzycoco算法
以下句子摘自卡洛斯·安德烈斯佩尼亚雷耶斯博士论文《共同进化模糊建模》,你可以找到here。
Fuzzy CoCo is a novel approach that combines two methodologies - fuzzy systems and coevolutionary algorithms - so as to automatically produce accurate and interpretable systems. The approach is based on two elements: (1) a system model capable of providing both accuracy and human understandability, and (2) an algorithm that allows to build such a model from available information.
简而言之,作为用户,该算法将为您提供一个模型,该模型在给定分类或回归问题的情况下是可解释和准确的(即您可以看到该模型是如何工作的)。从这个模型中,您可以读取它提取的特征。
它是怎么工作的?
- 加载数据集
- 配置实验,即规则数、生成数和其他模糊系统或进化参数
- 创建两个初始种群(也称为“物种”;一个用于模糊规则,另一个用于 变量定义)。两者都以位列表的形式表示个体。
- 运行进化算法。它将执行以下步骤。
- 选择
- 交叉
- 变异
- 将群体中的个体和代表结合起来进行评估 形成一个模糊系统
- 保存最好的一对(即来自pop1的个人和来自pop2的个人的组合)
- 重复这些步骤,直到达到最大生成数
- 检索最佳个体偶,即最佳模糊系统
- 可以选择使用LFA Toolbox来可视化或微调它
展开和测试
两者都可以在docs
文件夹中找到。
从源代码构建
医生在哪里?!
没有像狮身人面像这样的文件。首先查看docs文件夹或直接查看TrefleClassifier
的源代码。
学分
- Gary Marigliano
- 卡洛斯·安德烈斯·佩尼亚雷耶斯
- CI4CB Team