Avail 15% off on First assignment order NAH_FIRST_15%

Logo

For information and reference only – not to be submitted as your own work.

This BBIM708 Machine Learning and AI sample shows how a strong portfolio extract can be organised for the New Zealand School of Education assessment on algorithm evaluation and fine-tuning. It follows the structure of the BBIM708 Assessment 2 portfolio question and is paired with our guide to approaching the BBIM708 Machine Learning portfolio. All numbers, tables and results in this extract are illustrative and are not results from any specific dataset run. Rerun the code on your own data and report what you actually obtain.

What this sample covers

The table maps each part of the portfolio to the place where this extract answers it. Marks follow the weightings in the brief; the extract is shortened, so a full submission needs the word counts the brief sets for each item.

Brief task Marks Where answered below
Part A (a–c): working principles, suitable problems, strengths and limitations 12 Part A: algorithm selection and justification
Part A (d): training code and explanation 4 Part A: training code
Part A (e–g): diagnostic visual, two metrics, interpretation 28 Part A: diagnostics and metrics
Part B (a): Grid Search on a classifier 18 Part B: Grid Search
Part B (b): Random Search on a regressor 18 Part B: Random Search
Part B (c): 5-fold cross-validation, per-fold table, stability 20 Part B: cross-validation

Model answer extract

Part A: algorithm selection and justification

This extract uses the scikit-learn breast cancer dataset (a binary classification problem with 569 observations and 30 numeric features) for the supervised model, and the Iris measurements for the unsupervised model. Both are open datasets distributed with scikit-learn (Pedregosa et al., 2011). The supervised choice is a Random Forest classifier; the unsupervised choice is K-means.

Working principles. A Random Forest builds many decision trees on bootstrap samples and considers only a random subset of features at each split. Predictions are combined by majority vote, which reduces the variance of any single tree (Breiman, 2001). K-means partitions observations into k clusters by repeatedly assigning each point to its nearest centroid and recomputing centroids as cluster means until assignments stop changing, an iterative scheme first described by Lloyd (1982).

Suitable problems. Random Forests suit tabular data with mixed feature scales and non-linear interactions, such as diagnostic screening, because they need little preprocessing. K-means suits numeric, roughly spherical groupings where no labels exist, for example customer segmentation, and it needs features scaled to comparable ranges (James et al., 2023).

Strengths and limitations. The forest copes well with noisy features and gives feature importance, but it is harder to explain than a single tree and can be slow to train on very large data. K-means is fast and easy to interpret, but k must be chosen in advance and the result depends on initialisation and on outliers pulling centroids away from natural groups.

Part A: training code

The screenshot you submit should show code like the following, with a short comment on each step. Fixing random_state makes the run reproducible, which markers value.

from sklearn.datasets import load_breast_cancer, load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans

X, y = load_breast_cancer(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(
    X, y, test_size=0.2, stratify=y, random_state=42)

rf = RandomForestClassifier(n_estimators=200, random_state=42)
rf.fit(X_tr, y_tr)

X_iris = StandardScaler().fit_transform(load_iris().data)
km = KMeans(n_clusters=3, n_init=10, random_state=42).fit(X_iris)

The split is stratified so both classes keep their proportions, and the test set is left untouched until final evaluation. The scaler is applied before K-means because distance-based clustering is sensitive to feature units.

Part A: diagnostics and metrics

For the Random Forest, the diagnostic visual is a confusion matrix from ConfusionMatrixDisplay.from_estimator; for K-means, a scatter plot of two features coloured by cluster with centroids marked. Label axes, title each figure and state the dataset in the caption. Two metrics per model are reported below.

Model Metric 1 Metric 2
Random Forest (test set) Accuracy: 0.96 (illustrative) F1-score: 0.97 (illustrative)
K-means, k = 3 Silhouette: 0.46 (illustrative) Adjusted Rand index: 0.62 (illustrative)

Interpretation. If the confusion matrix showed few false negatives, the forest would be doing its job in a screening setting, where missing a malignant case costs more than a false alarm. Accuracy alone can hide this, which is why F1 is reported beside it. For K-means, a moderate silhouette value suggests clusters that are distinct but not cleanly separated, and a moderate adjusted Rand index shows partial agreement with the known species labels. The honest conclusion is that two groups overlap in feature space, so k = 3 is a reasonable but imperfect summary.

Have your own dataset and a deadline? Our team can review your scripts, plots and write-up through programming homework help for Python and machine learning tasks.

Part B: Grid Search on a classifier

The dataset is the same breast cancer data, with 30 features and a binary target. The aim is to tune a Random Forest over two hyperparameters, two values each, so Grid Search evaluates every one of the four combinations.

from sklearn.model_selection import GridSearchCV

param_grid = {
    "n_estimators": [100, 300],   # number of trees
    "max_depth": [5, None],       # tree depth limit
}
grid = GridSearchCV(
    RandomForestClassifier(random_state=42),
    param_grid, cv=5, scoring="f1")
grid.fit(X_tr, y_tr)
print(grid.best_params_, grid.best_score_)
n_estimators max_depth Mean CV F1 (illustrative)
100 5 0.962
100 None 0.965
300 5 0.964
300 None 0.968

In this illustrative table the best configuration is 300 trees with no depth limit. The gap to the baseline is small, and the comparison should say so: tuning gave a modest gain, and further trees would likely add little. Evaluate the chosen model once on the held-out test set and report accuracy and F1 there.

Part B: Random Search on a regressor

For regression, a Random Forest Regressor is tuned on the scikit-learn diabetes dataset. Random Search samples a fixed number of settings from distributions, which covers broad ranges more efficiently than a grid when only a few hyperparameters matter (Bergstra and Bengio, 2012).

from scipy.stats import randint, uniform
from sklearn.datasets import load_diabetes
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import RandomizedSearchCV

Xr, yr = load_diabetes(return_X_y=True)
Xr_tr, Xr_te, yr_tr, yr_te = train_test_split(
    Xr, yr, test_size=0.2, random_state=42)

dist = {
    "n_estimators": randint(50, 500),
    "max_features": uniform(0.3, 0.7),  # fraction from 0.3 to 1.0
}
rs = RandomizedSearchCV(
    RandomForestRegressor(random_state=42), dist,
    n_iter=20, cv=5,
    scoring="neg_root_mean_squared_error", random_state=42)
rs.fit(Xr_tr, yr_tr)
Model RMSE R² MAE
Default settings (illustrative) 56.0 0.42 45.0
Best random-search settings (illustrative) 54.0 0.46 43.5

Remember that scikit-learn reports error scores as negatives, so flip the sign of best_score_ before quoting RMSE. Discuss whether the improvement is worth the added run time, and note that the search used 20 draws, not an exhaustive sweep.

Part B: cross-validation

Five-fold cross-validation was applied to both tuned models using StratifiedKFold for the classifier and KFold for the regressor, so each observation is used for validation exactly once (Kohavi, 1995).

from sklearn.model_selection import cross_validate, StratifiedKFold, KFold

clf_cv = cross_validate(
    grid.best_estimator_, X, y,
    cv=StratifiedKFold(5, shuffle=True, random_state=42),
    scoring=["accuracy", "f1"])
reg_cv = cross_validate(
    rs.best_estimator_, Xr, yr,
    cv=KFold(5, shuffle=True, random_state=42),
    scoring=["neg_root_mean_squared_error", "r2"])
Fold Accuracy F1 RMSE R²
1 0.96 0.97 55.2 0.45
2 0.95 0.96 58.1 0.38
3 0.97 0.98 51.4 0.52
4 0.96 0.97 56.7 0.43
5 0.94 0.95 54.0 0.47
Mean 0.96 0.97 55.0 0.45

Stability. All fold values above are illustrative. The classifier scores sit in a narrow band, which would suggest the model is stable and the single test result is representative. The regressor varies more across folds, with R² ranging from 0.38 to 0.52, which would indicate sensitivity to which observations land in the validation set; this is common with a small dataset. Report the standard deviation beside each mean, and avoid claiming the model is good simply because the average looks acceptable.

About the expert

Harriet Blackwell is listed as a homework writing specialist with a Master’s in Business Analytics from the University of Auckland. Her profile focuses on tutorial questions, case exercises and problem sets for business, commerce and accounting papers.

Ready to check your own portfolio? Send your brief and datasets through statistics assignment help for model evaluation and interpretation, or upload your files directly via our assignment submission page.

References

  • Bergstra, J., & Bengio, Y. (2012). Random search for hyper-parameter optimization. Journal of Machine Learning Research, 13, 281–305.
  • Breiman, L. (2001). Random forests. Machine Learning, 45(1), 5–32.
  • Géron, A. (2022). Hands-on machine learning with Scikit-Learn, Keras, and TensorFlow (3rd ed.). O’Reilly Media.
  • James, G., Witten, D., Hastie, T., Tibshirani, R., & Taylor, J. (2023). An introduction to statistical learning: With applications in Python. Springer.
  • Kohavi, R. (1995). A study of cross-validation and bootstrap for accuracy estimation and model selection. In Proceedings of the 14th International Joint Conference on Artificial Intelligence (Vol. 2, pp. 1137–1143). Morgan Kaufmann.
  • Lloyd, S. P. (1982). Least squares quantization in PCM. IEEE Transactions on Information Theory, 28(2), 129–137.
  • Pedregosa, F., Varoquaux, G., Gramfort, A., Michel, V., Thirion, B., Grisel, O., … Duchesnay, É. (2011). Scikit-learn: Machine learning in Python. Journal of Machine Learning Research, 12, 2825–2830.

UP TO 15 % DISCOUNT

Get Your Assignment Completed At Lower Prices

Plagiarism Free Solutions
100% Original Work
24*7 Online Assistance
Native PhD Experts
Hire a Writer Now

Facing Issues with Assignments? Talk to Our Experts Now! Download Our App Now!