view bin2dec.py @ 0:e93aac1287c0

Initial commit
author Lewin Bormann <lbo@spheniscida.de>
date Thu, 24 Dec 2020 08:54:24 +0100
parents
children
line wrap: on
line source

#!/usr/bin/env python3

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import numpy as np


def gen_x(y):
    x = bin(y)[2:]
    n = 32 - len(x)
    return [int(c) for c in '0'*n + x]


# Generate array X of binary-character lists of random numbers
y = np.random.randint(0, 1 << 32, size=10000)
X = np.array([gen_x(x) for x in y])

model = LinearRegression()
model.fit(X, y)


def bin2dec_ai(array):
    return model.predict(array)


def bin2dec_ai_test(n):
    arr = gen_x(n)

    actual = n
    prediction = bin2dec_ai(np.array([arr]))
    print('Actual: {} Predicted: {} Diff: {}'.format(actual, prediction, prediction-actual))