E
E
Eugene Lerner2021-10-25 11:22:51
Mathematics
Eugene Lerner, 2021-10-25 11:22:51

What is the approximation formula for multidimensional polynomials?

Hello! there are M points in N dimensional space. we need a formula for approximating them by a polynomial of degree K using the least squares method.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
antares4045, 2021-10-25
@ehevnlem

solving the problem head-on is very memory-intensive: it’s better to look at how the regression problem is optimized by mashine learning libraries. but in the forehead
on numpy it looks like this:

XtX = X.T * X
result = inv(XtX).dot(X.T).dot(Y)

X = matrix MxN*K where rows correspond to points and columns correspond to coordinates raised to the appropriate powers
Y = column vector of M zeros
Matrix.T - matrix transpose
inv(Matrix) -- inv function computes matrix inverse
MatrixA.dot(MatrixB) -- computes product of matrices
At the output, you get the result vector -- the coefficients of the polynomial corresponding to the columns of the matrix X (Accordingly, if you want the free coefficient to be, the column of units in the matrix X should be
) real data with millions of measurements will just burn you what you think there.
I apologize for the notation - I forgot the classical mathematical one, and to be honest, I don’t really want to remember it.
UPD:

After some gatherings, I designed everything in this design:
import numpy as np

K = 2 #степень целевой плоскости
points = np.array([[a, 5 + 2*a + 6 * a**2] for a in range(0, 60)]) # точки, по которым строим



class Symbolic:
  def __init__(self, letter=None):
    self.letters = dict() if letter is None else {letter : 1}
  def addLetter(self, letter, power=1):
    if letter not in self.letters:
      self.letters[letter] = 0
    self.letters[letter] += power
  def __pow__(self, pow):
      res = Symbolic()
      for letter in self.letters:
        res.letters[letter] = self.letters[letter] * pow
      return res
  def __mul__(self, right):
    res = Symbolic()
    for letter in self.letters:
      res.addLetter(letter, self.letters[letter])
    for letter in right.letters:
      res.addLetter(letter, right.letters[letter])
    return res
  def __repr__(self):
    return '*'.join(map(lambda letter: f'{letter}**{self.letters[letter]}', self.letters.keys()))

def pointsToPower(points, power):
  result = points ** power

  if points.shape[1] > 1 and power > 1:
    for startpower in range(power-1, 0, -1):
      for index in range(points.shape[1] - 1) :
        base = np.array(points[:,index]) ** startpower
        submatrix = pointsToPower(
            points[:, index+1:],
            power - startpower
        )
        result = np.hstack([result, np.array([submatrix[lineIndex, :] * base[lineIndex] for lineIndex in range(base.shape[0])])])
  return result

def pointsToX(points, K=1):
  return np.hstack([
        #  np.array([[1]] * points.shape[0]), 
        *[pointsToPower(points, power=i+1) for i in range(K)]
    ])


def krammer(X, sigma=1):
  XtX = X.T.dot(X)
  baseDet = np.linalg.det(XtX)
  if baseDet == 0:
      return [[None]]
  Y = np.array([[sigma] * X.shape[0]]).T
  return np.linalg.inv(XtX).dot(X.T).dot(Y)


X = pointsToX(points,K)
values = krammer(X).T[0]
if values[0] is None:
    print('''Обратной матрицы не существует, и решить систему матричным методом невозможно. 
В этом случае система решается методом исключения неизвестных (методом Гаусса). 
Но его реализовывать мне влом''')

symbols = pointsToX(np.array([[Symbolic(chr(ord('a') + i)) for i in range(points.shape[1])]]),K)[0]print('Уравнение целевой плоскости:')
print(*[f'{value} * {symbol}' for symbol,value in  zip(symbols, values)], sep=' + \n', end=' = 1')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question