我希望能快速找到一种方法来使 pandas DataFrame 在 x 轴上单调。
我的当前解决方案如下:
def make_monotonic(df, cols=None):
"""make df monotonic"""
if not cols:
cols = df.columns
mycol = "_maximum"
dfm = df.copy()
for col in cols:
dfm[mycol] = np.maximum.accumulate(dfm[col])
dfm.drop_duplicates([mycol], keep="first", inplace=True)
del dfm[mycol]
return dfm
n=21
np.random.seed(21)
x = np.linspace(0,np.pi,n)
dx = .5 * (np.random.random(n)-.5)
df = pd.DataFrame.from_dict({"x0":x, "x":x+dx, "y":np.sin(x)})
dfm = make_monotonic(df, cols=["x"])
我想生成一个函数y = f(x)
x x0 y
0 -0.676913 0.000000 0.000000e+00
1 -0.002176 0.314159 3.090170e-01
2 0.959768 0.628319 5.877853e-01
3 0.224902 0.942478 8.090170e-01
4 0.815521 1.256637 9.510565e-01
5 0.896956 1.570796 1.000000e+00
6 1.588363 1.884956 9.510565e-01
7 2.444980 2.199115 8.090170e-01
8 2.225446 2.513274 5.877853e-01
9 2.952820 2.827433 3.090170e-01
10 2.495949 3.141593 1.224647e-16
为了
x x0 y
0 -0.676913 0.000000 0.000000
1 -0.002176 0.314159 0.309017
2 0.959768 0.628319 0.587785
6 1.588363 1.884956 0.951057
7 2.444980 2.199115 0.809017
9 2.952820 2.827433 0.309017
x
值,例如在np.interp
中使用该列。 - steller