27
Апр
2021

Написанная программа ищет лучшую формулу для предугадывания числа где 0 один результат, 1 другой . Насколько верно сравнивать её с нейросетью?

import random
import itertools
example = [[1.4,2.74,2.35,1.05],[2.42,1.5,0.7,1.5],[1.63,2.14,1.28,0.39],[1.37,2.87,0.92,0.62],[1.85,1.85,1.18,0.98],[2.09,1.66,0.95,0.42],[1.66,2.09,1.21,1.375],[2.0,1.72,0.57,1.02],[1.42,2.67,1.05,1.46],[2.11,1.65,0.61,0.79],[1.87,1.83,1.21,0.68],[1.38,2.81,1.02,0.42],[2.93,1.35,1.22,0.54],[1.85,1.85,1.41,1.14],[1.81,1.81,1.18,1.025],[1.88,1.83,0.73,0.66],[2.0,1.72,0.73,0.82],[1.44,2.61,0.97,1.73],[1.42,2.66,1.35,1.36],[1.41,2.71,1.85,0.82],[1.86,1.84,1.46,0.825],[1.85,1.85,1.03,1.35],]
answer = [1,1,0,0,1,0,0,0,1,0,1,0,1,1,1,1,1,0,0,0,1,1,]
list_0 = []
list_1 = []
list_0_copy = []
list_1_copy = []
index = [0, 1, 2, 3]
index_every = []
sign = [0, 1, 2, 3]
a = 0
b = []
c = []
x = []
w1 = 0
w2 = 0
w3 = 0
s1 = 0
s2 = 0
s3 = 0
evaluation_0 = []
evaluation_1 = []
evaluation_0_middle = []
evaluation_1_middle = []
fault_0 = []
fault_1 = []
fault_union = 0
best_evaluation = [0]
fault_every = 0
for i in range(len(answer)):    #Распределения в списки 0 и 1
    every = answer[a]
    if every == 0:
        list_0.append( example[a] )
    elif every == 1:
        list_1.append( example[a] )
    a += 1
index_every = list( itertools.permutations(index)) #Все возможные перестоновки 4 входных чисел
sign_stage = list( itertools.combinations_with_replacement(sign,3)) #Считает и заносит в список все варианты знаков
print((sign_stage))
for i in index_every:
    for j in list_0:
        x = [j[k] for k in i]
        list_0_copy.append( x )   #Одно из расположений 0 по индексу из index_every
    for l in list_1:
        x = [l[k] for k in i]   #Одно из расположений 1 по индексу из index_every
        list_1_copy.append( x )
    for q in sign_stage:        #Перебирает все возможные знаки
        n1 = q[0]
        n2 = q[1]
        n3 = q[2]
        for w in list_0_copy:   # 0_+    1_-    2_*    3_/
            if n1 == 0:     #n1
                w1 = w[0] + w[1]
            elif n1 == 1:
                w1 = w[0] - w[1]
            elif n1 == 2:
                w1 = w[0] * w[1]
            elif n1 == 3:
                w1 = w[0] / w[1]
            if n2 == 0:     #n2
                w2 = w1 + w[2]
            elif n2 == 1:
                w2 = w1 - w[2]
            elif n2 == 2:
                w2 = w1 * w[2]
            elif n2 == 3:
                w2 = w1 / w[2]
            if n3 == 0:     #3
                w3 = w2 + w[3]
            elif n3 == 1:
                w3 = w2 - w[3]
            elif n3 == 2:
                w3 = w2 * w[3]
            elif n3 == 3:
                w3 = w2 / w[3]
            evaluation_0.append( w3 )
        for s in list_1_copy:   # 0_+    1_-    2_*    3_/
            if n1 == 0:     #n1
                s1 = s[0] + s[1]
            elif n1 == 1:
                s1 = s[0] - s[1]
            elif n1 == 2:
                s1 = s[0] * s[1]
            elif n1 == 3:
                s1 = s[0] / s[1]
            if n2 == 0:     #n2
                s2 = s1 + s[2]
            elif n2 == 1:
                s2 = s1 - s[2]
            elif n2 == 2:
                s2 = s1 * s[2]
            elif n2 == 3:
                s2 = s1 / s[2]
            if n3 == 0:     #3
                s3 = s2 + s[3]
            elif n3 == 1:
                s3 = s2 - s[3]
            elif n3 == 2:
                s3 = s2 * s[3]
            elif n3 == 3:
                s3 = s2 / s[3]
            evaluation_1.append( s3 )
        evaluation_0_middle = sum( evaluation_0 ) / len( evaluation_0 )     #Среднее значение получаймых чисел для 0
        evaluation_1_middle = sum( evaluation_1 ) / len( evaluation_1 )     #Среднее значение получаймых чисел для 1
        if evaluation_0_middle > evaluation_1_middle:       # 0 > 1
            for t in evaluation_0:      #Погрешность для 0
                if t < evaluation_0_middle:
                    fault_0.append( evaluation_0_middle - t )
            for p in evaluation_1:      #Погрешность для 1
                if p > evaluation_1_middle:
                    fault_1.append( p - evaluation_1_middle )
        elif evaluation_0_middle < evaluation_1_middle:     # 0 < 1
            for m in evaluation_0:      #Погрешность для 0
                if m > evaluation_0_middle:
                    fault_0.append( m - evaluation_0_middle )
            for f in evaluation_1:      #Погрешность для 1
                if f < evaluation_1_middle:
                    fault_1.append( evaluation_1_middle - f )
        evaluation_0 = []
        evaluation_1 = []
        fault_0 = sum( fault_0) / len(fault_0)  #считает среднию погрешность
        fault_1 = sum( fault_1) / len(fault_1)
        fault_union = fault_0 + fault_1
        fault_every = fault_union
        fault_union = abs( evaluation_0_middle - evaluation_1_middle ) / fault_union    #среднем значение / Погрешность
        print(fault_union)
        if fault_union > best_evaluation[0]:        #Наименьшая погрешность и заносятся данные для расчета
            del best_evaluation[:]
            best_evaluation.append(fault_union)
            best_evaluation.append(i)
            best_evaluation.append(n1)
            best_evaluation.append(n2)
            best_evaluation.append(n3)
            best_evaluation.append(evaluation_0_middle)
            best_evaluation.append(evaluation_1_middle)
            best_evaluation.append(fault_every)
print(best_evaluation)

Источник: https://ru.stackoverflow.com/questions/1275636/%D0%9D%D0%B0%D0%BF%D0%B8%D1%81%D0%B0%D0%BD%D0%BD%D0%B0%D1%8F-%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%B0-%D0%B8%D1%89%D0%B5%D1%82-%D0%BB%D1%83%D1%87%D1%88%D1%83%D1%8E-%D1%84%D0%BE%D1%80%D0%BC%D1%83%D0%BB%D1%83-%D0%B4%D0%BB%D1%8F-%D0%BF%D1%80%D0%B5%D0%B4%D1%83%D0%B3%D0%B0%D0%B4%D1%8B%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F-%D1%87%D0%B8%D1%81%D0%BB%D0%B0-%D0%B3%D0%B4%D0%B5-0-%D0%BE%D0%B4%D0%B8%D0%BD-%D1%80%D0%B5%D0%B7

Тебе может это понравится...

Добавить комментарий