numpy 转存为matlab,提高将numpy数组转换为MATLAB的性能

Calling MATLAB from Python is bound to give some performance reduction that I could avoid by rewriting (a lot of) code in Python. However, this isn't a realistic option for me, but it annoys me that a huge loss of efficiency lies in the simple conversion from a numpy array to a MATLAB double.

I'm talking about the following conversion from data1 to data1m, where

data1 = np.random.uniform(low = 0.0, high = 30000.0, size = (1000000,))

data1m = matlab.double(list(data1))

Here matlab.double comes from Mathworks own MATLAB package / engine. The second line of code takes 20 s on my system, which just seems like too much for a conversion that doesn't really do anything other than making the numbers 'edible' for MATLAB.

So basically I'm looking for a trick opposite to the one given here that works for converting MATLAB output back to Python.

解决方案

Passing numpy arrays efficiently

Take a look at the file mlarray_sequence.py in the folder PYTHONPATH\Lib\site-packages\matlab\_internal. There you will find the construction of the MATLAB array object. The performance problem comes from copying data with loops within the generic_flattening function.

To avoid this behavior we will edit the file a bit. This fix should work on complex and non-complex datatypes.

Make a backup of the original file in case something goes wrong.

Add import numpy as np to the other imports at the beginning of the file

In line 38 you should find:

init_dims = _get_size(initializer) # replace this with

try:

init_dims=initializer.shape

except:

init_dims = _get_size(initializer)

In line 48 you should find:

if is_complex:

complex_array = flat(self, initializer,

init_dims, typecode)

self._real = complex_array['real']

self._imag = complex_array['imag']

else:

self._data = flat(self, initializer, init_dims, typecode)

#Replace this with:

if is_complex:

try:

self._real = array.array(typecode,np.ravel(initializer, order='F').real)

self._imag = array.array(typecode,np.ravel(initializer, order='F').imag)

except:

complex_array = flat(self, initializer,init_dims, typecode)

self._real = complex_array['real']

self._imag = complex_array['imag']

else:

try:

self._data = array.array(typecode,np.ravel(initializer, order='F'))

except:

self._data = flat(self, initializer, init_dims, typecode)

Now you can pass a numpy array directly to the MATLAB array creation method.

data1 = np.random.uniform(low = 0.0, high = 30000.0, size = (1000000,))

#faster

data1m = matlab.double(data1)

#or slower method

data1m = matlab.double(data1.tolist())

data2 = np.random.uniform(low = 0.0, high = 30000.0, size = (1000000,)).astype(np.complex128)

#faster

data1m = matlab.double(data2,is_complex=True)

#or slower method

data1m = matlab.double(data2.tolist(),is_complex=True)

The performance in MATLAB array creation increases by a factor of 15 and the interface is easier to use now.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值