Python 为什么我导入NumPy后sum的行为会有所不同
为什么导入NumPy后结果会不同Python 为什么我导入NumPy后sum的行为会有所不同,python,numpy,python-import,Python,Numpy,Python Import,为什么导入NumPy后结果会不同 print(sum(range(5),-1)) 答案是9 from numpy import * print(sum(range(5),-1)) 答案是10这是因为内置pythonsum函数被numpy.sum覆盖 当您计算内置pythonsum(范围(5),-1)时,它的计算结果类似于-1+sum([0,1,2,3,4]) 相反,numpy.sum假设-1是轴参数,表示输入数组的最后(也是唯一)轴。因此,您可以有效地获得np.sum(范围(5))应该小心地
print(sum(range(5),-1))
答案是9
from numpy import *
print(sum(range(5),-1))
答案是10这是因为内置python
sum
函数被numpy.sum
覆盖
当您计算内置pythonsum(范围(5),-1)
时,它的计算结果类似于-1+sum([0,1,2,3,4])
相反,
numpy.sum
假设-1
是轴参数,表示输入数组的最后(也是唯一)轴。因此,您可以有效地获得np.sum(范围(5))应该小心地重写内置函数
import*
可能很危险
内置的sum
和numpy
中定义的sum
有不同的用途,因此答案也不同
Help on built-in function sum in module __builtin__:
sum(...)
sum(iterable[, start]) -> value
Return the sum of an iterable or sequence of numbers (NOT strings)
plus the value of 'start' (which defaults to 0). When the sequence is
empty, return start.
(END)
>>> import numpy
>>> help(numpy.sum)
Help on function sum in module numpy.core.fromnumeric:
sum(a, axis=None, dtype=None, out=None, keepdims=<class numpy._globals._NoValue>)
Sum of array elements over a given axis.
Parameters
----------
a : array_like
Elements to sum.
axis : None or int or tuple of ints, optional
Axis or axes along which a sum is performed. The default,
axis=None, will sum all of the elements of the input array. If
axis is negative it counts from the last to the first axis.
.. versionadded:: 1.7.0
If axis is a tuple of ints, a sum is performed on all of the axes
specified in the tuple instead of a single axis or all the axes as
before.
dtype : dtype, optional
The type of the returned array and of the accumulator in which the
elements are summed. The dtype of `a` is used by default unless `a`
has an integer dtype of less precision than the default platform
integer. In that case, if `a` is signed then the platform integer
is used while if `a` is unsigned then an unsigned integer of the
same precision as the platform integer is used.
out : ndarray, optional
Alternative output array in which to place the result. It must have
the same shape as the expected output, but the type of the output
values will be cast if necessary.
keepdims : bool, optional
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the input array.
If the default value is passed, then `keepdims` will not be
passed through to the `sum` method of sub-classes of
`ndarray`, however any non-default value will be. If the
sub-classes `sum` method does not implement `keepdims` any
exceptions will be raised.
Returns
-------
sum_along_axis : ndarray
An array with the same shape as `a`, with the specified
axis removed. If `a` is a 0-d array, or if `axis` is None, a scalar
is returned. If an output array is specified, a reference to
`out` is returned.
See Also
.
.
.
>>>
模块中内置函数和的帮助
和(…)
总和(iterable[,start])->值
返回一个可数或数序列(不是字符串)的和
加上“开始”的值(默认为0)。当序列是
空,返回开始。
(完)
>>>进口numpy
>>>帮助(numpy.sum)
有关模块numpy.core.fromnumeric中函数和的帮助:
总和(a,axis=None,dtype=None,out=None,keepdims=)
给定轴上数组元素的总和。
参数
----------
a:你喜欢吗
要总结的元素。
轴:无或整数或整数元组,可选
执行求和的一个或多个轴。默认情况下,
axis=None,将对输入数组的所有元素求和。如果
轴为负,从最后一个轴到第一个轴计数。
.. versionadded::1.7.0
如果axis是int的元组,则对所有轴执行求和
在元组中指定,而不是将单个轴或所有轴指定为
之前
dtype:dtype,可选
返回数组的类型和
元素被汇总。默认情况下使用'a'的数据类型,除非'a'`
具有精度低于默认平台的整数数据类型
整数。在这种情况下,如果'a'是有符号的,那么平台整数
如果'a'是无符号的,则使用
使用与平台整数相同的精度。
out:ndarray,可选
用于放置结果的替代输出数组。一定有
与预期输出的形状相同,但输出的类型不同
如有必要,将强制转换值。
keepdims:bool,可选
如果设置为True,则缩小的轴将保留
在结果中显示为尺寸为1的尺寸标注。有了这个选择,,
结果将针对输入数组正确广播。
如果传递了默认值,则不会传递'keepdims'
传递给的子类的'sum'方法
`ndarray`,但任何非默认值都将被忽略。如果
子类'sum'方法不实现'keepdims'任何
将提出例外情况。
退换商品
-------
沿_轴求和:ndarray
与“a”形状相同的数组,具有指定的
移除轴。如果`a`是0-d数组,或者`axis`是无数组,则为标量
他回来了。如果指定了输出数组,则
`“out”返回。
另见
.
.
.
>>>
这是因为numpy.sum
中的第二个参数是轴
参数,如所示。由于输入是一个1d数组,sum(范围(5),-1)
沿最后(也是唯一)轴求和,因此相当于sum(范围(5))
,等于10
在标准库的sum()
中,第二个参数是,默认值为0
因此,您的代码相当于
-1+sum(范围(5))
,等于9。从numpy
导入所有内容后,sum
变为numpy.sum
numpy.sum
的第二个参数(即在您的案例中为-1)被理解为axis
,而不是执行sum
的另一个元素,您的问题的可能重复应该是“waynumpy.sum
返回的结果不同于内置的sum
?导入内容这一事实并不真正相关。这就是为什么说:“应该避免通配符导入(来自导入*
),因为它们使名称空间中存在的名称不清楚,混淆了读者和许多自动化工具。”在standard lib的总结中,第二个参数不被解释为输入列表的一部分,它是总和的起始值(默认为0)。看得很公平。我想强调的是,它被添加到最终的输出中,但是它可以放得更好。我想这对于非交换操作来说是有区别的。