Python Canopy NumPy: Running the code by pressing “Play” doesn't give the same result as copying and pasting the code in the command line

こ雲淡風輕ζ 提交于 2019-12-23 01:41:42

问题


I'm running Enthought Canopy 1.4.1 64 bit for a year now. For some reason, a code I just wrote produces completely different charts (using matplotlib) when pressing the "Play" button, vs. copying and pasting the code into the command line and pressing Enter.

In particular, the following line produces two different results:

w1 = array(dot(matrix(C).I,R - 0.03)/sum(dot(matrix(C).I,R - 0.03)))[0]

When pressing the Play, I get:

w1
Out[7]: array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])

When copying and pasting the exact same code and pressing Enter I get:

w1
Out[9]: 
array([-0.53497564,  0.77325699,  0.3289724 ,  0.2127899 ,  0.29026341,
        0.18743744, -0.24510907, -0.1117449 , -0.2534066 ,  0.15694775,
        0.19556833])

I think that's what messes up my chart, does anyone know why this is happening?


回答1:


Without seeing the rest of your code, the most likely candidate is sum.

  • In vanilla python (as when your script runs), sum is the python built-in function, which doesn't know about numpy arrays.

  • In IPython's Pylab mode (as at Canopy's ipython prompt), which implicitly starts with from numpy import * (terribly confusing, and one reason that the IPython team is now discouraging use of their Pylab mode, which I would guess Canopy will follow before long), sum is the numpy function, which behaves quite differently.

Look up both of these sum functions, and try using numpy.sum instead of sum in your script.

For more context:

@Senderle's first comment on your question points to the more general explanation of almost all such discrepancies in ipython -- when you run a script, it doesn't know any of the values in your global ipython namespace. As the script runs, by default, its global namespace is inserted into your ipython namespace, but not the other way around. So Ipython commands can inspect the results of a running script, but a running script cannot see/use the values (including imports) that were previously defined at the IPython prompt (unless they were explicitly passed to the running script).

The most common example of this is the one described in this article: Modules are already available in Canopy's Python (PyLab) prompt, but not in a script, but it also applies to data values as senderle was pointing to.

So to return to your problem -- 100-to-1 odds that you (or Pylab startup) have defined something at the IPython prompt which is not defined in the same way in the running script, and that accounts for the discrepancy. If it's not sum, then I suggest that you narrow it down to the simplest possible case (just a few lines), and then it should jump out at you; or if not, you can post it here and it will jump out at someone else.



来源:https://stackoverflow.com/questions/26407464/python-canopy-numpy-running-the-code-by-pressing-play-doesnt-give-the-same-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!