我有一个名为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
您知道我应该使用哪个sklearn模型或其他库来更好地适应模型吗
您错过了基本步骤“规范化数据”
修复
正如@mujjiga所指出的,扩展是该过程的重要组成部分
我想提请大家注意另外两个关键点:
scklearn
API,帮助您标准化解决方案开发李>让我们从数据集开始:
然后我们导入一些感兴趣的
sklearn
API对象:首先,我们为目标值创建一个scaler函数:
注意,我们可以使用不同的scalers,或者构建一个custom transformation
我们使用我们选择的缩放器缩放目标:
我们还构建了一个pipeline,其中包含特征标准化器和所选模型(我们调整了参数以提高拟合度)。我们使用管道将其适配到您的数据集:
此时,我们可以预测值并将其转换回原始比例:
我们检查适合度分数:
我们还可以计算每个点的绝对和相对误差:
最终结果是:
从图形上看,它导致:
线性化
在上面的示例中,我们不能使用
poly
内核,而是必须使用rbf
内核。这是因为,如果我们打算用多项式拟合有理函数,我们最好在拟合之前首先用p = x/(x-b)
代换变换数据。在这种情况下,它将仅仅归结为执行线性回归。下面的示例显示了它的工作原理:缩放器和转换也可以组合成管道。我们定义了一条线性化和缩放问题的管道:
然后我们可以使用经典的OLS回归数据:
这提供了正确的结果:
第二种解决方案利用了已知的线性化,因此无需对模型进行参数化
相关问题 更多 >
编程相关推荐