Created
November 15, 2013 00:07
-
-
Save jcrudy/7476764 to your computer and use it in GitHub Desktop.
An example showing how to combine py-earth with two of scikit-learn's meta-regressors, AdaBoostRegressor and GridSearchCV.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.ensemble import AdaBoostRegressor | |
from sklearn.metrics import r2_score | |
from sklearn.cross_validation import train_test_split | |
from sklearn.grid_search import GridSearchCV | |
from pyearth import Earth | |
import numpy as np | |
import pandas as pd | |
# Generate a data set | |
np.random.seed(1) | |
n = 10000 | |
X = pd.DataFrame(np.random.uniform(0.,10.,size=n),columns=['x0']) | |
X['x1'] = np.random.binomial(1,.01,size=n) | |
X['x2'] = np.random.uniform(0.,10.,size=n) | |
X['x3'] = np.random.binomial(1,.01,size=n) | |
X['x4'] = np.random.uniform(0.,10.,size=n) | |
y = 10. * np.abs(X['x0'] - 10.) - \ | |
20. * X['x1'] * (np.abs(X['x2']) + X['x3'] * np.abs(X['x4'])) + \ | |
5. * np.random.normal(size=n) | |
# Split into training and testing sets | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5, random_state=1) | |
# Fit the basic Earth model | |
print 'Fitting basic Earth model...' | |
earth_model = Earth().fit(X_train,y_train) | |
print 'Done.' | |
# Fit the boosted Earth model | |
print 'Fitting boosted Earth model...' | |
boosted_earth_model = AdaBoostRegressor(base_estimator=Earth()).fit(X_train,y_train) | |
print 'Done.' | |
# Fit the grid searched Earth model | |
print 'Fitting grid searched Earth model...' | |
param_grid = [{'max_degree': [1,2,3,4], 'allow_linear': [False, True], 'penalty': [0.,1.,2.,3.,4.,5.,6.]}] | |
grid_search_earth_model = GridSearchCV(Earth(),param_grid).fit(X_train, y_train) | |
print 'Done.' | |
print 'Earth Score: %f' % r2_score(y_test, earth_model.predict(X_test)) | |
print 'Boosted Earth Score: %f' % r2_score(y_test, boosted_earth_model.predict(X_test)) | |
print 'Grid Searched Earth Score: %f' % r2_score(y_test, grid_search_earth_model.predict(X_test)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment