Как стать автором
Обновить

Извлечение «знаний» или классификация в один if

Время на прочтение23 мин
Количество просмотров10K

В статье мы постараемся классифицировать злокачественную опухоль груди от доброкачественной основываясь на наборе данных взятом отсюда. Как бы странно не звучало, но точность не будет главным приоритетом в этот раз, так как уже есть довольно хорошие решения с упором именно на точность, что и понятно, ведь от данных тестов зависит жизнь человека. Например в 2012 году Бриттани Венгер победила в конкурсе Google Science Fair с проектом cloud4cancer.appspot.com, который был обучен именно по выше указанному набору.

Прежде чем начать советую Вам ознакомится со спецификациями dataset'а и прочесть мой предыдущий пост, так как алгоритм решения взят именно оттуда. Если коротко про данные, то первое число в строке — идентификационный номер случая, следующие 9 описывают опухоль, последнее — тип опухоли(4 — злокачественная, 2 — доброкачественная). Пара примеров:
1017122,8,10,10,8,7,10,9,7,1,4
1018099,1,1,1,1,2,10,3,1,1,2

Для тех кто не прочитал предыдущий пост. резюмирую его в этом абзаце.
Нейрон — сложная адаптивная логическая функция, которая не обладает функциональной полнотой, так как не может генерировать те таблицы истинности, в которых при всех неактивных параметрах(False), результатом становиться True. В частности к таким логическим функциям, которые нейрон не может симулировать, относятся: отрицание, штрих Шеффера, стрелка Пирса, эквивалентность, импликации и т.д. Но есть исключения: нейроны, которые периодически генерируют потенциал действия без стимуляции дендритов. Это лишь личное мнение, которое помогло сделать алгоритм решения.

Класс из предыдущего поста, который будет использован при решении:
import random
class LogicReconstructer:
    groups=[]
    threshold = 0.99
    maxmem = 10
    numparams = 0

    def __init__(self,numparams,threshold = 0.99, totalmem = 10):
        self.numparams=numparams
        self.threshold=threshold
        self.maxmem=totalmem

    def getactive(self,params):
        if len(params)!=self.numparams:
            raise Exception("LogicReconstructer: numparams mismatch")

        active=[]
        for x in range(self.numparams):
            if params[x]:
                active.append(x)
        return  active

    def extractgroups(self,params,result):
        active=self.getactive(params)
        exist = False
        ignore = False

        if result and active:
            ind=0
            while ind<len(self.groups):

                if len(active)>len(self.groups[ind][0]) and self.issublist(self.groups[ind][0],active):
                    ignore=True
                    break

                elif len(active)==len(self.groups[ind][0]) and self.issublist(active,self.groups[ind][0]):
                    exist=True
                    break

                elif len(active)<len(self.groups[ind][0]) and self.issublist(active,self.groups[ind][0]):
                    del self.groups[ind]
                    ind-=1

                ind+=1

            if not exist and not ignore:
                self.groups.append([active,[0]*self.numparams,False])

    def extractinhibitors(self,params,result):
        active=self.getactive(params)

        if result:
            count=0
            for _,grp in enumerate(self.groups):
                if self.issublist(grp[0],active):
                    count+=1
                    if count>1:
                        return

        for _,grp in enumerate(self.groups):
            if not grp[2] and self.issublist(grp[0],active):

                neg=[]
                negvalue=False
                for y in range(self.numparams):
                    if grp[1][y]<=-self.threshold:
                        neg.append(y)
                        negvalue|=params[y]
                    elif grp[1][y]>=self.threshold:
                        grp[2]=True

                for y in range(self.numparams):
                    if params[y]:
                        if y in neg or not negvalue:
                            grp[1][y] = self.counting(grp[1][y],self.maxmem,result)


    def counting(self,prc,total,item):
        result=prc-prc/total
        if not item:
            result-=1/total
        else:
            result+=1/total
        return result

    def issublist(self,a,b):
        for ind,item in enumerate(a):
            if item not in b:
                return False
        return True

    def getsublist(self,a,b):
        result=[]
        for ind,item in enumerate(a):
            if item in b:
                result.append(item)
        return result

    def simulate(self,params):
        result=False
        for ind,item in enumerate(self.groups):
            if item[2]:
                locres=True
                for x in range(len(item[0])):
                    locres&=params[item[0][x]]
                for x in range(len(item[1])):
                    if item[1][x]<=-self.threshold:
                        locres=locres&~params[x]
                result|=locres
        return result

    def getlogicfunc(self,guess=False):
        result=""
        for ind,item in enumerate(self.groups):
            if item[2] or guess:
                locres=""
                for x in range(len(item[0])):
                    if x!=0:
                        locres+=" and "
                    locres+=str(item[0][x])
                for x in range(len(item[1])):
                    if item[1][x]<=-self.threshold:
                        locres+=" and not "+str(x)
                if ind!=0:
                    result+=" or "
                result+=locres
        return result

    def randparams(self):
        result = []
        for x in range(self.numparams):
            result.append(random.choice([True, False]))
        return result

    def isready(self):
        result=bool(self.groups)
        for ind,item in enumerate(self.groups):
            result&=item[2]
        return result

    def getlogicstuct(self):
        result = []
        for _,item in enumerate(self.groups):
            grp=[]
            if item[2]:
                for x in range(len(item[0])):
                    grp.append([item[0][x],True])
                for x in range(len(item[1])):
                    if item[1][x]<=-self.threshold:
                        grp.append([x,False])
            if grp:
                result.append(grp)
        return result

    def simulatebystruct(self,params,grps):
        for _,item in enumerate(grps):
            locres=True
            for _,param in enumerate(item):
                if param[1]:
                    locres&=params[param[0]]
                else:
                    locres&=~params[param[0]]
                if not locres:
                    break
            if locres:
                return True

        return  False


Итак, к делу. Для начала нужно классифицировать хоть как-нибудь с удовлетворительной точностью.
Обучаем
def getbin(num,max,min=0):
    result=[]
    for i in range(min,max+1):
        if i<=num:
            result.append(True)
        else:
            result.append(False)
    return result

def med():
    file = open('C:/meddata.txt', 'r')
    lines = file.readlines()
    file.close()

    data = []

    for i in range(len(lines)):
        data.append(lines[i].strip().split(","))
    lines.clear()

    bindata = []
    numdata =[]
    for i in range(len(data)):
        tmp=[]
        tmp2=[]
        for x in range(len(data[i])):
            if x!=0 and x!=10:
                tmp.extend(getbin(int(data[i][x]),10,1))
                tmp2.extend([int(data[i][x])])
            elif x==10:
                if int(data[i][x])==4:
                    tmp.extend([True])
                else:
                    tmp.extend([False])
        bindata.append(tmp)
        numdata.append(tmp2)
    data.clear()

    neuron = LogicReconstructer(len(bindata[0])-1,totalmem=7, threshold=0.98)

    for _,item in enumerate(bindata):
        neuron.extractgroups(item[:-1],item[-1:][0])
    
    ready=False
    while not neuron.isready():
        rnd=random.randint(0,len(bindata)-1)
        neuron.extractinhibitors(bindata[rnd][:-1],bindata[rnd][-1:][0])
    
    logicstruct=neuron.getlogicstuct()
    print(logicstruct)
	
    falsepositive = 0
    falsenegative = 0
    for _,item in enumerate(bindata):
       res = neuron.simulatebystruct(item[:-1],logicstruct)
       if res!=item[-1:][0]:
           if res:
               falsepositive+=1
           else:
               falsenegative+=1
    print(falsenegative/len(bindata),falsepositive/len(bindata))


В данных встречались знаки вопроса, которые я заменил в блокноте на десятки, посчитав, что пусть уж лучше классификатор ложно сработает чем пропустит. Так же преобразовал числа в бинарную форму функцией getbin, пропустив при этом первое, так как оно особой роли не играет. В итоге у меня, после обучения, получилась структура которая даёт 1.4% ложно негативных и 1.1% ложно положительных ответов, обобщив 241 положительный пример 49-ю логическими группами. Это не лучший результат, но в дальнейшем будем использовать именно его(я получал 0 ложно негативных и 1.2% ложно положительных ответов).
Структура logicstruct
    logicstr = "[[[0, True], [1, True], [2, True], [3, True], [4, True], [10, True], [11, True], [12, True], [20, True], [21, True], [22, True], [30, True], [31, True], [32, True], [40, True], [41, True], [50, True], [51, True], [52, True], [60, True], [61, True], [62, True], [63, True], [70, True], [71, True], [72, True], [73, True], [80, True], [53, False], [54, False]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [10, True], [11, True], [12, True], [13, True], [20, True], [21, True], [22, True], [23, True], [24, True], [25, True], [30, True], [31, True], [32, True], [33, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [50, True], [60, True], [61, True], [62, True], [63, True], [70, True], [71, True], [72, True], [80, True]], [[0, True], [1, True], [10, True], [11, True], [12, True], [13, True], [14, True], [20, True], [21, True], [22, True], [30, True], [31, True], [32, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [60, True], [61, True], [62, True], [63, True], [64, True], [65, True], [66, True], [70, True], [71, True], [72, True], [73, True], [74, True], [80, True], [2, False], [3, False], [4, False], [5, False], [15, False], [23, False], [24, False], [25, False], [33, False], [34, False], [35, False], [36, False], [37, False], [38, False], [57, False], [58, False], [59, False], [75, False], [76, False], [77, False]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [8, True], [9, True], [10, True], [11, True], [12, True], [13, True], [20, True], [21, True], [22, True], [30, True], [40, True], [41, True], [42, True], [50, True], [51, True], [52, True], [60, True], [61, True], [62, True], [63, True], [64, True], [65, True], [70, True], [71, True], [72, True], [73, True], [74, True], [80, True], [81, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [20, True], [21, True], [22, True], [23, True], [24, True], [30, True], [31, True], [32, True], [33, True], [34, True], [35, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [46, True], [47, True], [48, True], [49, True], [50, True], [60, True], [61, True], [62, True], [70, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [8, True], [10, True], [11, True], [12, True], [13, True], [14, True], [20, True], [21, True], [22, True], [23, True], [24, True], [25, True], [26, True], [27, True], [30, True], [40, True], [41, True], [50, True], [51, True], [52, True], [60, True], [61, True], [70, True], [80, True], [81, True], [82, True], [83, True], [84, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [10, True], [11, True], [12, True], [20, True], [21, True], [22, True], [30, True], [31, True], [32, True], [33, True], [40, True], [41, True], [50, True], [51, True], [52, True], [53, True], [60, True], [61, True], [62, True], [70, True], [71, True], [72, True], [73, True], [80, True], [5, False], [34, False], [42, False], [54, False], [55, False], [56, False], [74, False]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [10, True], [11, True], [20, True], [21, True], [22, True], [23, True], [30, True], [40, True], [41, True], [42, True], [43, True], [44, True], [50, True], [60, True], [61, True], [62, True], [63, True], [64, True], [70, True], [71, True], [72, True], [73, True], [80, True], [81, True], [82, True], [83, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [10, True], [11, True], [20, True], [21, True], [22, True], [30, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [58, True], [59, True], [60, True], [61, True], [62, True], [63, True], [64, True], [70, True], [80, True], [5, False], [13, False], [14, False], [15, False], [25, False], [31, False], [32, False], [33, False], [34, False], [35, False], [36, False], [37, False], [38, False], [65, False], [66, False], [73, False], [74, False], [75, False], [76, False], [77, False]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [8, True], [10, True], [11, True], [12, True], [13, True], [14, True], [20, True], [21, True], [22, True], [23, True], [24, True], [30, True], [31, True], [40, True], [41, True], [50, True], [51, True], [60, True], [61, True], [62, True], [63, True], [64, True], [70, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [10, True], [11, True], [12, True], [20, True], [21, True], [22, True], [23, True], [30, True], [40, True], [41, True], [42, True], [43, True], [44, True], [50, True], [51, True], [60, True], [61, True], [62, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [76, True], [77, True], [78, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [8, True], [9, True], [10, True], [11, True], [12, True], [13, True], [20, True], [21, True], [30, True], [40, True], [41, True], [42, True], [50, True], [51, True], [60, True], [61, True], [62, True], [63, True], [70, True], [71, True], [72, True], [80, True], [81, True], [82, True], [83, True], [84, True], [85, True], [86, True], [87, True], [88, True], [89, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [10, True], [11, True], [12, True], [20, True], [21, True], [22, True], [23, True], [30, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [46, True], [47, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [58, True], [59, True], [60, True], [61, True], [62, True], [63, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [76, True], [77, True], [78, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [16, True], [17, True], [18, True], [19, True], [20, True], [21, True], [30, True], [31, True], [32, True], [33, True], [34, True], [35, True], [36, True], [37, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [46, True], [47, True], [48, True], [49, True], [50, True], [51, True], [60, True], [61, True], [62, True], [63, True], [64, True], [65, True], [66, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [76, True], [77, True], [80, True], [81, True], [82, True], [83, True], [84, True], [85, True], [86, True], [87, True], [88, True], [89, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [8, True], [9, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [20, True], [21, True], [22, True], [23, True], [30, True], [40, True], [41, True], [42, True], [50, True], [51, True], [52, True], [53, True], [60, True], [61, True], [62, True], [70, True], [71, True], [80, True], [81, True], [82, True]], [[0, True], [1, True], [2, True], [10, True], [11, True], [12, True], [20, True], [21, True], [22, True], [23, True], [24, True], [25, True], [30, True], [31, True], [32, True], [33, True], [40, True], [41, True], [42, True], [43, True], [44, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [60, True], [61, True], [62, True], [63, True], [70, True], [71, True], [72, True], [73, True], [80, True]], [[0, True], [1, True], [10, True], [11, True], [12, True], [20, True], [21, True], [22, True], [23, True], [30, True], [31, True], [32, True], [33, True], [40, True], [41, True], [50, True], [51, True], [52, True], [53, True], [54, True], [60, True], [61, True], [70, True], [71, True], [72, True], [73, True], [74, True], [80, True], [3, False]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [10, True], [11, True], [20, True], [21, True], [22, True], [30, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [50, True], [51, True], [52, True], [60, True], [61, True], [62, True], [63, True], [64, True], [65, True], [66, True], [70, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [10, True], [11, True], [12, True], [20, True], [21, True], [22, True], [23, True], [30, True], [31, True], [32, True], [33, True], [40, True], [41, True], [42, True], [50, True], [51, True], [52, True], [60, True], [61, True], [62, True], [70, True], [71, True], [80, True], [81, True], [82, True], [83, True], [84, True], [85, True], [86, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [10, True], [11, True], [12, True], [20, True], [21, True], [22, True], [23, True], [24, True], [30, True], [31, True], [32, True], [33, True], [40, True], [41, True], [42, True], [43, True], [44, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [58, True], [59, True], [60, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [80, True], [81, True]], [[0, True], [1, True], [2, True], [10, True], [11, True], [12, True], [13, True], [20, True], [21, True], [22, True], [23, True], [24, True], [30, True], [31, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [60, True], [61, True], [62, True], [63, True], [70, True], [80, True], [3, False], [59, False], [73, False], [74, False], [75, False], [76, False], [77, False]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [8, True], [9, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [16, True], [17, True], [20, True], [21, True], [22, True], [23, True], [24, True], [25, True], [26, True], [27, True], [28, True], [29, True], [30, True], [31, True], [32, True], [33, True], [34, True], [35, True], [36, True], [37, True], [38, True], [39, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [50, True], [60, True], [61, True], [62, True], [70, True], [80, True], [81, True], [82, True], [83, True], [84, True], [85, True], [86, True], [87, True], [88, True], [89, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [16, True], [17, True], [18, True], [19, True], [20, True], [21, True], [22, True], [23, True], [24, True], [25, True], [26, True], [27, True], [28, True], [29, True], [30, True], [31, True], [32, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [46, True], [47, True], [50, True], [60, True], [61, True], [62, True], [63, True], [64, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [76, True], [77, True], [78, True], [79, True], [80, True], [81, True], [82, True]], [[0, True], [10, True], [11, True], [12, True], [13, True], [14, True], [20, True], [21, True], [22, True], [23, True], [24, True], [25, True], [26, True], [27, True], [30, True], [31, True], [32, True], [33, True], [34, True], [35, True], [40, True], [41, True], [42, True], [43, True], [44, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [60, True], [61, True], [62, True], [63, True], [64, True], [65, True], [66, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [76, True], [77, True], [78, True], [79, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [10, True], [20, True], [30, True], [31, True], [32, True], [40, True], [50, True], [51, True], [52, True], [53, True], [54, True], [60, True], [61, True], [70, True], [80, True], [77, False]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [10, True], [11, True], [12, True], [13, True], [20, True], [21, True], [22, True], [23, True], [30, True], [40, True], [41, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [58, True], [60, True], [61, True], [62, True], [70, True], [71, True], [72, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [20, True], [21, True], [22, True], [23, True], [24, True], [25, True], [30, True], [31, True], [40, True], [41, True], [42, True], [43, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [58, True], [59, True], [60, True], [61, True], [62, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [80, True], [5, False], [32, False], [33, False], [34, False], [35, False], [36, False], [37, False], [38, False], [44, False], [45, False], [63, False], [64, False], [65, False], [66, False], [76, False], [77, False]], [[0, True], [1, True], [2, True], [10, True], [11, True], [12, True], [20, True], [21, True], [22, True], [23, True], [24, True], [30, True], [31, True], [40, True], [41, True], [42, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [58, True], [59, True], [60, True], [61, True], [62, True], [63, True], [64, True], [65, True], [66, True], [70, True], [80, True], [3, False], [4, False], [5, False], [13, False], [14, False], [15, False], [25, False], [32, False], [33, False], [34, False], [35, False], [36, False], [37, False], [38, False], [43, False], [44, False], [45, False], [71, False], [72, False], [73, False], [74, False], [75, False], [76, False], [77, False]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [10, True], [11, True], [20, True], [21, True], [22, True], [23, True], [30, True], [40, True], [41, True], [42, True], [50, True], [51, True], [52, True], [53, True], [60, True], [61, True], [62, True], [70, True], [71, True], [72, True], [80, True]], [[0, True], [10, True], [11, True], [12, True], [13, True], [20, True], [21, True], [22, True], [30, True], [31, True], [32, True], [33, True], [34, True], [35, True], [36, True], [37, True], [38, True], [39, True], [40, True], [41, True], [42, True], [43, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [58, True], [59, True], [60, True], [61, True], [62, True], [63, True], [64, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [10, True], [20, True], [21, True], [22, True], [30, True], [40, True], [41, True], [42, True], [43, True], [50, True], [51, True], [52, True], [53, True], [54, True], [60, True], [61, True], [62, True], [63, True], [64, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [76, True], [77, True], [78, True], [79, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [10, True], [11, True], [12, True], [13, True], [14, True], [20, True], [21, True], [22, True], [23, True], [24, True], [30, True], [31, True], [40, True], [41, True], [42, True], [43, True], [44, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [58, True], [59, True], [60, True], [61, True], [62, True], [63, True], [70, True], [71, True], [72, True], [80, True], [5, False], [15, False], [25, False], [32, False], [33, False], [34, False], [35, False], [36, False], [37, False], [38, False], [45, False], [64, False], [65, False], [66, False], [73, False], [74, False], [75, False], [76, False], [77, False]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [8, True], [9, True], [10, True], [20, True], [30, True], [40, True], [41, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [58, True], [59, True], [60, True], [61, True], [62, True], [63, True], [64, True], [70, True], [71, True], [72, True], [73, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [16, True], [17, True], [18, True], [19, True], [20, True], [21, True], [22, True], [30, True], [31, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [50, True], [51, True], [52, True], [53, True], [60, True], [61, True], [62, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [76, True], [77, True], [78, True], [79, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [8, True], [9, True], [10, True], [11, True], [12, True], [13, True], [20, True], [21, True], [22, True], [23, True], [24, True], [25, True], [26, True], [30, True], [31, True], [40, True], [41, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [60, True], [61, True], [62, True], [63, True], [64, True], [65, True], [70, True], [80, True]], [[0, True], [1, True], [2, True], [10, True], [11, True], [12, True], [13, True], [20, True], [21, True], [22, True], [23, True], [30, True], [31, True], [32, True], [33, True], [34, True], [35, True], [36, True], [37, True], [38, True], [39, True], [40, True], [41, True], [42, True], [43, True], [44, True], [50, True], [60, True], [61, True], [62, True], [70, True], [71, True], [72, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [10, True], [11, True], [12, True], [20, True], [21, True], [22, True], [30, True], [40, True], [41, True], [42, True], [50, True], [51, True], [52, True], [60, True], [61, True], [62, True], [70, True], [71, True], [72, True], [80, True], [81, True], [82, True], [5, False], [31, False], [32, False], [33, False], [34, False], [54, False], [55, False], [56, False], [57, False], [58, False], [59, False]], [[0, True], [1, True], [2, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [16, True], [17, True], [18, True], [19, True], [20, True], [21, True], [22, True], [30, True], [31, True], [32, True], [33, True], [34, True], [35, True], [36, True], [37, True], [38, True], [39, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [58, True], [59, True], [60, True], [61, True], [62, True], [63, True], [64, True], [70, True], [80, True], [81, True], [82, True], [83, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [16, True], [20, True], [21, True], [22, True], [23, True], [24, True], [25, True], [26, True], [27, True], [30, True], [31, True], [40, True], [41, True], [42, True], [43, True], [50, True], [51, True], [60, True], [61, True], [62, True], [63, True], [64, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [76, True], [77, True], [78, True], [79, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [8, True], [9, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [16, True], [17, True], [18, True], [19, True], [20, True], [21, True], [22, True], [23, True], [24, True], [25, True], [26, True], [27, True], [28, True], [29, True], [30, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [50, True], [60, True], [61, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [76, True], [77, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [8, True], [9, True], [10, True], [11, True], [20, True], [21, True], [30, True], [40, True], [41, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [60, True], [70, True], [80, True], [81, True]], [[0, True], [1, True], [2, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [20, True], [21, True], [22, True], [23, True], [30, True], [31, True], [32, True], [33, True], [34, True], [35, True], [36, True], [37, True], [38, True], [39, True], [40, True], [41, True], [42, True], [50, True], [51, True], [52, True], [60, True], [61, True], [62, True], [70, True], [71, True], [72, True], [73, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [10, True], [11, True], [12, True], [20, True], [21, True], [30, True], [40, True], [41, True], [42, True], [50, True], [51, True], [52, True], [53, True], [60, True], [61, True], [62, True], [63, True], [70, True], [80, True], [31, False], [32, False], [33, False], [34, False], [43, False], [71, False]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [10, True], [11, True], [12, True], [13, True], [20, True], [21, True], [22, True], [23, True], [30, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [50, True], [51, True], [52, True], [53, True], [54, True], [55, True], [56, True], [57, True], [58, True], [59, True], [60, True], [61, True], [70, True], [71, True], [72, True], [73, True], [74, True], [80, True], [81, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [5, True], [6, True], [7, True], [8, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [16, True], [17, True], [20, True], [21, True], [22, True], [23, True], [24, True], [25, True], [26, True], [27, True], [30, True], [31, True], [32, True], [33, True], [34, True], [35, True], [36, True], [37, True], [38, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [50, True], [51, True], [52, True], [60, True], [61, True], [62, True], [63, True], [70, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [16, True], [17, True], [18, True], [19, True], [20, True], [21, True], [22, True], [23, True], [24, True], [25, True], [26, True], [27, True], [30, True], [31, True], [32, True], [33, True], [34, True], [40, True], [41, True], [42, True], [43, True], [50, True], [60, True], [61, True], [62, True], [63, True], [64, True], [65, True], [66, True], [67, True], [68, True], [69, True], [70, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [10, True], [11, True], [12, True], [13, True], [20, True], [21, True], [22, True], [23, True], [24, True], [25, True], [30, True], [31, True], [32, True], [33, True], [34, True], [35, True], [36, True], [37, True], [40, True], [41, True], [42, True], [43, True], [50, True], [60, True], [61, True], [62, True], [63, True], [64, True], [65, True], [66, True], [67, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [76, True], [77, True], [78, True], [79, True], [80, True]], [[0, True], [1, True], [2, True], [3, True], [4, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [16, True], [20, True], [21, True], [22, True], [23, True], [30, True], [40, True], [41, True], [42, True], [43, True], [44, True], [45, True], [50, True], [60, True], [61, True], [62, True], [63, True], [64, True], [65, True], [66, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [76, True], [77, True], [78, True], [79, True], [80, True], [81, True], [82, True]], [[0, True], [1, True], [2, True], [3, True], [10, True], [11, True], [12, True], [13, True], [14, True], [15, True], [16, True], [17, True], [20, True], [21, True], [22, True], [23, True], [24, True], [25, True], [30, True], [31, True], [32, True], [33, True], [40, True], [41, True], [42, True], [50, True], [51, True], [52, True], [53, True], [60, True], [61, True], [62, True], [63, True], [64, True], [65, True], [66, True], [67, True], [68, True], [69, True], [70, True], [71, True], [72, True], [73, True], [74, True], [75, True], [80, True]]]"
    logicstruct=literal_eval(logicstr)


Человек это вряд ли поймёт, поэтому хорошо бы преобразовать эту структуру обратно в числа и составить из них логические правила.
Генерация if'а
Функции:
def numdecoder(binform=[]):
    greater=[1]*9
    less=[11]*9
    for _,item in enumerate(binform):
        if item[1]:
            greater[item[0]//10]=max(greater[item[0]//10],item[0]%10+1)
        else:
            less[item[0]//10]=min(less[item[0]//10],item[0]%10+1)
    return greater,less

def tologicfunc(numform=[]):
    result=""
    for _,item in enumerate(numform):
        tmp=""
        if _!=0:
            tmp = " or \\ \n"
        first=True
        for x in range(len(item[0])):

            if item[0][x]>1:
                if not first:
                    tmp+=" and "
                tmp+="x["+str(x)+"]>="+str(item[0][x])
                first=False
        for x in range(len(item[1])):
            if item[1][x]<11:
                tmp+=" and "
                tmp+="x["+str(x)+"]<"+str(item[1][x])
        result+=tmp
    return result

Пример генерации:
    numform=[]
    for _,item in enumerate(logicstruct):
        greater,less=numdecoder(item)
        numform.append([greater,less])

    print(tologicfunc(numform))


И финальный аккорд, тот самый if… хотя нет, не будет даже if'а.
Классификатор
def medfunc(x):
    return x[0]>=5 and x[1]>=3 and x[2]>=3 and x[3]>=3 and x[4]>=2 and x[5]>=3 and x[6]>=4 and x[7]>=4 and x[5]<4 or \
           x[0]>=7 and x[1]>=4 and x[2]>=6 and x[3]>=4 and x[4]>=6 and x[6]>=4 and x[7]>=3 or \
           x[0]>=2 and x[1]>=5 and x[2]>=3 and x[3]>=3 and x[4]>=6 and x[5]>=7 and x[6]>=7 and x[7]>=5 and x[0]<3 and x[1]<6 and x[2]<4 and x[3]<4 and x[5]<8 and x[7]<6 or \
           x[0]>=10 and x[1]>=4 and x[2]>=3 and x[4]>=3 and x[5]>=3 and x[6]>=6 and x[7]>=5 and x[8]>=2 or \
           x[0]>=5 and x[1]>=6 and x[2]>=5 and x[3]>=6 and x[4]>=10 and x[6]>=3 or \
           x[0]>=9 and x[1]>=5 and x[2]>=8 and x[4]>=2 and x[5]>=3 and x[6]>=2 and x[8]>=5 or \
           x[0]>=5 and x[1]>=3 and x[2]>=3 and x[3]>=4 and x[4]>=2 and x[5]>=4 and x[6]>=3 and x[7]>=4 and x[0]<6 and x[3]<5 and x[4]<3 and x[5]<5 and x[7]<5 or \
           x[0]>=8 and x[1]>=2 and x[2]>=4 and x[4]>=5 and x[6]>=5 and x[7]>=4 and x[8]>=4 or \
           x[0]>=5 and x[1]>=2 and x[2]>=3 and x[4]>=6 and x[5]>=10 and x[6]>=5 and x[0]<6 and x[1]<4 and x[2]<6 and x[3]<2 and x[6]<6 and x[7]<4 or \
           x[0]>=9 and x[1]>=5 and x[2]>=5 and x[3]>=2 and x[4]>=2 and x[5]>=2 and x[6]>=5 or \
           x[0]>=6 and x[1]>=3 and x[2]>=4 and x[4]>=5 and x[5]>=2 and x[6]>=3 and x[7]>=9 or \
           x[0]>=10 and x[1]>=4 and x[2]>=2 and x[4]>=3 and x[5]>=2 and x[6]>=4 and x[7]>=3 and x[8]>=10 or \
           x[0]>=5 and x[1]>=3 and x[2]>=4 and x[4]>=8 and x[5]>=10 and x[6]>=4 and x[7]>=9 or \
           x[0]>=6 and x[1]>=10 and x[2]>=2 and x[3]>=8 and x[4]>=10 and x[5]>=2 and x[6]>=7 and x[7]>=8 and x[8]>=10 or \
           x[0]>=10 and x[1]>=6 and x[2]>=4 and x[4]>=3 and x[5]>=4 and x[6]>=3 and x[7]>=2 and x[8]>=3 or \
           x[0]>=3 and x[1]>=3 and x[2]>=6 and x[3]>=4 and x[4]>=5 and x[5]>=8 and x[6]>=4 and x[7]>=4 or \
           x[0]>=2 and x[1]>=3 and x[2]>=4 and x[3]>=4 and x[4]>=2 and x[5]>=5 and x[6]>=2 and x[7]>=5 and x[0]<4 or \
           x[0]>=8 and x[1]>=2 and x[2]>=3 and x[4]>=6 and x[5]>=3 and x[6]>=7 or \
           x[0]>=7 and x[1]>=3 and x[2]>=4 and x[3]>=4 and x[4]>=3 and x[5]>=3 and x[6]>=3 and x[7]>=2 and x[8]>=7 or \
           x[0]>=8 and x[1]>=3 and x[2]>=5 and x[3]>=4 and x[4]>=5 and x[5]>=10 and x[7]>=6 and x[8]>=2 or \
           x[0]>=3 and x[1]>=4 and x[2]>=5 and x[3]>=2 and x[4]>=6 and x[5]>=8 and x[6]>=4 and x[0]<4 and x[5]<10 and x[7]<4 or \
           x[0]>=10 and x[1]>=8 and x[2]>=10 and x[3]>=10 and x[4]>=6 and x[6]>=3 and x[8]>=10 or \
           x[0]>=5 and x[1]>=10 and x[2]>=10 and x[3]>=3 and x[4]>=8 and x[6]>=5 and x[7]>=10 and x[8]>=3 or \
           x[1]>=5 and x[2]>=8 and x[3]>=6 and x[4]>=5 and x[5]>=8 and x[6]>=7 and x[7]>=10 or \
           x[0]>=4 and x[3]>=3 and x[5]>=5 and x[6]>=2 and x[7]<8 or \
           x[0]>=8 and x[1]>=4 and x[2]>=4 and x[4]>=2 and x[5]>=9 and x[6]>=3 and x[7]>=3 or \
           x[0]>=5 and x[1]>=6 and x[2]>=6 and x[3]>=2 and x[4]>=4 and x[5]>=10 and x[6]>=3 and x[7]>=6 and x[0]<6 and x[3]<3 and x[4]<5 and x[6]<4 and x[7]<7 or \
           x[0]>=3 and x[1]>=3 and x[2]>=5 and x[3]>=2 and x[4]>=3 and x[5]>=10 and x[6]>=7 and x[0]<4 and x[1]<4 and x[2]<6 and x[3]<3 and x[4]<4 and x[7]<2 or \
           x[0]>=7 and x[1]>=2 and x[2]>=4 and x[4]>=3 and x[5]>=4 and x[6]>=3 and x[7]>=3 or \
           x[1]>=4 and x[2]>=3 and x[3]>=10 and x[4]>=4 and x[5]>=10 and x[6]>=5 and x[7]>=6 or \
           x[0]>=6 and x[2]>=3 and x[4]>=4 and x[5]>=5 and x[6]>=5 and x[7]>=10 or \
           x[0]>=5 and x[1]>=5 and x[2]>=5 and x[3]>=2 and x[4]>=5 and x[5]>=10 and x[6]>=4 and x[7]>=3 and x[0]<6 and x[1]<6 and x[2]<6 and x[3]<3 and x[4]<6 and x[6]<5 and x[7]<4 or \
           x[0]>=10 and x[4]>=2 and x[5]>=10 and x[6]>=5 and x[7]>=4 or \
           x[0]>=8 and x[1]>=10 and x[2]>=3 and x[3]>=2 and x[4]>=6 and x[5]>=4 and x[6]>=3 and x[7]>=10 or \
           x[0]>=10 and x[1]>=4 and x[2]>=7 and x[3]>=2 and x[4]>=2 and x[5]>=8 and x[6]>=6 or \
           x[0]>=3 and x[1]>=4 and x[2]>=4 and x[3]>=10 and x[4]>=5 and x[6]>=3 and x[7]>=3 or \
           x[0]>=5 and x[1]>=3 and x[2]>=3 and x[4]>=3 and x[5]>=3 and x[6]>=3 and x[7]>=3 and x[8]>=3 and x[0]<6 and x[3]<2 and x[5]<5 or \
           x[0]>=3 and x[1]>=10 and x[2]>=3 and x[3]>=10 and x[4]>=6 and x[5]>=10 and x[6]>=5 and x[8]>=4 or \
           x[0]>=8 and x[1]>=7 and x[2]>=8 and x[3]>=2 and x[4]>=4 and x[5]>=2 and x[6]>=5 and x[7]>=10 or \
           x[0]>=10 and x[1]>=10 and x[2]>=10 and x[4]>=6 and x[6]>=2 and x[7]>=8 or \
           x[0]>=10 and x[1]>=2 and x[2]>=2 and x[4]>=2 and x[5]>=6 and x[8]>=2 or \
           x[0]>=3 and x[1]>=6 and x[2]>=4 and x[3]>=10 and x[4]>=3 and x[5]>=3 and x[6]>=3 and x[7]>=4 or \
           x[0]>=6 and x[1]>=3 and x[2]>=2 and x[4]>=3 and x[5]>=4 and x[6]>=4 and x[3]<2 and x[4]<4 and x[7]<2 or \
           x[0]>=8 and x[1]>=4 and x[2]>=4 and x[4]>=6 and x[5]>=10 and x[6]>=2 and x[7]>=5 and x[8]>=2 or \
           x[0]>=9 and x[1]>=8 and x[2]>=8 and x[3]>=9 and x[4]>=6 and x[5]>=3 and x[6]>=4 or \
           x[0]>=4 and x[1]>=10 and x[2]>=8 and x[3]>=5 and x[4]>=4 and x[6]>=10 or \
           x[0]>=5 and x[1]>=4 and x[2]>=6 and x[3]>=8 and x[4]>=4 and x[6]>=8 and x[7]>=10 or \
           x[0]>=5 and x[1]>=7 and x[2]>=4 and x[4]>=6 and x[6]>=7 and x[7]>=10 and x[8]>=3 or \
           x[0]>=4 and x[1]>=8 and x[2]>=6 and x[3]>=4 and x[4]>=3 and x[5]>=4 and x[6]>=10 and x[7]>=6


Входной параметр x — list из девяти чисел в диапазоне от 1 до 10. Точность соответственно такая же как и структуры. Вышло коротко и сердито. Ну и ладно.

P.S. Благодарю Anc за предоставление своих наработок по данной тематике.
Теги:
Хабы:
Всего голосов 35: ↑20 и ↓15+5
Комментарии19

Публикации

Истории

Работа

Data Scientist
79 вакансий
Python разработчик
117 вакансий

Ближайшие события

15 – 16 ноября
IT-конференция Merge Skolkovo
Москва
22 – 24 ноября
Хакатон «AgroCode Hack Genetics'24»
Онлайн
28 ноября
Конференция «TechRec: ITHR CAMPUS»
МоскваОнлайн
25 – 26 апреля
IT-конференция Merge Tatarstan 2025
Казань