uiz Space

September 2023 term · Machine Learning Practice · BSCS2008

Machine Learning Practice Quiz 1: 29 October 2023 (September 2023 term)

The IIT Madras BS Machine Learning Practice (MLP) Quiz 1 paper sat on 29 Oct 2023, in the September 2023 term: 23 questions for 50 marks in 120 minutes. Every question is below with its answer. Take it as a timed mock test to be marked, or read it through first.

Questions
23
Marks
50
Duration
120 min
MCQ
14
MSQ
4
Numerical
5

Updated

Official paper: IIT M DIPLOMA AN2 EXAM QPD2 29 Oct 2023 · No negative marking.

Question 1

+2 marksOne correct option
  1. A

    The highest score for each student.

  2. B

    A list of subjects sorted by their average scores.

  3. C

    The average score of each student across all subjects.

  4. D

    A DataFrame with the scores of all students for each subject.

Show answer

Correct answer

  • C

    The average score of each student across all subjects.

Question 2

+2 marksOne correct option

You are working on a machine learning project and have received a dataset containing numeric and categorical features. The dataset has some missing values and potential outliers. Given the following data cleaning steps:

  1. Use One-Hot Encoding for categorical variables.
  2. Impute missing values with feature’s mean for numeric features.
  3. Remove duplicates.
  4. Standardize numeric features using Z-score normalization.
  5. Identify and handle outliers using the IQR method.

Which of the following represents the MOST appropriate sequence for preparing the data for a machine learning model?

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • D

Question 3

+2 marksOne correct option

You’re working with a dataset that consists of training data (‘train_data’) and test data (‘test_data’). The dataset contains both numerical and categorical features. You decide to employ a combination of ‘StandardScaler’ (for numerical columns) and ‘OneHotEncoder’ (for categorical columns) from ‘scikit-learn’ using the ‘ColumnTransformer’ utility. Which of the following actions is MOST likely to introduce data leakage or potential modeling issues?

  1. A

    You utilize ‘fit_transform’ on ‘train_data’ and then ‘transform’ on ‘test_data’ using the ‘ColumnTransformer’.

  2. B

    After observing a new category in the test data that was not present in the training data, you set the ‘handle_unknown’ parameter to ’ignore’ in ‘OneHotEncoder’.

  3. C

    You first apply ‘fit’ on the ‘test_data’ and then ‘transform’ on ‘train_data’ using the ‘ColumnTransformer’.

  4. D

    Before using ‘ColumnTransformer’, you independently apply ‘fit_transform’ to ‘train_data’ for both ‘StandardScaler’ and ‘OneHotEncoder’.

Show answer

Correct answer

  • C

    You first apply ‘fit’ on the ‘test_data’ and then ‘transform’ on ‘train_data’ using the ‘ColumnTransformer’.

Question 4

+2 marksOne correct option

You are working on a machine learning project that aims to predict housing prices based on various features of the houses. As the first step, you decide to perform exploratory data analysis and visualize the data to understand its structure and relationships. Which of the following visualization techniques or principles is LEAST likely to provide meaningful insights for this kind of regression problem?

  1. A

    Plotting a heatmap of the correlation matrix to understand the linear relationship between the numeric features.

  2. B

    Using a scatter plot to visualize the relationship between the square footage of a house and its price.

  3. C

    Visualizing the distribution of housing prices using a pie chart.

  4. D

    Creating box plots for housing prices, grouped by the number of bedrooms, to detect outliers and understand the distribution across different categories.

Show answer

Correct answer

  • C

    Visualizing the distribution of housing prices using a pie chart.

Question 5

+2 marksOne correct option
  1. A

    Important parameter in MinMaxScaler was missing while transforming the data.

  2. B

    train_test_split shouldn’t be done while setting random_state parameter.

  3. C

    X_test was transformed incorrectly.

  4. D

    All the steps are correct

Show answer

Correct answer

  • C

    X_test was transformed incorrectly.

Question 6

+2 marksOne correct option

Imagine you’ve loaded a dataset with 1000 samples into a Pandas DataFrame, and each sample has 30 features. Unfortunately, some samples have missing values for a few features, and you want to remove samples with more than 3 null values present. Please select the method to accomplish this task.?

  1. A

    drop(how= 27)

  2. B

    drop(columns=[‘all’])

  3. C

    dropna(thresh = 27)

  4. D

    dropna(how=‘any’)

  5. E

    dropna(thresh=3)

Show answer

Correct answer

  • C

    dropna(thresh = 27)

Question 7

+2 marksOne correct option

Choose the options with respect to the given statements:
Statement1 : To apply various sklearn methods from in series on a column we should use Pipeline.
Statement2 : To apply various sklearn methods on various columns in parallel we should use ColumnTransformer.

  1. A

    Statement 1 False, Statement 2 False

  2. B

    Statement 1 True, Statement 2 False

  3. C

    Statement 1 False, Statement 2 True

  4. D

    Both statements are True

Show answer

Correct answer

  • D

    Both statements are True

Question 8

+2 marksOne correct option

Consider the following code:

python
import numpy as np
from sklearn.model_selection import ShuffleSplit
X = np.array([[24, 13],[19, 18],
[25, 18],[27, 23],
[11, 25],[22, 12],
[27, 16],[17, 25]])
y = np.array([-1, 1, -1, 1, 1, -1, 1, -1])
ss = ShuffleSplit(n_splits=3, test_size=.25, random_state= 42)
for train_index, test_index in ss.split(X):
print(train_index,test_index)

Which of the following may be the correct output of the above code?:

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • B

Question 9

+2 marksOne correct option

Which of the following is likely to be the correct output of the code given below?

python
from sklearn import linear_model
clf = linear_model.Ridge(alpha=0.01)
X= [[1,0], [2, 1], [3, 2]]
y= [1, 2, 3]
clf.fit(X, y)
linear_model.Ridge(alpha=0.01,max_iter=1000, tol=0.0001,fit_intercept=True)
clf.score(X,y)
  1. A

    5

  2. B

    99

  3. C

    0.999

  4. D

    No evaluation metrics is mentioned, hence it will produce error.

Show answer

Correct answer

  • C

    0.999

Question 10

+2 marksOne correct option

You are working on optimizing a machine learning model for predicting the energy efficiency of buildings. To capture potential non-linear relationships between features like floor area, wall area, and roof area, you decide to introduce polynomial features. However, considering the risk of multicollinearity due to the introduction of these polynomial features, you also want to ensure the data is appropriately scaled. You construct the following pipeline:

python
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures, MinMaxScaler
pipeline = Pipeline([
('poly', PolynomialFeatures(degree=2, interaction_only=True)),
('scaler', MinMaxScaler())
])

Given this setup, which of the following statements accurately describes the operation of this pipeline on the training data?

  1. A

    The pipeline will generate polynomial features (including squared terms) and then scale these features to a range between 0 and 1.

  2. B

    The transformed data will consist of the original features, their squares, and interaction terms, all scaled between 0 and 1.

  3. C

    The pipeline scales the original features between 0 and 1, then subsequently generates polynomial combinations including both square terms and interaction terms.

  4. D

    Only interaction terms between features are generated by the pipeline, which are then scaled between 0 and 1, without including the squared terms of individual features.

Show answer

Correct answer

  • D

    Only interaction terms between features are generated by the pipeline, which are then scaled between 0 and 1, without including the squared terms of individual features.

Question 11

+2 marksOne correct option

You’re developing a regression model for predicting house prices based on various attributes of a house. Given that some features might be redundant or irrelevant, you consider Lasso regression to help with feature selection. To determine the most appropriate regularization strength α\alpha, you decide to use LassoCV from scikit-learn. Here’s a part of your implemented code:

python
from sklearn.datasets import make_regression
from sklearn.linear_model import LassoCV
X, y = make_regression(n_samples=400, n_features=25, noise=1.0,
random_state=7)
lasso = LassoCV(cv=10)
lasso.fit(X, y)

Given the nature of Lasso regression and the purpose of the code, which potential benefit are you hoping to achieve?

  1. A

    Optimize the model’s complexity by automatically determining the best α through cross-validation.

  2. B

    Reduce overfitting by incorporating 10-fold cross-validation during model selection.

  3. C

    Make predictions using an ensemble of 10 different Lasso models trained on different subsets of the data.

  4. D

    Maximize the number of features retained in the model, ensuring a complex model representation.

Show answer

Correct answer

  • A

    Optimize the model’s complexity by automatically determining the best α through cross-validation.

Question 12

+3 marksOne correct option

You are working on a regression problem and decide to use the SGDRegressor from scikit-learn. You set up two different regressors with distinct parameter values and train them on the same dataset:

python
from sklearn.linear_model import SGDRegressor
# First SGDRegressor
sgd1 = SGDRegressor(max_iter=1000, tol= None, penalty='none')
sgd1.fit(X_train, y_train)
# Second SGDRegressor
sgd2 = SGDRegressor(max_iter=5, tol=None, penalty='none')
sgd2.fit(X_train, y_train)

Given the configurations above, which SGDRegressor is more likely to underfit the training data?

  1. A

    sgd1

  2. B

    sgd2

Show answer

Correct answer

  • B

    sgd2

Question 13

+2 marksOne or more correct options

Which columns may not be included in the selected data within the code below?

python
from sklearn.feature_selection import VarianceThreshold
data =[[ 95, 0.332, 112, 1, 0.56 ],
[ 146, 0.332, 177, 1, 9.2 ],
[ -96, 0.332, -139, 1, -0.82 ],
[ 116, 0.332, 117, 1, 4.8 ],
[ -87, 0.332, -63, 1, -1.1 ],
[ 5, 0.332, 139, 1, 1.40 ],
[-142, 0.332, -214, 1, -1.31 ],
[ 148, 0.332, 6, 1, -8.6 ],
[ 162, 0.332, 34, 1, -6.5 ],
[ -65, 0.332, -120, 1, -8.3 ],
[ 197, 0.332, 44, 1, -0.76 ]]
vf = VarianceThreshold(threshold=0)
selected_data = vf.fit_transform(data)
selected_data

Select all that apply.

  1. A

    Column indexed at 0

  2. B

    Column indexed at 1

  3. C

    Column indexed at 2

  4. D

    Column indexed at 3

  5. E

    Column indexed at 4

  6. F

    No columns

Show answer

Correct answers

  • B

    Column indexed at 1

  • D

    Column indexed at 3

Question 14

+2 marksOne or more correct options

Which of the following code blocks will correctly take the learning rate as ‘optimal’ ?

Select all that apply.

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • A

Question 15

+3 marksOne or more correct options

While performing exploratory data analysis (EDA) on a dataset, you come across some columns with a high percentage of missing values. Along with that, a few categorical columns have a large number of unique categories. Which of the following actions would typically be a recommended initial approach during EDA? (Choose multiple correct options.)

Select all that apply.

  1. A

    Visualizing the data distribution of columns to understand their characteristics.

  2. B

    Using dimensionality reduction techniques, like PCA, to handle columns with many unique categories.

  3. C

    Visualizing the distribution of missing values across the dataset to ascertain any patterns or systematic missingness.

  4. D

    Removing columns that have more than 90% missing values without any context.

Show answer

Correct answers

  • A

    Visualizing the data distribution of columns to understand their characteristics.

  • C

    Visualizing the distribution of missing values across the dataset to ascertain any patterns or systematic missingness.

  • D

    Removing columns that have more than 90% missing values without any context.

Question 16

+3 marksOne or more correct options

Given the following code snippet involving GridSearchCV for hyperparameter tuning of a LinearRegression model:

python
from sklearn.datasets import make_regression
from sklearn.linear_model import SGDRegressor
from sklearn.model_selection import GridSearchCV
X, y = make_regression(n_samples=200,
n_features=15,
noise=0.5,
random_state=24)
params = {'penalty': ['l1', 'l2'], 'max_iter': [500, 1000]}
reg = GridSearchCV(estimator= SGDRegressor(),
param_grid= params,
scoring= 'neg_mean_squared_error',
refit= True)
reg.fit(X, y)

Select all statements that are TRUE given this code snippet:

Select all that apply.

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answers

  • B
  • C

Question 17

+2 marksNumerical answer

Given the following code snippet, how many unique values will be present in the column ZZ of the resulting DataFrame dfdf?

python
import pandas as pd
# Creating a DataFrame
data = { 'X': ['apple', 'orange', 'apple', 'banana', 'banana', 'orange'],
'Y': [1, 2, 3, 3, 2, 2]}
df = pd.DataFrame(data)
df['Z'] = df['X'] + df['Y'].astype(str)
Show answer

Correct answer: 5

Question 18

+2 marksNumerical answer

What will be the output of the following code ?

python
data = [['apple', 120],
['cherry', 130],
['apple', 122],
['apple', 125],
['grapes', 70]]
from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder(sparse_output=False)
ohe.fit(data)
print(ohe.transform(data).shape[1])
Show answer

Correct answer: 8

Question 19

+2 marksNumerical answer

What will be the output of the following code ?

python
from sklearn.preprocessing import MinMaxScaler, StandardScaler
data = [[0, 5],
[8, 3],
[3, 4],
[7, 2],
[7, 9]]
scaler = StandardScaler()
scaler.fit(data)
print(scaler.var_[0])
Show answer

Correct answer: 9.2

Question 20

+2 marksNumerical answer

You’re using GridSearchCV to optimize a Ridge regression model from scikit-learn. Consider the following hyperparameter grid:

python
from sklearn.linear_model import Ridge
from sklearn.model_selection import GridSearchCV
param_grid = {
'alpha': [0.001, 0.01, 0.1, 1, 10, 100],
'fit_intercept': [True, False],
'solver': ['auto', 'lsqr', 'sag']
}
grid_search = GridSearchCV(Ridge(), param_grid, cv=5)

How many combinations will GridSearchCV evaluate?

Show answer

Correct answer: 36

Question 21

+3 marksNumerical answer

For LinearRegression with equation Y=W0X0+W1X1+W2X2+ϵY = W_0X_0+W_1X_1+W_2X2+\epsilon and given that W2=57∗W1W_2 = \frac{5}{7} * W_1 and ϵ=0\epsilon = 0. What will be the value of the W1W_1 for the below code? (Write 3 digits after the decimal)

Where X1X_1 and X2X_2 are column1 and column2 respectively and W1W_1 and W2W_2 are weights associated to the respected columns while fitting

python
from sklearn.linear_model import LinearRegression
X_train = [[0,0], [2,1.43], [4,2.86], [6,4.29]]
y_train = [0,1,2,3]
reg = LinearRegression(fit_intercept=False) #intercept=0
reg.fit(X_train,y_train)
print(reg.coef_[0])
Show answer

Correct answer: 0.33 (accepted within ±0.003)

Question 22

+2 marksOne correct option

Go through the code snippet given below and answer the given subquestions.

python
from sklearn.linear_model import SGDRegressor
from sklearn.pipeline import make_pipeline
n_samples, n_features = 18, 4
rng = np.random.RandomState(0)
y = rng.randn(n_samples)
X = rng.randn(n_samples, n_features)
reg = SGDRegressor(max_iter=1000,
tol=1e-3,
eta0= 0.04,
power_t=5,
n_iter_no_change=3,
validation_fraction=0.3 ,
random_state=42)
reg.fit(X, y)
print(reg.coef_)

Which of the following options will be the output of the given code?

  1. A

    [-0.02634908 0.01189399 0.0917284 0.08966849]

  2. B

    array([-0.22622766, -0.00582008, -0.1820344 , 0.03518086, -0.14490955])

  3. C

    array([-0.22622766, -0.00582008, -0.1820344 ])

  4. D

    Given code will return an error because the data set is not given.

Show answer

Correct answer

  • A

    [-0.02634908 0.01189399 0.0917284 0.08966849]

Question 23

+2 marksOne correct option

Go through the code snippet given below and answer the given subquestions.

python
from sklearn.linear_model import SGDRegressor
from sklearn.pipeline import make_pipeline
n_samples, n_features = 18, 4
rng = np.random.RandomState(0)
y = rng.randn(n_samples)
X = rng.randn(n_samples, n_features)
reg = SGDRegressor(max_iter=1000,
tol=1e-3,
eta0= 0.04,
power_t=5,
n_iter_no_change=3,
validation_fraction=0.3 ,
random_state=42)
reg.fit(X, y)
print(reg.coef_)

Which of the following could be the possible output of print(reg.score())?

  1. A

    -0.528

  2. B

    1

  3. C

    0.528

  4. D

    Given code will return an error

Show answer

Correct answer

  • D

    Given code will return an error