0
$\begingroup$

I am plotting decision region and prediction for svm but both are contradicting

from sklearn.svm import SVC
from sklearn.datasets import make_moons

moons = make_moons(500,noise = 0.5)

x,y = moons[0] , moons[1]

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.2)
    scaler = StandardScaler()
X_train = scaler.fit_transform(x_train)
svm_cl = SVC()
m = y_train.shape[0]
sample_weights = np.full_like(y_train,1/m,dtype =np.float64)
svm_cl.fit(X_train,y_train,sample_weight = sample_weights)

Decision function

from matplotlib.colors import ListedColormap
def plot_prediction_regions(x,y,classifier,resolution =0.02,sample_weight):
    markers = ['s','x','o','^','v']
    colors =  ['red' , 'blue' , 'lightgreen' , 'gray' ,'cyan']
    cmap = ListedColormap(colors[:len(np.unique(y))])
    x1_min , x1_max = x[:,0].min() -1 , x[:,0].max() +1
    x2_min  ,x2_max = x[:,1].min() -1 , x[:,1].max() +1
    xx1 ,xx2  = np.meshgrid(np.arange(x1_min,x1_max,resolution),np.arange(x2_min,x2_max,resolution))
    z = classifier.predict(np.c_[xx1.ravel(),xx2.ravel()])
    z = z.reshape(xx1.shape)
    plt.contourf(xx1,xx2,z,alpha= 0.5,cmap = plt.cm.bone)
#     plt.xlim(xmin = 0)
#     plt.ylim(ymin= 0)
#     for idx,cl in enumerate(np.unique(y)):
    plt.scatter(x[:,0], x[:,1],c= y,cmap =plt.cm.bone,s = sample_weight )
    plt.legend()

decision region :

from matplotlib.colors import ListedColormap
def plot_decision_regions(x,y,classifier,resolution =0.02,sample_weight):
    markers = ['s','x','o','^','v']
    colors =  ['red' , 'blue' , 'lightgreen' , 'gray' ,'cyan']
    cmap = ListedColormap(colors[:len(np.unique(y))])
    x1_min , x1_max = x[:,0].min() -1 , x[:,0].max() +1
    x2_min  ,x2_max = x[:,1].min() -1 , x[:,1].max() +1
    xx1 ,xx2  = np.meshgrid(np.arange(x1_min,x1_max,resolution),np.arange(x2_min,x2_max,resolution))
    z = classifier.decision_function(np.c_[xx1.ravel(),xx2.ravel()])
    z = z.reshape(xx1.shape)
    plt.contourf(xx1,xx2,z,alpha= 0.5,cmap = plt.cm.bone)
#     plt.xlim(xmin = 0)
#     plt.ylim(ymin= 0)
#     for idx,cl in enumerate(np.unique(y)):
    plt.scatter(x[:,0], x[:,1],c= y,cmap =plt.cm.bone,s = sample_weight )
    plt.legend()

prediction

svm only predicting 0 , while decision region show positive class . I don't understand if decision region is showing positive class than why prediction is always zero . If I don't use weights everything working fine .

$\endgroup$
0
$\begingroup$

i got answer , actually the problem is in decision function i.e. i'm using divergent color which uses mean value to separate class. All value of decision function are negative but it separates on the basis of mean. ploted colorbar alongside decision region enter image description here

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.