⬆️ ⬇️

Extraction of "knowledge" or classification into one if



In the article we will try to classify a malignant breast tumor from benign based on a data set taken from here . No matter how strange it may sound, accuracy will not be the main priority this time, as there are already fairly good solutions with an emphasis on accuracy, which is understandable, because human life depends on these tests. For example, in 2012, Brittany Wenger won the Google Science Fair competition with the project cloud4cancer.appspot.com , which was trained specifically for the above set.



Before starting, I advise you to familiarize yourself with the dataset specifications and read my previous post , since the solution algorithm was taken from there. Briefly about the data, the first number in the line is the case identification number, the next 9 describe the tumor, the last one is the type of the tumor (4 is malignant, 2 is benign). A couple of examples:

1017122,8,10,10,8,7,10,9,7,1,4

1018099,1,1,1,1,2,10,3,1,1,2


For those who have not read the previous post. I summarize it in this paragraph.

A neuron is a complex adaptive logic function that does not have functional completeness , since it cannot generate those truth tables in which, with all inactive parameters (False), the result is True. In particular, such logical functions that a neuron cannot simulate include: negation, Scheffer's stroke, Pierce arrow, equivalence, implications, etc. But there are exceptions: neurons that periodically generate an action potential without stimulating dendrites. This is just a personal opinion that helped make the decision algorithm.



The class from the previous post, which will be used in solving:
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 




So, to the point. First you need to classify at least somehow with satisfactory accuracy.

We train
 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)) 




There were question marks in the data, which I replaced in the notebook with dozens, considering that it would be better for the classifier to falsely work than to let it through. It also converted numbers into binary form with the getbin function, while skipping the first one, since it does not play a special role. As a result, after training, I got a structure that gives 1.4% false negative and 1.1% false positive answers, summarizing 241 positive examples by 49 logical groups. This is not the best result, but we will use it in the future (I received 0 false negatives and 1.2% false positive responses).

Logicstructuring structure
  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) 




A person is unlikely to understand this, so it would be good to transform this structure back into numbers and make logical rules out of them.

Generate if'a
Functions:

 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 


Generation example:

  numform=[] for _,item in enumerate(logicstruct): greater,less=numdecoder(item) numform.append([greater,less]) print(tologicfunc(numform)) 




And the final chord, the same if ... but no, there won't even be an ifa.

Classifier
 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 




The input parameter x is a list of nine numbers in the range from 1 to 10. The accuracy is respectively the same as the structure. It came out short and angry. Well, okay.



PS I thank Anc for providing my own work on this topic.


')

Source: https://habr.com/ru/post/208764/



All Articles