|
3 | 3 | from sklearn.datasets import make_classification
|
4 | 4 | from sklearn.datasets import make_regression
|
5 | 5 | from sklearn.metrics import roc_auc_score
|
6 |
| -from sklearn.model_selection import train_test_split |
| 6 | +try: |
| 7 | + from sklearn.model_selection import train_test_split |
| 8 | +except ImportError: |
| 9 | + from sklearn.cross_validation import train_test_split |
7 | 10 |
|
8 |
| -from mla.ensemble.gbm import * |
| 11 | +from mla.ensemble.gbm import GradientBoostingClassifier, GradientBoostingRegressor |
9 | 12 | from mla.metrics.metrics import mean_squared_error
|
10 | 13 |
|
11 | 14 | logging.basicConfig(level=logging.DEBUG)
|
12 | 15 |
|
13 | 16 |
|
14 | 17 | def classification():
|
15 | 18 | # Generate a random binary classification problem.
|
16 |
| - X, y = make_classification(n_samples=350, n_features=15, n_informative=10, random_state=1111, n_classes=2, |
| 19 | + X, y = make_classification(n_samples=350, n_features=15, n_informative=10, |
| 20 | + random_state=1111, n_classes=2, |
17 | 21 | class_sep=1., n_redundant=0)
|
18 |
| - X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=1111) |
| 22 | + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, |
| 23 | + random_state=1111) |
19 | 24 |
|
20 |
| - model = GradientBoostingClassifier(n_estimators=50, |
21 |
| - max_depth=4, max_features=8, learning_rate=0.1) |
| 25 | + model = GradientBoostingClassifier(n_estimators=50, max_depth=4, |
| 26 | + max_features=8, learning_rate=0.1) |
22 | 27 | model.fit(X_train, y_train)
|
23 | 28 | predictions = model.predict(X_test)
|
24 | 29 | print(predictions)
|
25 | 30 | print(predictions.min())
|
26 | 31 | print(predictions.max())
|
27 |
| - print('classification, roc auc score: %s' % roc_auc_score(y_test, predictions)) |
| 32 | + print('classification, roc auc score: %s' |
| 33 | + % roc_auc_score(y_test, predictions)) |
28 | 34 |
|
29 | 35 |
|
30 | 36 | def regression():
|
31 | 37 | # Generate a random regression problem
|
32 |
| - X, y = make_regression(n_samples=500, n_features=5, n_informative=5, n_targets=1, noise=0.05, random_state=1111, |
| 38 | + X, y = make_regression(n_samples=500, n_features=5, n_informative=5, |
| 39 | + n_targets=1, noise=0.05, random_state=1111, |
33 | 40 | bias=0.5)
|
34 |
| - X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1111) |
| 41 | + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, |
| 42 | + random_state=1111) |
35 | 43 |
|
36 |
| - model = GradientBoostingRegressor(n_estimators=25, max_depth=5, max_features=3, ) |
| 44 | + model = GradientBoostingRegressor(n_estimators=25, max_depth=5, |
| 45 | + max_features=3, ) |
37 | 46 | model.fit(X_train, y_train)
|
38 | 47 | predictions = model.predict(X_test)
|
39 |
| - print('regression, mse: %s' % mean_squared_error(y_test.flatten(), predictions.flatten())) |
| 48 | + print('regression, mse: %s' |
| 49 | + % mean_squared_error(y_test.flatten(), predictions.flatten())) |
40 | 50 |
|
41 | 51 |
|
42 | 52 | if __name__ == '__main__':
|
|
0 commit comments