uiz Space

January 2024 term · Machine Learning Practice · BSCS2008

Machine Learning Practice Quiz 2: 24 March 2024 (January 2024 term)

The IIT Madras BS Machine Learning Practice (MLP) Quiz 2 paper sat on 24 Mar 2024, in the January 2024 term: 22 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
22
Marks
50
Duration
120 min
MCQ
12
MSQ
6
Numerical
4

Updated

Official paper: IIT M DIPLOMA AN EXAM QDD2 24 Mar 2024 · No negative marking.

Question 1

+2 marksOne correct option

Consider following code snippet:

python
from sklearn.utils.multiclass import type_of_target
import numpy as np
print(type_of_target(np.array([['horror','fantasy'],
['adventure','fantasy'],
['adventure','fantasy']])))
print(type_of_target([72, 17.89, 63.00]))
print(type_of_target([0, 1, 1, 0]))

What will be the output of the above code snippet in the correct sequence?

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

Correct answer

  • D

Question 2

+2 marksOne correct option
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • A

Question 3

+2 marksOne correct option

Consider a binary classification dataset with labeled as 98% negative samples and 2% positive samples. A model is trained on this data, which of the following evaluation metrics are suitable for measuring effectiveness of this model:

  1. A

    accuracy

  2. B

    Mean Absolute Error

  3. C

    smote

  4. D

    F-1 score

Show answer

Correct answer

  • D

    F-1 score

Question 4

+2 marksOne correct option

Consider the following code block:

python
from sklearn.datasets import make_regression
X, y = make_regression(n_samples = 1000,
n_features = 5,
n_informative = 2,
random_state=42)
from sklearn.linear_model import SGDRegressor
sgd1 = SGDRegressor(alpha=1e-3,
random_state=42,
penalty='________________', )
sgd1.fit(X, y)
print(sgd1.coef_)
sgd2 = SGDRegressor(alpha=1e-3,
random_state=42,
penalty='_________________')
sgd2.fit(X, y)
print(sgd2.coef_)

What are the most suitable values to be filled in the two blank spaces (in that order) in the code to expect the following output?:

[ 1.68059576e+01, 1.89752021e+01, 7.49212536e-04, -6.53455275e-04, 3.01471918e-04]

[16.82258106, 18.99248887, 0., 0., 0.]

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

Correct answer

  • C

Question 5

+2 marksOne correct option
  1. A

    0.00

  2. B

    0.50

  3. C

    0.72

  4. D

    1.00

Show answer

Correct answer

  • B

    0.50

Question 6

+2 marksOne correct option

You’re building a machine learning pipeline to preprocess data and train a model on a classification task. You decide to use a pipeline that includes data preprocessing and a support vector machine (SVM) classifier. The following code snippet demonstrates the pipeline creation and usage:

python
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
import numpy as np
# Simulated data (features: X, target: y)
X = np.array([[2, 3], [5, 7], [8, 10]])
y = np.array([0, 1, 0])
# Create a pipeline with StandardScaler and SVM classifier
pipeline = Pipeline([('scaler', StandardScaler()),
('svm', SVC())])
# Fit the pipeline on training data
pipeline.fit(X, y)
# Make predictions using the trained pipeline
predictions = pipeline.predict(X)

What is the purpose of using the pipeline in this code snippet?

  1. A

    The pipeline combines multiple models for better model performance.

  2. B

    The pipeline allows for simultaneous training of the scaler and classifier.

  3. C

    The pipeline simplifies the code by encapsulating preprocessing and modeling steps.

  4. D

    The pipeline ensures that only linear SVM can be used for this classification task.

Show answer

Correct answer

  • C

    The pipeline simplifies the code by encapsulating preprocessing and modeling steps.

Question 7

+2 marksOne correct option

Given below code to load a huge file name as filename.csv and this file is not loading at once in the system which parameter should be added to pd.read_csv to load this file ?

python
import pandas as pd
from sklearn.linear_model import SGDRegressor
for train_df in pd.read_csv("filename.csv", __________=1024):
X = train_df.iloc[:, :-1]
y = train_df.iloc[:, -1]
model = SGDRegressor()
model.partial_fit(prep_X,y)
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • C

Question 8

+2 marksOne correct option

What will the output for below code

python
from sklearn.feature_extraction.text import CountVectorizer
corpus = [ 'This is the first document.',
'This document is the second document.']
vectorizer = CountVectorizer()
vectorizer.fit_transform(corpus)
print(vectorizer.get_feature_names_out())
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • C

Question 9

+2 marksOne correct option

Imagine you're training a Perceptron using sklearn with the following code:

python
from sklearn.linear_model import Perceptron
X = [[0, 0.5], [1, 1.5], [1, 2], [2, 3]]
y = [-1, -1, 1, 1]
clf = Perceptron(eta0 = 1, tol=None, shuffle=True, random_state=42)
clf.fit(X, y)
iterations = clf.n_iter_

Given the linearly separable nature of the data, how many iterations would it most likely take for the perceptron to converge? What will be the value of iterations?

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

Correct answer

  • C

Question 10

+2 marksOne correct option

Consider the following code snippet using scikit-learn:

python
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
pipeline = Pipeline([('scaler', StandardScaler()),
('classifier', SVC())])
param_grid = {'scaler__with_mean': [True, False],
'classifier__C': [0.1, 1, 10],
'classifier__kernel': ['linear', 'rbf'],
'classifier__gamma': [0.1, 1, 10]}
grid_search = GridSearchCV(estimator= pipeline,
param_grid= param_grid,
cv=5,
scoring='accuracy',
verbose=2)
grid_search.fit(X_train, y_train)

Assuming that X_train and y_train are given and the features are not sparse, which of the following statements about the given code is correct?

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

Correct answer

  • B

Question 11

+2 marksOne correct option

Consider the following code snippet that employs LogisticRegression from sklearn on a feature matrix X and corresponding label vector y:

python
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(class_weight='balanced', C=0.5)
model.fit(X, y)

Given the code above, which of the following statements is true?

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

Correct answer

  • D

Question 12

+2 marksOne correct option

Which of the following is true for a hard margin SVM algorithm ?

  1. A

    It does not create hyperplanes as a classification decision boundary

  2. B

    It is robust to outliers

  3. C

    It will correctly classify all the datapoints if the data is linearly separable.

  4. D

    It is mostly used for clustering the data

Show answer

Correct answer

  • C

    It will correctly classify all the datapoints if the data is linearly separable.

Question 13

+3 marksOne or more correct options

Which of the following is/are correct regarding RadiusNeighborsClassifier

Select all that apply.

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

Correct answers

  • B
  • C

Question 14

+3 marksOne or more correct options

Which of the following is correct?

Select all that apply.

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

Correct answers

  • A
  • C

Question 15

+3 marksOne or more correct options

Which of the following option(s) are correct for the precision-recall curve

Select all that apply.

  1. A

    A high area under the curve represents both high recall and high precision.

  2. B

    The precision-recall curve shows the trade-off between precision and recall for different threshold values.

  3. C

    The precision-recall curve used to evaluate unsupervised algorithm for imbalanced clustered data.

  4. D

    None of these

Show answer

Correct answers

  • A

    A high area under the curve represents both high recall and high precision.

  • B

    The precision-recall curve shows the trade-off between precision and recall for different threshold values.

Question 16

+3 marksOne or more correct options

Which of the following statements are true?

Select all that apply.

  1. A

    KNeighborsClassifier with low values of n_neighbors produces complex decision boundaries.

  2. B

    KNeighborsClassifier with low values of n_neighbors produces smooth decision boundaries.

  3. C

    In KNeighborsClassifier the scale of the features(columns) can impact the decision boundaries.

  4. D

    None of these

Show answer

Correct answers

  • A

    KNeighborsClassifier with low values of n_neighbors produces complex decision boundaries.

  • C

    In KNeighborsClassifier the scale of the features(columns) can impact the decision boundaries.

Question 17

+3 marksOne or more correct options

Select all that apply.

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

Correct answers

  • C
  • D

Question 18

+2 marksOne or more correct options

Select all that apply.

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

Correct answers

  • A
  • D

Question 19

+2 marksNumerical answer

Consider the following code snippet:

python
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import FeatureUnion
X = load_iris().data # X.shape = (150,4)
poly_feature = PolynomialFeatures(degree=2, include_bias=True)
union = FeatureUnion([('poly', poly_feature),
('pca', PCA(n_components=2))])
X_transformed = union.fit_transform(X)
print(X_transformed.shape)

If the shape of X is (150,4)(150, 4). How many total columns are there in the X_transformed ?

Show answer

Correct answer: 17

Question 20

+2 marksNumerical answer

Please consider the following data and code for a regression problem with symbols in mind:

  • >>>: Represents input code
  • # : Represents comment in a code
  • ... : Represents code continuation
  • Without any symbols at the beginning of a line then it is output of just above input line of code.
python
>>> import pandas as pd
>>> from sklearn.preprocessing import OneHotEncoder
>>> from sklearn.linear_model import LinearRegression
>>> data_array = [[19, 'Black', 74],
... [19, 'Blue', 75],
... [19, 'Red', 85],
... [24, 'Black', 70],
... [24, 'Blue', 70],
... [24, 'Red', 89],
... [30, 'Black', 78],
... [30, 'Blue', 76],
... [30, 'Red', 90]]
>>> data = pd.DataFrame(data_array,columns=["Age",
"Car_color",
"Accidents_per_1000_Driver"])
>>> X = data.drop("Accidents_per_1000_Driver", axis=1)
>>> y = data["Accidents_per_1000_Driver"]
>>> ohe = OneHotEncoder(sparse_output=False)
>>> X[['Black', 'Blue', 'Red']] = ohe.fit_transform(X[["Car_color"]])
>>> X.drop("Car_color", axis=1, inplace=True)
>>> lr = LinearRegression().fit(X, y)
>>> print(lr.coef_)
[0.32, -4.55, -4.88, 9.44]
>>> print(lr.intercept_)
70.75

How many Accidents per 1000 Driver predicted by the model for Age 27 and driving a Red car ?

Show answer

Correct answer: 88.8 (accepted within ±0.5)

Question 21

+2 marksNumerical answer

After training a multi-class classifier, you obtain the following confusion matrix. What will be the weighted average of the recall score for each class?

Show answer

Correct answer: 0.305 (accepted within ±0.01)

Question 22

+3 marksNumerical answer

What is the output of the following code?

python
from sklearn.neighbors import KNeighborsClassifier
X = [[2,3], [5,6], [8,9], [10, 11], [15,16], [20,21]]
y = [2, 1, 0, 1, 2, 1]
knn = KNeighborsClassifier (n_neighbors=3,
metric='euclidean',
weights='uniform')
knn.fit (X, y)
print (knn.predict([[8,9]]))
Show answer

Correct answer: 1