s5s*_*s5s 4 python numpy scipy pandas
我想计算2x2矩阵的行列式,它是通过在Nx2矩阵上滚动一个大小为2的窗口来获取的.我只是使用行列式作为示例函数.一般来说,我想将一个函数应用于数据帧,这是通过为更大的数据帧加窗来获取的.
例如,这是一个2x2矩阵,我像这样计算行列式:
import pandas as pd
import numpy as np
d = pd.DataFrame({
"X": [1,2],
"Y": [3,4]
})
np.linalg.det(d)
Run Code Online (Sandbox Code Playgroud)
现在,我可以通过沿着以下数据帧的轴= 0滑动大小为2的窗口来形成4个2x2矩阵:
df = pd.DataFrame({
"A": [1,2,3,4,5],
"B": [6,7,8,9,10],
})
Run Code Online (Sandbox Code Playgroud)
看起来像:
A B
0 1 6
1 2 7
2 3 8
3 4 9
4 5 10
Run Code Online (Sandbox Code Playgroud)
所以我会得到[-5.,-5.,-5., - .].
据我所知,pandas.DataFrame.rolling和rolling.apply只能应用于1D向量,而不是数据帧?你会怎么做?
从数据框中提取numpy数组:
>>> array = df.values
>>> array
array([[ 1, 6],
[ 2, 7],
[ 3, 8],
[ 4, 9],
[ 5, 10]])
Run Code Online (Sandbox Code Playgroud)
使用numpy的as_strided函数创建滑动窗口视图:
>>> from numpy.lib.stride_tricks import as_strided
>>> rows, cols = array.shape
>>> row_stride, col_stride = array.strides
>>> windowed_array = as_strided(
... array,
... shape=(rows - 2 + 1, 2, cols),
... strides=(row_stride, row_stride, col_stride))
>>> windowed_array
array([[[ 1, 6],
[ 2, 7]],
[[ 2, 7],
[ 3, 8]],
[[ 3, 8],
[ 4, 9]],
[[ 4, 9],
[ 5, 10]]])
Run Code Online (Sandbox Code Playgroud)
现在将您的函数应用于结果数组:
>>> np.linalg.det(windowed_array)
array([-5., -5., -5., -5.])
Run Code Online (Sandbox Code Playgroud)
#You can replace np.linalg.det with other functions as you like.
#use apply to get 'A' and 'B' from current row and next row and feed them into the function.
df.apply(lambda x: np.linalg.det(df.loc[x.name:x.name+1, 'A':'B']) if x.name <(len(df)-1) else None,axis=1)
Out[157]:
0 -5.0
1 -5.0
2 -5.0
3 -5.0
4 NaN
dtype: float64
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2338 次 |
| 最近记录: |