Ejemplo n.º 1
0
import sys
sys.path.append('src')
from polynomial_regressor import PolynomialRegressor
from matrix import Matrix

sandwich_data = Matrix(
    elements=[[1, 0, 0], [1, 1, 0], [1, 2, 0], [1, 4, 0], [1, 6, 0], [1, 0, 2],
              [1, 0, 4], [1, 0, 6], [1, 0, 8], [1, 2, 2], [1, 3, 4]])
sandwich_ratings = Matrix(
    elements=[[1], [2], [4], [8], [9], [2], [5], [7], [6], [0], [0]])

sandwich_regressor = PolynomialRegressor(2)
sandwich_regressor.solve_coefficients_with_inputs(sandwich_data,
                                                  sandwich_ratings)

coeffs = sandwich_regressor.coefficients

print('Sandwich Rating Equation')
print('Rating = ', coeffs[0], '+', coeffs[1], '* (Slices of Beef) +',
      coeffs[2], '* (Tbsp of peanut butter)')
print('\n')
rating = sandwich_regressor.evaluate_with_inputs([1, 5, 5])
print('Predicted Rating With 5,5 of peanut butter and beef:')
print(rating)
print('\n')
print('The company probably should still not trust this because')
print('while previously it was a rating of 12.5 and now its 8')
print('I can say sandwiches with both still probably do not taste good')
print(
    'and that is a really high rating, the test samples that came in rated any combinations'
)
Ejemplo n.º 2
0
import sys
sys.path.append('src')
from dataframe import DataFrame
from polynomial_regressor import PolynomialRegressor

df = DataFrame.from_array(
    [(0,1), (1,2), (2,5), (3,10), (4,20), (5,30)],
    columns = ['x', 'y']
)

constant_regressor = PolynomialRegressor(degree=0)
constant_regressor.fit(df, dependent_variable='y')
print(constant_regressor.coefficients)
{'constant': 11.3333}
print(constant_regressor.predict({'x': 2}))
11.3333

linear_regressor = PolynomialRegressor(degree=1)

linear_regressor.fit(df, dependent_variable='y')
print(linear_regressor.coefficients)
{'constant': -3.2381, 'x': 5.8286}
print(linear_regressor.predict({'x': 2}))
8.4190



quadratic_regressor = PolynomialRegressor(degree=2)
quadratic_regressor.fit(df, dependent_variable='y')
print(quadratic_regressor.coefficients)
{'constant': 1.1071, 'x': -0.6893, 'x^2': 1.3036}
Ejemplo n.º 3
0
 [1, 5, 0, 1, 0,  0,  5,  0,  0,  0,  0],\
 [1, 5, 0, 0, 1,  0,  0,  5,  0,  0,  0],\
 [1, 5, 0, 1, 1,  0,  5,  5,  0,  0,  1],\
 [1, 0, 5, 0, 0,  0,  0,  0,  0,  0,  0],\
 [1, 0, 5, 1, 0,  0,  0,  0,  5,  0,  0],\
 [1, 0, 5, 0, 1,  0,  0,  0,  0,  5,  0],\
 [1, 0, 5, 1, 1,  0,  0,  0,  5,  5,  1],\
 [1, 5, 5, 0, 0, 25,  0,  0,  0,  0,  0],\
 [1, 5, 5, 1, 0, 25,  5,  0,  5,  0,  0],\
 [1, 5, 5, 0, 1, 25,  0,  5,  0,  5,  0],\
 [1, 5, 5, 1, 1, 25,  5,  5,  5,  5,  1]])

sandwich_ratings = Matrix(elements=[[1], [1], [4], [0], [4], [8], [1], [0],
                                    [5], [0], [9], [0], [0], [0], [0], [0]])

sandwich_regressor = PolynomialRegressor(10)
sandwich_regressor.solve_coefficients_with_inputs(sandwich_data,
                                                  sandwich_ratings)

coeffs = sandwich_regressor.coefficients

terms = ['Bias Term', 'Beef Slices', 'Peanut Butter', 'Mayo', 'Jelly', 'Beef & PB', 'Beef & Mayo', 'Beef & Jelly',\
'PB & Mayo', 'PB & Jelly', 'Mayo & Jelly']

print('\nSandwich Rating Equation')
equation_str = 'Rating ='
coeffs = [round(coeffs[i], 5) for i in range(len(coeffs))]
for i in range(len(coeffs)):
    equation_str += ' '
    if i > 0:
        equation_str += '+ '
Ejemplo n.º 4
0
import sys
sys.path.append('src')
from dataframe import DataFrame
from polynomial_regressor import PolynomialRegressor

data = [(1, 3.1), (2, 10.17), (3, 20.93), (4, 38.71), (5, 60.91), (6, 98.87), (7, 113.92), (8, 146.95), (9, 190.09), (10, 232.65)]

columns = ['time', 'distance']
df = DataFrame.from_array(data, columns)

# Part A

print("\nquadratic_regressor")

quadratic_regressor = PolynomialRegressor(degree=2)
quadratic_regressor.fit(df, dependent_variable='distance')
quadratic_regressor.solve_coefficients()

n_list = [5, 10, 200]
for n in n_list:
    prediction = quadratic_regressor.predict({'time': n})
    print(n, "->", prediction)

# Part B

print("\ncubic_regressor")

cubic_regressor = PolynomialRegressor(degree=3)
cubic_regressor.fit(df, dependent_variable='distance')
cubic_regressor.solve_coefficients()
Ejemplo n.º 5
0
    new_data = []
    for i in range(len(data)):
        new_data.append([])
        for j in range(len(data[0])):
            if j == len(data[0]) - 1:
                results.append([math.log((10 / data[i][j]) - 1)])
            else:
                new_data[i].append(data[i][j])
    return [new_data, results]


split = split_data(data)
sandwich_data = Matrix(elements=split[0])
sandwich_ratings = Matrix(elements=split[1])

regressor = PolynomialRegressor(2)

regressor.solve_coefficients_with_inputs(sandwich_data, sandwich_ratings)

coeffs = regressor.coefficients


def make_interactions(starters):
    all_interactions = []
    for i in range(len(starters)):
        all_interactions.append(starters[i])
    for i in range(len(starters)):
        for j in range(len(starters)):
            if j > i:
                all_interactions.append(starters[i] * starters[j])
    all_interactions.insert(0, 1)
import sys
sys.path.append('src')
from polynomial_regressor import PolynomialRegressor
from dataframe import DataFrame

data = [(1, 3.1), (2, 10.17), (3, 20.93), (4, 38.71), (5, 60.91), (6, 98.87),
        (7, 113.92), (8, 146.95), (9, 190.09), (10, 232.65)]

df = DataFrame.from_array(data, ['time', 'distance'])

quadratic_regressor = PolynomialRegressor(degree=2)
quadratic_regressor.fit(df, 'distance')
print('Quadratic Regressor:')
print(quadratic_regressor.coefficients)

for t in [5, 10, 200]:
    print('Distance after ' + str(t) + ' seconds:',
          quadratic_regressor.predict({'time': t}))

df = DataFrame.from_array(data, ['time', 'distance'])

cubic_regressor = PolynomialRegressor(degree=3)
cubic_regressor.fit(df, 'distance')
print('Cubic Regressor:')
print(cubic_regressor.coefficients)

for t in [5, 10, 200]:
    print('Distance after ' + str(t) + ' seconds:',
          cubic_regressor.predict({'time': t}))
import sys
sys.path.append('src')
from polynomial_regressor import PolynomialRegressor
from matrix import Matrix
from dataframe import DataFrame

df = DataFrame.from_array([(0, 1), (1, 2), (2, 5), (3, 10), (4, 20), (5, 30)],
                          columns=['x', 'y'])

print('Testing degree 0 polynomial regressor...')
constant_regressor = PolynomialRegressor(degree=0)
constant_regressor.fit(df, dependent_variable='y')
assert [
    round(constant_regressor.coefficients[n], 4)
    for n in constant_regressor.coefficients
] == [11.3333]
assert round(constant_regressor.predict({'x': 2}), 4) == 11.3333
print('PASSED')

print('Testing degree 1 polynomial regressor...')
linear_regressor = PolynomialRegressor(degree=1)
linear_regressor.fit(df, dependent_variable='y')
assert [
    round(linear_regressor.coefficients[n], 4)
    for n in linear_regressor.coefficients
] == [-3.2381, 5.8286]
assert round(linear_regressor.predict({'x': 2}), 4) == 8.4190
print('PASSED')

print('Testing degree 2 polynomial regressor...')
quadratic_regressor = PolynomialRegressor(degree=2)