From 3552ceb56cfa935b0696c88d72b39468ca5448ed Mon Sep 17 00:00:00 2001 From: EricC Date: Sat, 22 Nov 2025 10:04:33 -0500 Subject: [PATCH 1/2] Assignment 1 - submission --- 02_activities/assignments/assignment_1.ipynb | 41 +++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index 28d4df017..9d66e064d 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -96,7 +96,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "wine_df.shape[0]" ] }, { @@ -114,7 +115,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "wine_df.shape[1]" ] }, { @@ -132,7 +134,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "wine_df['class'].dtype, wine_df['class'].unique()" ] }, { @@ -151,7 +154,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "wine_df.shape[1] - 1" ] }, { @@ -204,7 +208,7 @@ "id": "403ef0bb", "metadata": {}, "source": [ - "> Your answer here..." + "KNN uses distance calculations; features on larger scales would dominate the distance. Standardization makes everything on the same scale for easier comparison." ] }, { @@ -220,7 +224,7 @@ "id": "fdee5a15", "metadata": {}, "source": [ - "> Your answer here..." + "Class / type does not have realy numerical meaning: the response is a class label, not a numeric measurement. " ] }, { @@ -236,7 +240,7 @@ "id": "f0676c21", "metadata": {}, "source": [ - "> Your answer here..." + "np.random.seed(2025)" ] }, { @@ -261,7 +265,8 @@ "\n", "# split the data into a training and testing set. hint: use train_test_split !\n", "\n", - "# Your code here ..." + "# Your code here ...\n", + "X_train, X_test, y_train, y_test = train_test_split(predictors_standardized, wine_df['class'], test_size=0.25, random_state=123)" ] }, { @@ -289,7 +294,12 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here..." + "# Your code here...\n", + "knn = KNeighborsClassifier()\n", + "param_grid = {'n_neighbors': np.arange(1,51)}\n", + "grid = GridSearchCV(knn, param_grid, cv=10)\n", + "grid.fit(X_train, y_train)\n", + "grid.best_params_" ] }, { @@ -310,7 +320,10 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here..." + "# Your code here...\n", + "best_k = grid.best_params_['n_neighbors']\n", + "model = KNeighborsClassifier(n_neighbors=best_k).fit(X_train, y_train)\n", + "accuracy_score(y_test, model.predict(X_test))" ] }, { @@ -354,10 +367,10 @@ " * Open a private window in your browser. Copy and paste the link to your pull request into the address bar. Make sure you can see your pull request properly. This helps the technical facilitator and learning support staff review your submission easily.\n", "\n", "Checklist:\n", - "- [ ] Created a branch with the correct naming convention.\n", - "- [ ] Ensured that the repository is public.\n", - "- [ ] Reviewed the PR description guidelines and adhered to them.\n", - "- [ ] Verify that the link is accessible in a private browser window.\n", + "- [X] Created a branch with the correct naming convention.\n", + "- [X] Ensured that the repository is public.\n", + "- [X] Reviewed the PR description guidelines and adhered to them.\n", + "- [X] Verify that the link is accessible in a private browser window.\n", "\n", "If you encounter any difficulties or have questions, please don't hesitate to reach out to our team via our Slack at `#dc2-help`. Our Technical Facilitators and Learning Support staff are here to help you navigate any challenges.\n" ] From 947a74d770af431173a570dfad06b4672ad7b2ec Mon Sep 17 00:00:00 2001 From: EricC Date: Sun, 30 Nov 2025 11:37:29 -0500 Subject: [PATCH 2/2] Complete Assignment 2 --- 02_activities/assignments/assignment_2.ipynb | 47 ++++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/02_activities/assignments/assignment_2.ipynb b/02_activities/assignments/assignment_2.ipynb index a05da5cd3..a154a93b2 100644 --- a/02_activities/assignments/assignment_2.ipynb +++ b/02_activities/assignments/assignment_2.ipynb @@ -87,7 +87,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here..." + "# Your answer here...\n", + "mpg_data.shape" ] }, { @@ -105,7 +106,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here..." + "# Your answer here...\n", + "mpg_data['mpg'].dtype" ] }, { @@ -131,7 +133,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here... " + "# Your answer here... \n", + "mpg_data.nlargest(5, 'horsepower')" ] }, { @@ -149,7 +152,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here..." + "# Your answer here...\n", + "mpg_data.shape[1] - 1" ] }, { @@ -225,7 +229,7 @@ "id": "f67e57ab", "metadata": {}, "source": [ - "> Your answer here..." + "Most showed either a positive or negative linear relationship with mpg. Weight, horse power tend to have negative associations with mpg, whiel others, such as acceleration show less clear, or sig. relationship with mpg. " ] }, { @@ -241,7 +245,7 @@ "id": "843f9eef", "metadata": {}, "source": [ - "> Your answer here..." + "linear regression model (best linear fit), by least sq. for each feature vs. mpg. " ] }, { @@ -257,7 +261,7 @@ "id": "2ea782fc", "metadata": {}, "source": [ - "> Your answer here..." + "No. The dots on line are predicted dots. There is a distance between the observed dots and the line, which is random noise or variability, or residuals. Meaning, the model can explain, but not all of the variability in our data. " ] }, { @@ -279,7 +283,11 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here..." + "# Your answer here...\n", + "numeric_predictors = mpg_data.select_dtypes(include=[float, int]).drop(columns=['mpg'])\n", + "X_train, X_test, y_train, y_test = train_test_split(\n", + " numeric_predictors, mpg_data['mpg'], test_size=0.25, random_state=42\n", + ")" ] }, { @@ -299,7 +307,15 @@ "source": [ "# Your code here ...\n", "\n", - "numeric_predictors = 🤷‍♂️\n", + "numeric_predictors = mpg_data.select_dtypes(include=[float, int]).drop(columns=['mpg'])\n", + "X_train, X_test, y_train, y_test = train_test_split(\n", + " numeric_predictors, mpg_data['mpg'], test_size=0.25, random_state=42\n", + ")\n", + "\n", + "lm = LinearRegression()\n", + "lm.fit(X_train, y_train)\n", + "\n", + "numeric_predictors = X_train\n", "\n", "\n", "# Create a DataFrame containing the slope (coefficients) and intercept\n", @@ -335,7 +351,10 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here ..." + "# Your code here ...\n", + "y_pred = lm.predict(X_test)\n", + "rmspe = np.sqrt(mean_squared_error(y_test, y_pred))\n", + "rmspe" ] }, { @@ -375,10 +394,10 @@ " * Open a private window in your browser. Copy and paste the link to your pull request into the address bar. Make sure you can see your pull request properly. This helps the technical facilitator and learning support staff review your submission easily.\n", "\n", "Checklist:\n", - "- [ ] Created a branch with the correct naming convention.\n", - "- [ ] Ensured that the repository is public.\n", - "- [ ] Reviewed the PR description guidelines and adhered to them.\n", - "- [ ] Verify that the link is accessible in a private browser window.\n", + "- [X] Created a branch with the correct naming convention.\n", + "- [X] Ensured that the repository is public.\n", + "- [X] Reviewed the PR description guidelines and adhered to them.\n", + "- [X] Verify that the link is accessible in a private browser window.\n", "\n", "If you encounter any difficulties or have questions, please don't hesitate to reach out to our team via our Slack at `#dc2-help`. Our Technical Facilitators and Learning Support staff are here to help you navigate any challenges.\n" ]