Related questions
question 5
1. Set a random seed for reproducibility.
2. Generate a regression dataset with 250 examples:
a. Generate 250 random values for input (X) uniformly drawn from the range (0, 2).
b. Add Gaussian noise with zero mean and standard deviation 0.04 to create the output (y) using the formula: y = X^3 - 3*X^2 + 2*X + ε.
3. Split the dataset into a train set (150 examples) and a test set (100 examples) using train_test_split.
4. Define a K-NN regression model with K=5 and weighted by the inverse of distance.
5. Train the K-NN model on the train set.
6. Make predictions with the K-NN model on the test set.
7. Compute the Mean Squared Error (MSE) for the K-NN model by comparing its predictions to the true values in the test set.
8. Visualize the K-NN model:
a. Generate 100 evenly spaced points between 0 and 2.
b. Use the trained K-NN model to predict the outputs for these points.
c. Plot the train set, test set, and K-NN predictions on the same graph.
9. Train a Decision Tree regression model on the train set using sklearn's default settings.
10. Make predictions with the Decision Tree model on the test set.
11. Compute the MSE for the Decision Tree model on the test set.
12. Visualize the Decision Tree model in a manner similar to step 8.
13. Compare the MSE values of the K-NN and Decision Tree models.
a. If K-NN's MSE is lower, print "K-NN performs better on the dataset."
b. Otherwise, print "Decision Tree performs better on the dataset."
Step by stepSolved in 4 steps with 5 images