Tengo dos conjuntos de datos:
x: [(1, -14580.0, -3389.0), (1, -18403.0, 19052.0), (1, -31845.0, -39952.0), (1, -10634.0, -220.0), (1, -9180.0, 18411.0), (1, -27952.0, -8415.0), (1, -25686.0, 8325.0), (1, -22570.0, 8041.0), (1, -34124.0, -5791.0), (1, -20119.0, 2468.0), (1, -12164.0, 53333.0), (1, -14587.0, -66443.0), (1, -5928.0, -7212.0), (1, -25489.0, 5610.0), (1, -33283.0, -18741.0), (1, -18729.0, 1234.0), (1, -25495.0, -361.0), (1, -33315.0, -6149.0), (1, -28093.0, 11441.0), (1, -36642.0, -3513.0), (1, -26365.0, 8214.0), (1, -36366.0, 10423.0)]
ts:
[131479.0, 128090.0, 147142.0, 107190.0, 106970.0, 125381.0, 116966.0, 125291.0, 133332.0, 127541.0, 130009.0, 183342.0, 116899.0, 109687.0, 115297.0, 96556.0, 97790.0, 97429.0, 91280.0, 102721.0, 99208.0, 107422.0, 117845.0, 168755.0, 110971.0, 84198.0, 82014.0, 77827.0, 72295.0, 64114.0, 63187.0, 66079.0, 72843.0, 71056.0]
value: 422080882435.25397
y quiero hacer una regresión lineal múltiple para predecir el beta.
import random
def dot(v,w):
return sum(v_i*w_i for v_i, w_i in zip(v,w))
def predict(x_i, beta):
"""assumes that the first element of each x_i is one"""
return dot(x_i, beta)
def error(x_i, y_i, beta):
return y_i - predict(x_i, beta)
def squarred_error(x_i, y_i, beta):
return error(x_i, y_i, beta)**2
def squarred_error_gradient(x_i, y_j, beta):
return [-2 * x_ij * error(x_i, y_j, beta)
for x_ij in x_i]
def in_random_order(data):
"""generator that returns the elements if data in random order"""
indexes = [i for i, _ in enumerate(data)] # create a list of indexes
random.shuffle(indexes) # suffle them
for i in indexes:
# print("data[i]: ", data[i])
return data[i]
def minimize_stochastic(target_fn, gradient_fn, x,y, theta_0, alpha_0=0.01):
data = zip(x,y)
theta = theta_0 #initial guess
almpah = alpha_0 # initial step size
min_theta, min_value = None, float('inf') # the minimum so far
iterations_with_no_improvment = 0
# if we ever go 100 iterations with no improvment, stop
while iterations_with_no_improvment < 100:
value = sum(target_fn(x_i, y_i, theta) for x_i, y_i in data)
print("value: ", value)
if value < min_value:
# if we've found a new minimum, remember it
# and go back to the original step size
min_theta, min_value = theta, value
iterations_with_no_improvment = 0
alpha = alpha_0
else:
# otherwise we're not improving, so try shrinking the step size
iterations_with_no_improvment +=1
alpha *=0.9
# and take a gradient step for each of the data points
# print("data: ", [x for x in data])
# print("data: ", data)
for x_i, y_i in in_random_order(data):
gradient_i = gradient_fn(x_i, y_i, theta)
theta = vector_substract(theta, scalar_multiply(alpha_gradient_i))
return min_theta
def estimate_beta(x,y):
beta_initial = [random.random() for x_i in x[0]]
return minimize_stochastic(squarred_error,
squarred_error_gradient,
x,y,
beta_initial,
0.001)
random.seed(0)
beta = estimate_beta(x, ts)
Sin embargo, parece que in_random_order(data)
no funciona porque el data es un zip
que esta None
. En efecto obtengo:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-109-c0f3fa276b50> in <module>()
69
70 random.seed(0)
---> 71 beta = estimate_beta(x, ts)
1 frames
<ipython-input-109-c0f3fa276b50> in minimize_stochastic(target_fn, gradient_fn, x, y, theta_0, alpha_0)
54 # print("data: ", [x for x in data])
55 # print("data: ", data)
---> 56 for x_i, y_i in in_random_order(data):
57 gradient_i = gradient_fn(x_i, y_i, theta)
58 theta = vector_substract(theta, scalar_multiply(alpha_gradient_i))
TypeError: 'NoneType' object is not iterable
ts