svm機器學習算法_SVM機器學習算法介紹

svm機器學習算法

According to OpenCV's "Introduction to Support Vector Machines", a Support Vector Machine (SVM):

根據OpenCV“支持向量機簡介”,支持向量機(SVM):

...is a discriminative classifier formally defined by a separating hyperplane. In other words, given labeled training data (supervised learning), the algorithm outputs an optimal hyperplane which categorizes new examples.
...是由分離的超平面正式定義的判別式分類器。 換句話說,給定帶標簽的訓練數據(監督學習),該算法會輸出對新示例進行分類的最佳超平面。

An SVM cost function seeks to approximate the logistic function with a piecewise linear. This machine learning algorithm is used for classification problems and is part of the subset of supervised learning algorithms.

SVM成本函數試圖以分段線性近似邏輯函數。 這種機器學習算法用于分類問題,是監督學習算法子集的一部分。

成本函數 (The Cost Function)

The Cost Function is used to train the SVM. By minimizing the value of J(theta), we can ensure that the SVM is as accurate as possible. In the equation, the functions cost1 and cost0 refer to the cost for an example where y=1 and the cost for an example where y=0. For SVMs, cost is determined by kernel (similarity) functions.

成本函數用于訓練SVM。 通過最小化J(theta)的值,我們可以確保SVM盡可能準確。 在等式中,函數cost1和cost0表示y = 1的示例的成本和y = 0的示例的成本。 對于SVM,成本由內核(相似性)函數確定。

核仁 (Kernels)

Polynomial features tend to be computationally expensive, and may increase runtime with large datasets. Instead of adding more polynomial features, it's better to add landmarks to test the proximity of other datapoints against. ?Each member of the training set can be considered a landmark, and a kernel is the similarity function that measures how close an input is to said landmarks.

多項式特征在計算上趨向于昂貴,并且對于大型數據集可能會增加運行時間。 與其添加更多的多項式特征,不如添加界標來測試其他數據點的接近度。 訓練集的每個成員都可以被視為地標,并且核是相似度函數,其測量輸入與所述地標的接近程度。

大保證金分類器 (Large Margin Classifier)

An SVM will find the line or hyperplane that splits the data with the largest margin possible. Though there will be outliers that sway the line in a certain direction, a C value that is small enough will enforce regularization throughout.

SVM將找到可能以最大裕量分割數據的線或超平面。 盡管會有異常值在一定方向上影響直線,但足夠小的C值將在整個過程中強制進行正則化。

The following is code written for training, predicting and finding accuracy for SVM in Python:

以下是編寫的用于訓練,預測和發現Python中SVM準確性的代碼:

import numpy as npclass Svm (object):"""" Svm classifier """def __init__ (self, inputDim, outputDim):self.W = None# - Generate a random svm weight matrix to compute loss                 ##   with standard normal distribution and Standard deviation = 0.01.    #sigma =0.01self.W = sigma * np.random.randn(inputDim,outputDim)def calLoss (self, x, y, reg):"""Svm loss functionD: Input dimension.C: Number of Classes.N: Number of example.Inputs:- x: A numpy array of shape (batchSize, D).- y: A numpy array of shape (N,) where value < C.- reg: (float) regularization strength.Returns a tuple of:- loss as single float.- gradient with respect to weights self.W (dW) with the same shape of self.W."""loss = 0.0dW = np.zeros_like(self.W)# - Compute the svm loss and store to loss variable.                        ## - Compute gradient and store to dW variable.                              ## - Use L2 regularization                                                  ##Calculating score matrixs = x.dot(self.W)#Score with yis_yi = s[np.arange(x.shape[0]),y]#finding the deltadelta = s- s_yi[:,np.newaxis]+1#loss for samplesloss_i = np.maximum(0,delta)loss_i[np.arange(x.shape[0]),y]=0loss = np.sum(loss_i)/x.shape[0]#Loss with regularizationloss += reg*np.sum(self.W*self.W)#Calculating dsds = np.zeros_like(delta)ds[delta > 0] = 1ds[np.arange(x.shape[0]),y] = 0ds[np.arange(x.shape[0]),y] = -np.sum(ds, axis=1)dW = (1/x.shape[0]) * (x.T).dot(ds)dW = dW + (2* reg* self.W)return loss, dWdef train (self, x, y, lr=1e-3, reg=1e-5, iter=100, batchSize=200, verbose=False):"""Train this Svm classifier using stochastic gradient descent.D: Input dimension.C: Number of Classes.N: Number of example.Inputs:- x: training data of shape (N, D)- y: output data of shape (N, ) where value < C- lr: (float) learning rate for optimization.- reg: (float) regularization strength.- iter: (integer) total number of iterations.- batchSize: (integer) number of example in each batch running.- verbose: (boolean) Print log of loss and training accuracy.Outputs:A list containing the value of the loss at each training iteration."""# Run stochastic gradient descent to optimize W.lossHistory = []for i in range(iter):xBatch = NoneyBatch = None# - Sample batchSize from training data and save to xBatch and yBatch   ## - After sampling xBatch should have shape (batchSize, D)              ##                  yBatch (batchSize, )                                 ## - Use that sample for gradient decent optimization.                   ## - Update the weights using the gradient and the learning rate.        ##creating batchnum_train = np.random.choice(x.shape[0], batchSize)xBatch = x[num_train]yBatch = y[num_train]loss, dW = self.calLoss(xBatch,yBatch,reg)self.W= self.W - lr * dWlossHistory.append(loss)# Print loss for every 100 iterationsif verbose and i % 100 == 0 and len(lossHistory) is not 0:print ('Loop {0} loss {1}'.format(i, lossHistory[i]))return lossHistorydef predict (self, x,):"""Predict the y output.Inputs:- x: training data of shape (N, D)Returns:- yPred: output data of shape (N, ) where value < C"""yPred = np.zeros(x.shape[0])# -  Store the predict output in yPred                                    #s = x.dot(self.W)yPred = np.argmax(s, axis=1)return yPreddef calAccuracy (self, x, y):acc = 0# -  Calculate accuracy of the predict value and store to acc variable    yPred = self.predict(x)acc = np.mean(y == yPred)*100return acc

翻譯自: https://www.freecodecamp.org/news/support-vector-machines/

svm機器學習算法

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390590.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390590.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390590.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

6.3 遍歷字典

遍歷所有的鍵—值對 遍歷字典時&#xff0c;鍵—值對的返回順序也與存儲順序不同。 6.3.2 遍歷字典中的所有鍵 在不需要使用字典中的值時&#xff0c;方法keys() 很有用。 6.3.3 按順序遍歷字典中的所有鍵 要以特定的順序返回元素&#xff0c;一種辦法是在for 循環中對返回的鍵…

Google Guava新手教程

以下資料整理自網絡 一、Google Guava入門介紹 引言 Guavaproject包括了若干被Google的 Java項目廣泛依賴 的核心庫&#xff0c;比如&#xff1a;集合 [collections] 、緩存 [caching] 、原生類型支持 [primitives support] 、并發庫 [concurrency libraries] 、通用注解 [comm…

HTML DOM方法

querySelector() (querySelector()) The Document method querySelector() returns the first element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.Document方法querySelector()返回文檔中與…

leetcode 773. 滑動謎題

題目 在一個 2 x 3 的板上&#xff08;board&#xff09;有 5 塊磚瓦&#xff0c;用數字 1~5 來表示, 以及一塊空缺用 0 來表示. 一次移動定義為選擇 0 與一個相鄰的數字&#xff08;上下左右&#xff09;進行交換. 最終當板 board 的結果是 [[1,2,3],[4,5,0]] 謎板被解開。…

數據科學領域有哪些技術_領域知識在數據科學中到底有多重要?

數據科學領域有哪些技術Jeremie Harris: “In a way, it’s almost like a data scientist or a data analyst has to be like a private investigator more than just a technical person.”杰里米哈里斯(Jeremie Harris) &#xff1a;“ 從某種意義上說&#xff0c;這就像是數…

python 算術運算

1. 算術運算符與優先級 # -*- coding:utf-8 -*-# 運算符含有,-,*,/,**,//,% # ** 表示^ , 也就是次方 a 2 ** 4 print 2 ** 4 , aa 16 / 5 print 16 / 5 , aa 16.0 / 5 print 16.0 / 5 , a# 結果再進行一次floor a 16.0 // 5.0 print 16.0 // 5.0 , aa 16 // 5 print …

c語言編程時碰到取整去不了_碰到編程墻時如何解開

c語言編程時碰到取整去不了Getting stuck is part of being a programmer, no matter the level. The so-called “easy” problem is actually pretty hard. You’re not exactly sure how to move forward. What you thought would work doesn’t.無論身在何處&#xff0c;陷…

初創公司怎么做銷售數據分析_為什么您的初創企業需要數據科學來解決這一危機...

初創公司怎么做銷售數據分析The spread of coronavirus is delivering a massive blow to the global economy. The lockdown and work from home restrictions have forced thousands of startups to halt expansion plans, cancel services, and announce layoffs.冠狀病毒的…

leetcode 909. 蛇梯棋

題目 N x N 的棋盤 board 上&#xff0c;按從 1 到 N*N 的數字給方格編號&#xff0c;編號 從左下角開始&#xff0c;每一行交替方向。 例如&#xff0c;一塊 6 x 6 大小的棋盤&#xff0c;編號如下&#xff1a; r 行 c 列的棋盤&#xff0c;按前述方法編號&#xff0c;棋盤格…

Python基礎之window常見操作

一、window的常見操作&#xff1a; cd c:\ #進入C盤d: #從C盤切換到D盤 cd python #進入目錄cd .. #往上走一層目錄dir #查看目錄文件列表cd ../.. #往上上走一層目錄 二、常見的文件后綴名&#xff1a; .txt 記事本文本文件.doc word文件.xls excel文件.ppt PPT文件.exe 可執行…

WPF效果(GIS三維篇)

二維的GIS已經被我玩爛了&#xff0c;緊接著就是三維了&#xff0c;哈哈&#xff01;先來看看最簡單的效果&#xff1a; 轉載于:https://www.cnblogs.com/OhMonkey/p/8954626.html

css注釋_CSS注釋示例–如何注釋CSS

css注釋Comments are used in CSS to explain a block of code or to make temporary changes during development. The commented code doesn’t execute.CSS中使用注釋來解釋代碼塊或在開發過程中進行臨時更改。 注釋的代碼不執行。 Both single and multi-line comments in…

r軟件時間序列分析論文_高度比較的時間序列分析-一篇論文評論

r軟件時間序列分析論文數據科學 &#xff0c; 機器學習 (Data Science, Machine Learning) In machine learning with time series, using features extracted from series is more powerful than simply treating a time series in a tabular form, with each date/timestamp …

leetcode 168. Excel表列名稱

題目 給你一個整數 columnNumber &#xff0c;返回它在 Excel 表中相對應的列名稱。 例如&#xff1a; A -> 1 B -> 2 C -> 3 … Z -> 26 AA -> 27 AB -> 28 … 示例 1&#xff1a; 輸入&#xff1a;columnNumber 1 輸出&#xff1a;“A” 示例 2&…

飛機訂票系統

1 #include <stdio.h>2 #include <stdlib.h>3 #include <string.h>4 #include <conio.h>5 typedef struct flightnode{6 char flight_num[10]; //航班號7 char start_time[10]; //起飛時間8 char end_time[10]; //抵達時間9 char st…

解決Mac10.13 Pod報錯 -bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.fram

升級10.13以后Pod命令失效&#xff0c;解決辦法如下&#xff1a; 終端執行 brew link --overwrite cocoapods 復制代碼嘗試 Pod 命令是否已經恢復 若報錯繼續執行 brew reinstall cocoapodsbrew install rubybrew link --overwrite cocoapods 復制代碼嘗試 Pod 命令是否已經恢復…

angular示例_用示例解釋Angular動畫

angular示例為什么要使用動畫&#xff1f; (Why use Animations?) Modern web components frequently use animations. Cascading Style-sheets (CSS) arms developers with the tools to create impressive animations. Property transitions, uniquely named animations, mu…

selenium抓取_使用Selenium的網絡抓取電子商務網站

selenium抓取In this article we will go through a web scraping process of an E-Commerce website. I have designed this particular post to be beginner friendly. So, if you have no prior knowledge about web scraping or Selenium you can still follow along.在本文…

劍指 Offer 37. 序列化二叉樹

題目 序列化是將一個數據結構或者對象轉換為連續的比特位的操作&#xff0c;進而可以將轉換后的數據存儲在一個文件或者內存中&#xff0c;同時也可以通過網絡傳輸到另一個計算機環境&#xff0c;采取相反方式重構得到原數據。 請設計一個算法來實現二叉樹的序列化與反序列化…

ie8 ajaxSubmit 上傳文件提示下載

轉載 解決ie下ajaxsubmit上傳文件提示下載文件問題 主要是應為放回類型為json&#xff0c;返回text/html轉載于:https://www.cnblogs.com/yang-C-J/p/8963278.html