如何在Python中预测y=1/x值?

2024-06-16 10:37:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为df的数据帧:

import pandas as pd

df = pd.DataFrame({'p': [15-x for x in range(14)]
                  , 'x': [x for x in range(14)]})

df['y'] = 1000 * (10 / df['p'])

x仅用于打印目的。 我试图根据p值预测y值。我正在使用SVR from sklearn

from sklearn.svm import SVR

nlm = SVR(kernel='poly').fit(df[['p']], df['y'])
df['nml'] = nlm.predict(df[['p']])

我已经尝试了所有的内核,但仍然不够正确

     p   x            y          nml
0   15   0   666.666667   524.669572
1   14   1   714.285714   713.042459
2   13   2   769.230769   876.338765
3   12   3   833.333333  1016.349674

grey: p, blue: y, orange: nlm

您知道我应该使用哪个sklearn模型或其他库来更好地适应模型吗


Tags: 数据infrom模型importpandasdffor
2条回答

您错过了基本步骤“规范化数据”

修复

df = pd.DataFrame({'p': [15-x for x in range(14)]
                  , 'x': [x for x in range(14)]})

df['y'] = 1000 * (10 / df['p'])

# Normalize the data (x - mean(x))/std(x)
s_p = np.std(df['p'])
m_p = np.mean(df['p'])

s_y = np.std(df['y'])
m_y = np.mean(df['y'])
                        
df['p_'] = (df['p'] - s_p)/m_p
df['y_'] = (df['y'] - s_y)/m_y

# Fit and make prediction
nlm = SVR(kernel='rbf').fit(df[['p_']], df['y_'])
df['nml'] = nlm.predict(df[['p_']])

# Plot
plt.plot(df['p_'], df['y_'], 'r')
plt.plot(df['p_'], df['nml'], 'g')
plt.show()

# Rescale back and plot
plt.plot(df['p_']*s_p+m_p, df['y_']*s_y+m_y, 'r')
plt.plot(df['p_']*s_p+m_p, df['nml']*s_y+m_y, 'g')
plt.show()

enter image description here

enter image description here

正如@mujjiga所指出的,扩展是该过程的重要组成部分

我想提请大家注意另外两个关键点:

  • 模型选择决定您解决某类问题的能力
  • 新的scklearnAPI,帮助您标准化解决方案开发

让我们从数据集开始:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x = np.arange(14)
df = pd.DataFrame({'x': x, 'p': 15-x})
df['y'] = 1e4/df['p']

然后我们导入一些感兴趣的sklearnAPI对象:

from sklearn.svm import SVR
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler, RobustScaler, FunctionTransformer

首先,我们为目标值创建一个scaler函数:

ysc = StandardScaler()

注意,我们可以使用不同的scalers,或者构建一个custom transformation

# Scaler robust against outliers:
ysc = RobustScaler()

# Logarithmic Transformation:
ysc = FunctionTransformer(func=np.log, inverse_func=np.exp, check_inverse=True)

我们使用我们选择的缩放器缩放目标:

ysc.fit(df[['y']])
df['yn'] = ysc.transform(df[['y']])

我们还构建了一个pipeline,其中包含特征标准化器和所选模型(我们调整了参数以提高拟合度)。我们使用管道将其适配到您的数据集:

reg = make_pipeline(StandardScaler(), SVR(kernel='rbf', C=1e3, epsilon=1e-3))
reg.fit(df[['p']], df['yn'])

此时,我们可以预测值并将其转换回原始比例:

df['ynhat'] = reg.predict(df[['p']])
df['yhat'] = ysc.inverse_transform(df[['ynhat']])

我们检查适合度分数:

reg.score(df[['p']], df['yn']) # 0.9999646718755011

我们还可以计算每个点的绝对和相对误差:

df['yaerr'] = df['yhat'] - df['y']
df['yrerr'] = df['yaerr']/df['y']

最终结果是:

     x   p            y        yn     ynhat         yhat      yaerr     yrerr
0    0  15   666.666667 -0.834823 -0.833633   668.077018   1.410352  0.002116
1    1  14   714.285714 -0.794636 -0.795247   713.562403  -0.723312 -0.001013
2    2  13   769.230769 -0.748267 -0.749627   767.619013  -1.611756 -0.002095
3    3  12   833.333333 -0.694169 -0.693498   834.128425   0.795091  0.000954
4    4  11   909.090909 -0.630235 -0.629048   910.497550   1.406641  0.001547
5    5  10  1000.000000 -0.553514 -0.555029   998.204445  -1.795555 -0.001796
6    6   9  1111.111111 -0.459744 -0.460002  1110.805275  -0.305836 -0.000275
7    7   8  1250.000000 -0.342532 -0.341099  1251.697707   1.697707  0.001358
8    8   7  1428.571429 -0.191830 -0.193295  1426.835676  -1.735753 -0.001215
9    9   6  1666.666667  0.009105  0.010458  1668.269984   1.603317  0.000962
10  10   5  2000.000000  0.290414  0.291060  2000.764717   0.764717  0.000382
11  11   4  2500.000000  0.712379  0.690511  2474.088446 -25.911554 -0.010365
12  12   3  3333.333333  1.415652  1.416874  3334.780642   1.447309  0.000434
13  13   2  5000.000000  2.822199  2.821420  4999.076799  -0.923201 -0.000185

从图形上看,它导致:

fig, axe = plt.subplots()
axe.plot(df['p'], df['y'], label='$y(p)$')
axe.plot(df['p'], df['yhat'], 'o', label='$\hat{y}(p)$')
axe.set_title(r"SVR Fit for $y(x) = \frac{k}{x-a}$")
axe.set_xlabel('$p = x-a$')
axe.set_ylabel('$y, \hat{y}$')
axe.legend()
axe.grid()

Fit Result

线性化

在上面的示例中,我们不能使用poly内核,而是必须使用rbf内核。这是因为,如果我们打算用多项式拟合有理函数,我们最好在拟合之前首先用p = x/(x-b)代换变换数据。在这种情况下,它将仅仅归结为执行线性回归。下面的示例显示了它的工作原理:

缩放器和转换也可以组合成管道。我们定义了一条线性化和缩放问题的管道:

# Rational Fraction Substitution with consecutive Standardization
ysc = make_pipeline(
          FunctionTransformer(func=lambda x: x/(x+1),
                              inverse_func=lambda x: x/(1-x),
                              check_inverse=True),
          StandardScaler()
)

然后我们可以使用经典的OLS回归数据:

reg = make_pipeline(StandardScaler(), LinearRegression())
reg.fit(df[['p']], df['yn'])

这提供了正确的结果:

reg.score(df[['p']], df['yn']) # 0.9999998722172933

第二种解决方案利用了已知的线性化,因此无需对模型进行参数化

相关问题 更多 >