Advertisement

SVM from Scratch in Python 用 Python 从头开始 SVM

阅读量:

Recently, we delved into the mathematical theory behind Support Vector Machines (SVM). This time, I’d like to focus on implementing a SVM from scratch using Python. I can’t wait to dive into it!
The last story delved into the mathematical theory behind SVMs. This time, I’d like to focus on implementing a SVM from scratch using Python.

Complete code can be found on my GitHub ([GitHub](https://github.com/Madhu009/Medium_Machine_learning_Blog/blob/master/SVM-FromScratch.ipynb))**.

First and foremost, we begin with a synthetic dataset, which enables us to utilize the sklearn make_blobs function to generate systematically distributed datasets X and y. These datasets are then employed to train our models effectively.

We have two self-variables (自变量) and one dependent variable (因变量),其取值为0或+1.为了SVM的方便处理(easily handling),我们将它们视为-1或+1,并将数据集分成两类(分隔开),并定义必要的变量.

We possess all that we require, so let's discuss the training process.

Certainly, as deduced from our previous narrative, if the training points fulfill the stipulated condition outlined below, they will result in the existence of a separable hyperplane between these points.

yi*(w.x+b)≥1

If it meets requirements, we calculate the magnitude and store them in a designated storage area.

else we update the weights.
否则我们更新权重。

The below code does the same thing.
下面的代码做了同样的事情。

Note : 仅涉及必要的代码段即可实现目标功能;完整的代码可以在我的GitHub仓库中找到详细实现。

The main code has been taken from the awesome guy

Sentdex 森泰克斯

so the credit goes to him.
主要代码取自Sentdex,所以功劳归于他。

Optimization code. 优化代码。

我们正在将权重(w)的大小存储在一个字典中,在其优化完成时,则会获得一个包含(|w|, [w,b])键值对的字典;为了便于后续操作,请注意以下步骤:

from them we choose the minimum |w|
我们从中选择最小值 |w|

An example picture for above is this
上面的示例图片是这样的

if we obtain these two pictures, we select the one with the lowest level of intensity |w|

That’s cool right??? 这很酷吧???

After implementing our optimization strategy, but hold on, a potential issue arises when attempting to optimize the weights. Specifically, the problem is: what if we overshoot the minimum value?

Following an overshoot, the system returns to the equilibrium point. However, this learning rate adjustment requires us to reduce its magnitude due to the observed magnitude differences in step sizes.

We should provide a list of step sizes in accordance with our conviction, because I initially declared a list variable at the start of the code.

复制代码
     learning_rate = [max_feature_value * 0.1, max_feature_value * 0.01, max_feature_value * 0.001,] 
    
    代码解读

For each specified step size, executing a comprehensive and precise optimization process (as outlined above) ensures that we ultimately attain the global minimum point.

复制代码
  for lrate in learning_rate:

    
      Optimization code(lrate) 
    
    
    
    
    代码解读

Upon completing all stages of the entire optimization process, we have successfully obtained all necessary final weights.

If visualizing the dataset, this result is obtained.

So What’s next??? 下一个是什么???

Predicting new values. 预测新值。

As we talked in the last story ,
正如我们在上一个故事中谈到的,

Bam! 嘭!

Here's the end of this story. I hope you enjoyed it and learned something. Please let me know if it helps.

During the upcoming story, I will delve into another fascinating machine learning concept. Until then, say cheese!

Complete code can be found on my Medium Machine learning Blog

打开链接https://pythonprogramming.net/predictions-svm-machine-learning-tutorial/

全部评论 (0)

还没有任何评论哟~