| 14 | |
| 15 | |
| 16 | def classification(): |
| 17 | # Generate a random binary classification problem. |
| 18 | X, y = make_classification( |
| 19 | n_samples=1200, |
| 20 | n_features=10, |
| 21 | n_informative=5, |
| 22 | random_state=1111, |
| 23 | n_classes=2, |
| 24 | class_sep=1.75, |
| 25 | ) |
| 26 | # Convert y to {-1, 1} |
| 27 | y = (y * 2) - 1 |
| 28 | X_train, X_test, y_train, y_test = train_test_split( |
| 29 | X, y, test_size=0.2, random_state=1111 |
| 30 | ) |
| 31 | |
| 32 | for kernel in [RBF(gamma=0.1), Linear()]: |
| 33 | model = SVM(max_iter=500, kernel=kernel, C=0.6) |
| 34 | model.fit(X_train, y_train) |
| 35 | predictions = model.predict(X_test) |
| 36 | print( |
| 37 | "Classification accuracy (%s): %s" % (kernel, accuracy(y_test, predictions)) |
| 38 | ) |
| 39 | |
| 40 | |
| 41 | if __name__ == "__main__": |