Skip to content

Instantly share code, notes, and snippets.

@walkingpendulum
Created November 23, 2017 22:26
Show Gist options
  • Save walkingpendulum/9d1aca862f07c16a3ef0d134f4988547 to your computer and use it in GitHub Desktop.
Save walkingpendulum/9d1aca862f07c16a3ef0d134f4988547 to your computer and use it in GitHub Desktop.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
X, y = np.matrix([np.array([58.8,65.2,70.9,77.4,79.3,81.0,71.9,63.9,54.5,39.5,44.5,43.6,56.0,64.7,73.0,78.9,79.4]),np.array([7107,6373,6796,9208,14792,14564,11964,13526,12656,14119,16691,14571,13619,14575,14556,18573,15618]),np.array([21,22,22,20,25,23,20,23,20,20,22,19,22,22,21,21,22]),np.array([129,141,153,166,193,189,175,186,190,187,195,206,198,192,191,200,200]),np.array([0.26,1.48,1.58,1.70,0.43,0.35,-0.07,-0.03,-0.73,1.88,-0.18,0.08,0.97,-0.37,0.18,-0.88,-0.89])]).T,np.matrix(np.array([3067,2828,2891,2994,3082,3898,3502,3060,3211,3286,3542,3125,3022,2922,3950,4488,3295])).T
X = (X - X.mean(axis=0)) / X.std(axis=0)
y = (y - y.mean()) / y.std()
def weights(X, y, lambda_):
units = np.matrix(np.eye(5))
product = (lambda_ * units + X.T.dot(X))
result = product.I.dot(X.T).dot(y)
return result
errors = []
errors_4 = []
lambdas = np.arange(0, 1.01, 0.01)
for lambda_ in lambdas:
w = weights(X, y, lambda_)
y_pred = X.dot(w)
error = np.linalg.norm(y - y_pred)
errors.append(error)
y_pred_4 = X[:, :-1].dot(w[:-1])
error = np.linalg.norm(y - y_pred_4)
errors_4.append(error)
plt.figure(figsize=[20, 7])
plt.plot(np.arange(y.shape[0]), np.array(y).reshape(-1))
for lambda_ in [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]:
w = get_weight(X, y, lambda_)
y_pred = X.dot(w)
plt.plot(np.arange(y.shape[0]), np.array(y_pred).reshape(-1))
plt.figure(figsize=[10, 3])
l_1, = plt.plot(lambdas, errors, label=u'без x5')
l_2, = plt.plot(lambdas, errors_4, label=u'с x5')
plt.legend(handles=[l_1, l_2]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment