简体   繁体   中英

Convert array to a single float in Python

I am trying to write a function which would estimate data noise (σ2) based on three NP arrays - One augmented X-matrix and the two vectors - the y-target and the MAP weights:

MAP 估计数据噪声

This function should return the empirical data noise estimate, σ2.

I have the following function:

def estimDS (X, output_y, W):
    n = X.shape[0] # observations rows
    d = X.shape[1] # number of features in columns
    matmul = np.matmul(aug_x, ml_weights)
    mult_left = (1/(n-d))
    mult_right = (output_y-matmul)**2
    estimDS = mult_left * mult_right
    return estimDS 

And this is an example on which I run function:

output_y = np.array([208500, 181500, 223500, 
                            140000, 250000, 143000, 
                            307000, 200000, 129900, 
                            118000])
aug_x = np. array([[   1., 1710., 2003.],
                               [   1., 1262., 1976.],
                               [   1., 1786., 2001.],
                               [   1., 1717., 1915.],
                               [   1., 2198., 2000.],
                               [   1., 1362., 1993.],
                               [   1., 1694., 2004.],
                               [   1., 2090., 1973.],
                               [   1., 1774., 1931.],
                               [   1., 1077., 1939.]])

W = [-2.29223802e+06  5.92536529e+01  1.20780450e+03]

sig2 = estimDS(aug_x, output_y, W)
print(sig2)

Function returns an array, but I need to get this result as a float 3700666577282.7227

[5.61083809e+07 2.17473754e+07 6.81288433e+06 4.40198178e+07
 1.86225354e+06 3.95549405e+08 8.78575426e+08 3.04530677e+07
 3.32164594e+07 2.87861673e+06]

You forgot to sum over i=1 to n . Therefore mult_right should be defined as:

mult_right=np.sum((output_y-matmul)**2, axis=0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM