我有一些类似于时间序列的矩阵,我想对每个列和行组合进行kendall tau回归。我可以使用以下代码进行此操作:
import numpy as np
import pandas as pd
import scipy as sp
import random
#make some random data
test = []
for i in range(50):
m = np.random.random((50, 50))
test.append(m)
#copy a matrix to repopulate with the tau value
coef = test[0].copy()
#copy to repopulate with a p-value
p = test[0].copy()
stacked = np.dstack(test)
#loop through each pixel and run the trend test, output two matrix, one of slope and one of p-value
for r in range(test[0].shape[0]):
for c in range(test[0].shape[1]):
pixel = list(stacked[r: r+1, c:c+1, :].flatten())
df = pd.DataFrame({"Values": pixel})
#reset the index to use as a independent variable
df = df.reset_index(drop = True)
#run the regression
tau, p_value = sp.stats.kendalltau(df.index, df.Values)
#update the copied matrices
coef[r: r+1, c:c+1] = tau
p[r: r+1, c:c+1] = p_value
我想知道是否可能有一种更有效的方法来完成此任务,尽管循环遍历堆叠矩阵中的每个元素似乎都是不正确的方法。也许甚至可以向量化吗?