|
| 1 | +from sklearn.model_selection import GridSearchCV |
| 2 | +import numpy as np |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +from sklearn import datasets |
| 5 | +from sklearn.model_selection import train_test_split |
| 6 | +from sklearn.svm import SVC |
| 7 | +from sklearn.metrics import accuracy_score, classification_report, confusion_matrix |
| 8 | +from sklearn.datasets import load_breast_cancer |
| 9 | +data=load_breast_cancer() |
| 10 | +X,y=data.data,data.target |
| 11 | + |
| 12 | +X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3,random_state=42) |
| 13 | + |
| 14 | +param_grid={ |
| 15 | + 'C':[1, 10], |
| 16 | + 'kernel':['linear','rbf'], |
| 17 | + 'gamma':['scale'] |
| 18 | +} |
| 19 | + |
| 20 | +svm=SVC(random_state=42) |
| 21 | + |
| 22 | +grid_search=GridSearchCV(svm, param_grid, cv=3, scoring='accuracy', n_jobs=-1, verbose=1) |
| 23 | +grid_search.fit(X_train,y_train) |
| 24 | + |
| 25 | +print("Best parameters:",grid_search.best_params_) |
| 26 | +print("Best cross validation score",grid_search.best_score_) |
| 27 | + |
| 28 | +best_svm=grid_search.best_estimator_ |
| 29 | +y_pred=best_svm.predict(X_test) |
| 30 | +test_accuracy=accuracy_score(y_test,y_pred) |
| 31 | +print(f"Test accuracy:,{test_accuracy:.2f}") |
| 32 | + |
0 commit comments