Exemple #1
0
def regression (alpha, epsilon, gamma, secondsToRun, data, ridge):
	if not ridge:
		gamma = 0
	size = data[0].size - 1
	thetas = Cost.randomThetas(0,0, size) ## make a random list of thetas
	cur = 0
	timeout = time.time() + secondsToRun
	itera = 0
	previousCost = 0
	while True:
		va = np.asarray(data[cur])
		v = va[0]
		thetaT = np.zeros(size)
		dev = Cost.deviation(v, thetas)
		cost = Cost.costFunction(thetas, data, gamma)
		if abs(cost - previousCost) < epsilon or timeout < time.time():
			break
		for i in range(size):
			thetaT[i] = (1 - alpha * gamma) * thetas[i] - alpha * Cost.costDeriv(dev, v[i])
			
		
		thetas = thetaT
		
		if cur < data.shape[0] - 1: # I don't think this is the number you want. You want the number of rows right?
			cur += 1
		else:
			cur = 0
			itera += 1
		##print costFunction(thetas, data[cur], )
	##print cur
	##plt.plot(range(len(lst)), lst,'ro')
	##plt.axis([0, len(lst), 0, 40])
	##plt.show()  ##plot the size of gradient
		temp = cost
		previousCost = cost
	return thetas