Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Numpy修改阵列是否到位?_Python_Arrays_Numpy_In Place - Fatal编程技术网

Python Numpy修改阵列是否到位?

Python Numpy修改阵列是否到位?,python,arrays,numpy,in-place,Python,Arrays,Numpy,In Place,我有以下代码,它试图规范化m x n数组的值(它将被用作神经网络的输入,其中m是训练示例的数量,n是特征的数量) 但是,当我在脚本运行后检查解释器中的数组时,我看到值没有规范化;也就是说,它们仍然具有原始值。我想这是因为函数中数组变量的赋值只能在函数中看到 如何在适当的位置执行此规范化?或者我必须从normalize函数返回一个新数组吗 import numpy def normalize(array, imin = -1, imax = 1): """I = Imin + (Imax

我有以下代码,它试图规范化
m x n
数组的值(它将被用作神经网络的输入,其中
m
是训练示例的数量,
n
是特征的数量)

但是,当我在脚本运行后检查解释器中的数组时,我看到值没有规范化;也就是说,它们仍然具有原始值。我想这是因为函数中
数组
变量的赋值只能在函数中看到

如何在适当的位置执行此规范化?或者我必须从normalize函数返回一个新数组吗

import numpy

def normalize(array, imin = -1, imax = 1):
    """I = Imin + (Imax-Imin)*(D-Dmin)/(Dmax-Dmin)"""

    dmin = array.min()
    dmax = array.max()

    array = imin + (imax - imin)*(array - dmin)/(dmax - dmin)
    print array[0]


def main():

    array = numpy.loadtxt('test.csv', delimiter=',', skiprows=1)
    for column in array.T:
        normalize(column)

    return array

if __name__ == "__main__":
    a = main()

如果要将数学运算应用于就地numpy数组,只需使用标准就地运算符
+=
-=
/=
等即可。例如:

>>> def foo(a):
...     a += 10
... 
>>> a = numpy.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> foo(a)
>>> a
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
这些操作的就地版本启动速度稍快,特别是对于较大的阵列:

>>> def normalize_inplace(array, imin=-1, imax=1):
...         dmin = array.min()
...         dmax = array.max()
...         array -= dmin
...         array *= imax - imin
...         array /= dmax - dmin
...         array += imin
...     
>>> def normalize_copy(array, imin=-1, imax=1):
...         dmin = array.min()
...         dmax = array.max()
...         return imin + (imax - imin) * (array - dmin) / (dmax - dmin)
... 
>>> a = numpy.arange(10000, dtype='f')
>>> %timeit normalize_inplace(a)
10000 loops, best of 3: 144 us per loop
>>> %timeit normalize_copy(a)
10000 loops, best of 3: 146 us per loop
>>> a = numpy.arange(1000000, dtype='f')
>>> %timeit normalize_inplace(a)
100 loops, best of 3: 12.8 ms per loop
>>> %timeit normalize_copy(a)
100 loops, best of 3: 16.4 ms per loop

在使用numpy时,有一种很好的方法可以进行就地规范化
np.矢量化
lambda
函数结合应用于数组时非常有用。请参见下面的示例:

import numpy as np

def normalizeMe(value,vmin,vmax):

    vnorm = float(value-vmin)/float(vmax-vmin)

    return vnorm

imin = 0
imax = 10
feature = np.random.randint(10, size=10)

# Vectorize your function (only need to do it once)
temp = np.vectorize(lambda val: normalizeMe(val,imin,imax)) 
normfeature = temp(np.asarray(feature))

print feature
print normfeature
可以将性能与生成器表达式进行比较,但是可能有许多其他方法可以做到这一点

%%timeit
temp = np.vectorize(lambda val: normalizeMe(val,imin,imax)) 
normfeature1 = temp(np.asarray(feature))
10000 loops, best of 3: 25.1 µs per loop


%%timeit
normfeature2 = [i for i in (normalizeMe(val,imin,imax) for val in feature)]
100000 loops, best of 3: 9.69 µs per loop

%%timeit
normalize(np.asarray(feature))
100000 loops, best of 3: 12.7 µs per loop

因此,矢量化肯定不是最快的,但在性能不那么重要的情况下,矢量化可以很方便。

这是一个技巧,它比这里的其他有用答案更为通用:

def normalize(array, imin = -1, imax = 1):
    """I = Imin + (Imax-Imin)*(D-Dmin)/(Dmax-Dmin)"""

    dmin = array.min()
    dmax = array.max()

    array[...] = imin + (imax - imin)*(array - dmin)/(dmax - dmin)
这里,我们将值分配给视图
数组[…]
,而不是将这些值分配给函数范围内的某个新局部变量

x = np.arange(5, dtype='float')
print x
normalize(x)
print x

>>> [0. 1. 2. 3. 4.]
>>> [-1.  -0.5  0.   0.5  1. ]
编辑:

它比较慢;它分配一个新数组。但是,如果您正在做一些更复杂的事情,而内置的就地操作很麻烦或不够,那么它可能很有价值

def normalize2(array, imin=-1, imax=1):
    dmin = array.min()
    dmax = array.max()

    array -= dmin;
    array *= (imax - imin)
    array /= (dmax-dmin)
    array += imin

A = np.random.randn(200**3).reshape([200] * 3)
%timeit -n5 -r5 normalize(A)
%timeit -n5 -r5 normalize2(A)

>> 47.6 ms ± 678 µs per loop (mean ± std. dev. of 5 runs, 5 loops each)
>> 26.1 ms ± 866 µs per loop (mean ± std. dev. of 5 runs, 5 loops each)

性能方面,这样做有什么问题吗?它与创建一个新数组相比如何?我的意思是,为此,您必须进行基准测试。它取决于数组的大小。对于小问题,我当然会创建新数组。什么是
%timeit
?这看起来很有趣,是内置的吗?我在这里使用的版本只是内置的。但是它是基于模块中的
timeit
函数。有趣的是,我一直把它和ironpython联系在一起,但现在我错了。@User,是的,它有时非常有用。我通常只使用常规的python shell,但对于计时,
%timeit
“magic command”非常方便,因为它可以为您处理所有棘手的设置。它可以完成任务,但非常慢,因为根据文档,它像for循环一样实现。这类事情有什么基准吗?你希望矢量化可以帮助它更快地运行。那么它的时间安排是什么呢?
def normalize2(array, imin=-1, imax=1):
    dmin = array.min()
    dmax = array.max()

    array -= dmin;
    array *= (imax - imin)
    array /= (dmax-dmin)
    array += imin

A = np.random.randn(200**3).reshape([200] * 3)
%timeit -n5 -r5 normalize(A)
%timeit -n5 -r5 normalize2(A)

>> 47.6 ms ± 678 µs per loop (mean ± std. dev. of 5 runs, 5 loops each)
>> 26.1 ms ± 866 µs per loop (mean ± std. dev. of 5 runs, 5 loops each)