纳速健身

标题: python error : RuntimeWarning: invalid value encountered in log2 [打印本页]

作者: awagink    时间: 2021-1-17 11:40
标题: python error : RuntimeWarning: invalid value encountered in log2
python error : RuntimeWarning: invalid value encountered in log2
python 错误提示警告 RuntimeWarning: invalid value encountered in log2

log函数不能处理负数,如果数据中有负数,则会提示该错误


This error message can emerge at two expression your code contains:

np.log(0.6*c) and np.log(a**2)
in the for loop with:

np.random.normal()
you will get random numbers at this distribution, whose values will be negative numbers.

That's why np.log() will drop up the error message:

RuntimeWarning: invalid value encountered in log
Example:

np.random.normal(10,4,100)
Out:

array([ 8.04664247, 14.4991884 , 10.89789303, 13.37593183,  3.29981902,
       16.6316143 , 10.64138342,  4.0459445 , 10.49192082, -3.04538967!!!!!,
       13.30443781,  4.13345961, 12.06508196, 10.4286879 ,  7.39431349,
       12.36789249,  9.20424736, 11.13161087, 12.15404482, 12.69897663,
        9.43633904, 12.77818913,  9.02926639,  4.78638573, 13.13104605,
       12.71197993,  6.1550897 ,  7.18496505,  4.3160573 ,  9.12631992,
        8.52408627, 12.45941119,  5.34780934,  5.7023213 , 13.53096085,
       12.1087058 ,  3.65110834,  5.15466232,  8.78768562, 12.54764999,
       15.12211713,  3.26481809,  9.8623701 , 15.88784306,  5.83355467,
        5.32775214,  8.81188865, 13.21886467,  6.78984216,  8.67260897,
        7.06100605, 13.75314668, 15.56562533, 10.33916552,  7.72745465,
       11.27606127, 11.56813697,  7.03177164, 10.63155512, 11.67072579,
       11.70855769, 10.78372397,  5.11327436, 15.61581808,  9.53446815,
       11.21806808, 11.2235412 ,  7.68339223, 12.71484256,  9.99613038,
       13.51834424,  7.73615596,  8.75145457, 13.02222188,  6.76757021,
       13.03580839, 10.67504642, 15.36110384, 15.66816384, -0.0952157 !!!!!!!! ,
        2.23551198, 11.21584659,  4.37791786,  5.45895529, 15.44411348,
       14.7077441 , 14.52080519,  3.70418827,  5.03132122,  5.24810117,
       16.35309566,  7.08504246,  6.81224092, 14.69274684,  8.43257572,
       12.87468578,  7.01621364,  7.62879265,  7.14646032, 20.16254855])

Stepping into your function inside of np.log()

c = np.array([ 8.04664247, 14.4991884 , 10.89789303, 13.37593183,  3.29981902,
       16.6316143 , 10.64138342,  4.0459445 , 10.49192082, -3.04538967,
       13.30443781,  4.13345961, 12.06508196, 10.4286879 ,  7.39431349,
       12.36789249,  9.20424736, 11.13161087, 12.15404482, 12.69897663,
        9.43633904, 12.77818913,  9.02926639,  4.78638573, 13.13104605,
       12.71197993,  6.1550897 ,  7.18496505,  4.3160573 ,  9.12631992,
        8.52408627, 12.45941119,  5.34780934,  5.7023213 , 13.53096085,
       12.1087058 ,  3.65110834,  5.15466232,  8.78768562, 12.54764999,
       15.12211713,  3.26481809,  9.8623701 , 15.88784306,  5.83355467,
        5.32775214,  8.81188865, 13.21886467,  6.78984216,  8.67260897,
        7.06100605, 13.75314668, 15.56562533, 10.33916552,  7.72745465,
       11.27606127, 11.56813697,  7.03177164, 10.63155512, 11.67072579,
       11.70855769, 10.78372397,  5.11327436, 15.61581808,  9.53446815,
       11.21806808, 11.2235412 ,  7.68339223, 12.71484256,  9.99613038,
       13.51834424,  7.73615596,  8.75145457, 13.02222188,  6.76757021,
       13.03580839, 10.67504642, 15.36110384, 15.66816384, -0.0952157 ,
        2.23551198, 11.21584659,  4.37791786,  5.45895529, 15.44411348,
       14.7077441 , 14.52080519,  3.70418827,  5.03132122,  5.24810117,
       16.35309566,  7.08504246,  6.81224092, 14.69274684,  8.43257572,
       12.87468578,  7.01621364,  7.62879265,  7.14646032, 20.16254855])

print(np.log(0.6*c))
Out:

[1.5744293  2.16326705 1.87774385 2.08263134 0.683042   2.30047974
1.85392487 0.8868894  1.83977989    nan!!!! 2.07727203 0.90828911
1.97948987 1.83373484 1.48988563 2.00427818 1.70883942 1.89896326
1.9868364  2.03069579 1.73374247 2.03691412 1.6896455  1.05494996
2.06415373 2.03171923 1.30645371 1.46116503 0.9515167  1.70033691
1.63207021 2.01165063 1.16586138 1.23004771 2.09415483 1.98309906
0.78420515 1.12907599 1.66252576 2.01870777 2.20533276 0.67237842
1.77790089 2.25472861 1.25280091 1.16210379 1.66527617 2.07081933
1.40460207 1.64934404 1.44376192 2.11044202 2.23423935 1.82511354
1.5339539  1.91185638 1.93742888 1.43961306 1.85300085 1.94625801
1.94949438 1.86721233 1.12101435 2.23745876 1.74408784 1.90670008
1.90718784 1.52823552 2.03194439 1.79137243 2.09322197 1.53507929
1.6583943  2.05583165 1.40131649 2.05687444 1.85708328 2.22101297
2.24080525   nan!!!!! 0.29364465 1.90650203 0.96574761 1.18643181
2.2264023  2.17754854 2.16475684 0.79863852 1.10485699 1.14704071
2.28359159 1.44716024 1.40789551 2.17652834 1.62127664 2.04443742
1.43739808 1.52110397 1.45579155 2.49300123]

/untitled0.py:37: RuntimeWarning: invalid value encountered in log


https://stackoverflow.com/questions/53300465/runtimewarning-invalid-value-encountered-in-log





欢迎光临 纳速健身 (https://nasue.com/) Powered by Discuz! X3.4