# Hack to detect whether we are running by the sphinx builder
return
"__file__"
in
globals
()
Benchmark and plot helper functions
defatomic_benchmark_estimator(estimator,X_test,verbose=False):"""Measure runtime prediction of each instance."""n_instances=X_test.shape[0]runtimes=np.zeros(n_instances,dtype=float)foriinrange(n_instances):instance=X_test[[i],:]start=time.time()estimator.predict(instance)runtimes[i]=time.time()-startifverbose:print("atomic_benchmark runtimes:",min(runtimes),np.percentile(runtimes,50),max(runtimes),)returnruntimesdefbulk_benchmark_estimator(estimator,X_test,n_bulk_repeats,verbose):"""Measure runtime prediction of the whole input."""n_instances=X_test.shape[0]runtimes=np.zeros(n_bulk_repeats,dtype=float)foriinrange(n_bulk_repeats):start=time.time()estimator.predict(X_test)runtimes[i]=time.time()-startruntimes=np.array(list(map(lambdax:x/float(n_instances),runtimes)))ifverbose:print("bulk_benchmark runtimes:",min(runtimes),np.percentile(runtimes,50),max(runtimes),)returnruntimesdefbenchmark_estimator(estimator,X_test,n_bulk_repeats=30,verbose=False):""" Measure runtimes of prediction in both atomic and bulk mode. Parameters ---------- 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. """atomic_runtimes=atomic_benchmark_estimator(estimator,X_test,verbose)bulk_runtimes=bulk_benchmark_estimator(estimator,X_test,n_bulk_repeats,verbose)returnatomic_runtimes,bulk_runtimesdefgenerate_dataset(n_train,n_test,n_features,noise=0.1,verbose=False):"""Generate a regression dataset with the given parameters."""ifverbose:print("generating dataset...")X,y,coef=make_regression(n_samples=n_train+n_test,n_features=n_features,noise=noise,coef=True)random_seed=13X_train,X_test,y_train,y_test=train_test_split(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]gc.collect()ifverbose:print("ok")returnX_train,y_train,X_test,y_testdefboxplot_runtimes(runtimes,pred_type,configuration):""" 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' """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"],)forestimator_confinconfiguration["estimators"]]plt.setp(ax1,xticklabels=cls_infos)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",alpha=0.5)ax1.set_axisbelow(True)ax1.set_title("Prediction Time per Instance - %s, %d feats."%(pred_type.capitalize(),configuration["n_features"]))ax1.set_ylabel("Prediction Time (us)")plt.show()defbenchmark(configuration):"""Run the whole benchmark."""X_train,y_train,X_test,y_test=generate_dataset(configuration["n_train"],configuration["n_test"],configuration["n_features"])stats={}forestimator_confinconfiguration["estimators"]:print("Benchmarking",estimator_conf["instance"])estimator_conf["instance"].fit(X_train,y_train)gc.collect()a,b=benchmark_estimator(estimator_conf["instance"],X_test)stats[estimator_conf["name"]]={"atomic":a,"bulk":b}cls_names=[estimator_conf["name"]forestimator_confinconfiguration["estimators"]]runtimes=[1e6*stats[clf_name]["atomic"]forclf_nameincls_names]boxplot_runtimes(runtimes,"atomic",configuration)runtimes=[1e6*stats[clf_name]["bulk"]forclf_nameincls_names]boxplot_runtimes(runtimes,"bulk (%d)"%configuration["n_test"],configuration)defn_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)) """percentiles=defaultdict(defaultdict)forninn_features:print("benchmarking with %d features"%n)X_train,y_train,X_test,y_test=generate_dataset(n_train,n_test,n)forcls_name,estimatorinestimators.items():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)returnpercentilesdefplot_n_features_influence(percentiles,percentile):fig,ax1=plt.subplots(figsize=(10,6))colors=["r","g","b"]fori,cls_nameinenumerate(percentiles.keys()):x=np.array(sorted(percentiles[cls_name].keys()))y=np.array([percentiles[cls_name][n]forninx])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()defbenchmark_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()forestimator_configinconfiguration["estimators"]:estimator_config["instance"].fit(X_train,y_train)start_time=time.time()n_predictions=0while(time.time()-start_time)<duration_secs:estimator_config["instance"].predict(X_test[[0]])n_predictions+=1throughputs[estimator_config["name"]]=n_predictions/duration_secsreturnthroughputsdefplot_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"],)forestimator_confinconfiguration["estimators"]]cls_values=[throughputs[estimator_conf["name"]]forestimator_confinconfiguration["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.2ax.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()
Benchmark bulk/atomic prediction speed for various regressors