想要真正的建议而不使用Scikit-Learn在python中构建Support Vector Machine [英] Want genuine suggestion to build Support Vector Machine in python without using Scikit-Learn

查看:63
本文介绍了想要真正的建议而不使用Scikit-Learn在python中构建Support Vector Machine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我知道如何使用Scikit-Learn构建支持向量机,但是现在我想在不使用Scikit-Learn的情况下从头开始使用python创建它.由于我很困惑,缺乏对内部流程的了解,如果能得到帮助并把它弄出来,我将非常高兴.

As i know how to build a Support Vector Machine using Scikit-Learn but now i want to make it from scratch in python without using Scikit-Learn. As i am confused and having lack of knowledge about the internal processes i would be greatly pleased if getting help and make it out.

推荐答案

您只能使用 numpy 实现简单的线性SVM,如下所示.顺便说一句,请在下次问问题之前先用google搜索.在线上有很多资源和教程.

You can implement a simple linear SVM with numpy only like below. BTW, please google before you ask question next time. There are lots of resources and tutorial online.

    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

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

Generate dataset for training and testing

    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]

这篇关于想要真正的建议而不使用Scikit-Learn在python中构建Support Vector Machine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆