Skip to content
Laviyx Learn

Try

Building Better Models Practice

Practise ensembles, feature engineering, pipelines with mixed columns, cross-validation, hyperparameter tuning and imbalanced data with scikit-learn.

Intermediate7 problemsUpdated

Review first: Random Forests, Gradient Boosting, Feature Engineering, Pipelines and Column Transformers, Cross-Validation, Hyperparameter Tuning, Imbalanced Data

  1. 1.Forest versus single tree

    Intermediate

    Compare a DecisionTreeClassifier(random_state=0) with a RandomForestClassifier(n_estimators=200, random_state=0) on the breast cancer data using 5-fold cross-validation. Print each model's mean accuracy (3 decimals).

    Show hint

    cross_val_score(model, X, y, cv=5).mean() for each model.

    Show solution
    Python
    from sklearn.datasets import load_breast_cancer
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.model_selection import cross_val_score
    from sklearn.tree import DecisionTreeClassifier
    
    X, y = load_breast_cancer(return_X_y=True)
    for model in [DecisionTreeClassifier(random_state=0), RandomForestClassifier(n_estimators=200, random_state=0)]:
        print(type(model).__name__, round(cross_val_score(model, X, y, cv=5).mean(), 3))
    Output
    DecisionTreeClassifier 0.917
    RandomForestClassifier 0.96

    Averaging 200 trees, each trained on different samples and features, reduces variance, so the forest beats the single tree.

  2. 2.Most important features

    Intermediate

    Fit RandomForestClassifier(n_estimators=200, random_state=0) on the full breast cancer data and print the names of the three features with the highest feature_importances_, most important first.

    Show hint

    Put the importances in a pandas Series indexed by data.feature_names and use nlargest(3).

    Show solution
    Python
    import pandas as pd
    from sklearn.datasets import load_breast_cancer
    from sklearn.ensemble import RandomForestClassifier
    
    data = load_breast_cancer()
    forest = RandomForestClassifier(n_estimators=200, random_state=0).fit(data.data, data.target)
    importance = pd.Series(forest.feature_importances_, index=data.feature_names)
    print(importance.nlargest(3).index.tolist())
    Output
    ['worst concave points', 'worst perimeter', 'worst radius']
  3. 3.Mixed column pipeline

    Intermediate

    Customers have age, income and city columns. Build a ColumnTransformer that scales the numeric columns and one-hot encodes city, fit it on the data below, and print the output feature names and the shape of the transformed data.

    Python
    df = pd.DataFrame({"age": [25, 40, 33, 51], "income": [30000, 82000, 54000, 91000],
                       "city": ["Pune", "Delhi", "Pune", "Chennai"]})
    Show hint

    ColumnTransformer([("num", StandardScaler(), ["age", "income"]), ("cat", OneHotEncoder(), ["city"])]), then get_feature_names_out().

    Show solution
    Python
    import pandas as pd
    from sklearn.compose import ColumnTransformer
    from sklearn.preprocessing import OneHotEncoder, StandardScaler
    
    df = pd.DataFrame({"age": [25, 40, 33, 51], "income": [30000, 82000, 54000, 91000],
                       "city": ["Pune", "Delhi", "Pune", "Chennai"]})
    ct = ColumnTransformer([("num", StandardScaler(), ["age", "income"]), ("cat", OneHotEncoder(), ["city"])])
    out = ct.fit_transform(df)
    print(ct.get_feature_names_out().tolist())
    print(out.shape)
    Output
    ['num__age', 'num__income', 'cat__city_Chennai', 'cat__city_Delhi', 'cat__city_Pune']
    (4, 5)
  4. 4.Features from dates

    Intermediate

    From the order dates ["2026-01-05", "2026-01-10", "2026-02-18", "2026-03-01"], build a DataFrame with columns month, day_of_week (Monday = 0) and is_weekend (True for Saturday and Sunday), and print it.

    Show hint

    Convert with pd.to_datetime, then use .dt.month and .dt.dayofweek.

    Show solution
    Python
    import pandas as pd
    
    dates = pd.to_datetime(pd.Series(["2026-01-05", "2026-01-10", "2026-02-18", "2026-03-01"]))
    features = pd.DataFrame({"month": dates.dt.month, "day_of_week": dates.dt.dayofweek,
                             "is_weekend": dates.dt.dayofweek >= 5})
    print(features)
    Output
       month  day_of_week  is_weekend
    0      1            0       False
    1      1            5        True
    2      2            2       False
    3      3            6        True
  5. 5.Cross-validation spread

    Intermediate

    Run 10-fold stratified cross-validation (shuffle=True, random_state=0) of a scaled logistic regression on the breast cancer data. Print the lowest, highest and mean fold accuracy (3 decimals).

    Show hint

    Pass cv=StratifiedKFold(n_splits=10, shuffle=True, random_state=0) to cross_val_score.

    Show solution
    Python
    from sklearn.datasets import load_breast_cancer
    from sklearn.linear_model import LogisticRegression
    from sklearn.model_selection import StratifiedKFold, cross_val_score
    from sklearn.pipeline import make_pipeline
    from sklearn.preprocessing import StandardScaler
    
    X, y = load_breast_cancer(return_X_y=True)
    cv = StratifiedKFold(n_splits=10, shuffle=True, random_state=0)
    scores = cross_val_score(make_pipeline(StandardScaler(), LogisticRegression()), X, y, cv=cv)
    print(round(scores.min(), 3), round(scores.max(), 3), round(scores.mean(), 3))
    Output
    0.947 1.0 0.977
  6. 6.Tune regularisation strength

    Intermediate

    Use GridSearchCV with 5 folds to choose C from [0.01, 0.1, 1, 10] for a scaled logistic regression on the breast cancer data. Print the best C and its cross-validated accuracy (3 decimals).

    Show hint

    In a pipeline made with make_pipeline, the parameter name is logisticregression__C.

    Show solution
    Python
    from sklearn.datasets import load_breast_cancer
    from sklearn.linear_model import LogisticRegression
    from sklearn.model_selection import GridSearchCV
    from sklearn.pipeline import make_pipeline
    from sklearn.preprocessing import StandardScaler
    
    X, y = load_breast_cancer(return_X_y=True)
    pipe = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
    search = GridSearchCV(pipe, {"logisticregression__C": [0.01, 0.1, 1, 10]}, cv=5).fit(X, y)
    print(search.best_params_["logisticregression__C"], round(search.best_score_, 3))
    Output
    1 0.981
  7. 7.Lower the threshold

    Intermediate

    Create imbalanced data with make_classification(n_samples=3000, weights=[0.95, 0.05], random_state=1) and split it 70/30 (stratified, random_state=0). Fit a logistic regression, then print the recall of class 1 at thresholds 0.5, 0.3 and 0.1 (3 decimals).

    Show hint

    Compare predict_proba(X_test)[:, 1] >= threshold with y_test using recall_score.

    Show solution
    Python
    from sklearn.datasets import make_classification
    from sklearn.linear_model import LogisticRegression
    from sklearn.metrics import recall_score
    from sklearn.model_selection import train_test_split
    
    X, y = make_classification(n_samples=3000, weights=[0.95, 0.05], random_state=1)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)
    proba = LogisticRegression(max_iter=1000).fit(X_train, y_train).predict_proba(X_test)[:, 1]
    for threshold in [0.5, 0.3, 0.1]:
        print(threshold, round(recall_score(y_test, proba >= threshold), 3))
    Output
    0.5 0.633
    0.3 0.796
    0.1 0.918

    Lowering the threshold flags more cases as class 1, so recall rises from 0.633 to 0.918. Precision falls in exchange; choose the threshold from the cost of each kind of mistake.