|
| 1 | +import numpy as np |
| 2 | +from sklearn import datasets |
| 3 | +from sklearn.model_selection import train_test_split, GridSearchCV |
| 4 | +from sklearn.svm import SVC |
| 5 | +from sklearn.metrics import accuracy_score |
| 6 | + |
| 7 | + |
| 8 | +breast_cancer = datasets.load_breast_cancer() |
| 9 | +X = breast_cancer.data |
| 10 | +y = breast_cancer.target |
| 11 | + |
| 12 | +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
| 13 | + |
| 14 | +svm = SVC(kernel='rbf') |
| 15 | +param_grid = { |
| 16 | + 'C': [0.1, 1, 10], |
| 17 | + 'gamma': [0.001, 0.01, 0.1, 1] |
| 18 | +} |
| 19 | + |
| 20 | +grid_search = GridSearchCV(estimator=svm, param_grid=param_grid, cv=5) |
| 21 | +grid_search.fit(X_train, y_train) |
| 22 | +best_params = grid_search.best_params_ |
| 23 | +best_model = grid_search.best_estimator_ |
| 24 | +y_pred = best_model.predict(X_test) |
| 25 | +accuracy = accuracy_score(y_test, y_pred) |
| 26 | + |
| 27 | +print(accuracy) |
0 commit comments