この記事では、ここから取得したデータセットに基づいて、悪性乳房腫瘍を良性腫瘍から分類しようとします 。 どんなに奇妙に聞こえても、今回は精度が最優先事項ではありません。なぜなら、人間の生命はこれらのテストに依存しているため、精度に重点を置いたかなり良い解決策が既にあるからです。 たとえば、2012年にBrittany Wengerは、上記のセット専用にトレーニングされたcloud4cancer.appspot.comプロジェクトでGoogle Science Fairを受賞しました。
開始する前に、データセットの仕様に精通し、 以前の投稿を読むことをお勧めします。ソリューションアルゴリズムはそこから取得されるためです。 データについて簡単に説明すると、行の最初の番号は症例識別番号、次の9は腫瘍の説明、最後は腫瘍の種類(4-悪性、2-良性)です。 いくつかの例:
1017122,8,10,10,8,7,10,9,7,1,4
1018099,1,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%の偽陽性の回答が得られる構造を取得し、49の論理グループによる241の陽性例をまとめました。 これは最良の結果ではありませんが、将来的に使用します(誤検出が0個、誤検出が1.2%でした)。
論理構造
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)
人がこれを理解する可能性は低いため、この構造を数値に戻し、それらから論理的なルールを作成することは良いことです。
世代の場合
:
:
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さえありません。
分類子
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は、1〜10の範囲の9つの数値のリストです。それぞれの精度は、構造体と同じです。短く元気になりました。いいでしょう
PS このトピックに関するベストプラクティスを提供してくれたAncに感謝します。