Unit 14: Machine learning algorithms - Subjective Questions
ECAP776 • Practice Questions with Detailed Answers
20 questions
Define linear regression. Explain the roles of the dependent variable, independent variables, slope, and intercept.
Linear regression is a supervised learning algorithm used to predict a continuous dependent variable from one or more independent variables.
For simple linear regression, the model is:
- Dependent variable (): The continuous value to be predicted.
- Independent variable (): The input feature used to make the prediction.
- Intercept (): The expected value of when .
- Slope (): The expected change in for a one-unit increase in .
- Error term (): The unexplained difference between an observed value and the value predicted by the model.
With several input features, the method is called multiple linear regression.
Derive the least-squares formulas for the slope and intercept of a simple linear regression model.
For observations , the predicted value is:
The least-squares method minimizes the sum of squared errors:
Differentiating with respect to and , setting both derivatives to zero, and solving the resulting normal equations gives:
Therefore, the fitted regression equation is:
The formulas select the line for which the total squared vertical distance between observed and predicted values is minimum.
Explain the major assumptions of linear regression and describe how the performance of a regression model can be evaluated.
The major assumptions are:
- Linearity: The relationship between features and the target is approximately linear.
- Independence: Observations and their errors are independent.
- Homoscedasticity: The variance of residuals remains approximately constant.
- Normality of residuals: Residuals are approximately normally distributed, particularly when statistical inference is required.
- Low multicollinearity: Independent variables should not be excessively correlated with one another.
Common evaluation measures include:
- Mean Absolute Error:
- Mean Squared Error:
- Root Mean Squared Error:
- Coefficient of determination:
Lower error values and an value closer to generally indicate a better fit, although validation data should be used to check generalization.
Describe the steps required to build and use a linear regression model in Python with scikit-learn.
A typical scikit-learn workflow is:
- Prepare data: Store input features in
Xand the continuous target iny. - Split the data: Use
train_test_splitto produce training and test sets. - Create the model: Instantiate
LinearRegression(). - Train it: Call
model.fit(X_train, y_train). - Predict: Call
model.predict(X_test). - Evaluate: Calculate measures such as MAE, MSE, RMSE, and .
- Interpret: Examine
model.coef_andmodel.intercept_.
Example outline:
model = LinearRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
The test set must not be used during training because it is intended to estimate performance on unseen observations.
Explain how the K-nearest neighbours algorithm performs classification.
K-nearest neighbours (KNN) is a supervised, instance-based learning algorithm. It does not build an explicit mathematical model during training; instead, it stores the training examples.
To classify a new observation, KNN:
- Calculates its distance from the training observations.
- Selects the observations having the smallest distances.
- Finds the class labels of these neighbours.
- Assigns the class occurring most frequently among them.
For example, if and three neighbours belong to class A while two belong to class B, the new observation is assigned to class A.
An odd value of is often selected for binary classification to reduce ties. Distance-weighted KNN may give closer neighbours more influence than distant neighbours.
Discuss the importance of distance measures, feature scaling, and the choice of in K-nearest neighbours.
KNN predictions depend directly on distances between observations. A common measure is Euclidean distance:
Other choices include Manhattan and Minkowski distance.
- Feature scaling: A feature with large numeric values can dominate the distance. Standardization or normalization places features on comparable scales.
- Small : Produces flexible decision boundaries but is sensitive to noise and may overfit.
- Large : Produces smoother boundaries but may underfit and favour the majority class.
- Choosing : Test candidate values using a validation set or cross-validation and select the value with the best validation performance.
Scaling parameters must be learned from the training set and then applied to both training and test data to avoid data leakage.
Distinguish between KNN classification and KNN regression.
Both methods locate the nearest training observations, but they combine their targets differently.
- KNN classification: Predicts a categorical label using majority voting or a distance-weighted vote.
- KNN regression: Predicts a continuous value, usually by taking the mean of the neighbours' target values:
A weighted regression prediction can be calculated as:
where a common choice is .
Classification may be evaluated using accuracy, precision, recall, or F1-score, whereas regression generally uses MAE, MSE, RMSE, or .
Describe the advantages and limitations of K-nearest neighbours, including the effect of high-dimensional data.
Advantages:
- Simple to understand and implement.
- Makes few assumptions about the underlying data distribution.
- Can model nonlinear decision boundaries.
- Supports both classification and regression.
- Requires little conventional training time.
Limitations:
- Prediction can be slow because distances to many training records must be computed.
- It may require substantial memory to store the training data.
- It is sensitive to feature scales, irrelevant features, noise, and outliers.
- It can be biased toward the majority class.
- Its performance depends strongly on and the distance measure.
In high-dimensional spaces, distances between points tend to become less distinctive. This curse of dimensionality makes identifying meaningful neighbours difficult. Feature selection, dimensionality reduction, and appropriate scaling can reduce this problem.
Explain how a decision tree performs classification. Include entropy, Gini impurity, and information gain in your answer.
A decision tree repeatedly splits data into increasingly pure subsets. Each internal node tests a feature, each branch represents an outcome, and each leaf gives a predicted class.
Two common impurity measures are:
- Gini impurity:
- Entropy:
Here, is the proportion of records belonging to class . An entirely pure node has impurity zero.
For entropy, the benefit of a split is measured by information gain:
The algorithm generally selects the split with the largest impurity reduction, applies the same process recursively, and stops when a stopping rule is reached.
What is overfitting in a decision tree? Explain pre-pruning and post-pruning techniques used to control it.
A decision tree overfits when it becomes so complex that it learns noise and unusual details in the training set. Such a tree may achieve very high training accuracy but poor test accuracy.
Pre-pruning stops growth before the tree becomes excessively complex. Common restrictions include:
- Maximum tree depth (
max_depth). - Minimum samples required to split a node (
min_samples_split). - Minimum samples allowed in a leaf (
min_samples_leaf). - Maximum number of leaf nodes.
- Minimum impurity reduction required for a split.
Post-pruning first grows a larger tree and then removes branches that provide little predictive benefit. Cost-complexity pruning balances fit against tree size.
Cross-validation should be used to select pruning parameters. Pruning normally reduces variance and improves generalization, although excessive pruning can cause underfitting.
Describe how a prediction from a decision tree can be interpreted, and state the main advantages and disadvantages of decision trees.
To interpret a prediction, begin at the root and follow the branch selected by each feature test until a leaf is reached. The complete path forms a readable rule, such as if age is at most 30 and income exceeds a threshold, predict class A.
Advantages:
- Easy to visualize and explain.
- Models nonlinear relationships and feature interactions.
- Requires little feature scaling.
- Works with classification and regression problems.
- Performs implicit feature selection through its splits.
Disadvantages:
- Deep trees are prone to overfitting.
- Small changes in data may produce a very different tree.
- Greedy split selection does not guarantee a globally optimal tree.
- A tree can be biased toward features offering many possible splits.
- Axis-aligned splits may require a complex tree for some decision boundaries.
Interpretability decreases as the tree becomes larger.
Explain how a regression tree selects splits and produces predictions.
A regression tree predicts a continuous target. For each candidate feature and split point, it divides the records into child nodes and measures the resulting prediction error.
If the prediction in a node is its mean target value, the node's squared error is:
The tree selects a split that produces the greatest reduction in total error:
Splitting continues recursively until a stopping condition is satisfied. For a new observation, feature tests are followed from the root to a leaf. The prediction is usually the mean target value of the training observations in that leaf.
Unlike linear regression, a regression tree produces piecewise-constant predictions and can naturally model nonlinear relationships.
Define a random forest and explain how it combines multiple decision trees.
A random forest is an ensemble learning algorithm that combines many decision trees.
Each tree is usually trained as follows:
- Draw a bootstrap sample from the training data by sampling records with replacement.
- At each node, consider only a random subset of features when searching for the best split.
- Grow the tree, usually without requiring it to match the structure of other trees.
The individual predictions are combined:
- For classification, the forest uses majority voting or averaged class probabilities.
- For regression, it averages the tree predictions:
Bootstrap sampling and random feature selection make the trees less correlated. Combining these diverse trees reduces variance and usually improves generalization.
Compare a single decision tree with a random forest.
Decision tree:
- Uses one tree to make predictions.
- Is easy to visualize and interpret.
- Trains and predicts relatively quickly.
- Has high variance and can overfit.
- May change substantially following a small change in training data.
Random forest:
- Combines predictions from many randomized trees.
- Usually achieves better test performance.
- Is more stable and resistant to overfitting than one unrestricted tree.
- Requires more computation and memory.
- Is harder to explain as a complete model.
A single tree is appropriate when transparent decision rules are especially important. A random forest is often preferred when stronger predictive performance and robustness are more important than direct model interpretation.
Explain bootstrap aggregation, random feature selection, and out-of-bag evaluation in random forests.
Bootstrap aggregation, or bagging, trains each tree on a sample drawn with replacement from the training set. Some records may appear several times in a tree's sample, while others are omitted.
Random feature selection allows only a subset of features to be considered at each split. It prevents a few dominant features from making all trees too similar and therefore increases diversity.
Records omitted from a tree's bootstrap sample are called its out-of-bag (OOB) observations. Each training record can be predicted using only trees for which that record was out of bag. Comparing these predictions with the true targets gives the OOB score or OOB error.
OOB evaluation provides an internal estimate of generalization performance without requiring a separate validation set, although a final untouched test set is still valuable.
Discuss important random forest hyperparameters and methods for estimating feature importance.
Important hyperparameters include:
n_estimators: Number of trees; more trees generally improve stability but increase computation.max_features: Number or proportion of features considered at each split.max_depth: Maximum depth of each tree.min_samples_split: Minimum records needed to split a node.min_samples_leaf: Minimum records required in a leaf.bootstrap: Whether bootstrap samples are used.class_weight: Weights used to address class imbalance.
Feature importance can be estimated with:
- Impurity-based importance: Adds the weighted impurity reductions produced by each feature. It is fast but may favour continuous or high-cardinality features.
- Permutation importance: Shuffles one feature and measures the decline in predictive performance. It is often easier to interpret but can be affected by correlated features.
Hyperparameters should be tuned using cross-validation rather than the final test set.
State the objective of K-means clustering and explain its iterative algorithm.
K-means is an unsupervised algorithm that divides observations into clusters. It attempts to minimize the within-cluster sum of squares:
where is cluster and is its centroid.
The algorithm performs these steps:
- Select initial centroids.
- Assignment step: Assign every observation to its nearest centroid.
- Update step: Recalculate each centroid as the mean of the observations assigned to its cluster.
- Repeat assignment and update steps until assignments stop changing, centroid movement is sufficiently small, or a maximum number of iterations is reached.
K-means converges to a local minimum, so different initial centroids can produce different results.
Explain how the elbow method, silhouette score, feature scaling, and centroid initialization are used when applying K-means.
- Elbow method: Run K-means for several values of and plot inertia against . A possible choice is the bend after which additional clusters yield relatively small reductions in inertia.
- Silhouette score: For observation , let be its average distance within its cluster and its smallest average distance to another cluster. Then:
A score near suggests a well-separated observation, a score near suggests overlap, and a negative score may indicate poor assignment.
- Feature scaling: Since K-means uses distances, high-magnitude features can dominate. Standardization is commonly applied.
- Initialization: Poor initial centroids can produce weak local solutions. K-means++ spreads initial centroids apart, and multiple initial runs increase the chance of finding a better solution.
Statistical measures should be considered alongside domain knowledge when selecting .
Perform one K-means update for the one-dimensional observations using and initial centroids and .
Assignment step: Compare each observation with the initial centroids and .
- , , and are closer to , so .
- , , and are closer to , so .
Update step: Calculate the mean of each cluster:
The updated centroids are therefore and . Repeating the assignment step does not change the clusters, so the algorithm has converged for these initial centroids.
The final within-cluster sum of squares is:
Compare linear regression, KNN, decision trees, random forests, and K-means in terms of learning type, output, and suitable applications.
- Linear regression: A supervised method for predicting continuous values. It is appropriate when relationships are reasonably linear and coefficient interpretation is useful.
- KNN: A supervised, distance-based method supporting both classification and regression. It is useful for smaller scaled datasets with meaningful local neighbourhoods.
- Decision tree: A supervised method supporting classification and regression. It is suitable when nonlinear rules, interactions, and model interpretability are important.
- Random forest: A supervised ensemble of decision trees supporting classification and regression. It is useful for robust prediction on complex tabular data.
- K-means: An unsupervised clustering method. It groups unlabelled observations into clusters based on distance from centroids.
The first four methods learn from labelled targets, whereas K-means does not use target labels. Algorithm selection should consider target type, data size, feature scaling, interpretability, computational cost, expected relationship shape, and validation performance.
Define linear regression. Explain the roles of the dependent variable, independent variables, slope, and intercept.
Linear regression is a supervised learning algorithm used to predict a continuous dependent variable from one or more independent variables.
For simple linear regression, the model is:
- Dependent variable (): The continuous value to be predicted.
- Independent variable (): The input feature used to make the prediction.
- Intercept (): The expected value of when .
- Slope (): The expected change in for a one-unit increase in .
- Error term (): The unexplained difference between an observed value and the value predicted by the model.
With several input features, the method is called multiple linear regression.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →