用numpy

时间:2016-10-25 18:28:27

标签: python performance numpy matrix vectorization

我有一些图像处理软件可以很好地工作,但它很慢。我目前正在使用numpy来处理某些事情,但我不禁想到我可以利用更多这个库来获得更好的收益。

另外,请原谅我" bicubic"在这里不是正确的术语 - 我理解数学,但在词汇上很糟糕;)

我的工具以下列形式求解双三次方程的系数(a,b,...,j):

f(x,y) = ax^3 + by^3 + cx^2*y + dy^2*x + ex^2 + fy^2 + gxy + hx + iy + j

我这样做的方法是首先使用源数组和目标数据集中较小的点集生成求解器。这是通过第一个"矩阵行"对于形状为(n,10)的每个集合,然后使用最小二乘法求解。从那里,我们遍历源数据中的每个其他项,生成"矩阵行"并且针对"解决了#34;做了一个np.dot。系数矩阵。

import numpy as np

def matrix_row(x, y):
    row = [0] * 10
    row[0] = 1.0
    row[1] = x
    row[2] = y
    row[3] = x * y
    row[4] = y * y
    row[5] = x * x
    row[6] = (y * y) * x
    row[7] = (x * x) * y
    row[8] = y**3
    row[9] = x**3
    return row

def gen_matrix(items, num_items=24):
    mat = []
    for i in xrange(0, num_items):
        mat.append(_matrix_row(items[i, 0], items[i, 1]))
    return np.array(mat)

# Generate source data
n = 24
srcdata = np.random.rand(100, 2)
dstdata = np.random.rand(n, 2)

# Determine the coefficients for the solver for the first n components
# The resultant 'solved' matrix will be of shape (10, 2)
srcmat = gen_matrix(srcdata[:n, :], num_items=n)
solved, residuals, rank, s = np.linalg.lstsq(srcmat, dstdata)

# Apply the solution to all the src data
for item in srcdata:
    mrow = matrix_row(item[0], item[1])

    # Obviously, the print statements aren't important
    print 'orig   ', item
    print 'solved ', np.dot(mrow, solved)
    print '\n'

我有很多数据,因此Python中的for循环确实会破坏性能。是否有一种更加笨拙的方式来优化它?

1 个答案:

答案 0 :(得分:0)

这是一种矢量化方法 -

# Form an array of shape (10,100) that represents pairwise implementation
# of all those functions using all rows off srcdata
x,y = srcdata.T
a = np.ones((10,100))
a[1] = x
a[2] = y
a[3] = x * y
a[4] = y * y
a[5] = x * x
a[6] = (y * y) * x
a[7] = (x * x) * y
a[8] = y**3
a[9] = x**3

# Finally use dot product in one go to get solutions across all iterations
out = solved.T.dot(a).T