想要真正的建议在python中构建支持向量机而不使用ScikitLearn吗

2024-03-27 22:54:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道如何使用Scikit Learn构建一个支持向量机,但是现在我想在python中从头开始,而不使用Scikit Learn。 由于我很困惑,而且对内部流程缺乏了解,如果能得到帮助,我会非常高兴。你知道吗


Tags: 流程scikit向量learn我会
1条回答
网友
1楼 · 发布于 2024-03-27 22:54:21

您可以使用numpy实现一个简单的线性支持向量机,如下所示。顺便说一句,下次提问前请先谷歌一下。网上有很多资源和教程。你知道吗

    import numpy as np
    def my_svm(dataset, label):
        rate = 1 # rate for gradient descent
        epochs = 10000 # no of iterations
        weights = np.zeros(dataset.shape[1]) # Create an array for storing the weights

        # Min. the objective function(Hinge loss) by gradient descent
        for epoch in range(1,epochs):
            for n, data in enumerate(dataset):
                if (label[n] * np.dot(dataset[n], weights)) < 1:
                    weights = weights + rate * ( (dataset[n] * label[n]) + (-2  *(1/epoch)* weights) )
                else:
                    weights = weights + rate * (-2  * (1/epoch) * weights)

        return weights

    def predict(test_data,weights):
        results = []
        for data in test_data:
            result = np.dot(data,weights)
            results.append(-1 if result < 0 else 1)
        return results

生成用于培训和测试的数据集

    dataset = np.array([
        [-2, 4,-1], #x_cood,y_cood,bias
        [4, 1, -1],
        [0, 2, -1],
        [1, 6, -1],
        [2, 5, -1],
        [6, 2, -1]
        ])
    label = np.array([-1,-1,-1,1,1,1])

    weights = my_svm(dataset,label)

测试一下

    test_data = np.array([
                [0,3,-1], #Should belong to -1
                [4,5,-1]  #Should belong to 1
                ])
    predict(test_data, weights)
    >Out[10]: [-1, 1]

相关问题 更多 >