Numpy产生错误的价值观

时间:2016-05-01 16:37:47

标签: python arrays list numpy

有人可以解释为什么列表中的值远远超过(numpy)数组中的值:

import numpy

R=75
NS=16
tmp_pi = numpy.zeros(NS*NS).astype(numpy.float32)
tmp_pj = numpy.zeros((NS*NS,1),dtype=numpy.float32)
calc = (float(NS)+1.)/2.
areaS= (float(2*R)/NS)
row = 95.
col = 74.
theta =3.5343

P_rotMatrix =numpy.dot(areaS, numpy.array([[numpy.cos(theta), numpy.sin(theta)], [-numpy.sin(theta),  numpy.cos(theta)]])).astype(numpy.float32)

a=[]

for i in range(0,NS*NS):
    a.append(0.)

for i in range (0,NS):
    for j in range (0,NS):
        index = (i*NS)+j
        value = col+((P_rotMatrix[1][0]*(j+1-calc))+(P_rotMatrix[1][1]*(i+1-calc)))
        a[index] = value
        tmp_pj[index] = value



print a
print tmp_pj

在进行一些调试时,我注意到当索引值为245时,tmp_pj中的值才开始变得不同(比在a中)。另外如果我将col值更改为75或73,它工作正常但是当我改变时col值为74,它再次表现异常。我使用的是python 2.7.6和numpy 1.11.0

a = [112.0520898103714, 115.63981854915619, 119.22754728794098,
122.81527602672577, 126.40300476551056, 129.99073350429535, 133.57846224308014, 137.16619098186493, 140.75391972064972, 144.34164845943451, 147.9293771982193, 151.51710593700409, 155.10483467578888, 158.69256341457367, 162.28029215335846, 165.86802089214325, 103.39074909687042, 106.97847783565521, 110.56620657444, 114.15393531322479, 117.74166405200958, 121.32939279079437, 124.91712152957916, 128.50485026836395, 132.09257900714874, 135.68030774593353, 139.26803648471832, 142.85576522350311, 146.4434939622879, 150.03122270107269, 153.61895143985748, 157.20668017864227, 94.729408383369446, 98.317137122154236, 101.90486586093903, 105.49259459972382, 109.08032333850861, 112.6680520772934, 116.25578081607819, 119.84350955486298, 123.43123829364777, 127.01896703243256, 130.60669577121735, 134.19442451000214, 137.78215324878693, 141.36988198757172, 144.95761072635651, 148.5453394651413, 86.068067669868469, 89.655796408653259, 93.243525147438049, 96.831253886222839, 100.41898262500763, 104.00671136379242, 107.59444010257721, 111.182168841362, 114.76989758014679, 118.35762631893158, 121.94535505771637, 125.53308379650116, 129.12081253528595, 132.70854127407074, 136.29627001285553, 139.88399875164032, 77.406726956367493, 80.994455695152283, 84.582184433937073, 88.169913172721863, 91.757641911506653, 95.345370650291443, 98.933099389076233, 102.52082812786102, 106.10855686664581, 109.6962856054306, 113.28401434421539, 116.87174308300018, 120.45947182178497, 124.04720056056976, 127.63492929935455, 131.22265803813934, 68.745386242866516, 72.333114981651306, 75.920843720436096, 79.508572459220886, 83.096301198005676, 86.684029936790466, 90.271758675575256, 93.859487414360046, 97.447216153144836, 101.03494489192963, 104.62267363071442, 108.21040236949921, 111.798131108284, 115.38585984706879, 118.97358858585358, 122.56131732463837, 60.08404552936554, 63.67177426815033, 67.25950300693512, 70.84723174571991, 74.4349604845047, 78.02268922328949, 81.61041796207428, 85.19814670085907, 88.78587543964386, 92.37360417842865, 95.96133291721344, 99.54906165599823, 103.13679039478302, 106.72451913356781, 110.3122478723526, 113.89997661113739, 51.422704815864563, 55.010433554649353, 58.598162293434143, 62.185891032218933, 65.773619771003723, 69.361348509788513, 72.949077248573303, 76.536805987358093, 80.124534726142883, 83.712263464927673, 87.299992203712463, 90.887720942497253, 94.475449681282043, 98.063178420066833, 101.65090715885162, 105.23863589763641, 42.761364102363586, 46.349092841148376, 49.936821579933167, 53.524550318717957, 57.112279057502747, 60.700007796287537, 64.287736535072327, 67.875465273857117, 71.463194012641907, 75.050922751426697, 78.638651490211487, 82.226380228996277, 85.814108967781067, 89.401837706565857, 92.989566445350647, 96.577295184135437, 34.10002338886261, 37.6877521276474, 41.27548086643219, 44.86320960521698, 48.45093834400177, 52.03866708278656, 55.62639582157135, 59.21412456035614, 62.80185329914093, 66.38958203792572, 69.97731077671051, 73.5650395154953, 77.15276825428009, 80.74049699306488, 84.32822573184967, 87.91595447063446, 25.438682675361633, 29.026411414146423, 32.614140152931213, 36.201868891716003, 39.789597630500793, 43.377326369285583, 46.965055108070374, 50.552783846855164, 54.140512585639954, 57.728241324424744, 61.315970063209534, 64.903698801994324, 68.491427540779114, 72.079156279563904, 75.666885018348694, 79.254613757133484, 16.777341961860657, 20.365070700645447, 23.952799439430237, 27.540528178215027, 31.128256916999817, 34.715985655784607, 38.303714394569397, 41.891443133354187, 45.479171872138977, 49.066900610923767, 52.654629349708557, 56.242358088493347, 59.830086827278137, 63.417815566062927, 67.005544304847717, 70.593273043632507, 8.1160012483596802, 11.70372998714447, 15.29145872592926, 18.87918746471405, 22.46691620349884, 26.05464494228363, 29.64237368106842, 33.23010241985321, 36.817831158638, 40.405559897422791, 43.993288636207581, 47.581017374992371, 51.168746113777161, 54.756474852561951, 58.344203591346741, 61.931932330131531, -0.54533946514129639, 3.0423892736434937, 6.6301180124282837, 10.217846751213074, 13.805575489997864, 17.393304228782654, 20.981032967567444, 24.568761706352234, 28.156490445137024, 31.744219183921814, 35.331947922706604, 38.919676661491394, 42.507405400276184, 46.095134139060974, 49.682862877845764, 53.270591616630554, -9.2066801786422729, -5.6189514398574829, -2.0312227010726929, 1.5565060377120972, 5.1442347764968872, 8.7319635152816772, 12.319692254066467, 15.907420992851257, 19.495149731636047, 23.082878470420837, 26.670607209205627, 30.258335947990417, 33.846064686775208, 37.433793425559998, 41.021522164344788, 44.609250903129578, -17.86802089214325, -14.280292153358459, -10.692563414573669, -7.1048346757888794, -3.5171059370040894, 0.070622801780700684, 3.6583515405654907, 7.2460802793502808, 10.833809018135071, 14.421537756919861, 18.009266495704651, 21.596995234489441, 25.184723973274231, 28.772452712059021, 32.360181450843811, 35.947910189628601]


tmp_pj=  [[  1.12052094e+02]  [  1.15639816e+02]  [  1.19227547e+02]  [  1.22815277e+02]  [  1.26403008e+02]  [  1.29990738e+02]  [ 
1.33578461e+02]  [  1.37166183e+02]  [  1.40753922e+02]  [  1.44341644e+02]  [  1.47929382e+02]  [  1.51517105e+02]  [  1.55104828e+02]  [  1.58692566e+02]  [  1.62280289e+02]  [  1.65868027e+02]  [  1.03390747e+02]  [  1.06978477e+02]  [  1.10566208e+02]  [  1.14153938e+02]  [  1.17741661e+02]  [  1.21329391e+02]  [  1.24917122e+02]  [  1.28504852e+02]  [  1.32092575e+02]  [  1.35680313e+02]  [  1.39268036e+02]  [  1.42855759e+02]  [  1.46443497e+02]  [  1.50031219e+02]  [  1.53618958e+02]  [  1.57206680e+02]  [  9.47294083e+01]  [  9.83171387e+01]  [  1.01904869e+02]  [  1.05492592e+02]  [  1.09080322e+02]  [  1.12668053e+02]  [  1.16255783e+02]  [  1.19843506e+02]  [  1.23431236e+02]  [  1.27018967e+02]  [  1.30606689e+02]  [  1.34194427e+02]  [  1.37782150e+02]  [  1.41369888e+02]  [  1.44957611e+02]  [  1.48545334e+02]  [  8.60680695e+01]  [  8.96557999e+01]  [  9.32435226e+01]  [  9.68312531e+01]  [  1.00418983e+02]  [  1.04006714e+02]  [  1.07594437e+02]  [  1.11182167e+02]  [  1.14769897e+02]  [  1.18357628e+02]  [  1.21945358e+02]  [  1.25533081e+02]  [  1.29120819e+02]  [  1.32708542e+02]  [  1.36296265e+02]  [  1.39884003e+02]  [  7.74067307e+01]  [  8.09944534e+01]  [  8.45821838e+01]  [  8.81699142e+01]  [  9.17576447e+01]  [  9.53453674e+01]  [  9.89330978e+01]  [  1.02520828e+02]  [  1.06108559e+02]  [  1.09696289e+02]  [  1.13284012e+02]  [  1.16871742e+02]  [  1.20459473e+02]  [  1.24047203e+02]  [  1.27634926e+02]  [  1.31222656e+02]  [  6.87453842e+01]  [  7.23331146e+01]  [  7.59208450e+01]  [  7.95085754e+01]  [  8.30962982e+01]  [  8.66840286e+01]  [  9.02717590e+01]  [  9.38594894e+01]  [  9.74472198e+01]  [  1.01034943e+02]  [  1.04622673e+02]  [  1.08210403e+02]  [  1.11798134e+02]  [  1.15385857e+02]  [  1.18973587e+02]  [  1.22561317e+02]  [  6.00840454e+01]  [  6.36717758e+01]  [  6.72595062e+01]  [  7.08472290e+01]  [  7.44349594e+01]  [  7.80226898e+01]  [  8.16104202e+01]  [  8.51981430e+01]  [  8.87858734e+01]  [  9.23736038e+01]  [  9.59613342e+01]  [  9.95490646e+01]  [  1.03136787e+02]  [  1.06724518e+02]  [  1.10312248e+02]  [  1.13899979e+02]  [  5.14227066e+01]  [  5.50104332e+01]  [  5.85981636e+01]  [  6.21858902e+01]  [  6.57736206e+01]  [  6.93613510e+01]  [  7.29490738e+01]  [  7.65368042e+01]  [  8.01245346e+01]  [  8.37122650e+01]  [  8.72999954e+01]  [  9.08877182e+01]  [  9.44754486e+01]  [  9.80631790e+01]  [  1.01650909e+02]  [  1.05238632e+02]  [  4.27613640e+01]  [  4.63490944e+01]  [  4.99368210e+01]  [  5.35245514e+01]  [  5.71122780e+01]  [  6.07000084e+01]  [  6.42877350e+01]  [  6.78754654e+01]  [  7.14631958e+01]  [  7.50509262e+01]  [  7.86386490e+01]  [  8.22263794e+01]  [  8.58141098e+01]  [  8.94018402e+01]  [  9.29895630e+01]  [  9.65772934e+01]  [  3.41000252e+01]  [  3.76877518e+01]  [  4.12754822e+01]  [  4.48632088e+01]  [  4.84509392e+01]  [  5.20386658e+01]  [  5.56263962e+01]  [  5.92141228e+01]  [  6.28018532e+01]  [  6.63895798e+01]  [  6.99773102e+01]  [  7.35650406e+01]  [  7.71527710e+01]  [  8.07404938e+01]  [  8.43282242e+01]  [  8.79159546e+01]  [  2.54386826e+01]  [  2.90264111e+01]  [  3.26141396e+01]  [  3.62018700e+01]  [  3.97895966e+01]  [  4.33773270e+01]  [  4.69650536e+01]  [  5.05527840e+01]  [  5.41405144e+01]  [  5.77282410e+01]  [  6.13159714e+01]  [  6.49037018e+01]  [  6.84914246e+01]  [  7.20791550e+01]  [  7.56668854e+01]  [  7.92546158e+01]  [  1.67773418e+01]  [  2.03650703e+01]  [  2.39527988e+01]  [  2.75405273e+01]  [  3.11282578e+01]  [  3.47159843e+01]  [  3.83037148e+01]  [  4.18914413e+01]  [  4.54791718e+01]  [  4.90669022e+01]  [  5.26546288e+01]  [  5.62423592e+01]  [  5.98300858e+01]  [  6.34178162e+01]  [  6.70055466e+01]  [  7.05932693e+01]  [  8.11600113e+00]  [  1.17037296e+01]  [  1.52914591e+01]  [  1.88791866e+01]  [  2.24669170e+01]  [  2.60546455e+01]  [  2.96423740e+01]  [  3.32301025e+01]  [  3.68178329e+01]  [  4.04055595e+01]  [  4.39932899e+01]  [  4.75810165e+01]  [  5.11687469e+01]  [  5.47564735e+01]  [  5.83442039e+01]  [  6.19319305e+01]  [ -5.45339465e-01]  [  3.04238939e+00]  [  6.63011789e+00]  [  1.02178469e+01]  [  1.38055754e+01]  [  1.73933048e+01]  [  2.09810333e+01]  [  2.45687618e+01]  [  2.81564903e+01]  [  3.17442188e+01]  [  3.53319473e+01]  [  3.89196777e+01]  [  4.25074043e+01]  [  4.60951347e+01]  [  4.96828613e+01]  [  5.32705917e+01]  [ -9.20668030e+00]  [ -5.61895132e+00]  [ -2.03122282e+00]  [  1.55650604e+00]  [  5.14423466e+00]  [  8.73196316e+00]  [  1.23196926e+01]  [  1.59074211e+01]  [  1.94951496e+01]  [  2.30828781e+01]  [  2.66706066e+01]  [  3.02583351e+01]  [  3.38460655e+01]  [  3.74337921e+01]  [  4.10215225e+01]  [  4.46092491e+01]  [ -1.78680210e+01]  [ -1.42802925e+01]  [ -1.06925631e+01]  [ -7.10483456e+00]  [ -3.51710606e+00]  [  7.06228018e-02]  [  3.65835142e+00]  [  7.24608040e+00]  [  1.08338089e+01]  [  1.44215374e+01]  [  1.80092659e+01]  [  2.15969944e+01]  [  2.51847248e+01]  [  2.87724533e+01]  [  3.23601799e+01]  [  3.59479103e+01]]

1 个答案:

答案 0 :(得分:1)

舍入错误:您将float64存储在float32数组中,因此值会四舍五入为32位,而在列表中它会保持不变:

>>> n = numpy.float64(1./3)
>>> n
>>> 0.33333333333333331
>>> numpy.float32(n)
>>> 0.33333334
>>> n == numpy.float32(n)
>>> False