From fb2df2f2668c3a28478fd68020709a823d5191a6 Mon Sep 17 00:00:00 2001 From: AdrianAlan Date: Thu, 20 Jul 2023 14:30:02 +0200 Subject: [PATCH] Solutions and bugfixes for part1 --- part1/2023-CoDaS-HEP-Exercises-1.ipynb | 31 ++++++++++++++------------ 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/part1/2023-CoDaS-HEP-Exercises-1.ipynb b/part1/2023-CoDaS-HEP-Exercises-1.ipynb index d48fe56..80c9c3e 100644 --- a/part1/2023-CoDaS-HEP-Exercises-1.ipynb +++ b/part1/2023-CoDaS-HEP-Exercises-1.ipynb @@ -168,9 +168,9 @@ "outputs": [], "source": [ "F = np.array([0, 12, 45.21, 34, 99.91])\n", - "C = np.zeros(5) # Your solution here\n", - "\n", - "C_exp = np.array([-17.77777778, 42.22222222, 208.27222222, 152.22222222, 481.77222222])\n", + "C = (F - 32) * 5 / 9\n", + "print(C)\n", + "C_exp = np.array([-17.77777778, -11.11111111, 7.33888889, 1.11111111, 37.72777778])\n", "\n", "assert np.all(np.isclose(C_exp, C, rtol=1e-05, atol=1e-08, equal_nan=False))" ] @@ -378,7 +378,6 @@ " def __init__(self):\n", " self.coef_ = None\n", " self.intercept_ = None\n", - " self.init_weights()\n", "\n", " def init_weights(self, num_features):\n", " \"\"\"Accepts an input num_features and sets the coef_\n", @@ -386,22 +385,24 @@ " with mean 0 and standard deviation 1.\n", " Use a numpy array for coef_ and a float for intercept_\"\"\"\n", " # Hint: use `np.random.normal`\n", - " self.coef_ = None # TODO: Change\n", - " self.intercept_ = None # TODO: Change\n", + " self.coef_ = np.random.normal(size=num_features)\n", + " self.intercept_ = np.random.normal()\n", " \n", " def score(self, X, y_true):\n", " \"\"\"Accepts inputs X and y_true and outputs the r2 score of the model.\"\"\"\n", " y_pred = self.predict(X)\n", - " pass\n", + " u = ((y_true - y_pred)**2).sum()\n", + " v = ((y_true - y_true.mean())**2).sum()\n", + " return 1 - u/v\n", "\n", " def calc_loss(self, X, y_true):\n", " \"\"\"Calculates the loss value using current coef/intercept values\"\"\"\n", " y_pred = self.predict(X)\n", - " pass\n", + " return np.mean((y_pred - y_true)**2)/2.\n", "\n", " def predict(self, X):\n", " \"\"\"Creates a prediction with the current coef/intercept values\"\"\"\n", - " pass\n", + " return (X@self.coef_ + self.intercept_)\n", "\n", " def fit(self, X, y_true, max_iter=10000, learning_rate=0.01):\n", " \"\"\"Optimizing models according to a gradient as a way of minimizing loss.\n", @@ -409,13 +410,15 @@ " # Hint: https://ml-cheatsheet.readthedocs.io/en/latest/gradient_descent.html#step-by-step\n", " for _ in range(max_iter):\n", " coef_grad, intercept_grad = self.calc_grad(X, y_true)\n", - " self.coef_ -= None # TODO: Change\n", - " self.intercept_ -= None # TODO: Change\n", + " self.coef_ -= learning_rate * coef_grad\n", + " self.intercept_ -= learning_rate * intercept_grad\n", " \n", " def calc_grad(self, X, y_true):\n", " \"\"\"Calculates gradients for coef/intercept values\"\"\"\n", - " coef_grad = None # TODO: Change\n", - " intercept_grad = None # TODO: Change\n", + " res = y_true - self.predict(X)\n", + " res_mat = np.tile(res.reshape(-1, 1), (1, X.shape[1]))\n", + " coef_grad = -1 * np.mean(res_mat * X, axis=0)\n", + " intercept_grad = -1 * np.mean(res)\n", " return coef_grad, intercept_grad" ] }, @@ -453,7 +456,7 @@ "source": [ "import matplotlib.pyplot as plt\n", "\n", - "plt.scatter(target, lr.predict(inputs))\n", + "plt.scatter(target, my_regressor.predict(inputs))\n", "plt.plot([0, 100], [0, 100], 'r')\n", "plt.xlabel('Expected')\n", "plt.ylabel('Predicted');"