Skip to content

Commit

Permalink
Solutions and bugfixes for part1
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianAlan committed Jul 20, 2023
1 parent 4d004ec commit fb2df2f
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions part1/2023-CoDaS-HEP-Exercises-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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))"
]
Expand Down Expand Up @@ -378,44 +378,47 @@
" 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",
" and intercept_ attributes to random values drawn from a normal distribution\n",
" 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 ([email protected]_ + 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",
" Should update the coef_ and intercept_ attributes\"\"\"\n",
" # 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"
]
},
Expand Down Expand Up @@ -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');"
Expand Down

0 comments on commit fb2df2f

Please sign in to comment.