Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

How to read element by element an array inside an array in python

I want to calculate the MSE of between two vectors, the original one and a prediction. X_train is a vector with all the original inputs and reconstructed32 are all the predictions, but I only want to calculate the MSE for the first element (X_train[0] and its prediction reconstructed32[0]), and I want do do it manually using some sort of function as the following:

mse=[]
for coef, coef32 in zip(X_train[0], reconstructed32[0]):
    mse.append((coef-coef32)**2)
print("MSE is", np.mean(mse))

But coef and coef32 do not access all the coefficients of the vector, instead they take the full vector and the for does only one iteration. This is how X_train[0] looks like:

array([[0.00000000e+00, 0.00000000e+00, 2.26577651e-02, 4.44901595e-03,
        0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
        1.19896736e-02, 2.88007129e-03, 0.00000000e+00, 0.00000000e+00,
        1.89431056e-01, 0.00000000e+00, 3.64951454e-02, 0.00000000e+00,
        3.19024414e-01, 7.74396257e-03, 6.00360870e-01, 0.00000000e+00,
        6.25879224e-03, 6.58161461e-01, 8.55771124e-01, 0.00000000e+00,
        9.72116515e-02, 1.20666134e-03, 1.45429194e-01, 0.00000000e+00,
        8.25748563e-01, 6.86123502e-03, 5.37337780e-01, 0.00000000e+00,
        0.00000000e+00, 5.24137542e-03, 0.00000000e+00, 3.74730385e-04,
        0.00000000e+00, 1.18224040e-01, 8.56024176e-02, 0.00000000e+00,
        2.49160156e-02, 0.00000000e+00, 8.91805351e-01, 2.61418521e-03,
        0.00000000e+00, 1.73141190e-03, 0.00000000e+00, 4.95929737e-04,
        0.00000000e+00, 1.00000000e+00, 4.93207388e-02, 5.37037617e-03,
        3.81159922e-03, 2.94345170e-01, 4.77780886e-02, 0.00000000e+00,
        5.86739518e-02, 1.02838585e-02, 1.41941339e-01, 3.23100435e-03,
        0.00000000e+00, 1.34332031e-01, 2.35870923e-03, 0.00000000e+00,
        5.09367557e-03, 0.00000000e+00, 1.07753032e-03, 0.00000000e+00,
        0.00000000e+00, 0.00000000e+00, 3.47328931e-01, 0.00000000e+00,
        0.00000000e+00, 2.65773922e-01, 1.57823116e-02, 5.41949784e-03,
        7.56190493e-05, 1.40293926e-01, 5.02460590e-03, 6.51085284e-03,
        9.38402303e-03, 2.63461888e-01, 1.96186375e-04, 1.39610067e-01,
        0.00000000e+00, 2.39743218e-02, 3.54982950e-02, 6.54916763e-02,
        5.11557400e-01, 3.56561318e-03, 5.72423302e-02, 0.00000000e+00,
        1.21502653e-01, 2.71597654e-02, 2.47318600e-03, 6.85532205e-03,
        5.31266391e-01, 0.00000000e+00, 0.00000000e+00, 2.94529963e-02,
        0.00000000e+00, 3.75285745e-01, 3.31357837e-01, 1.18085509e-03,
        1.19613800e-02, 6.72158494e-05, 0.00000000e+00, 0.00000000e+00,
        0.00000000e+00, 7.86180317e-01, 0.00000000e+00, 0.00000000e+00,
        4.27392730e-03, 2.27557942e-02, 1.35929761e-02, 1.27607910e-02,
        0.00000000e+00, 2.63269083e-03, 1.48336608e-02, 0.00000000e+00,
        1.77292936e-02, 1.18409727e-04, 0.00000000e+00, 5.49420249e-03,
        0.00000000e+00, 1.70363311e-03, 1.75287843e-01, 4.07821295e-04]],
      dtype=float32)

As an example, if I try this simple code:

for coef in X_train[0]:
    print("coef is", coef)

I get this output:

coef is [0.00000000e+00 0.00000000e+00 2.26577651e-02 4.44901595e-03
 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
 1.19896736e-02 2.88007129e-03 0.00000000e+00 0.00000000e+00
 1.89431056e-01 0.00000000e+00 3.64951454e-02 0.00000000e+00
 3.19024414e-01 7.74396257e-03 6.00360870e-01 0.00000000e+00
 6.25879224e-03 6.58161461e-01 8.55771124e-01 0.00000000e+00
 9.72116515e-02 1.20666134e-03 1.45429194e-01 0.00000000e+00
 8.25748563e-01 6.86123502e-03 5.37337780e-01 0.00000000e+00
 0.00000000e+00 5.24137542e-03 0.00000000e+00 3.74730385e-04
 0.00000000e+00 1.18224040e-01 8.56024176e-02 0.00000000e+00
 2.49160156e-02 0.00000000e+00 8.91805351e-01 2.61418521e-03
 0.00000000e+00 1.73141190e-03 0.00000000e+00 4.95929737e-04
 0.00000000e+00 1.00000000e+00 4.93207388e-02 5.37037617e-03
 3.81159922e-03 2.94345170e-01 4.77780886e-02 0.00000000e+00
 5.86739518e-02 1.02838585e-02 1.41941339e-01 3.23100435e-03
 0.00000000e+00 1.34332031e-01 2.35870923e-03 0.00000000e+00
 5.09367557e-03 0.00000000e+00 1.07753032e-03 0.00000000e+00
 0.00000000e+00 0.00000000e+00 3.47328931e-01 0.00000000e+00
 0.00000000e+00 2.65773922e-01 1.57823116e-02 5.41949784e-03
 7.56190493e-05 1.40293926e-01 5.02460590e-03 6.51085284e-03
 9.38402303e-03 2.63461888e-01 1.96186375e-04 1.39610067e-01
 0.00000000e+00 2.39743218e-02 3.54982950e-02 6.54916763e-02
 5.11557400e-01 3.56561318e-03 5.72423302e-02 0.00000000e+00
 1.21502653e-01 2.71597654e-02 2.47318600e-03 6.85532205e-03
 5.31266391e-01 0.00000000e+00 0.00000000e+00 2.94529963e-02
 0.00000000e+00 3.75285745e-01 3.31357837e-01 1.18085509e-03
 1.19613800e-02 6.72158494e-05 0.00000000e+00 0.00000000e+00
 0.00000000e+00 7.86180317e-01 0.00000000e+00 0.00000000e+00
 4.27392730e-03 2.27557942e-02 1.35929761e-02 1.27607910e-02
 0.00000000e+00 2.63269083e-03 1.48336608e-02 0.00000000e+00
 1.77292936e-02 1.18409727e-04 0.00000000e+00 5.49420249e-03
 0.00000000e+00 1.70363311e-03 1.75287843e-01 4.07821295e-04]

And I would like to get "coef is" in front of every coefficient, not in front of the full vector. How can I solve this?

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this,

for coef in X_train[0][0]:
    print("coef is", coef)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...