Найти - Пользователи
Полная версия: Интерполяция, аппроксимация
Начало » Python для новичков » Интерполяция, аппроксимация
1 2
4kpt
К тому же статья не достаточно чесна. Вот

http://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html

Обратите внимание на параметр w
4kpt
Привожу код. Можно допилить… Возможно добавить автоматический выбор параметра метода класса из атрибутов класса при вызове метода класса без одного или нескольких параметров.
import numpy as np
class Approximation:
    """Class for performance of approximation by a polynomial."""
    def __init__(self, x, y, deg):
        self.x, self.y, self.deg = x, y, deg
        self.coefficients = self.update_coefficients(x, y, deg)
        self.function = self.update_function(self.coefficients)
        self.deviation = self.find_square_deviation(x, y, self.coefficients)
    def __repr__(self):
        return ("Class Approximation copy:\n" +
                "Scale: %s\n" % str(self.x) +
                "Empirical frequency: %s\n" % str(self.y) +
                "Coefficients: %s\n" % str(self.coefficients) +
                "Root mean square deviation: %s\n" % str(self.deviation))
    def update_coefficients(self, x, y, deg):
        """Get polynomial coefficients."""
        coefficients = np.polyfit(x, y, deg)
        return coefficients
    def update_function(self, coefficients):
        """Get polynomial functions."""
        function = np.poly1d(coefficients)
        return function
    def find_square_deviation(self, x, y, coefficients):
        """Find root mean square deviation."""
        deviation = 0
        function = self.update_function(coefficients)
        for pos in xrange(len(x)):
            deviation += (y[pos] - function(x[pos])) ** 2
        return deviation**0.5
x_scale = np.array(xrange(20))
y_scale = np.array([i**2 + i*2 for i in xrange(20)])
polynom = Approximation(x_scale, y_scale, 3)
print "СКО: ", polynom.deviation
print "Коэффициенты многочлена: ", polynom.coefficients
print "Значение полинома в точке х = 5: ", polynom.function(5)
print polynom
Win95
Спасибо 4kpt!
Буду разбирать код
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB