Pandas数据帧上的滚动PCA

2024-06-02 05:06:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道是否有人知道如何在pandas数据帧上实现滚动/移动窗口PCA。我到处看看,发现了R和MATLAB中的实现,但没有Python。任何帮助都将不胜感激!在

这不是重复的-移动窗口PCA与整个数据帧上的PCA不同。请看熊猫.DataFrame.rolling()如果你不明白区别


Tags: 数据dataframepandasmatlabrollingpca区别
1条回答
网友
1楼 · 发布于 2024-06-02 05:06:15

不幸的是,pandas.DataFrame.rolling()似乎在滚动之前使df变平,因此不能像人们期望的那样在df的行上滚动并将行的窗口传递给PCA。在

下面是一个基于滚动索引而不是行的解决方法。它可能不是很优雅,但它很管用:

# Generate some data (1000 time points, 10 features)
data = np.random.random(size=(1000,10))
df = pd.DataFrame(data)

# Set the window size
window = 100

# Initialize an empty df of appropriate size for the output
df_pca = pd.DataFrame( np.zeros((data.shape[0] - window + 1, data.shape[1])) )

# Define PCA fit-transform function
# Note: Instead of attempting to return the result, 
#       it is written into the previously created output array.
def rolling_pca(window_data):
    pca = PCA()
    transf = pca.fit_transform(df.iloc[window_data])
    df_pca.iloc[int(window_data[0])] = transf[0,:]
    return True

# Create a df containing row indices for the workaround
df_idx = pd.DataFrame(np.arange(df.shape[0]))

# Use `rolling` to apply the PCA function
_ = df_idx.rolling(window).apply(rolling_pca)

# The results are now contained here:
print df_pca

快速检查会发现,这产生的值与手动切片相应窗口并在其上运行PCA计算的控制值相同。在

相关问题 更多 >