python矩阵最小二乘法解方程_的Python:对于套1的,-1的,和0的内存有效的矩阵创建由SciPy的最小二乘法进行优化...

I'm iterating through a list of strings and translating them into arrays of 1's, -1's, and 0's. For example - I may have the following list:

A,B,-C

A,-D

B,C,-D

Which will become a "biglist" equal to:

[

[1 1 -1 0],

[1 0 0 -1],

[0 1 1 -1]

]

At the moment, I'm simply looping through every line of strings, assigning values of 1 or -1 to the string if it is unique, and zeroing out the ones that do not exist (for example, D is not present in the first line, so it is 0). The silly way I'm doing the above is basically:

for line_of_strings in all_strings:

for the_string in line_of_strings:

entry[string_index] = (1 or -1)

biglist.append(entry)

Eventually, I have a nice set of lists on which I run:

scipy.optimize.nnls(biglist)

This works, but winds up taking up a truckload of memory and time. Is there a more efficient way to go about this? Using numpy or scipy arrays/matrices, perhaps?

解决方案

Using numpy arrays instead of lists seems to make quite a bit of difference timewise, at least in a trivial example:

$ python -mtimeit -s"from scipy.optimize import nnls; m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; b=[1, 2, 3]" "nnls(m, b)"

10000 loops, best of 3: 38.5 usec per loop

$ python -mtimeit -s"import numpy as np; from scipy.optimize import nnls; m = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); b=[1, 2, 3]" "nnls(m, b)"

100000 loops, best of 3: 20 usec per loop

$ python -mtimeit -s"import numpy as np; from scipy.optimize import nnls; m = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); b=np.array([1, 2, 3])" "nnls(m, b)"

100000 loops, best of 3: 11.4 usec per loop

I'd expect that numpy arrays would have smaller memory footprint as well. If your input is reasonably sparse, and if the performance is still not satisfactory, it might be worth investigating if nnls accepts sparse matrices.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值