Context:FIT1043_MOC Β· Weeks 5β7 modelling in ONE skeleton Β· code canon in the LAB notebooks (30_Projects/FIT1043_Labs/Week7-Classification-sklearn.ipynb, Week7-Clustering-KMeans.ipynb) Β· applied depth in Scikit-learn Classification and Clustering.
Unit-specific: FIT1043's linear regression is done with scipy linregress, not sklearn (Week 5 lab) β see Linear Regression in Python (scipy). sklearn here is for classification and clustering (Week 7). The sklearn regression rows below are the standard API for reference/other units.
Quick Revision
π― Objective: every sklearn model, same four verbs β Estimator() β fit(X_train, y_train) β predict(X_test) β evaluate.
β‘ Key Constraint: shapes β XβRNΓD (2-D, even for one feature via df.loc[:, ['weight']]), yβRN (1-D); most beginner errors are a 1-D X.
Read coefficients β model.coef_, model.intercept_ β same quantities as Rβs fit$coefficients (R Toolkit (Cheatsheet)).
βοΈ Practice
df has hours, attendance β passed (0/1). Build an 80/20 split, fit a decision tree, print the confusion matrix and accuracy, and state which metric you'd report if failing students are rare and missing one is costly.
Reference solution
from sklearn.model_selection import train_test_splitfrom sklearn.tree import DecisionTreeClassifierfrom sklearn.metrics import confusion_matrix, accuracy_scoreX, y = df[['hours', 'attendance']], df['passed']X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=42)model = DecisionTreeClassifier().fit(X_tr, y_tr)y_pred = model.predict(X_te)print(confusion_matrix(y_te, y_pred), accuracy_score(y_te, y_pred))# rare + costly misses β report RECALL on the 'fail' class, not accuracy
Key moves: 2-D X; fit on train only; metric justified by cost structure.
β οΈ Common Mistakes
π‘ 1-D X crash β a single-bracket df['w'] is (N,); estimators need 2-D (N,1) β use df.loc[:, ['w']] or df.iloc[:, [0]].
π‘ Fitting on everything β split FIRST; any statistic learned from test rows (even StandardScaler) is leakage β fit_transform train, transform test.
π‘ Accuracy on imbalance β 95% accuracy on a 95/5 class split is the majority-class baseline, not skill.
π‘ Set random_state β without it, splits/forests/KMeans seeds change every run and results arenβt reproducible.