我如何根据这些数据创建函数?

How can I create a function from this data?

我有一个 table:

形式的数据集
Score   Percentile
 381         1
 382         2
 383         2
      ...
 569        98
 570        99

完整的 table 是 here as a Google spreadsheet

目前,我正在计算一个分数,然后对该数据集 (table) 进行查找以找到相应的百分位排名。

是否可以创建一个函数来使用公式计算给定分数的相应百分等级,而不是在 table 中查找?

您的问题很含糊,但似乎您所做的任何计算最终都会得到一个 381-570 范围内的数字,这是正确的吗?你有一个给出这个数字的多行计算?我猜你在代码中的很多地方都重复了这一点,这就是你想要对其进行程序化的原因?

对于任何计算,您都可以将其包装在一个函数中。例如:

answer = variable_1 * variable_2 + variable_3

可以写成:

def calculate(v1, v2, v3):
    ''' calculate the result from the inputs
    '''
    return v1 * v2 + v3

answer = calculate(variable_1, variable_2, variable_3)

如果您想要一个明确的答案,那么只需post您的计算,我可以为您将其变成一个函数

如果没有提供有关该数据背后的过程的信息,则不可能重新创建生成给定 table 数据的函数。

话虽这么说,我们可以做一些推测。

由于它是一个“百分位”函数,它可能表示某种概率分布的累积值。一种非常常见的概率分布是正态分布,其“累积”对应物(即积分)是所谓的“误差函数”(“erf”)。

事实上,您的表格数据看起来很像平均值为 473.09 的变量的误差函数:

你的数据集:橙色;拟合误差函数 (erf): blue

然而,协议并不完美,这可能是由于三个原因:

  1. 我用来生成误差函数参数的拟合过程没有使用正确的约束(因为我不知道我在建模什么!)
  2. 您的数据集不代表精确 正态分布,而是真实世界的数据,其基础分布是正态分布。您的示例数据中偏离模型的特征将被完全忽略。
  3. 基础分布根本不是正态分布,它的积分只是偶然看起来像误差函数。

我真的没法说!

如果你想使用这个函数,这是它的定义:

import numpy as np
from scipy.special import erf
def fitted_erf(x):
    c = 473.09090474
    w =  37.04826334
    return 50+50*erf((x-c)/(w*np.sqrt(2)))

测试:

In [2]: fitted_erf(439) # 17 from the table
Out[2]: 17.874052406601457

In [3]: fitted_erf(457) # 34 from the table
Out[3]: 33.20270318344252

In [4]: fitted_erf(474) # 51 from the table
Out[4]: 50.97883169390196

In [5]: fitted_erf(502) # 79 from the table
Out[5]: 78.23955071273468

但是我强烈建议您检查在不了解您的数据源的情况下制作的拟合函数是否适合您的任务。


P.S.

如果您有兴趣,这是用于获取参数的代码:

import numpy as np
from scipy.special import erf
from scipy.optimize import curve_fit

tab=np.genfromtxt('table.csv', delimiter=',', skip_header=1)
# using a 'table.csv' file generated by Google Spreadsheets
x = tab[:,0]
y = tab[:,1]

def parametric_erf(x, c, w):
    return 50+50*erf((x-c)/(w*np.sqrt(2)))

pars, j = curve_fit(parametric_erf, x, y, p0=[475,10])

print(pars)
# outputs [  473.09090474,   37.04826334]

并生成情节

import matplotlib.pyplot as plt

plt.plot(x,parametric_erf(x,*pars))
plt.plot(x,y)
plt.show()