<jupyter_start><jupyter_text>One against all SVM<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
def SVM(SV, y):
    matrix = []
    for i, sv1 in enumerate(SV):
        temp = []
        for j, sv2 in enumerate(SV):
            temp.append(y[j] * np.dot(sv2.T,  sv1))
        #temp.append(y[i])
        temp.append(1)
        matrix.append(np.array(temp))
    
    y_new = list(y)
    y_new.append(0)
    y_new = np.array(y_new)
    matrix.append(y_new)  
    matrix = np.matrix(np.array(matrix))
    
    matrix_inv = np.linalg.inv(matrix)
    
    print(matrix)
    print(matrix_inv)
    print(y_new)
    
    lambdas = np.dot(np.array(matrix_inv), y_new)
    lambdas = np.array(lambdas)
    
    print(lambdas)
    w = []
    for i in range(len(SV)):
        w.append(y[i] * np.dot(SV[i], lambdas[i]))
    w = np.array(w)
    W = w.sum(axis=0)
    print('w =', W, 'w0 =', lambdas[-1])
    return (W, lambdas[-1])<jupyter_output><empty_output><jupyter_text>- train R classifiers; R =  of classes- the aim is to classify one class against all the others- choose the class with more votesNote: this method leaves blank uncategorisable spaces  Examples<jupyter_code>X = np.array([
    # class_1
    [0, 10],
    [1, 8],
    [2, 9],
    [2, 8],
    [3, 10],
    [3, 9],
    [4, 10],
    [4, 9],
    [4, 8],
    [5, 9],
    # class_2
    [2, 6],
    [0, 5],
    [4, 5],
    [2, 4],
    [5, 3],
    [0, 2],
    [2, 2],
    [5, 2],
    [2, 1],
    [3, 0],
    # class_3
    [9, 6],
    [8, 5],
    [7, 3],
    [8, 3],
    [9, 3],
    [10, 3],
    [7, 1],
    [8, 1],
    [8, 0],
    [9, 0]  
])
y = np.array([
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3
])
# SVs
SV12_X = np.array([
    [1, 8], [2, 8], [4, 8], [2, 6], [9, 6]
])
SV12_y = np.array([1, 1, 1, -1, -1])

SV23_X = np.array([
    [7, 3], [7, 1], [5, 9], [5, 3], [5, 2]
])
SV23_y = np.array([1, 1, -1, -1, -1])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20361990950226244, 'fraction_numerical': 0.07290095525389644, 'mean_word_length': 2.1487341772151898, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '472777', 'license': 'MIT', 'n_tokens_mistral': 996, 'n_tokens_neox': 908, 'n_words': 235, 'path': 'SVM-one_against_all.ipynb', 'repo_name': 'Sblbl/ML_implementations', 'size': '1989'}