1+
12import warnings
23warnings .filterwarnings ('ignore' )
34from multiprocessing import cpu_count
45
5- # linear models: http://scikit-learn.org/stable/modules/linear_model.html#stochastic-gradient-descent-sgd
6- from sklearn .linear_model import \
7- LinearRegression , Ridge , Lasso , ElasticNet , \
8- Lars , LassoLars , \
9- OrthogonalMatchingPursuit , \
10- BayesianRidge , ARDRegression , \
11- SGDRegressor , \
12- PassiveAggressiveRegressor , \
13- RANSACRegressor , HuberRegressor
14-
6+ from sklearn .linear_model import LinearRegression , Ridge , Lasso , ElasticNet , Lars , LassoLars , OrthogonalMatchingPursuit , BayesianRidge , ARDRegression , SGDRegressor , PassiveAggressiveRegressor , RANSACRegressor , HuberRegressor
157from sklearn .kernel_ridge import KernelRidge
168from sklearn .preprocessing import StandardScaler
17-
18- # svm models: http://scikit-learn.org/stable/modules/svm.html
199from sklearn .svm import SVR , NuSVR , LinearSVR
20-
21- # neighbor models: http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.RadiusNeighborsRegressor.html#sklearn.neighbors.RadiusNeighborsRegressor
2210from sklearn .neighbors import RadiusNeighborsRegressor , KNeighborsRegressor
23-
2411from sklearn .gaussian_process import GaussianProcessRegressor
2512from sklearn .gaussian_process .kernels import RBF , ConstantKernel , DotProduct , WhiteKernel
2613from sklearn .neural_network import MLPRegressor
27-
2814from sklearn .ensemble import AdaBoostRegressor , ExtraTreesRegressor , RandomForestRegressor
2915from sklearn .tree import DecisionTreeRegressor
3016from sklearn .base import BaseEstimator
3117from sklearn .base import ClassifierMixin
3218from sklearn .base import RegressorMixin
3319from sklearn .base import is_classifier
3420
21+
3522from utilities import *
3623from universal_params import *
3724
3825
39-
4026linear_models_n_params = [
4127 (LinearRegression , normalize ),
4228
297283 'criterion' : ['mse' , 'mae' ]})
298284]
299285
300- def run_linear_models (x , y , small = True , normalize_x = True ):
301- return big_loop (linear_models_n_params_small if small else linear_models_n_params ,
302- StandardScaler ().fit_transform (x ) if normalize_x else x , y , isClassification = False )
303-
304- def run_svm_models (x , y , small = True , normalize_x = True ):
305- return big_loop (svm_models_n_params_small if small else svm_models_n_params ,
306- StandardScaler ().fit_transform (x ) if normalize_x else x , y , isClassification = False )
307-
308- def run_neighbor_models (x , y , normalize_x = True ):
309- return big_loop (neighbor_models_n_params ,
310- StandardScaler ().fit_transform (x ) if normalize_x else x , y , isClassification = False )
311-
312- def run_gaussian_models (x , y , normalize_x = True ):
313- return big_loop (gaussianprocess_models_n_params ,
314- StandardScaler ().fit_transform (x ) if normalize_x else x , y , isClassification = False )
315-
316- def run_nn_models (x , y , small = True , normalize_x = True ):
317- return big_loop (nn_models_n_params_small if small else nn_models_n_params ,
318- StandardScaler ().fit_transform (x ) if normalize_x else x , y , isClassification = False )
319-
320- def run_tree_models (x , y , small = True , normalize_x = True ):
321- return big_loop (tree_models_n_params_small if small else tree_models_n_params ,
322- StandardScaler ().fit_transform (x ) if normalize_x else x , y , isClassification = False )
323-
324- def run_all (x , y , small = True , normalize_x = True , n_jobs = cpu_count ()- 1 , brain = False ):
325-
326- all_params = (linear_models_n_params_small if small else linear_models_n_params ) + \
327- (nn_models_n_params_small if small else nn_models_n_params ) + \
328- ([] if small else gaussianprocess_models_n_params ) + \
329- neighbor_models_n_params + \
330- (svm_models_n_params_small if small else svm_models_n_params ) + \
331- (tree_models_n_params_small if small else tree_models_n_params )
332-
333- return big_loop (all_params ,
334- StandardScaler ().fit_transform (x ) if normalize_x else x , y ,
335- isClassification = False , n_jobs = n_jobs , brain = brain )
336-
337286
338287def gen_reg_data (x_mu = 10. , x_sigma = 1. , num_samples = 100 , num_features = 3 , y_formula = sum , y_sigma = 1. ):
339288 x = np .random .normal (x_mu , x_sigma , (num_samples , num_features ))
340289 y = np .apply_along_axis (y_formula , 1 , x ) + np .random .normal (0 , y_sigma , (num_samples ,))
341290 return x , y
342291
292+ def run_all_regressors (x , y , small = True , normalize_x = True , n_jobs = cpu_count ()- 1 , brain = False , test_size = 0.2 , n_splits = 5 , upsample = True , scoring = None , verbose = False ):
293+ all_params = (linear_models_n_params_small if small else linear_models_n_params ) + (nn_models_n_params_small if small else nn_models_n_params ) + ([] if small else gaussianprocess_models_n_params ) + neighbor_models_n_params + (svm_models_n_params_small if small else svm_models_n_params ) + (tree_models_n_params_small if small else tree_models_n_params )
294+ return main_loop (all_params , StandardScaler ().fit_transform (x ) if normalize_x else x , y , isClassification = False , n_jobs = n_jobs , brain = brain )
295+
343296
344297class HungaBungaRegressor (RegressorMixin ):
345- def __init__ (self , brain = False ):
298+ def __init__ (self , brain = False , test_size = 0.2 , n_splits = 5 , random_state = None , upsample = True , scoring = None , verbose = True , normalize_x = True , n_jobs = cpu_count () - 1 ):
346299 self .model = None
347300 self .brain = brain
301+ self .test_size = test_size
302+ self .n_splits = n_splits
303+ self .random_state = random_state
304+ self .upsample = upsample
305+ self .scoring = None
306+ self .verbose = verbose
307+ self .n_jobs = n_jobs
308+ self .normalize_x = normalize_x
309+ super (HungaBungaRegressor , self ).__init__ ()
310+
348311 def fit (self , x , y ):
349- self .model = run_all (x , y , normalize_x = True , brain = self .brain )[0 ]
312+ self .model = run_all_regressors (x , y , normalize_x = self .normalize_x , test_size = self .test_size , n_splits = self .n_splits , upsample = self .upsample , scoring = self .scoring , verbose = self .verbose , brain = self .brain , n_jobs = self .n_jobs )[0 ]
313+
350314 def predict (self , x ):
351315 return self .model .predict (x )
352316
353317
354318if __name__ == '__main__' :
355319 x , y = gen_reg_data (10 , 3 , 100 , 3 , sum , 0.3 )
356- # print run_all(x, y, small=True, normalize_x=True)
357- a = HungaBungaRegressor ()
358- a .fit (x , y )
359- a .predict (x )
320+ mdl = HungaBungaRegressor ()
321+ mdl .fit (x , y )
322+ print (mdl .predict (x ).shape )
0 commit comments