python按哪个键运行代码,Python Canopy NumPy:通过按“ Play”键运行代码与在命令行中复制和粘贴代码的结果不同...

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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值