Created
June 6, 2019 20:09
-
-
Save sbsatter/3ec2c9ebb116ea078ef06820e2e555a0 to your computer and use it in GitHub Desktop.
Day 6: Multiple Linear Regression: Predicting House Prices (https://www.hackerrank.com/challenges/predicting-house-prices/problem)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Enter your code here. Read input from STDIN. Print output to STDOUT | |
import numpy as np | |
def cost(y, y_): | |
m = y.shape[1] | |
cost = 1/(2*m) * np.sum(np.square(y-y_)) | |
return cost | |
dimensions = input() | |
F, H = map(int, dimensions.split()) | |
X = np.array([[float(x) for x in input().split()] for i in range(H)]) | |
y = X[:, -1].reshape(H, 1) # y => (mx1) | |
X = X[:, :-1] # remove label y -> X => (mxn) | |
# print(X, y) | |
W = np.zeros((F, 1)) # W => (nx1) | |
b = 0 # b => (1x1) | |
# print(W, b) | |
num_iter = 1500 | |
for i in range(num_iter): | |
# print('X - W', X.shape, W.shape) | |
added = np.matmul(X, W) | |
# print('added', added, added.shape) | |
y_ = added + b | |
# print('y', y.shape) | |
alpha = 0.02 | |
# cost = cost(y, y_) | |
diff = y_- y # diff => (mx1) | |
difference = np.sum(diff, axis=0) | |
# print('diff', diff, diff.shape) | |
W = W - alpha * 1/H * np.matmul(X.T, diff) | |
b = b - alpha * 1/H * difference | |
# print('W', W) | |
# print('b', b) | |
t = int(input()) | |
test = np.array([[float(x) for x in input().split()] for i in range(t)]) | |
y_ = np.matmul(test, W) + b | |
for i in y_: | |
print(i[0]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment