-2

I have multiple vaiables and a list of experimental results, and Im wondering what will be the best way to create a polinomial fitting function.

ex :

| param1   | param2 | param3| res|
|--------------------------------|
|0.5       | 0.004  |  40   |6.75|
|0.55      | 0.684  |   0   |10.1|
|0.6       | 0.001  | -40   |13.2|
...

so far I used python to find the polinomial regsression like so:

import numpy as np
x = np.array([[0.5 ,0.004 ,40],[0.55, 0.684, 0],[0.6, 0.001, -40]...more parameters values ..]) # matrix describing all parameters
z = [6.75, 10.1, 13.2, ....more results....] # vector to describe all results according to parameters

degrees = [(i, j, k) for i in range(3) for j in range(3) for k in range(3)]  # list of monomials x**i * y**j to use

matrix = np.stack([np.prod(x**d, axis=1) for d in degrees], axis=-1)   # stack monomials like columns

coeff = np.linalg.lstsq(matrix, z)[0]    # lstsq returns some additional info we ignore

fit = np.dot(matrix, coeff)

as suggested by #user6655984 here: How can I use multiple dimensional polynomials with numpy.polynomial?

Now im trying to understand how to measure the accuracy? and to determine the max degree of each variable of the polynom.

Amir Zror
  • 1
  • 1
  • Welcome to SO! You are asking two questions here, so I would suggest you split them up for future users to find them easier. :-) – Jackolai Dec 23 '20 at 12:37
  • Have you had a look at [`numpy.polyfit`](https://numpy.org/doc/stable/reference/generated/numpy.polyfit.html)? If so, what were the specific problems you encountered? – Mr. T Dec 23 '20 at 12:45
  • Normally result accuracy is measured by cost functions.You can try different square error functions but I couldn't get your question completely. – Hakan Akgün Dec 23 '20 at 12:20
  • well, I gatherd some data from an experiment, and trying to find a polynomial fitting function to describe it. – Amir Zror Dec 23 '20 at 12:28
  • Oh got that. I misunderstood because it was saying parameters. As I know parameter is statical coefficients of a function, I think you meant variables in there. If you have 3 variable system basically you can use TensorFlow logistic regression systems or if you wanna do it by yourself.You can create a function with arbitrary coefficients and rearrange them with a function like a gradient descent. I hope it helps – Hakan Akgün Dec 23 '20 at 12:42
  • im not sure how to use polyfit to find a solution for more then one variable – Amir Zror Dec 23 '20 at 13:26

1 Answers1

0

The best way always depends on your criteria. If you want to minimize the squared error you could use the numpy function polyfit. You can choose the order of the polynomial (in your case 3) and it will return the solution together with the residuals.

You can also use least squares for this, but then you have to define the matrices yourself. "A" will then be your parameter matrix and b your experimental results.

Mr. T
  • 11,960
  • 10
  • 32
  • 54
joostblack
  • 2,465
  • 5
  • 14