def fit(self, X, Y):
        """
        Callable method of an instance of this class
        to determine a set of coefficients for the 
        given data
            :param X: The conditioned input data
            :param Y: The conditioned output data
        """
        # Section 1: Condition the input and output data
        self.X = self.__condition_data(X)
        self.Y = self.__condition_data(Y)

        # Section 2: Append a column of 1's unless the 
        #     the user knows this is NOT necessary
        if self.fit_intercept:
            self.X = self.__add_ones_column_for_intercept(self.X)

        # Section 3: Transpose the data into the null 
        #     space of the X matrix using the transpose of X
        #     and solve for the coefficients using our general 
        #     solve equations function
        AT = la.transpose(self.X)
        ATA = la.matrix_multiply(AT, self.X)
        ATB = la.matrix_multiply(AT, self.Y)
        self.coefs = la.solve_equations(ATA,ATB,tol=self.tol)
 def __orient_data_correctly(self, D):
     """
     Private function to ensure data is oriented 
     correctly for least squares operations;
     This allows more flexible entry of data
         :param D: The data structure to be 
             oriented; want more rows than columns
         :returns: Correctly oriented data
     """
     if len(D) < len(D[0]): 
         return la.transpose(D)
     else:
         return D
    def predict(self, X_test):
        """
        Condition the test data and then use the existing 
        model (existing coefficients) to solve for test
        results
            :param X_test: Input test data
            :returns:  Output results for test data
        """
        # Section 1: Condition the input data
        self.X_test = self.__condition_data(X_test)

        # Section 2: Append a column of 1's unless the
        #     the user knows this is NOT necessary
        if self.fit_intercept and self.add_ones_column:
            self.X_test = self.__add_ones_column_for_intercept(self.X_test)

        # Section 3: Apply the conditioned input data to the
        #     model coefficients
        return la.matrix_multiply(self.X_test, self.coefs)
# Import conditioned data
X_train = cd.X_train
Y_train = cd.Y_train
X_test = cd.X_test
Y_test = cd.Y_test

# Solve for coefficients
mlls = ml.Least_Squares()
mlls.fit(X_train, Y_train)
print('Pure Coefficients:')
print(mlls.coefs, '\n')

# Make a prediction
YLS = mlls.predict(X_test)
YLST = la.transpose(YLS)[0]

# Look at our predictions and the actual values
print('PurePredictions:\n', YLST[0], '\n')

# Compare to sklearn
SKLearnData = [
    103015.20159796, 132582.27760816, 132447.73845175, 71976.09851258,
    178537.48221056, 116161.24230165, 67851.69209676, 98791.73374687,
    113969.43533013, 167921.06569551
]

print('Delta Between SKLearnPredictions and Pure Predictions:')
for i in range(len(SKLearnData)):
    delta = round(SKLearnData[i], 6) - round(YLST[i], 6)
    print('\tDelta for outputs {} is {}'.format(i, delta))
Ejemplo n.º 5
0
import LinearAlgebraPurePython as la
import numpy as np

print('Shtuff from Basic Tools Post')
print()
la.print_matrix(la.zeros_matrix(3, 3))
print()
ID = la.identity_matrix(4)
la.print_matrix(ID)
print()
A = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
la.print_matrix(A)
print()
AM = la.copy_matrix(A)
la.print_matrix(AM)
print()
AT = la.transpose(A)
la.print_matrix(AT)
print()
C = la.matrix_addition(A, AT)
la.print_matrix(C)
print()
D = la.matrix_subtraction(C, AT)
la.print_matrix(D)
print()
AID = la.matrix_multiply(A, ID)
la.print_matrix(AID)
print()
MatrixList = [A, AT, ID]
Prod3 = la.multiply_matrices(MatrixList)
la.print_matrix(Prod3)
#   and visualization
X = [float(x) / 20.0 for x in range(100)]


def y_of_x(x):
    return 0.2 * x + 0.7 * x**2 + random.uniform(-1, 1)


Y = []
for x in X:
    Y.append(y_of_x(x))

plt.scatter(X, Y)  # sys.exit()

# Section 2: Get fake data in correct format
X = la.transpose([X])
Y = la.transpose([Y])

# Section 3: Pure Python Tools Fit
poly_pp = ml.Poly_Features_Pure_Py(order=2)
Xpp = poly_pp.fit_transform(X)
ls_pp = ml.Least_Squares(tol=2, add_ones_column=False)
ls_pp.fit(Xpp, Y)

# Section 4: SciKit Learn Fit
poly_sk = PolynomialFeatures(degree=2)
Xsk = poly_sk.fit_transform(X)
ls_sk = LinearRegression()
ls_sk.fit(Xsk, Y)

# Section 5: Coefficients Comparison
import LinearAlgebraPurePython as la
import conditioned_data as cd
import sys

# Import conditioned data
X_train = cd.X_train
Y_train = cd.Y_train
X_test = cd.X_test
Y_test = cd.Y_test

# Solve for coefficients
coefs = la.least_squares(X_train, Y_train)
print('Pure Coefficients:')
la.print_matrix(coefs)
print()

# Make a prediction
XLS1 = la.insert_at_nth_column_of_matrix(1, X_test, len(X_test[0]))
YLS = la.matrix_multiply(XLS1, coefs)
YLST = la.transpose(YLS)

# Look at our predictions and the actual values
print('PurePredictions:\n', YLST[0], '\n')

# Compare to sklearn
SKLearnData = [
    103015.20159796, 132582.27760816, 132447.73845175, 71976.09851258,
    178537.48221056, 116161.24230165, 67851.69209676, 98791.73374687,
    113969.43533013, 167921.06569551
]
import LinearAlgebraPurePython as la
import MachineLearningPurePy as ml
import matplotlib.pyplot as plt

X = [[1, 2], [1, 4]]
Y = [2, 3]
plt.scatter(la.transpose(X)[1], Y)

mlls = ml.Least_Squares(fit_intercept=False)
mlls.fit(X, Y)

print(mlls.coefs)

XLS = [[1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5]]
YLS = mlls.predict(XLS)

plt.plot(la.transpose(XLS)[1], YLS)
plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.title('Pure Python Least Squares Line Fit')
plt.show()
import LinearAlgebraPurePython as la
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import sys

X = [[2, 3, 4, 2, 3, 4], [1, 2, 3, 1, 2, 3]]
Y = [1.8, 2.3, 2.8, 2.2, 2.7, 3.2]
fig = plt.figure()
ax = plt.axes(projection='3d')

ax.scatter3D(X[0], X[1], Y)
ax.set_xlabel('X1 Values')
ax.set_ylabel('X2 Values')
ax.set_zlabel('Y Values')
ax.set_title('Pure Python Least Squares Two Inputs Data Fit')

coefs = la.least_squares(X, Y)
la.print_matrix(coefs)

XLS = [[1, 1.5, 2, 2.5, 3, 3.5, 4], [0, 0.5, 1, 1.5, 2, 2.5, 3]]
XLST = la.transpose(XLS)
XLST1 = la.insert_at_nth_column_of_matrix(1, XLST, len(XLST[0]))
YLS = la.matrix_multiply(XLST1, coefs)
YLST = la.transpose(YLS)

ax.plot3D(XLS[0], XLS[1], YLST[0])
plt.show()
Ejemplo n.º 10
0
        if type(X) != list:
            raise TypeError(error_string)
        if type(X[0]) != list:
            raise TypeError(error_string)
        

import LinearAlgebraPurePython as la 

###############################################################################

max_range = 2

X = [[1*x for x in range(max_range)],
     [2*x for x in range(max_range)]]

X = la.transpose(X) 
la.print_matrix(X)
print()

my_poly = Poly_Features_Pure_Py(order=2)
my_poly.fit(X)

feature_names = my_poly.get_feature_names()
feature_names.sort()
print(feature_names)
print()

wxMax = ['x1^2', 'x0 x1', 'x1', 'x0^2', 'x0', '1']
wxMax.sort()
sklearn_poly_features = ['1', 'x0', 'x0 x1', 'x0^2', 'x1', 'x1^2']
sklearn_poly_features.sort()
Ejemplo n.º 11
0
import LinearAlgebraPurePython as la

max_range = 2

X = [[1 * x for x in range(max_range)], [2 * x for x in range(max_range)]]
# [3*x for x in range(max_range)],
# [4*x for x in range(max_range)],
# [5*x for x in range(max_range)]]

X = la.transpose(X)  # print(X)

# Fitting Polynomial Regression to the dataset
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree=2)

X_poly_features = poly_reg.fit(X)
# print(X_poly_features)
# print()

# fit or fit_transform must be called before this is called
feature_names = poly_reg.get_feature_names()
feature_names.sort()
print(feature_names)
print()

# X_poly_transform = poly_reg.transform(X)
# print(len(poly_reg.get_feature_names()))
# print(X_poly_transform)
# print()

# print(poly_reg.get_feature_names())
def y_of_x(x):
    return 0.2 * x + 0.5 * x**2


Y = []
for x in X:
    Y.append(y_of_x(x))

print('Calculated Y Values:', Y)
print()

plt.scatter(X, Y)

poly_sk = PolynomialFeatures(degree=2)
Xsk = poly_sk.fit_transform(la.transpose([X]))

ls_sk = LinearRegression()
ls_sk.fit(Xsk, Y)
print('SKLearn LS coefficients:', ls_sk.coef_, '\n')

XLS = [0, 1, 2, 3, 4, 5]
XLSsk = poly_sk.transform(la.transpose([XLS]))

YLS = ls_sk.predict(XLSsk)

plt.plot(XLS, YLS)
plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.title('Pure Python Least Squares Line Fit')
plt.show()
def y_of_x(x):
    return 0.2 * x + 0.5 * x**2


Y = []
for x in X:
    Y.append(y_of_x(x))

print('Calculated Y Values:', Y)
print()

plt.scatter(X, Y)

poly_pp = ml.Poly_Features_Pure_Py(order=2)
Xpp = poly_pp.fit_transform(la.transpose([X]))

ls_pp = ml.Least_Squares(add_ones_column=False)
ls_pp.fit(Xpp, Y)
print('LS coefficients:', ls_pp.coefs)

XLS = [0, 1, 2, 3, 4, 5]
XLSpp = poly_pp.transform(la.transpose([XLS]))

YLS = ls_pp.predict(XLSpp)

plt.plot(XLS, la.transpose(YLS)[0])
plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.title('Pure Python Least Squares Line Fit')
plt.show()
import LinearAlgebraPurePython as la
import ShortImplementation as si

print('A matrix')
A = [[5, 4, 3, 2, 1], [4, 3, 2, 1, 5], [3, 2, 9, 5, 4], [2, 1, 5, 4, 3],
     [1, 2, 3, 4, 5]]
la.print_matrix(A)
print()

print('X matrix')
X = [[3], [4], [2], [5], [1]]
la.print_matrix(X)
print()

print('Calculate B from X')
B = la.matrix_multiply(A, X)
la.print_matrix(B)
print()

print('Solve for X')
XS = la.solve_equations(A, B, 9)
la.print_matrix(XS)
print()

# print('Solve for X')
# XS = si.solve_equations(A,B)
# la.print_matrix(XS)