|
19 | 19 | def classification(): |
20 | 20 | # Generate a random binary classification problem. |
21 | 21 | X, y = make_classification( |
22 | | - n_samples=500, n_features=10, n_informative=10, random_state=1111, n_classes=2, class_sep=2.5, n_redundant=0 |
| 22 | + n_samples=500, |
| 23 | + n_features=10, |
| 24 | + n_informative=10, |
| 25 | + random_state=1111, |
| 26 | + n_classes=2, |
| 27 | + class_sep=2.5, |
| 28 | + n_redundant=0, |
23 | 29 | ) |
24 | 30 |
|
25 | | - X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=1111) |
| 31 | + X_train, X_test, y_train, y_test = train_test_split( |
| 32 | + X, y, test_size=0.15, random_state=1111 |
| 33 | + ) |
26 | 34 |
|
27 | 35 | model = RandomForestClassifier(n_estimators=10, max_depth=4) |
28 | 36 | model.fit(X_train, y_train) |
29 | 37 |
|
30 | 38 | predictions_prob = model.predict(X_test)[:, 1] |
31 | 39 | predictions = np.argmax(model.predict(X_test), axis=1) |
32 | | - #print(predictions.shape) |
| 40 | + # print(predictions.shape) |
33 | 41 | print("classification, roc auc score: %s" % roc_auc_score(y_test, predictions_prob)) |
34 | 42 | print("classification, accuracy score: %s" % accuracy_score(y_test, predictions)) |
35 | 43 |
|
36 | 44 |
|
37 | 45 | def regression(): |
38 | 46 | # Generate a random regression problem |
39 | 47 | X, y = make_regression( |
40 | | - n_samples=500, n_features=5, n_informative=5, n_targets=1, noise=0.05, random_state=1111, bias=0.5 |
| 48 | + n_samples=500, |
| 49 | + n_features=5, |
| 50 | + n_informative=5, |
| 51 | + n_targets=1, |
| 52 | + noise=0.05, |
| 53 | + random_state=1111, |
| 54 | + bias=0.5, |
| 55 | + ) |
| 56 | + X_train, X_test, y_train, y_test = train_test_split( |
| 57 | + X, y, test_size=0.1, random_state=1111 |
41 | 58 | ) |
42 | | - X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1111) |
43 | 59 |
|
44 | 60 | model = RandomForestRegressor(n_estimators=50, max_depth=10, max_features=3) |
45 | 61 | model.fit(X_train, y_train) |
46 | 62 | predictions = model.predict(X_test) |
47 | | - print("regression, mse: %s" % mean_squared_error(y_test.flatten(), predictions.flatten())) |
| 63 | + print( |
| 64 | + "regression, mse: %s" |
| 65 | + % mean_squared_error(y_test.flatten(), predictions.flatten()) |
| 66 | + ) |
48 | 67 |
|
49 | 68 |
|
50 | 69 | if __name__ == "__main__": |
|
0 commit comments