I need to convert a binary input into a decimal integer. I know how to go from a decimal to a binary:
n = int(raw_input(\'enter a number: \'))
print \'{0:b}\'.fo
There is actually a much faster alternative to convert binary numbers to decimal, based on artificial intelligence (linear regression) model:
See example and time comparison below:
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import numpy as np
y = np.random.randint(0, 2**32, size=10_000)
def gen_x(y):
_x = bin(y)[2:]
n = 32 - len(_x)
return [int(sym) for sym in '0'*n + _x]
X = np.array([gen_x(x) for x in y])
model = LinearRegression()
model.fit(X, y)
def convert_bin_to_dec_ai(array):
return model.predict(array)
y_pred = convert_bin_to_dec_ai(X)
Time comparison:
This AI solution converts numbers almost x10 times faster than conventional way!