Пример #1
0
def main(args):
    args_len = len(args)
    if args_len in [1, 2]:
        x, y = utilities.load_points_from_file(args[0])
        model_funcs = get_model(x, y)
        model_y = apply_funcs(x, model_funcs)
        # print(model_y)
        model_error = error(model_y, y)
        print(model_error)

    if args_len == 2:
        if args[1] == '--plot':
            # Print out graph
            segments = len(x) // 20
            for i in range(segments):
                segment_x = x[i * 20:i * 20 + 20]
                min_x = segment_x.min()
                max_x = segment_x.max()
                x_plot = np.linspace(min_x, max_x, 100)
                y_plot = model_funcs[i](x_plot)
                plt.plot(x_plot, y_plot)
            utilities.view_data_segments(x, y)
Пример #2
0
from utilities import load_points_from_file, view_data_segments

import sys, os
from functools import reduce, partial

import numpy as np
import math
from matplotlib import pyplot as plt

# read in options
file = sys.argv[1]
plot = False
if len(sys.argv) >= 3:
    plot = True if sys.argv[2] == '--plot' else False
xs, ys = load_points_from_file(file)

# returns whatever you give it :)
identity = lambda x: x


# returns y val for a point x
# for a poly a + bf(X) + cf(X)^2 + ...
def getLineVal(coeff, f, x):
    y = 0
    for deg in range(0, len(coeff)):
        y += coeff[deg] * (f(x)**deg)
    return y


class Segment():
    def __init__(self, xs, ys):
Пример #3
0
    return sum([c * (x**i) for i, c in enumerate(coefficients)])


def split(array, n):
    if len(array) == n:
        return [array]
    return [array[:n]] + split(array[n:len(array)], n)


#returns the total squared error between the original data points and the estimated ones
def error(X, Y, estY):
    range = abs(max(Y) - min(Y)) * abs(max(X) - min(X))
    return sum([(y - y1) * (y - y1) for y, y1 in zip(Y, estY)]) / range


data = load_points_from_file(sys.argv[1])

dataX = split(data[0], segmentLength)
dataY = split(data[1], segmentLength)


def getError(X, Y, n):
    poly = leastSquares(X, Y, n)
    evalY = evalArray(X, poly)
    return error(X, Y, evalY)


def getPolynomial(X, Y, lastError, n=2):
    poly = leastSquares(X, Y, n)
    evalY = evalArray(X, poly)
    e = error(X, Y, evalY)
Пример #4
0
        line = 0
        k = 0
        while k < segs[j].shape[1]:
            line = line + A[j][k] * segs[j][:, k]
            k = k + 1
        lines = np.append(lines, line)
        i = i + 20
        j = j + 1
    lines = lines.flatten()
    plt.plot(x, lines, c='r')
    plt.show()


args = sys.argv[1:]
file = args[0]
x, y = ut.load_points_from_file(file)
X = genXMatrix(x, 3)  # linear, cubed and sin
r, = x.shape
i = 0
segs = []
As = []
sse = 0
while i < r:  #Calculate A matrix for each line segment
    X1 = genXMatrix(x[i:i + 20], 1)  #Linear
    A1 = leastSquares(X1, y[i:i + 20])
    err1 = findError(X1, y[i:i + 20], A1)
    X2 = genXMatrix(x[i:i + 20], 3)  #Cubed
    A2 = leastSquares(X2, y[i:i + 20])
    err2 = findError(X2, y[i:i + 20], A2)
    X3 = genXMatrix(x[i:i + 20], 1, 1)  #Sine
    A3 = leastSquares(X3, y[i:i + 20])
Пример #5
0
                'across the '
                'entire data set. \n o   Run python lsr.py data_filename.csv --plot to calculate the total error of '
                'the line approximations across the entire data set and plot the lines of best fit. \n o   Run '
                'python lsr.py --help for the documentation again. \n o   Note that the "data_filename.csv" file '
                'for data must be in the same path folder as lsr.py and must be formatted correctly.'
            )
            exit()

        if data_filename[-4:data_filename_length] != '.csv':
            print(
                'Invalid first argument type. Try running "python lsr.py --help" for documentation.'
            )
            exit()

        # Calculate the total error, don't plot the graph
        adv_x, adv_y = ut1.load_points_from_file(data_filename)
        ut2.plot_least_squares(adv_x, adv_y, 20, hide=False)

    elif len(arguments) == 2:
        # For the case: data_filename.csv --plot
        if data_filename[-4:data_filename_length] != '.csv':
            print(
                'Invalid first argument type. Try running "python lsr.py --help" for documentation.'
            )
            exit()

        if arguments[1] == '--plot':
            # Calculate the total error, plot the graph
            adv_x, adv_y = ut1.load_points_from_file(data_filename)
            ut2.plot_least_squares(adv_x, adv_y, 20, hide=False, debug=False)
        else:
Пример #6
0
def readFile(filename):
    if (filename != None):
        return load_points_from_file(filename)
    else:
        print("Please enter a file name")