Exemple #1
0
import numpy as np
import matplotlib.pyplot as plt
import a_linear_regression.linear_utils as linear

X = []
Y = []

# Read csv and populate arrays
for line in open('csv/data_1d.csv'):
    x, y = line.split(',')
    X.append(float(x))
    Y.append(float(y))

# Converting regular arrays into numpy arrays
X = np.array(X)
Y = np.array(Y)

# Visualize the input data
plt.scatter(X, Y)
# plt.show()

Y_hat = linear.calculate_y_hat(X, Y)

# Plot with predictions
plt.scatter(X, Y)
plt.plot(X, Y_hat)
plt.show()

# Calculate R squared
print(linear.calculate_r_squared(Y, Y_hat))
Exemple #2
0
def get_r_squared(X, Y):
    w = linear.calculate_W(X, Y)
    Y_hat = np.dot(X, w)
    return linear.calculate_r_squared(Y, Y_hat)
Exemple #3
0
X = []
Y = []

for line in open('csv/data_2d.csv'):
    x1, x2, y = line.split(',')
    X.append([float(x1), float(x2), BIAS])
    Y.append(float(y))

X = np.array(X)
Y = np.array(Y)

print('Shape of X is ' + str(X.shape))
print('Shape of Y is ' + str(Y.shape))

# Plot in 3D coordinate space
# TODO: Code example didn't work...need to investigate

w = linear.calculate_W(X, Y)

print('Shape of w is ' + str(w.shape))

Y_hat = np.dot(X, w)
print('Shape of Y Hat is ' + str(Y_hat.shape))

r_squared = linear.calculate_r_squared(Y, Y_hat)

residual = Y - Y_hat
some = 1 - residual.dot(residual)

print('r_squared is ' + str(r_squared))