def gradient(X, Y, gd_lambda, descent=True, max_iterations=1000, binary=False): accepted = False iterations = 0 if type(X) is pd.DataFrame: X = pandas_to_data(X) # add column of ones X = add_col(X, 1) w = [0 for _ in range(len(X[0]))] hrows = [0 for _ in range(len(X))] print type(list(Y)) not_converged = len(X) while iterations < max_iterations and not_converged > 0: not_converged = len(X) iterations += 1 for r in range(len(X)): # r = i, c = j row = X[r] h = get_hw(row, w, binary) hrows[r] = h #TODO this doesn't seem right! #print 'values: {} {} '.format(list(Y)[r], h) if h-list(Y)[r] == 0: not_converged -= 1 for c in range(len(row)): w[c] = w[c] - (gd_lambda * (h - list(Y)[r]) * row[c]) debug_print(iterations, not_converged, hrows, Y) return w
def test_add_col(): array = [] for i in range(5): row = [] for j in range(3): row.append(0) array.append(row) print array array = utils.add_col(array, 1) print array