Numpy 与直接 python 不同的问题? [英] gotchas where Numpy differs from straight python?

查看:38
本文介绍了Numpy 与直接 python 不同的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位,

是否有一些 Numpy 与 python 不同的陷阱,有困惑和花费时间的点?

<块引用>

那一刻的恐怖我将永远不会忘记!"
不过,你会的,"女王说,如果你不做个备忘录."

例如,NaN 在任何地方总是很麻烦.如果你不用运行就可以解释这一点,给自己一个点 --

from numpy import array, NaN, isnanpynan = float("nan")打印 pynan 是 pynan,pynan 是 NaN,NaN 是 NaNa = (0, pynan)print a, a[1] is pynan, any([aa is pynan for aa in a])a = 数组(( 0, NaN ))打印 a, a[1] 是 NaN, isnan( a[1] )

(我不是在敲麻木,那里有很多好的工作,只是认为常见问题解答或陷阱的 Wiki 会很有用.)

我希望收集六个陷阱(对于学习 Numpy 的人来说是个惊喜).
然后,如果有共同的问题,或者更好的共同解释,我们可以讨论将它们添加到社区 Wiki(在哪里?)到目前为止,我们似乎还不够.

解决方案

对我来说最大的问题是几乎每个标准运算符都被重载以分布在数组中.

定义一个列表和一个数组

<预><代码>>>>l = 范围(10)>>>升[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>>导入 numpy>>>a = numpy.array(l)>>>一种数组([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

乘法复制 python 列表,但分布在 numpy 数组上

<预><代码>>>>升*2[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>>a2数组([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])

python 列表中没有定义加法和除法

<预><代码>>>>升 + 2回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:只能将列表(不是int")连接到列表>>>一个 + 2数组([ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])>>>升/2.0回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:不支持/的操作数类型:'list' 和 'float'>>>一个/2.0数组([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])

Numpy 重载有时将列表视为数组

<预><代码>>>>一个 + 一个数组([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])>>>一+一数组([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])

Folks,

is there a collection of gotchas where Numpy differs from python, points that have puzzled and cost time ?

"The horror of that moment I shall never never forget !"
"You will, though," the Queen said, "if you don't make a memorandum of it."

For example, NaNs are always trouble, anywhere. If you can explain this without running it, give yourself a point --

from numpy import array, NaN, isnan

pynan = float("nan")
print pynan is pynan, pynan is NaN, NaN is NaN
a = (0, pynan)
print a, a[1] is pynan, any([aa is pynan for aa in a])

a = array(( 0, NaN ))
print a, a[1] is NaN, isnan( a[1] )

(I'm not knocking numpy, lots of good work there, just think a FAQ or Wiki of gotchas would be useful.)

Edit: I was hoping to collect half a dozen gotchas (surprises for people learning Numpy).
Then, if there are common gotchas or, better, common explanations, we could talk about adding them to a community Wiki (where ?) It doesn't look like we have enough so far.

解决方案

The biggest gotcha for me was that almost every standard operator is overloaded to distribute across the array.

Define a list and an array

>>> l = range(10)
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> import numpy
>>> a = numpy.array(l)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Multiplication duplicates the python list, but distributes over the numpy array

>>> l * 2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a * 2
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])

Addition and division are not defined on python lists

>>> l + 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "int") to list
>>> a + 2
array([ 2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
>>> l / 2.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'list' and 'float'
>>> a / 2.0
array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5])

Numpy overloads to treat lists like arrays sometimes

>>> a + a
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
>>> a + l
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])

这篇关于Numpy 与直接 python 不同的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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