SVM支持向量机--sklearn研究
Support vector machines (SVMs) are a set of supervised learning methods used for classification, regression and outliers detection.
支持向量機(SVM)是一組有監督學習方法,被用于分類,回歸和邊界探測
支持向量機有以下的幾個優點:
- Effective in high dimensional spaces. 在高維空間有效性
- Still effective in cases where number of dimensions is greater than the number of samples. 在維度數量大于樣本數量的時候仍然有效
- Uses a subset of training points in the decision function (called support vectors), so it is also memory efficient. 再決策函數上(支持向量)使用訓練點的一個子集,因此內存有效性(占用的空間小)
- Versatile: different Kernel functions can be specified for the decision function. Common kernels are provided, but it is also possible to specify custom kernels. 多功能的:不同的核函數,可以被特別的用于決策函數,普通的核被提供,但是這仍然可能去特異化核函數。
支持向量機也有下面的這些缺點:
- If the number of features is much greater than the number of samples, avoid over-fitting in choosing Kernel functions and regularization term is crucial. 如果特征的數量遠比樣本的數量要大,選擇核函數和正則化余項在避免過擬合上是至關重要的。
- SVMs do not directly provide probability estimates, these are calculated using an expensive five-fold cross-validation (see Scores and probabilities, below). 支持向量機們不直接提供概率估計,而是用耗費時間的5-fold 交叉驗證來計算的。
sklearn中的支持向量機同時支持dense(密集)和 sparse(稀疏)的樣本數據作為輸入。但是,如果是在稀疏的數據上做預測,那么一定也是要在稀疏的數據上做訓練才行。
分類
SVC, NuSVC and LinearSVC are classes capable of performing multi-class classification on a dataset.
SVC,NuSVC 和LinearSVC是一類在數據集上有能力去實現多類分類的分類器。
SVC and NuSVC are similar methods, but accept slightly different sets of parameters and have different mathematical formulations (see section Mathematical formulation). On the other hand, LinearSVC is another implementation of Support Vector Classification for the case of a linear kernel.
- SVC和NuSVC是比較類似的方法,但是在接受的參數上有輕微的不同,同時也有不同的數學表達式。
- LinearSVC是對于另一種針對于線性核情況的支持向量機分類器的實現
注意: LinearSVC不接受key wordkernal,因為被假設為線性了的。同時相比于SVC和NuSVC也缺少了一些方法,例如:support_方法
SVC,NuSVC和LinearSVC都一樣,接受兩個輸入X([n_samples, n_features])和 y ([n_samples] )。前者表示樣本特征,后者表示樣本標簽,用于訓練。
簡單測試
>>> from sklearn import svm D:\SoftWare\Python\lib\site-packages\sklearn\externals\joblib\externals\cloudpickle\cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative usesimport imp >>> X = [[0, 1], [1, 0]] >>> y = [0, 1] >>> clf = svm.SVC(gamma='scale') >>> clf.fit(X, y) SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',max_iter=-1, probability=False, random_state=None, shrinking=True,tol=0.001, verbose=False)- 測試也很簡單
多類分類
svc和NuSVC提供了一種“一對一”的方式來實現多類分類。如果n_class是類的數量的話,那么n_class * (n_class - 1) / 2 個分類器被建立,來不同的兩個類別之間的相互區分。
To provide a consistent interface with other classifiers, the decision_function_shape option allows to aggregate the results of the “one-against-one” classifiers to a decision function of shape (n_samples, n_classes):
為了提供一個和其他分類器一致性的接口,這這個 decision_function_shape 選項,允許去累積這個“1對1”的分類器們去一個決策函數的shape
- 例如:
On the other hand, LinearSVC implements “one-vs-the-rest” multi-class strategy, thus training n_class models. If there are only two classes, only one model is trained:
另一方面,LinearSVC 實現了一個“One-vs-the-rest”的多類分類方式,因此,訓練n_class個模型。如果這只有兩個類別,那就只有一個模型被訓練。(注意最后的這個強調,這是一種特殊的情況,因為one和the rest是重復的~)
非均衡的問題
In problems where it is desired to give more importance to certain classes or certain individual samples keywords class_weight and sample_weight can be used.
在某些問題上,需要更多的關注特定的類別,或者是特定的樣本個體。這時候,可以使用 class_weight 和 sample_weight
總結
以上是生活随笔為你收集整理的SVM支持向量机--sklearn研究的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 五种排序方式gif展示【python】
- 下一篇: numpy.core.umath fai