Python Forum

Full Version: Python 2.7 - looking for methods to take into account variaton in a data set for non
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello there. I've been running into some issues with multi-variate polynomial regression. I've got a data set Y, X1,X2,X3 obtained by measurements.
My output Y is supposed to depend on three variables, with P a polynom, as follow :


----------


Y = P(X1,X2,X3)


----------


The thing is that I've got several outputs for the same given set of X1,X2,X3. e.g : P(0,0,0) = 0.7 and P(0,0,0) = 0.75. I'm not sure if I should perform regular multi-variate regression, or if there are some python libraries that take into account those variations on Y. I'm currently using sklearn.
Hope my question is clear.
Thanks a lot.
def make_poly(coef):
    """
    Reeturn a polynomial function to evaluate.

    """
    def polynom(x):
        return sum(factor * x ** exp for factor, exp in factors[1:]) + factors[0][0]
    factors = [(factor, exponent) for exponent, factor in enumerate(coef)]
    return polynom

# Y, X1, X2, X3
values = [
    (6, 1,2,3),
    (7, 3,2,1),
    (8, 4,5,6),
]

for Y, X1, X2, X3 in values:
    poly = make_poly((X1, X2, X3))
    result = poly(Y)
    print(result)

# same with numpy
import numpy as np


for Y, X1, X2, X3 in values:
    poly = np.polynomial.Polynomial((X1, X2, X3))
    result = poly(Y)
    print(result)
And finally the test with P(0,0,0)
poly = make_poly((0,0,0))
for i in range(10):
    print(poly(i))

# and the check with numpy


poly = np.polynomial.Polynomial((0, 0, 0))
for i in range(10):
    print(poly(i))
The coefs are from low order -> high order.
If coefs are 1,2,3, then it's the polynom is: 3x² + 2x + 1
Another example: 1,2,3,4 -> 4x³ + 3x² + 2x + 1

If you want to have a one-shot-function (calling it -> result), the code must be changed.
Instead of returning a function, it should return the final result:

def calculate_poly(coef, X):
    """
    Return the result of polynomial.
    """
    def polynom(x):
        return sum(factor * x ** exp for factor, exp in factors[1:]) + factors[0][0]
    factors = [(factor, exponent) for exponent, factor in enumerate(coef)]
    return polynom(X)



coefs = [55,44,33,6,5,4]
Y = 8
result1 = make_poly(coefs)(Y)
result2 = calculate_poly(coefs, Y)
result3 = np.polynomial.Polynomial(coefs)(Y)
print(result1, result2, result3)
if result1 == result2 == result3:
    print("All results are equal")
Output:
157143 157143 157143.0 All results are equal
Calculating this polynomial manually:
Y = 8
# coef = [55,44,33,6,5,4]
print(+55 + 44 * Y + 33 * Y ** 2 + 6 * Y ** 3 + 5 * Y ** 4 + 4 * Y ** 5)
Output:
157143