Practice
1.
from sklearn.tree import DecisionTreeClassifier
import numpy as np
X = np.array([[0, 0], [1, 1], [1, 0], [0, 1]])
y = np.array([0, 1, 1, 0])
clf = DecisionTreeClassifier(criterion='gini')
clf.fit(X, y)
print(clf.predict([[0, 0], [1, 1]]))What is the output of the above code?easy
Solution
Step 1: Understand the training data.
The data is perfectly separable, and the decision tree will learn to classify based on the given labels.Step 2: Predict using the trained model.
For input [0, 0], the label is 0, and for [1, 1], the label is 1.Final Answer:
Option C -> Option CQuick Check:
Check training labels for predictions. [OK]
Hint: Check training labels for predictions. [OK]
Common Mistakes:
2. Given the following Python code snippet, what is the output?
import numpy as np
X = np.array([[1, 2], [3, 4]])
Y = X.T
print(Y.shape)easy
Solution
Step 1: Understand the operation.
The code transposes the matrix X, which is a 2x2 matrix.Step 2: Determine the shape after transpose.
The transpose of a 2x2 matrix is still 2x2.Final Answer:
Option C -> Option CQuick Check:
Transpose keeps square matrix shape. [OK]
Hint: Transpose keeps square matrix shape. [OK]
Common Mistakes:
3. Given the following Python code using BPE tokenization, what is the output?
from tokenizers import Tokenizer
from tokenizers.models import BPE
from tokenizers.pre_tokenizers import Whitespace
# Initialize a BPE tokenizer
bpe_tokenizer = Tokenizer(BPE())
bpe_tokenizer.pre_tokenizer = Whitespace()
# Train the tokenizer
bpe_tokenizer.train(["hello world", "hello there"], vocab_size=10)
# Encode a sentence
output = bpe_tokenizer.encode("hello world")
print(output.tokens)easy
Solution
Step 1: Understand BPE tokenization with whitespace pre-tokenization.
BPE with whitespace pre-tokenization will initially split on spaces.Step 2: Analyze the output of the encode method.
The sentence 'hello world' is tokenized as ['hello', 'world'] due to whitespace splitting.Final Answer:
Option D -> Option DQuick Check:
BPE with whitespace splits on spaces first. [OK]
Hint: BPE with whitespace splits on spaces first. [OK]
Common Mistakes:
4. You are choosing between a decision tree and a linear regression model for a dataset with a complex, non-linear relationship. Which model should you choose and why?
medium
Solution
Step 1: Analyze the dataset characteristics
The dataset has a complex, non-linear relationship.Step 2: Choose the appropriate model
Decision trees can capture non-linear relationships, making them suitable for this dataset.Final Answer:
Option C -> Option CQuick Check:
Decision trees capture non-linear patterns. [OK]
Hint: Decision trees capture non-linear patterns. [OK]
Common Mistakes:
5. When designing a CNN for image classification, why might you choose to use a smaller receptive field in the initial layers?
medium
Solution
Step 1: Understand receptive fields
Smaller receptive fields in initial layers help capture fine-grained local features, which are crucial for understanding detailed patterns.Step 2: Eliminate wrong options
To capture more global features early on is incorrect because global features are captured in deeper layers. To reduce the computational cost of the network is incorrect as smaller receptive fields don't necessarily reduce computational cost. To increase the depth of the network without increasing parameters is incorrect because depth and parameters are not directly related to receptive field size.Final Answer:
Option C -> Option CQuick Check:
Smaller fields capture local details. [OK]
Hint: Smaller fields capture local details. [OK]
Common Mistakes:
