我有一个小数据框,其中包含几类插值数据,其值已在 0 和 1 之间标准化。我正在尝试跟踪平滑曲线,如 EXCEL 中所示,但阅读论坛中的其他问题,这需要进一步插值,不知道这样做是否正确?如何获得与 EXCEL 中相同的图表?
xl = pd.ExcelFile('C:/.../test.xlsx')
df1 = xl.parse(0, skipfooter= nrows-(10),index_col='Classes',header=0).dropna(axis=1, how='all')
df1
A B C D E F G H I L
Classes
class 1 0.167205 0.117160 0.293759 0.114839 0.010403 0.009577 0.013579 0.010279 0.295320 0.496107
class 2 1.000000 1.000000 1.000000 1.000000 0.393559 0.301260 0.281466 0.230577 0.198755 0.416739
class 3 0.008582 0.054056 0.007861 0.072378 0.703360 0.817691 0.803952 0.803575 0.000000 0.000000
class 4 0.135236 0.087106 0.255319 0.077556 0.000000 0.000000 0.000000 0.000000 0.252681 0.443720
class 5 0.041120 0.140389 0.033002 0.279836 1.000000 1.000000 1.000000 1.000000 0.615051 0.248261
class 6 0.000000 0.000000 0.000000 0.000000 0.667189 0.796265 0.782126 0.768850 0.078520 0.035903
class 7 0.654654 0.644665 0.740677 0.784618 0.508319 0.427955 0.426612 0.401502 1.000000 1.000000
class 8 0.121820 0.073066 0.268800 0.099552 0.000940 0.003957 0.010434 0.012108 0.352075 0.529671
class 9 0.139109 0.118368 0.215398 0.127073 0.262349 0.270412 0.263293 0.257149 0.266421 0.347188
df1.T.plot(figsize=(8,5))
Excel
最佳答案
您需要对数据进行插值,即添加一些辅助点。
df = df1.T.reset_index(drop=True) # transpose dataframe
df = df.reindex(df.index.union(pd.np.linspace(df.index.min(),df.index.max(), df.index.shape[0]*10))).reset_index(drop=True) # insert 10 "empty" points between existing ones
df = df.interpolate('pchip', order=2) # fill the gaps with values
df.plot() # draw new Dataframe
插值函数的选择取决于您。还有很多其他功能,但 IMO pchip
提供了平滑度和精度之间的最佳权衡
关于pandas - 如何平滑 pandas 数据框中的线条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61338944/