c_values = np.logspace(-4, 8, num=13, base=2.0) gamma_values = np.arange(48,97,4) param_grid = dict(gamma=gamma_values, C=c_values) grid = GridSearchCV(svm, param_grid=param_grid) grid.fit(X, Y) C = grid.best_params_['C'] gamma = grid.best_params_['gamma'] clf = SVC(C=C,kernel='rbf',gamma=gamma,class_weight={1:2,2:1}) clf.fit(X,Y) plt.figure(figsize=(8,6)) xx, yy = np.meshgrid(np.linspace(0, 1, 200), np.linspace(0, 1, 200)) Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8) plt.contour(xx, yy, Z, colors='g', levels=[-1, 0, 1], alpha=0.5, linestyles=['--', '-', '--']) ax.pcolormesh(xx, yy, -Z, cmap=plt.cm.RdBu, shading='auto') plt.scatter(svm.support_vectors_[:, 0], svm.support_vectors_[:, 1], s=100, linewidth=1, facecolors='none', edgecolors='k') plt.scatter(t1_train,x1_train,c='w', edgecolors='b') plt.scatter(t2_train,x2_train,c='r', marker='x') plt.show()