Bird
Raised Fist0
ML Pythonml~20 mins

Simple neural network with scikit-learn in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Neural Network Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of training a simple neural network
What will be the printed output of the training accuracy after fitting this neural network on the given data?
ML Python
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

model = MLPClassifier(hidden_layer_sizes=(5,), max_iter=500, random_state=1)
model.fit(X_train, y_train)

print(f"Training accuracy: {model.score(X_train, y_train):.2f}")
ATraining accuracy: 1.00
BTraining accuracy: 0.50
CTraining accuracy: 0.75
DTraining accuracy: 0.00
Attempts:
2 left
💡 Hint
The model is trained with enough iterations on a simple dataset, so it should fit well.
❓ Model Choice
intermediate
1:30remaining
Choosing the correct hidden layer size
Which hidden_layer_sizes parameter will create a neural network with two hidden layers, the first with 10 neurons and the second with 5 neurons?
A(5, 10)
B(10, 5)
C10
D(15,)
Attempts:
2 left
💡 Hint
The tuple defines the number of neurons per hidden layer in order.
❓ Hyperparameter
advanced
1:30remaining
Effect of max_iter on training
What will happen if max_iter is set too low (e.g., max_iter=1) when training an MLPClassifier on a dataset?
AThe model will overfit and training accuracy will be very high.
BThe model will raise a runtime error and stop.
CThe model will train normally with no effect on accuracy.
DThe model will likely underfit and training accuracy will be low.
Attempts:
2 left
💡 Hint
Training too few iterations means the model does not learn enough.
❓ Metrics
advanced
1:30remaining
Interpreting loss_curve_ attribute
What does the loss_curve_ attribute of a trained MLPClassifier represent?
AA list of loss values at each iteration during training.
BThe final loss value after training completes.
CThe weights of the neural network layers.
DThe accuracy values on the test set during training.
Attempts:
2 left
💡 Hint
Think about what 'curve' means in this context.
🔧 Debug
expert
2:00remaining
Identifying error in neural network code
What error will this code raise when run? from sklearn.neural_network import MLPClassifier model = MLPClassifier(hidden_layer_sizes=5, max_iter=200) model.fit([[0,0],[1,1]], [0,1])
ML Python
from sklearn.neural_network import MLPClassifier

model = MLPClassifier(hidden_layer_sizes=5, max_iter=200)
model.fit([[0,0],[1,1]], [0,1])
AValueError: hidden_layer_sizes must be a tuple
BTypeError: 'int' object is not iterable
CNo error, code runs successfully
DAttributeError: 'MLPClassifier' object has no attribute 'fit'
Attempts:
2 left
💡 Hint
scikit-learn accepts integers for hidden_layer_sizes, converting to a single-layer tuple.

Practice

(1/5)
1. What is the main purpose of using MLPClassifier in scikit-learn?
easy
A. To perform data scaling and normalization
B. To create and train a simple neural network for classification tasks
C. To visualize data distributions
D. To split data into training and testing sets

Solution

  1. Step 1: Understand the role of MLPClassifier

    MLPClassifier is a tool in scikit-learn designed to build and train neural networks for classification problems.
  2. Step 2: Compare options with MLPClassifier's purpose

    The other options describe tasks like scaling, visualization, and data splitting, which are not the main function of MLPClassifier.
  3. Final Answer:

    To create and train a simple neural network for classification tasks -> Option B
  4. Quick Check:

    MLPClassifier = neural network classifier [OK]
Hint: MLPClassifier builds neural nets for classification [OK]
Common Mistakes:
  • Confusing MLPClassifier with data preprocessing tools
  • Thinking it is for regression only
  • Assuming it splits data automatically
2. Which of the following is the correct way to import the MLPClassifier from scikit-learn?
easy
A. import neural_network.MLPClassifier from sklearn
B. import MLPClassifier from sklearn.network
C. from sklearn.ml import MLPClassifier
D. from sklearn.neural_network import MLPClassifier

Solution

  1. Step 1: Recall the correct module path for MLPClassifier

    MLPClassifier is located in the sklearn.neural_network module.
  2. Step 2: Check each import statement for syntax and correctness

    The other options have incorrect module names or import syntax.
  3. Final Answer:

    from sklearn.neural_network import MLPClassifier -> Option D
  4. Quick Check:

    Correct import = from sklearn.neural_network import MLPClassifier [OK]
Hint: MLPClassifier is in sklearn.neural_network module [OK]
Common Mistakes:
  • Using wrong module names like sklearn.ml
  • Incorrect import syntax
  • Confusing neural_network with network
3. What will be the output of the following code snippet?
from sklearn.neural_network import MLPClassifier
X = [[0., 0.], [1., 1.]]
y = [0, 1]
clf = MLPClassifier(hidden_layer_sizes=(2,), max_iter=1000, random_state=1)
clf.fit(X, y)
pred = clf.predict([[2., 2.]])
print(pred)
medium
A. [0]
B. Error due to wrong input shape
C. [1]
D. [2]

Solution

  1. Step 1: Understand the training data and labels

    The model is trained on two points: [0., 0.] labeled 0 and [1., 1.] labeled 1.
  2. Step 2: Predict the label for input [2., 2.]

    Since [2., 2.] is closer to [1., 1.] which has label 1, the model predicts 1.
  3. Final Answer:

    [1] -> Option C
  4. Quick Check:

    Input near class 1 predicts 1 [OK]
Hint: Prediction matches closest trained label [OK]
Common Mistakes:
  • Assuming output is 0 by default
  • Confusing input shape errors
  • Ignoring random_state effect
4. Identify the error in the following code snippet:
from sklearn.neural_network import MLPClassifier
X = [[0, 0], [1, 1]]
y = [0, 1]
clf = MLPClassifier(hidden_layer_sizes=2, max_iter=500)
clf.fit(X, y)
medium
A. hidden_layer_sizes should be a tuple, not an integer
B. max_iter value is too low
C. X and y have mismatched lengths
D. MLPClassifier requires data scaling before fitting

Solution

  1. Step 1: Check the hidden_layer_sizes parameter

    hidden_layer_sizes expects a tuple indicating the number of neurons per hidden layer, e.g., (2,), not an integer.
  2. Step 2: Verify other parameters and data

    max_iter=500 is valid, X and y lengths match, and scaling is recommended but not mandatory.
  3. Final Answer:

    hidden_layer_sizes should be a tuple, not an integer -> Option A
  4. Quick Check:

    hidden_layer_sizes = tuple [OK]
Hint: hidden_layer_sizes must be tuple like (2,) [OK]
Common Mistakes:
  • Passing integer instead of tuple for hidden_layer_sizes
  • Assuming max_iter causes error
  • Thinking scaling is mandatory for fit
5. You want to improve your MLPClassifier model's accuracy on a complex dataset. Which of these changes is most likely to help?
hard
A. Increase the number of neurons in hidden layers and train longer
B. Reduce the number of hidden layers to zero
C. Remove the activation function to speed up training
D. Use fewer training samples to avoid overfitting

Solution

  1. Step 1: Understand model complexity and training

    Increasing neurons and training longer allows the model to learn more complex patterns.
  2. Step 2: Evaluate other options

    Reducing hidden layers to zero removes learning capacity, removing activation breaks non-linearity, and fewer samples usually reduce accuracy.
  3. Final Answer:

    Increase the number of neurons in hidden layers and train longer -> Option A
  4. Quick Check:

    More neurons + training = better learning [OK]
Hint: More neurons and training improve complex learning [OK]
Common Mistakes:
  • Thinking fewer layers always improve accuracy
  • Removing activation functions to simplify
  • Using less data to prevent overfitting blindly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard