我有一些类似于时间序列的矩阵,我想通过每一列和每一行的组合来运行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
我想知道是否有一个更有效的方法来完成这项任务,虽然循环通过堆叠矩阵中的每个元素似乎是不正确的方式。也许有办法把它矢量化?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐