Converting binary to decimal integer output

前端 未结 9 2338
暗喜
暗喜 2021-02-13 16:58

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         


        
9条回答
  •  走了就别回头了
    2021-02-13 17:36

    There is actually a much faster alternative to convert binary numbers to decimal, based on artificial intelligence (linear regression) model:

    1. Train an AI algorithm to convert 32-binary number to decimal based.
    2. Predict a decimal representation from 32-binary.

    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!

提交回复
热议问题