<jupyter_start><jupyter_text>Data Importing and Visualization<jupyter_code>data = loadmat('ex6data1.mat')
X = data['X']
y = data['y']
def plotData(X,y):
    plt.figure()
    X_zero = X[np.where(y==0)[0]]
    X_one = X[np.where(y==1)[0]]
    plt.scatter(X_zero[:,0], X_zero[:,1], marker = 'o', label = 'y = 0')
    plt.scatter(X_one[:,0], X_one[:,1], marker = '+', label = 'y = 1')
    plt.legend()
    
plotData(X,y)<jupyter_output><empty_output><jupyter_text>Ready to use functions for SVM (like in Andrew's course) are already there in sklearn<jupyter_code>def visualizeBoundary(X, y, model, mind = -1, maxd = 5):
    plotData(X, y)
    
    d1 = np.linspace(mind, maxd, 100).reshape(-1,1)
    d2 = d1
    Xd1, yd1 = np.meshgrid(d1, d2)
    
    plt.contour(Xd1, yd1, model.predict(list(zip(Xd1.ravel(),yd1.ravel()))).reshape(Xd1.shape))

def gaussiankernel(x1, x2, sigma):
    dist = x1 - x2
    return np.e ** (-1 * np.squeeze(dist.T @ dist)/(2*sigma*sigma))

c = 1
svm_model = svm.SVC(C = c, kernel = 'linear', tol = 0.001).fit(X, y.ravel())
visualizeBoundaryLinear(X, y, svm_model)<jupyter_output><empty_output><jupyter_text>data 2<jupyter_code>data = loadmat('ex6data2.mat')
X = data['X']
y = data['y']
c = 1 
sigma = 0.1
x1 = np.array([1, 2, 1])
x2 = np.array([0, 4, -1])
# rbf is similar to gaussian so setting to similar values
svm_gauss_model= svm.SVC(c, 'rbf', tol = 0.001, gamma = 30).fit(X,y.ravel())
visualizeBoundary(X, y, svm_gauss_model, mind = 0, maxd = 1)
gaussiankernel(x1, x2, 2)   # ans = 0.32465246735834974<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18834080717488788, 'fraction_numerical': 0.05445227418321589, 'mean_word_length': 4.386206896551724, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '261778', 'license': 'MIT', 'n_tokens_mistral': 732, 'n_tokens_neox': 685, 'n_words': 139, 'path': 'ex6/SVM.ipynb', 'repo_name': 'PriyantJain/AndrewNgML_course_python', 'size': '1561'}