Python Canopy NumPy:通过按"Play"(运行)来运行代码.与在命令行中复制和粘贴代码的结果不同 [英] 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

查看:118
本文介绍了Python Canopy NumPy:通过按"Play"(运行)来运行代码.与在命令行中复制和粘贴代码的结果不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在运行Enthought Canopy 1.4.1 64位已有一年了. 由于某种原因,我刚刚编写的代码在按下播放"按钮时会产生完全不同的图表(使用matplotlib),而不是将代码复制并粘贴到命令行中并按Enter.

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]

按Play时,我得到:

When pressing the Play, I get:

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

在复制和粘贴完全相同的代码并按Enter时,我得到:

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?

推荐答案

在没有看到其余代码的情况下,最有可能的候选对象是sum.

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

  • 在普通python中(如您的脚本运行时),sum是python内置函数,它不了解numpy数组.

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

在IPython的Pylab模式下(如Canopy的ipython提示符),该模式隐式以from numpy import *开头(非常令人困惑,这也是IPython团队现在不鼓励使用其Pylab模式的原因之一,我猜这是Canopy将很快出现),sum是numpy函数,其行为截然不同.

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.

查找这两个sum函数,然后尝试在脚本中使用numpy.sum而不是sum.

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

更多背景信息:

@Senderle对您的问题的第一条评论指出了对ipython中几乎所有此类差异的更一般的解释-当您运行脚本时,它不知道全局ipython命名空间中的任何值.脚本运行时,默认情况下,其全局名称空间插入到您的ipython名称空间中,但不会插入其他位置.因此,Ipython命令可以检查正在运行的脚本的结果,但是正在运行的脚本无法查看/使用先前在IPython提示符下定义的值(包括导入)(除非将它们显式传递给正在运行的脚本).

@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).

最常见的示例是本文中描述的示例: 模块已在Canopy的Python(PyLab)提示符下提供,但在脚本中没有提供,但它也适用于senderle指向的数据值.

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.

所以要回到您的问题上来-您(或Pylab启动)在IPython提示符下定义了某些东西(在运行的脚本中未以相同的方式定义)的几率为100: .如果不是sum,那么我建议您将其范围缩小到最简单的情况(仅几行),然后就应该跳出来.否则,您可以在此处发布它,它会跳到其他人那里.

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.

这篇关于Python Canopy NumPy:通过按"Play"(运行)来运行代码.与在命令行中复制和粘贴代码的结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆