Skip to content
Laviyx Learn

Try

Regression and Classification Practice

Practise train/test splits, linear and logistic regression, regression and classification metrics, k-nearest neighbours and decision trees with scikit-learn.

Beginner7 problemsUpdated

Review first: Features, Labels and Train/Test Splits, Linear Regression, Regression Metrics, Logistic Regression, Classification Metrics, k-Nearest Neighbours, Decision Trees

  1. 1.Stratified split

    Beginner

    Load the wine dataset with load_wine(return_X_y=True). Split it into 75% training and 25% test data with random_state=0, stratified by class. Print the number of rows in each part and the class counts in the test set.

    Show hint

    train_test_split(X, y, test_size=0.25, random_state=0, stratify=y), then np.bincount(y_test).

    Show solution
    Python
    import numpy as np
    from sklearn.datasets import load_wine
    from sklearn.model_selection import train_test_split
    
    X, y = load_wine(return_X_y=True)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
    print(len(X_train), len(X_test))
    print(np.bincount(y_test))
    Output
    133 45
    [15 18 12]
  2. 2.Rent from flat size

    Beginner

    Flat sizes (sq ft) [450, 600, 750, 900, 1100, 1300] have monthly rents (₹) [9000, 11500, 14200, 16800, 20500, 24000]. Fit a linear regression, print the rent increase per extra square foot (2 decimals), and predict the rent for a 1,000 sq ft flat (rounded to the nearest rupee).

    Show hint

    Reshape sizes into a column with .reshape(-1, 1). The slope is model.coef_[0].

    Show solution
    Python
    import numpy as np
    from sklearn.linear_model import LinearRegression
    
    size = np.array([450, 600, 750, 900, 1100, 1300]).reshape(-1, 1)
    rent = np.array([9000, 11500, 14200, 16800, 20500, 24000])
    model = LinearRegression().fit(size, rent)
    print(round(model.coef_[0], 2))
    print(round(model.predict([[1000]])[0]))
    Output
    17.74
    18661

    Each extra square foot adds about ₹17.74 to the monthly rent, so a 1,000 sq ft flat is predicted at about ₹18,661.

  3. 3.Regression errors

    Beginner

    Actual delivery times are [30, 45, 25, 60, 40] minutes and a model predicted [35, 40, 30, 50, 42]. Print the MAE, RMSE and R², each rounded to 2 decimals.

    Show hint

    mean_absolute_error, np.sqrt(mean_squared_error(...)) and r2_score from sklearn.metrics.

    Show solution
    Python
    import numpy as np
    from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
    
    actual = [30, 45, 25, 60, 40]
    predicted = [35, 40, 30, 50, 42]
    print(round(mean_absolute_error(actual, predicted), 2))
    print(round(np.sqrt(mean_squared_error(actual, predicted)), 2))
    print(round(r2_score(actual, predicted), 2))
    Output
    5.4
    5.98
    0.76
  4. 4.Logistic regression pipeline

    Beginner

    On the breast cancer dataset, split 75/25 (random_state=0, stratified), fit a pipeline of StandardScaler and LogisticRegression, and print the test accuracy (3 decimals) and the predicted probability of class 1 for the first test row (3 decimals).

    Show hint

    make_pipeline(StandardScaler(), LogisticRegression()); predict_proba(X_test[:1])[0, 1].

    Show solution
    Python
    from sklearn.datasets import load_breast_cancer
    from sklearn.linear_model import LogisticRegression
    from sklearn.model_selection import train_test_split
    from sklearn.pipeline import make_pipeline
    from sklearn.preprocessing import StandardScaler
    
    X, y = load_breast_cancer(return_X_y=True)
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0, stratify=y)
    model = make_pipeline(StandardScaler(), LogisticRegression()).fit(X_train, y_train)
    print(round(model.score(X_test, y_test), 3))
    print(round(model.predict_proba(X_test[:1])[0, 1], 3))
    Output
    0.958
    0.996
  5. 5.Confusion matrix by hand

    Beginner

    True labels are [1, 0, 1, 1, 0, 0, 1, 0, 1, 0] and predictions are [1, 0, 0, 1, 0, 1, 1, 0, 0, 0]. Print the confusion matrix, then precision, recall and F1 rounded to 2 decimals.

    Show hint

    confusion_matrix, precision_score, recall_score and f1_score from sklearn.metrics.

    Show solution
    Python
    from sklearn.metrics import confusion_matrix, f1_score, precision_score, recall_score
    
    y_true = [1, 0, 1, 1, 0, 0, 1, 0, 1, 0]
    y_pred = [1, 0, 0, 1, 0, 1, 1, 0, 0, 0]
    print(confusion_matrix(y_true, y_pred))
    print(round(precision_score(y_true, y_pred), 2), round(recall_score(y_true, y_pred), 2), round(f1_score(y_true, y_pred), 2))
    Output
    [[4 1]
     [2 3]]
    0.75 0.6 0.67

    The matrix rows are actual classes and columns predicted: 3 true positives, 1 false positive and 2 false negatives. Precision is 3/4 = 0.75 and recall 3/5 = 0.6.

  6. 6.Choosing k for k-NN

    Intermediate

    On the scaled wine data, compute the 5-fold cross-validated accuracy of k-nearest neighbours for k = 1, 5, 15 and 45. Print each k with its accuracy (3 decimals).

    Show hint

    Put StandardScaler and KNeighborsClassifier(n_neighbors=k) in a pipeline and pass it to cross_val_score.

    Show solution
    Python
    from sklearn.datasets import load_wine
    from sklearn.model_selection import cross_val_score
    from sklearn.neighbors import KNeighborsClassifier
    from sklearn.pipeline import make_pipeline
    from sklearn.preprocessing import StandardScaler
    
    X, y = load_wine(return_X_y=True)
    for k in [1, 5, 15, 45]:
        model = make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=k))
        print(k, round(cross_val_score(model, X, y, cv=5).mean(), 3))
    Output
    1 0.95
    5 0.949
    15 0.955
    45 0.961

    Accuracy changes only a little across these values of k; here the largest k scores slightly best. Small k follows individual neighbours (more variance); large k smooths over more of them.

  7. 7.Tree depth and overfitting

    Intermediate

    Split the breast cancer data 75/25 (random_state=0). For decision trees with max_depth 1, 3, 5 and None (random_state=0), print the training and test accuracy (3 decimals). Which depth generalises best?

    Show hint

    Loop over the depths, fit each tree on the training data and call .score on both parts.

    Show solution
    Python
    from sklearn.datasets import load_breast_cancer
    from sklearn.model_selection import train_test_split
    from sklearn.tree import DecisionTreeClassifier
    
    X, y = load_breast_cancer(return_X_y=True)
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
    for depth in [1, 3, 5, None]:
        tree = DecisionTreeClassifier(max_depth=depth, random_state=0).fit(X_train, y_train)
        print(depth, round(tree.score(X_train, y_train), 3), round(tree.score(X_test, y_test), 3))
    Output
    1 0.923 0.881
    3 0.977 0.937
    5 0.993 0.895
    None 1.0 0.881

    Depth 3 gives the best test accuracy (0.937). Deeper trees fit the training data better and better (up to 1.0) but do worse on the test set: overfitting.