June 2018
Intermediate to advanced
276 pages
6h 26m
English
Let's look at a real-world demonstration of overfitting and underfitting, with scikit-learn. Import the required modules:
import numpy as npimport matplotlib.pyplot as pltfrom sklearn.pipeline import Pipelinefrom sklearn.preprocessing import PolynomialFeaturesfrom sklearn.linear_model import LinearRegressionfrom sklearn.model_selection import cross_val_score
We will now build a small model and visualize the model, the samples, and the true function, to see overfitting and underfitting. We will use the following code:
np.random.seed(0)n_samples = 30degrees = [1, 4, 15]X = np.sort(np.random.rand(n_samples))y = np.cos(1.5 * np.pi * X) + np.random.randn(n_samples) * 0.1plt.figure(figsize=(14, 5))for i ...
Read now
Unlock full access