简体   繁体   English

Python 评估多项式回归

[英]Python Evaluating Polynomial Regression

I need a python function evaluates a polynomial at a set of input points.The function takes as input a vector of polynomial weights, and a vector of input points where x : a vector of input values, and w : a vector of polynomial weights (ordered so the jth element is the linear coefficient for the j th-order monomial, ie, x(j) ). I need a python function evaluates a polynomial at a set of input points.The function takes as input a vector of polynomial weights, and a vector of input points where x : a vector of input values, and w : a vector of polynomial weights (有序,因此第 j 个元素是第j个单项式的线性系数,即x(j) )。 The function outputs the predictions of the polynomial at each input point. function 在每个输入点输出多项式的预测。

Is there a python built in function for this? function 中是否有为此内置的 python ?

See numpy.poly1dnumpy.poly1d

Construct the polynomial x^2 + 2x + 3:构造多项式 x^2 + 2x + 3:

p = np.poly1d([1, 2, 3])
p(1) # prints 6

It seems you are describing numpy.polyval() :您似乎在描述numpy.polyval()

import numpy as np

# 1 * x**2 + 2 * x**1 + 3 * x**0
# computed at points: [0, 1, 2]
y = np.polyval([1, 2, 3], [0, 1, 2])
print(y)
# [ 3  6 11]

Note that the same could be achieved with np.poly1d() , which should be more efficient if you are computing values from the same polynomial multiple times:请注意,同样可以使用np.poly1d()来实现,如果您多次计算来自同一个多项式的值,这应该会更有效:

import numpy as np

# 1 * x**2 + 2 * x**1 + 3 * x**0
my_poly_func = np.poly1d([1, 2, 3])
# computed at points: [0, 1, 2]
y = my_poly_func([0, 1, 2])
print(y)
# [ 3  6 11]

If you want to use only Python built-ins, you could easily define a polyval() version yourself, eg:如果您只想使用 Python 内置插件,您可以自己轻松定义一个polyval()版本,例如:

def polyval(p, x):
   return [sum(p_i * x_i ** i for i, p_i in enumerate(p[::-1])) for x_i in x]


y = polyval([1, 2, 3], [0, 1, 2])
print(y)
# [3, 6, 11]

or, more efficiently:或者,更有效地:

def polyval_horner(p, x):
    y = [] 
    for x_i in x:
        y_i = 0
        for p_i in p:
            y_i = x_i * y_i + p_i
        y.append(y_i)
    return y


y = polyval_horner([1, 2, 3], [0, 1, 2])
print(y)
# [3, 6, 11]

but I would recommend using NumPy unless you have a good reason not to (for example, if your result would overflow with NumPy but not with pure Python).但我建议使用 NumPy 除非你有充分的理由不这样做(例如,如果你的结果会溢出 NumPy 而不是纯 Python)。

For fun you could try implementing the basic algorithms, iter being the worst, horner being better (I believe Karatsuba is the best).为了好玩,您可以尝试实现基本算法,iter 是最差的,horner 更好(我相信 Karatsuba 是最好的)。 Here are the first two:这是前两个:

def iterative(coefficients, x): #very inefficient
    i = len(coefficients)
    result = coefficients[i - 1]
    for z in range(i - 1, 0, -1):
        temp = x
        for y in range(z - 1):
            temp = temp * x
        result += temp * coefficients[i - z - 1]
    return (x, result)

def horner(coefficients, x): 
    result = 0
    for c in coefficients:
        result = x * result + c
    return (x, result)

I think numpy is faster in all cases as it goes into the C-level code.我认为 numpy 在所有情况下都更快,因为它进入了 C 级代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM