如何在Python中正确应用赋值运算符? [英] How can I apply the assignment operator correctly in Python?

查看:86
本文介绍了如何在Python中正确应用赋值运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须对大型数组进行一些数学运算(例如加法,乘法). 为了防止出现任何'MemoryError',我正在根据此线程的答案进行计算

I have to do some math operations (e.g., add, multiply) on a large array. To prevent any 'MemoryError' , I am doing my computations as suggested on the answer from this thread.

但是,按照线程中的建议应用赋值操作时,我遇到了一些麻烦.我将使用小型3x3阵列演示我的问题.

However, I am running into some trouble while applying the assignment operations as per suggested in the thread. I will demonstrate my problem using a small 3x3 array.

我有以下输入数组K:

array([[ 0.        ,  0.51290339,  0.24675368],
       [ 0.51290339,  0.        ,  0.29440921],
       [ 0.24675368,  0.29440921,  0.        ]])

我想将以下计算应用于输入数组K:

I want to apply the following computation to the input array K:

output = K* (1.5 - 0.5 * K* K)

我使用上面的公式来计算所需的输出,如下所示:

I apply the above equation to compute the desired output as follows in Python:

K*= (1.5+np.dot(np.dot(-0.5,K),K))

但是,输出答案不正确.

However, the output answer is not correct.

我想要的答案应该是:

0.0000000 0.7018904 0.3626184
0.7018904 0.0000000 0.4288546
0.3626184 0.4288546 0.0000000

欢迎任何帮助.

推荐答案

之所以会出现差异,是因为dot计算点积,而*计算逐元素积.尝试使用

The difference arises because dot computes the dot product whereas * computes the element-wise product. Try using

K *= 1.5 - 0.5 * K * K

相反.

添加

不幸的是,这还不能解决内存问题.我建议使用cython来计算所需的函数,而无需分配额外的内存.

Unfortunately, that does not yet solve the memory problems. I would recommend using cython to compute the desired function without allocating extra memory.

# This cython function must be compiled
def evaluate_function_inplace(double[:] values):
    cdef int i
    for i in range(values.shape[0]):
        values[i] *= 1.5 - 0.5 * values[i] * values[i]

随后,您可以像这样使用该功能.

Subsequently, you can use the function like so.

K = ...
evaluate_function_inplace(K.ravel())

K.ravel()调用将使数组变平,但不会分配新的内存.

The K.ravel() call will flatten the array but will not allocate new memory.

当然,您也可以在不依靠cython的情况下使用上述方法,但是在python中对如此大数组的元素进行迭代的性能开销非常大.

Of course, you can also use the above approach without resorting to cython but the performance overhead of iterating over the elements of such a large array in python are very large.

这篇关于如何在Python中正确应用赋值运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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