scikit-learn/examples/applications/plot_prediction_latency.py

312 lines
11 KiB
Python
Raw Normal View History

2013-10-04 14:44:20 +08:00
"""
==================
Prediction Latency
==================
This is an example showing the prediction latency of various scikit-learn
estimators.
2013-10-10 14:53:17 +08:00
The goal is to measure the latency one can expect when doing predictions
either in bulk or atomic (i.e. one by one) mode.
The plots represent the distribution of the prediction latency as a boxplot.
2013-10-04 14:44:20 +08:00
"""
# Authors: Eustache Diemert <eustache@diemert.fr>
# License: BSD 3 clause
from __future__ import print_function
2013-10-28 16:41:23 +08:00
from collections import defaultdict
2013-10-04 14:44:20 +08:00
import time
import gc
import numpy as np
2013-10-04 15:26:14 +08:00
import matplotlib.pyplot as plt
2013-10-04 14:44:20 +08:00
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
2013-10-04 14:44:20 +08:00
from sklearn.datasets.samples_generator import make_regression
2013-10-04 15:26:14 +08:00
from sklearn.ensemble.forest import RandomForestRegressor
2013-10-04 14:44:20 +08:00
from sklearn.linear_model.ridge import Ridge
from sklearn.linear_model.stochastic_gradient import SGDRegressor
2013-11-08 18:32:15 +08:00
from sklearn.svm.classes import SVR
from sklearn.utils import shuffle
2013-10-04 14:44:20 +08:00
def _not_in_sphinx():
# Hack to detect whether we are running by the sphinx builder
return '__file__' in globals()
2013-10-04 15:26:14 +08:00
2013-10-07 18:07:05 +08:00
def atomic_benchmark_estimator(estimator, X_test, verbose=False):
2013-10-10 14:53:17 +08:00
"""Measure runtime prediction of each instance."""
2013-10-04 15:26:14 +08:00
n_instances = X_test.shape[0]
runtimes = np.zeros(n_instances, dtype=np.float)
for i in range(n_instances):
instance = X_test[[i], :]
2013-10-04 15:26:14 +08:00
start = time.time()
estimator.predict(instance)
runtimes[i] = time.time() - start
if verbose:
print("atomic_benchmark runtimes:", min(runtimes), np.percentile(
2013-10-04 15:26:14 +08:00
runtimes, 50), max(runtimes))
2013-10-07 18:07:05 +08:00
return runtimes
2013-10-04 15:26:14 +08:00
2013-10-07 18:07:05 +08:00
def bulk_benchmark_estimator(estimator, X_test, n_bulk_repeats, verbose):
2013-10-10 14:53:17 +08:00
"""Measure runtime prediction of the whole input."""
2013-10-04 15:26:14 +08:00
n_instances = X_test.shape[0]
runtimes = np.zeros(n_bulk_repeats, dtype=np.float)
for i in range(n_bulk_repeats):
start = time.time()
estimator.predict(X_test)
runtimes[i] = time.time() - start
runtimes = np.array(list(map(lambda x: x / float(n_instances), runtimes)))
2013-10-04 15:26:14 +08:00
if verbose:
print("bulk_benchmark runtimes:", min(runtimes), np.percentile(
2013-10-04 15:26:14 +08:00
runtimes, 50), max(runtimes))
2013-10-07 18:07:05 +08:00
return runtimes
2013-10-04 15:26:14 +08:00
2013-10-07 18:07:05 +08:00
def benchmark_estimator(estimator, X_test, n_bulk_repeats=30, verbose=False):
2013-10-04 14:44:20 +08:00
"""
2013-10-10 14:53:17 +08:00
Measure runtimes of prediction in both atomic and bulk mode.
2013-10-04 14:44:20 +08:00
Parameters
----------
2013-10-10 14:53:17 +08:00
estimator : already trained estimator supporting `predict()`
X_test : test input
n_bulk_repeats : how many times to repeat when evaluating bulk mode
Returns
-------
atomic_runtimes, bulk_runtimes : a pair of `np.array` which contain the
runtimes in seconds.
2013-10-04 14:44:20 +08:00
"""
2013-10-07 18:07:05 +08:00
atomic_runtimes = atomic_benchmark_estimator(estimator, X_test, verbose)
bulk_runtimes = bulk_benchmark_estimator(estimator, X_test, n_bulk_repeats,
verbose)
return atomic_runtimes, bulk_runtimes
2013-10-04 14:44:20 +08:00
2013-10-10 14:53:17 +08:00
2013-10-04 14:44:20 +08:00
def generate_dataset(n_train, n_test, n_features, noise=0.1, verbose=False):
2013-10-10 14:53:17 +08:00
"""Generate a regression dataset with the given parameters."""
2013-10-04 14:44:20 +08:00
if verbose:
print("generating dataset...")
2013-10-04 14:44:20 +08:00
X, y, coef = make_regression(n_samples=n_train + n_test,
n_features=n_features, noise=noise, coef=True)
random_seed = 13
X_train, X_test, y_train, y_test = train_test_split(
2018-07-17 13:08:04 +08:00
X, y, train_size=n_train, test_size=n_test, random_state=random_seed)
X_train, y_train = shuffle(X_train, y_train, random_state=random_seed)
X_scaler = StandardScaler()
X_train = X_scaler.fit_transform(X_train)
X_test = X_scaler.transform(X_test)
y_scaler = StandardScaler()
y_train = y_scaler.fit_transform(y_train[:, None])[:, 0]
y_test = y_scaler.transform(y_test[:, None])[:, 0]
2013-10-04 14:44:20 +08:00
gc.collect()
if verbose:
print("ok")
return X_train, y_train, X_test, y_test
2013-10-10 14:53:17 +08:00
def boxplot_runtimes(runtimes, pred_type, configuration):
2013-10-10 14:53:17 +08:00
"""
Plot a new `Figure` with boxplots of prediction runtimes.
Parameters
----------
runtimes : list of `np.array` of latencies in micro-seconds
cls_names : list of estimator class names that generated the runtimes
pred_type : 'bulk' or 'atomic'
2013-10-10 14:57:26 +08:00
2013-10-10 14:53:17 +08:00
"""
2015-03-03 07:00:58 +08:00
2013-10-07 18:07:05 +08:00
fig, ax1 = plt.subplots(figsize=(10, 6))
bp = plt.boxplot(runtimes, )
cls_infos = ['%s\n(%d %s)' % (estimator_conf['name'],
estimator_conf['complexity_computer'](
estimator_conf['instance']),
estimator_conf['complexity_label']) for
estimator_conf in configuration['estimators']]
2015-03-03 07:00:58 +08:00
plt.setp(ax1, xticklabels=cls_infos)
2013-10-07 18:07:05 +08:00
plt.setp(bp['boxes'], color='black')
plt.setp(bp['whiskers'], color='black')
plt.setp(bp['fliers'], color='red', marker='+')
ax1.yaxis.grid(True, linestyle='-', which='major', color='lightgrey',
2013-10-10 14:53:17 +08:00
alpha=0.5)
2013-10-07 18:07:05 +08:00
ax1.set_axisbelow(True)
2013-10-28 16:41:23 +08:00
ax1.set_title('Prediction Time per Instance - %s, %d feats.' % (
pred_type.capitalize(),
configuration['n_features']))
2013-10-07 18:07:05 +08:00
ax1.set_ylabel('Prediction Time (us)')
plt.show()
def benchmark(configuration):
2013-10-10 14:53:17 +08:00
"""Run the whole benchmark."""
X_train, y_train, X_test, y_test = generate_dataset(
configuration['n_train'], configuration['n_test'],
configuration['n_features'])
stats = {}
for estimator_conf in configuration['estimators']:
print("Benchmarking", estimator_conf['instance'])
estimator_conf['instance'].fit(X_train, y_train)
2013-10-07 18:07:05 +08:00
gc.collect()
a, b = benchmark_estimator(estimator_conf['instance'], X_test)
stats[estimator_conf['name']] = {'atomic': a, 'bulk': b}
2013-10-07 18:07:05 +08:00
cls_names = [estimator_conf['name'] for estimator_conf in configuration[
'estimators']]
2013-10-10 14:57:26 +08:00
runtimes = [1e6 * stats[clf_name]['atomic'] for clf_name in cls_names]
boxplot_runtimes(runtimes, 'atomic', configuration)
2013-10-10 14:57:26 +08:00
runtimes = [1e6 * stats[clf_name]['bulk'] for clf_name in cls_names]
boxplot_runtimes(runtimes, 'bulk (%d)' % configuration['n_test'],
configuration)
2013-10-28 16:41:23 +08:00
def n_feature_influence(estimators, n_train, n_test, n_features, percentile):
"""
Estimate influence of the number of features on prediction time.
Parameters
----------
estimators : dict of (name (str), estimator) to benchmark
n_train : nber of training instances (int)
n_test : nber of testing instances (int)
n_features : list of feature-space dimensionality to test (int)
percentile : percentile at which to measure the speed (int [0-100])
Returns:
--------
percentiles : dict(estimator_name,
dict(n_features, percentile_perf_in_us))
"""
2013-10-28 16:41:23 +08:00
percentiles = defaultdict(defaultdict)
for n in n_features:
2013-10-28 16:41:23 +08:00
print("benchmarking with %d features" % n)
X_train, y_train, X_test, y_test = generate_dataset(n_train, n_test, n)
for cls_name, estimator in estimators.items():
2013-10-28 16:41:23 +08:00
estimator.fit(X_train, y_train)
gc.collect()
runtimes = bulk_benchmark_estimator(estimator, X_test, 30, False)
percentiles[cls_name][n] = 1e6 * np.percentile(runtimes,
percentile)
2013-10-28 16:41:23 +08:00
return percentiles
def plot_n_features_influence(percentiles, percentile):
fig, ax1 = plt.subplots(figsize=(10, 6))
colors = ['r', 'g', 'b']
for i, cls_name in enumerate(percentiles.keys()):
x = np.array(sorted([n for n in percentiles[cls_name].keys()]))
2013-10-28 16:41:23 +08:00
y = np.array([percentiles[cls_name][n] for n in x])
plt.plot(x, y, color=colors[i], )
ax1.yaxis.grid(True, linestyle='-', which='major', color='lightgrey',
alpha=0.5)
ax1.set_axisbelow(True)
ax1.set_title('Evolution of Prediction Time with #Features')
ax1.set_xlabel('#Features')
ax1.set_ylabel('Prediction Time at %d%%-ile (us)' % percentile)
plt.show()
2013-10-07 18:07:05 +08:00
def benchmark_throughputs(configuration, duration_secs=0.1):
"""benchmark throughput for different estimators."""
X_train, y_train, X_test, y_test = generate_dataset(
configuration['n_train'], configuration['n_test'],
configuration['n_features'])
throughputs = dict()
for estimator_config in configuration['estimators']:
estimator_config['instance'].fit(X_train, y_train)
start_time = time.time()
n_predictions = 0
while (time.time() - start_time) < duration_secs:
estimator_config['instance'].predict(X_test[[0]])
n_predictions += 1
throughputs[estimator_config['name']] = n_predictions / duration_secs
return throughputs
def plot_benchmark_throughput(throughputs, configuration):
fig, ax = plt.subplots(figsize=(10, 6))
colors = ['r', 'g', 'b']
cls_infos = ['%s\n(%d %s)' % (estimator_conf['name'],
estimator_conf['complexity_computer'](
estimator_conf['instance']),
estimator_conf['complexity_label']) for
estimator_conf in configuration['estimators']]
cls_values = [throughputs[estimator_conf['name']] for estimator_conf in
configuration['estimators']]
plt.bar(range(len(throughputs)), cls_values, width=0.5, color=colors)
ax.set_xticks(np.linspace(0.25, len(throughputs) - 0.75, len(throughputs)))
ax.set_xticklabels(cls_infos, fontsize=10)
ymax = max(cls_values) * 1.2
ax.set_ylim((0, ymax))
ax.set_ylabel('Throughput (predictions/sec)')
ax.set_title('Prediction Throughput for different estimators (%d '
'features)' % configuration['n_features'])
plt.show()
# #############################################################################
# Main code
start_time = time.time()
# #############################################################################
# Benchmark bulk/atomic prediction speed for various regressors
configuration = {
'n_train': int(1e3),
'n_test': int(1e2),
'n_features': int(1e2),
'estimators': [
{'name': 'Linear Model',
'instance': SGDRegressor(penalty='elasticnet', alpha=0.01,
l1_ratio=0.25, fit_intercept=True,
tol=1e-4),
'complexity_label': 'non-zero coefficients',
2014-01-06 21:45:40 +08:00
'complexity_computer': lambda clf: np.count_nonzero(clf.coef_)},
{'name': 'RandomForest',
'instance': RandomForestRegressor(n_estimators=100),
'complexity_label': 'estimators',
'complexity_computer': lambda clf: clf.n_estimators},
{'name': 'SVR',
'instance': SVR(kernel='rbf'),
'complexity_label': 'support vectors',
'complexity_computer': lambda clf: len(clf.support_vectors_)},
]
}
benchmark(configuration)
# benchmark n_features influence on prediction speed
percentile = 90
percentiles = n_feature_influence({'ridge': Ridge()},
configuration['n_train'],
configuration['n_test'],
[100, 250, 500], percentile)
plot_n_features_influence(percentiles, percentile)
# benchmark throughput
throughputs = benchmark_throughputs(configuration)
plot_benchmark_throughput(throughputs, configuration)
2013-10-28 16:41:23 +08:00
stop_time = time.time()
print("example run in %.2fs" % (stop_time - start_time))