python - 我正在尝试使用 numpy.interp 从 pandas 数据帧插入值,但它不断返回错误的插值

标签 python pandas numpy

import pandas as pd
import numpy as np

# defining a function for interpolation
def interpolate(x, df, xcol, ycol):
    return np.interp([x], df[xcol], df[ycol])

# function call
print(interpolate(0.4, freq_data, 'Percent_cum_freq', 'cum_OGIP'))

尝试更直接的方法:

print(np.interp(0.4, freq_data['Percent_cum_freq'], freq_data['cum_OGIP']))

输出:

from function [2.37197912e+10]
from direct 23719791158.266743

对于我传递的 x 的任何值:0.4、0.6 和 0.9,它给出相同的结果,即 2.37197912e+10 freq_data数据框

Percent_cum_freq      cum_OGIP
0              0.999  4.455539e+07
1              0.981  1.371507e+08
2              0.913  2.777860e+08
3              0.824  4.664612e+08
4              0.720  7.031764e+08
5              0.615  9.879315e+08
6              0.547  1.320727e+09
7              0.464  1.701562e+09
8              0.396  2.130436e+09
9              0.329  2.607351e+09
10             0.285  3.132306e+09
11             0.245  3.705301e+09
12             0.199  4.326336e+09
13             0.167  4.995410e+09
14             0.136  5.712525e+09
15             0.115  6.477680e+09
16             0.085  7.290874e+09
17             0.072  8.152108e+09
18             0.056  9.061383e+09
19             0.042  1.001870e+10
20             0.034  1.102405e+10
21             0.027  1.207745e+10
22             0.022  1.317888e+10
23             0.015  1.432835e+10
24             0.013  1.552587e+10
25             0.010  1.677142e+10
26             0.007  1.806502e+10
27             0.002  1.940665e+10
28             0.002  2.079632e+10
29             0.002  2.223404e+10
30             0.001  2.371979e+10

出了什么问题?我该如何解决这个问题?

最佳答案

当我实现您提供的代码时,我对结果也感到惊讶。经过一番搜索后 documentation for np.interp ,发现x 坐标必须始终递增

np.interp(x,list_of_x_coordinates,list_of_y_coordinates)

其中 x 是您想要 y 的值。

list_of_x_coordinatesdf[xcol] -> 该值必须始终递增但是随着您的数据帧不断减少,它永远不会给出正确的结果。

list_of_y_coordinatesdf[ycol] -> 这必须与 df[xcol] 具有相同的维度和顺序

我的复制代码:

import numpy as np
list_1=np.interp([0.1,0.5,0.8],[0.999,0.547,0.199,0.056,0.013,0.001],[4.455539e+07,1.320727e+09,4.326336e+09,9.061383e+09,1.552587e+10, 2.371979e+10])

list_2=np.interp([0.1,0.5,0.8],[0.001,0.013,0.056,0.199,0.547,0.999],[2.371979e+10,1.552587e+10,9.061383e+09,4.326336e+09,1.320727e+09,4.455539e+07])
print("In decreasing order -> As in your case",list_1)
print("In increasing order of x xoordinates",list_2)

输出:

In decreasing order -> As in your case [2.371979e+10 2.371979e+10 2.371979e+10]
In increasing order of x xoordinates [7.60444546e+09 1.72665695e+09 6.06409705e+08]

正如您现在可以理解的,您必须对 df[x_col] 进行排序并相应地传递 df[y_col] ​

关于python - 我正在尝试使用 numpy.interp 从 pandas 数据帧插入值,但它不断返回错误的插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58975393/

相关文章:

python - 有没有测试文件部分写入失败的好方法?

python - 如果每个值都相等,则删除 pandas 数据框行

php - python 和 php 中的字符串编码结果不同

python - Pandas 循环遍历 Excel 工作表并附加到 df

python - Pandas如何将列按日期时间移动到不在索引中的日期时间

python - 访问 Numpy 列表中的值范围

python - 使用 Matplotlib 绘制数据子集

python - 读取另一个进程的命令行参数(Win32 C 代码)

python - Value Pandas Dataframe 中的列内的项目计数,其中包含字符串列表作为值

python - Tensorflow Keras 嵌入层错误 : Layer weight shape not compatible