Skip to content

Instantly share code, notes, and snippets.

@yakovenkodenis
Last active October 14, 2016 19:43
Show Gist options
  • Save yakovenkodenis/29ab3dd18627fa52b8ae7621bb4649e7 to your computer and use it in GitHub Desktop.
Save yakovenkodenis/29ab3dd18627fa52b8ae7621bb4649e7 to your computer and use it in GitHub Desktop.
Weighted Linear Regression thoughts

Weighted Linear Regression $$ J(\theta)=\Sigma w_i (y_i - \theta^T x_i)^2 $$ $$ J'(\theta)=2\Sigma w_i x_i (\theta^T x_i - y_i) $$ $$ w_i = \exp(-\frac {(x_i - x) ^ 2} {2\tau^2}) $$

# x_query - точка для расчёта предсказания, x_query.shape - (9,)
# x.shape - (76, 9), y.shape - (76, 1)
def getWeightedLRPrediction(alpha, n_iterations, x_query, tau, x, y):
	# находим вектор весов w по формуле выше
	w = [np.exp(-((x[i] - x_query).T.dot(x[i] - x_query)) / (2 * tau ** 2) for i in range(len(x)]  # w.shape --> (76, 1)
	
	# преобразуем w в numpy.ndarray
	w = np.array(w)
	
	theta = np.zeros(shape=x_query.shape) # theta.shape --> (9,)
	for _ in range(n_iterations):
		pred = w * (np.dot(x, theta) - y) # pred.shape --> (76,)
		gradient = pred.dot(x) * 2 # gradient.shape --> (9,)
		theta -= alpha * gradient
		
	return np.dot(x_query, theta)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment