import numpy as np import random # m denotes the number of examples # x = x values # y = y values # theta = our solution # alpha = step size # numIterations = how many times we're going to loop def gradientDescent(x, y, theta, alpha, m, numIterations): return theta def genData(numPoints, bias, variance): x = np.zeros(shape=(numPoints, 2)) y = np.zeros(shape=numPoints) # basically a straight line for i in range(0, numPoints): # bias feature x[i][0] = 1 x[i][1] = i # our target variable y[i] = (i + bias) + random.uniform(0, 1) * variance return x, y # gen 100 points with a bias of 25 and 10 variance as a bit of noise x, y = genData(100, 25, 10) #fix for higher dimensions. #here, m = 100, n = 2 m, n = np.shape(x) numIterations= 100000 alpha = 0.0005 theta = np.ones(n) #answer: #[offset, slope] theta = gradientDescent(x, y, theta, alpha, m, numIterations) print "Our solution line has a y-intercept of ", theta[0], "and a slope of", theta[1] print "The data we're trying to fit is as follows" for i in range (0, 100): print x[i][1], y[i]