Skip to content

GridSearchCV of sklearn.model_selection is slower than its deprecated predecessor from sklearn.grid_search #9619

@normanius

Description

@normanius

Description

The GridSearchCV from module sklearn.model_selection operates slower than the "old" one from sklearn.grid_selection, that has been marked as deprecated.

Steps/Code to Reproduce

The below example has been taken from this thread.

Example:

    import matplotlib.pyplot as plt
    from scipy.stats.distributions import norm
    import time

    # Draw points from a bimodal distribution in 1D
    xgrid = np.linspace(-4.5, 3.5, 1000)
    np.random.seed(0)
    x = np.concatenate([norm(-1, 1.).rvs(400),
                        norm(1, 0.3).rvs(100)])
    pdfTrue = (0.8 * norm(-1, 1).pdf(xgrid) +
               0.2 * norm(1, 0.3).pdf(xgrid))
    fig, ax = plt.subplots()
    ax.hist(x, 30, fc='gray', histtype='stepfilled', alpha=0.3, normed=True)
    ax.set_xlim(-4.5, 3.5)

    from sklearn.neighbors import KernelDensity

    #####################################################
    # HERE SWITCH BETWEEN ONE OF THE GRIDSEARCHCV CLASSES
    #from sklearn.grid_search import GridSearchCV
    from sklearn.model_selection import GridSearchCV
    #####################################################
    
    start = time.time()
    grid = GridSearchCV(KernelDensity(),
                        {'bandwidth': np.linspace(0.1, 1.0, 30)},
                        cv=20) # 20-fold cross-validation
    grid.fit(x[:, None])
    kde = grid.best_estimator_
    pdf = np.exp(kde.score_samples(xgrid[:, None]))
    ax.plot(xgrid, pdf, linewidth=3, alpha=0.5, label='bw=%.2f' % kde.bandwidth)
    print time.time()-start, 'seconds'
    plt.show()

Expected Results

Results are the same, runtime is the same.

Actual Results

Results look the same, but the runtime of the recommended GridSearchCV is 5-6 times slower than the one marked as deprecated.

Versions

Darwin-15.6.0-x86_64-i386-64bit
('Python', '2.7.12 (v2.7.12:d33e0cf91556, Jun 26 2016, 12:10:39) \n[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]')
('NumPy', '1.14.0.dev0+029863e')
('SciPy', '0.18.1')
('Scikit-Learn', '0.19.0')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions