2

这个问题在结构上类似于将行向量和列向量相乘以产生矩阵,然后对结果矩阵的行求和。

除了在行向量中每个元素都有两个值 A 和 B,在列向量中每个元素都有两个值 X 和 Y。并且该操作不是乘法,而是评估 A、B、X 和 Y 的函数。

下面的代码实现了这个目标。但是有没有办法在没有循环和诉诸 iterrows() 的情况下做到这一点?在实际问题中,行向量有数千个元素,列向量可以有数百万个元素。

from numpy import sin, cos, exp, nan 
from numpy.random import random

# Sample function that can operate on ndarrays
def myfun(a, b, x, y):
    return sin(a+x), exp(b+y) 

# sort of a "row vector"
df_ab = pd.DataFrame(random([2,6]), 
                     index=['A','B'],
                     columns=['AB%d'%i for i in range(6)])
# sort of a "column vector"
df_xy = pd.DataFrame(random([8,2]), 
                     columns=['X','Y'],
                     index=['XY%d'%i for i in range(8)])

# pre-add columns for the summarized results
df_xy['SUM_FUN0'] = nan
df_xy['SUM_FUN1'] = nan

# for each pair of values X,Y
for _, xy in df_xy.iterrows():
    # calculate myfun with each pair of values A,B
    funout0, funout1 = myfun(df_ab.loc['A'], df_ab.loc['B'], xy.X, xy.Y)
    # summarize and store the result
    xy['SUM_FUN0'] = funout0.sum()
    xy['SUM_FUN1'] = funout1.sum()    
4

1 回答 1

1

这样的事情怎么样?我没有测试过性能,但apply通常比iterrows.

import pandas as pd
from numpy import sin, cos, exp, nan, sum
from numpy.random import random
from numba import jit

# Sample function that can operate on ndarrays
@jit(nopython=True)
def myfun(a, b, x, y):
    return sum(sin(a+x)), sum(exp(b+y))

# sort of a "row vector"
df_ab = pd.DataFrame(random([2,6]), 
                     index=['A','B'],
                     columns=['AB%d'%i for i in range(6)])

# sort of a "column vector"
df_xy = pd.DataFrame(random([8,2]), 
                     columns=['X','Y'],
                     index=['XY%d'%i for i in range(8)])

A = df_ab.loc['A'].values
B = df_ab.loc['B'].values

df_xy['SUM_FUN0'], df_xy['SUM_FUN1'] = list(zip(*df_xy.apply(lambda x: myfun(A, B, x['X'], x['Y']), axis=1)))
于 2018-01-31T14:03:55.527 回答