Hướng dẫn svm forecasting python - python dự báo svm
Các máy Vector hỗ trợ (SVM) là một tập hợp các phương pháp học tập có giám sát được sử dụng để phân loại, hồi quy và phát hiện ngoại lệ. are a set of supervised learning methods used for classification, regression and outliers detection. Show
Ưu điểm của máy vectơ hỗ trợ là:
Những nhược điểm của các máy vectơ hỗ trợ bao gồm:
Các máy vectơ hỗ trợ trong scikit-learn hỗ trợ cả hai vectơ mẫu dày đặc (____10 và chuyển đổi theo đó bằng >>> clf.predict([[2., 2.]]) array([1])1) và các vectơ mẫu thưa (bất kỳ >>> clf.predict([[2., 2.]]) array([1])2) làm đầu vào. Tuy nhiên, để sử dụng SVM để đưa ra dự đoán cho dữ liệu thưa thớt, nó phải phù hợp với dữ liệu đó. Để thực hiện tối ưu, hãy sử dụng >>> clf.predict([[2., 2.]]) array([1])0 (dày đặc) hoặc >>> clf.predict([[2., 2.]]) array([1])4 (thưa thớt) với >>> clf.predict([[2., 2.]]) array([1])5. 1.4.1. Phân loại %Classification¶>>> clf.predict([[2., 2.]]) array([1])6, >>> clf.predict([[2., 2.]]) array([1])7 và >>> clf.predict([[2., 2.]]) array([1])8 là các lớp có khả năng thực hiện phân loại nhị phân và đa lớp trên bộ dữ liệu. >>> clf.predict([[2., 2.]]) array([1])6 và >>> clf.predict([[2., 2.]]) array([1])7 là các phương pháp tương tự, nhưng chấp nhận các tập hợp các tham số hơi khác nhau và có các công thức toán học khác nhau (xem phần Công thức toán học). Mặt khác, >>> clf.predict([[2., 2.]]) array([1])8 là một triển khai khác (nhanh hơn) về phân loại vector hỗ trợ cho trường hợp hạt nhân tuyến tính. Lưu ý rằng >>> clf.predict([[2., 2.]]) array([1])8 không chấp nhận tham số >>> # get support vectors >>> clf.support_vectors_ array([[0., 0.], [1., 1.]]) >>> # get indices of support vectors >>> clf.support_ array([0, 1]...) >>> # get number of support vectors for each class >>> clf.n_support_ array([1, 1]...)3, vì điều này được coi là tuyến tính. Nó cũng thiếu một số thuộc tính của >>> clf.predict([[2., 2.]]) array([1])6 và >>> clf.predict([[2., 2.]]) array([1])7, như >>> # get support vectors >>> clf.support_vectors_ array([[0., 0.], [1., 1.]]) >>> # get indices of support vectors >>> clf.support_ array([0, 1]...) >>> # get number of support vectors for each class >>> clf.n_support_ array([1, 1]...)6.Mathematical formulation). On the other hand, >>> clf.predict([[2., 2.]]) array([1])8 is another (faster) implementation of Support Vector Classification for the case of a linear kernel. Note that >>> clf.predict([[2., 2.]]) array([1])8 does not accept parameter >>> # get support vectors >>> clf.support_vectors_ array([[0., 0.], [1., 1.]]) >>> # get indices of support vectors >>> clf.support_ array([0, 1]...) >>> # get number of support vectors for each class >>> clf.n_support_ array([1, 1]...)3, as this is assumed to be linear. It also lacks some of the attributes of >>> clf.predict([[2., 2.]]) array([1])6 and >>> clf.predict([[2., 2.]]) array([1])7, like >>> # get support vectors >>> clf.support_vectors_ array([[0., 0.], [1., 1.]]) >>> # get indices of support vectors >>> clf.support_ array([0, 1]...) >>> # get number of support vectors for each class >>> clf.n_support_ array([1, 1]...)6. Là các phân loại khác, >>> clf.predict([[2., 2.]]) array([1])6, >>> clf.predict([[2., 2.]]) array([1])7 và >>> clf.predict([[2., 2.]]) array([1])8 lấy làm hai mảng đầu vào: một mảng >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 40 của hình dạng >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 41 giữ các mẫu đào tạo và một mảng >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 42 của nhãn lớp (chuỗi hoặc số nguyên), có hình dạng >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 43: >>> from sklearn import svm >>> X = [[0, 0], [1, 1]] >>> y = [0, 1] >>> clf = svm.SVC() >>> clf.fit(X, y) SVC() Sau khi được trang bị, mô hình sau đó có thể được sử dụng để dự đoán các giá trị mới: >>> clf.predict([[2., 2.]]) array([1]) Chức năng quyết định của SVMS (chi tiết trong công thức toán học) phụ thuộc vào một số tập hợp con của dữ liệu đào tạo, được gọi là vectơ hỗ trợ. Một số thuộc tính của các vectơ hỗ trợ này có thể được tìm thấy trong các thuộc tính >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 44, >>> # get support vectors >>> clf.support_vectors_ array([[0., 0.], [1., 1.]]) >>> # get indices of support vectors >>> clf.support_ array([0, 1]...) >>> # get number of support vectors for each class >>> clf.n_support_ array([1, 1]...)6 và >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 46:Mathematical formulation) depends on some subset of the training data, called the support vectors. Some properties of these support vectors can be found in attributes >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 44, >>> # get support vectors >>> clf.support_vectors_ array([[0., 0.], [1., 1.]]) >>> # get indices of support vectors >>> clf.support_ array([0, 1]...) >>> # get number of support vectors for each class >>> clf.n_support_ array([1, 1]...)6 and >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 46: >>> # get support vectors >>> clf.support_vectors_ array([[0., 0.], [1., 1.]]) >>> # get indices of support vectors >>> clf.support_ array([0, 1]...) >>> # get number of support vectors for each class >>> clf.n_support_ array([1, 1]...) 1.4.1.1. Phân loại đa lớpMulti-class classification¶>>> clf.predict([[2., 2.]]) array([1])6 và >>> clf.predict([[2., 2.]]) array([1])7 thực hiện phương pháp tiếp cận một so với một người khác để phân loại nhiều lớp. Tổng cộng, các phân loại >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 49 được xây dựng và mỗi người đào tạo dữ liệu từ hai lớp. Để cung cấp một giao diện nhất quán với các bộ phân loại khác, tùy chọn >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 40 cho phép chuyển đổi một cách đơn điệu kết quả của các phân loại một so với một người khác thành một chức năng quyết định của một VS-VS-Rest của hình dạng >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 41. >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 4 Mặt khác, >>> clf.predict([[2., 2.]]) array([1])8 thực hiện chiến lược đa tầng của một lớp VS-VS-Rest, do đó đào tạo các mô hình >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 43. >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 4 Xem công thức toán học để biết mô tả đầy đủ về chức năng quyết định.Mathematical formulation for a complete description of the decision function. Lưu ý rằng >>> clf.predict([[2., 2.]]) array([1])8 cũng thực hiện một chiến lược đa lớp thay thế, cái gọi là SVM đa lớp được xây dựng bởi CRAMR và SIM [16], bằng cách sử dụng tùy chọn >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 45. Trong thực tế, phân loại một-VS-Rest thường được ưa thích, vì kết quả chủ yếu giống nhau, nhưng thời gian chạy ít hơn đáng kể.[16], by using the option >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 45. In practice, one-vs-rest classification is usually preferred, since the results are mostly similar, but the runtime is significantly less. Đối với một số VS-Rest, >>> clf.predict([[2., 2.]]) array([1])8 Các thuộc tính >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 47 và >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 48 có hình dạng tương ứng >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 49 và >>> from sklearn import svm >>> X = [[0, 0], [2, 2]] >>> y = [0.5, 2.5] >>> regr = svm.SVR() >>> regr.fit(X, y) SVR() >>> regr.predict([[1, 1]]) array([1.5])0. Mỗi hàng của các hệ số tương ứng với một trong các phân loại >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 43 one-vs-rest và tương tự cho các đánh chặn, theo thứ tự của lớp One One. Trong trường hợp của một-VS-One, >>> clf.predict([[2., 2.]]) array([1])6 và >>> clf.predict([[2., 2.]]) array([1])7, bố cục của các thuộc tính có liên quan nhiều hơn một chút. Trong trường hợp của một hạt nhân tuyến tính, các thuộc tính >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 47 và >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 48 có hình dạng tương ứng >>> from sklearn import svm >>> X = [[0, 0], [2, 2]] >>> y = [0.5, 2.5] >>> regr = svm.SVR() >>> regr.fit(X, y) SVR() >>> regr.predict([[1, 1]]) array([1.5])6 và >>> from sklearn import svm >>> X = [[0, 0], [2, 2]] >>> y = [0.5, 2.5] >>> regr = svm.SVR() >>> regr.fit(X, y) SVR() >>> regr.predict([[1, 1]]) array([1.5])7. Điều này tương tự như bố cục cho >>> clf.predict([[2., 2.]]) array([1])8 được mô tả ở trên, với mỗi hàng bây giờ tương ứng với một phân loại nhị phân. Thứ tự cho các lớp 0 đến N là 0 0 so với 1, 0, 0, 0, . . N N-1 vs n. Hình dạng của >>> from sklearn import svm >>> X = [[0, 0], [2, 2]] >>> y = [0.5, 2.5] >>> regr = svm.SVR() >>> regr.fit(X, y) SVR() >>> regr.predict([[1, 1]]) array([1.5])9 là >>> from sklearn.pipeline import make_pipeline >>> from sklearn.preprocessing import StandardScaler >>> from sklearn.svm import SVC >>> clf = make_pipeline(StandardScaler(), SVC())0 với bố cục hơi khó nắm bắt. Các cột tương ứng với các vectơ hỗ trợ liên quan đến bất kỳ bộ phân loại >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 49 nào một VS-One. Mỗi vectơ hỗ trợ >>> from sklearn.pipeline import make_pipeline >>> from sklearn.preprocessing import StandardScaler >>> from sklearn.svm import SVC >>> clf = make_pipeline(StandardScaler(), SVC())2 có hệ số kép trong mỗi phân loại >>> from sklearn.pipeline import make_pipeline >>> from sklearn.preprocessing import StandardScaler >>> from sklearn.svm import SVC >>> clf = make_pipeline(StandardScaler(), SVC())3 so sánh lớp >>> from sklearn.pipeline import make_pipeline >>> from sklearn.preprocessing import StandardScaler >>> from sklearn.svm import SVC >>> clf = make_pipeline(StandardScaler(), SVC())2 với lớp khác. Lưu ý rằng một số, nhưng không phải tất cả, trong số các hệ số kép này, có thể bằng không. Các mục >>> from sklearn.pipeline import make_pipeline >>> from sklearn.preprocessing import StandardScaler >>> from sklearn.svm import SVC >>> clf = make_pipeline(StandardScaler(), SVC())3 trong mỗi cột là các hệ số kép này, được đặt hàng bởi lớp đối lập. Điều này có thể rõ ràng hơn với một ví dụ: xem xét vấn đề ba lớp với lớp 0 có ba vectơ hỗ trợ \ (v^{0} _0, v^{1} _0, v^{2} _0 \) và lớp 1 và 2 có hai vectơ hỗ trợ \ (v^{0} _1, v^{1} _1 \) và \ (v^{0} _2, v^{1} _2 \). Đối với mỗi vectơ hỗ trợ \ (v^{j} _i \), có hai hệ số kép. Hãy gọi cho hệ số hỗ trợ vector \ (v^{j} _i \) trong phân loại giữa các lớp \ (i \) và \ (k \) \ (\ alpha^{j} _ {i, k} \). Sau đó >>> from sklearn import svm >>> X = [[0, 0], [2, 2]] >>> y = [0.5, 2.5] >>> regr = svm.SVR() >>> regr.fit(X, y) SVR() >>> regr.predict([[1, 1]]) array([1.5])9 trông như thế này:\(v^{0}_0, v^{1}_0, v^{2}_0\) and class 1 and 2 having two support vectors \(v^{0}_1, v^{1}_1\) and \(v^{0}_2, v^{1}_2\) respectively. For each support vector \(v^{j}_i\), there are two dual coefficients. Let’s call the coefficient of support vector \(v^{j}_i\) in the classifier between classes \(i\) and \(k\) \(\alpha^{j}_{i,k}\). Then >>> from sklearn import svm >>> X = [[0, 0], [2, 2]] >>> y = [0.5, 2.5] >>> regr = svm.SVR() >>> regr.fit(X, y) SVR() >>> regr.predict([[1, 1]]) array([1.5])9 looks like this:
1.4.1.2. Điểm số và xác suấtScores and probabilities¶Phương pháp >>> from sklearn.pipeline import make_pipeline >>> from sklearn.preprocessing import StandardScaler >>> from sklearn.svm import SVC >>> clf = make_pipeline(StandardScaler(), SVC())7 của >>> clf.predict([[2., 2.]]) array([1])6 và >>> clf.predict([[2., 2.]]) array([1])7 cho điểm mỗi lớp cho mỗi mẫu (hoặc một điểm duy nhất cho mỗi mẫu trong trường hợp nhị phân). Khi tùy chọn hàm tạo >>> linear_svc = svm.SVC(kernel='linear') >>> linear_svc.kernel 'linear' >>> rbf_svc = svm.SVC(kernel='rbf') >>> rbf_svc.kernel 'rbf'0 được đặt thành >>> linear_svc = svm.SVC(kernel='linear') >>> linear_svc.kernel 'linear' >>> rbf_svc = svm.SVC(kernel='rbf') >>> rbf_svc.kernel 'rbf'1, ước tính xác suất thành viên lớp (từ các phương thức >>> linear_svc = svm.SVC(kernel='linear') >>> linear_svc.kernel 'linear' >>> rbf_svc = svm.SVC(kernel='rbf') >>> rbf_svc.kernel 'rbf'2 và >>> linear_svc = svm.SVC(kernel='linear') >>> linear_svc.kernel 'linear' >>> rbf_svc = svm.SVC(kernel='rbf') >>> rbf_svc.kernel 'rbf'3) được bật. Trong trường hợp nhị phân, các xác suất được hiệu chỉnh bằng cách sử dụng tỷ lệ Platt [9]: hồi quy logistic trên điểm số SVM, phù hợp với một xác thực chéo bổ sung trên dữ liệu đào tạo. Trong trường hợp đa lớp, điều này được mở rộng theo [10].[9]: logistic regression on the SVM’s scores, fit by an additional cross-validation on the training data. In the multiclass case, this is extended as per [10]. Việc xác thực chéo liên quan đến tỷ lệ Platt là một hoạt động đắt tiền cho các bộ dữ liệu lớn. Ngoài ra, ước tính xác suất có thể không phù hợp với điểm số:
Phương pháp Platt cũng được biết là có vấn đề lý thuyết. Nếu điểm số tự tin là bắt buộc, nhưng những điều này không phải là xác suất, thì nên đặt >>> linear_svc = svm.SVC(kernel='linear') >>> linear_svc.kernel 'linear' >>> rbf_svc = svm.SVC(kernel='rbf') >>> rbf_svc.kernel 'rbf'7 và sử dụng >>> from sklearn.pipeline import make_pipeline >>> from sklearn.preprocessing import StandardScaler >>> from sklearn.svm import SVC >>> clf = make_pipeline(StandardScaler(), SVC())7 thay vì >>> linear_svc = svm.SVC(kernel='linear') >>> linear_svc.kernel 'linear' >>> rbf_svc = svm.SVC(kernel='rbf') >>> rbf_svc.kernel 'rbf'2. Xin lưu ý rằng khi >>> import numpy as np >>> from sklearn import svm >>> def my_kernel(X, Y): ... return np.dot(X, Y.T) ... >>> clf = svm.SVC(kernel=my_kernel)0 và >>> import numpy as np >>> from sklearn import svm >>> def my_kernel(X, Y): ... return np.dot(X, Y.T) ... >>> clf = svm.SVC(kernel=my_kernel)1, không giống như >>> from sklearn.pipeline import make_pipeline >>> from sklearn.preprocessing import StandardScaler >>> from sklearn.svm import SVC >>> clf = make_pipeline(StandardScaler(), SVC())7, phương thức >>> linear_svc = svm.SVC(kernel='linear') >>> linear_svc.kernel 'linear' >>> rbf_svc = svm.SVC(kernel='rbf') >>> rbf_svc.kernel 'rbf'4 không cố gắng phá vỡ các mối quan hệ theo mặc định. Bạn có thể đặt >>> import numpy as np >>> from sklearn import svm >>> def my_kernel(X, Y): ... return np.dot(X, Y.T) ... >>> clf = svm.SVC(kernel=my_kernel)4 cho đầu ra của >>> linear_svc = svm.SVC(kernel='linear') >>> linear_svc.kernel 'linear' >>> rbf_svc = svm.SVC(kernel='rbf') >>> rbf_svc.kernel 'rbf'4 giống như >>> import numpy as np >>> from sklearn import svm >>> def my_kernel(X, Y): ... return np.dot(X, Y.T) ... >>> clf = svm.SVC(kernel=my_kernel)6, nếu không lớp đầu tiên trong số các lớp bị ràng buộc sẽ luôn được trả về; Nhưng hãy nhớ rằng nó đi kèm với một chi phí tính toán. Xem ví dụ phá vỡ cà vạt SVM cho một ví dụ về phá vỡ.SVM Tie Breaking Example for an example on tie breaking. 1.4.1.3. Các vấn đề không cân bằngUnbalanced problems¶Trong các vấn đề mà mong muốn có tầm quan trọng hơn đối với một số lớp hoặc một số mẫu riêng lẻ, các tham số >>> import numpy as np >>> from sklearn import svm >>> def my_kernel(X, Y): ... return np.dot(X, Y.T) ... >>> clf = svm.SVC(kernel=my_kernel)7 và >>> import numpy as np >>> from sklearn import svm >>> def my_kernel(X, Y): ... return np.dot(X, Y.T) ... >>> clf = svm.SVC(kernel=my_kernel)8 có thể được sử dụng. >>> clf.predict([[2., 2.]]) array([1])6 (nhưng không phải >>> clf.predict([[2., 2.]]) array([1])7) thực hiện tham số >>> import numpy as np >>> from sklearn import svm >>> def my_kernel(X, Y): ... return np.dot(X, Y.T) ... >>> clf = svm.SVC(kernel=my_kernel)7 trong phương thức >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])2. Nó có một từ điển có dạng >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])3, trong đó giá trị là số điểm nổi> 0 đặt tham số >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])4 của lớp >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])5 thành >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])6. Hình dưới đây minh họa ranh giới quyết định của một vấn đề không cân bằng, có và không có hiệu chỉnh trọng lượng. >>> clf.predict([[2., 2.]]) array([1])6, >>> clf.predict([[2., 2.]]) array([1])7, >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])9, >>> clf.predict([[2., 2.]]) array([1])00, >>> clf.predict([[2., 2.]]) array([1])8, >>> clf.predict([[2., 2.]]) array([1])02 và >>> clf.predict([[2., 2.]]) array([1])03 cũng có trọng số cho các mẫu riêng lẻ trong phương thức >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])2 thông qua tham số >>> import numpy as np >>> from sklearn import svm >>> def my_kernel(X, Y): ... return np.dot(X, Y.T) ... >>> clf = svm.SVC(kernel=my_kernel)8. Tương tự như >>> import numpy as np >>> from sklearn import svm >>> def my_kernel(X, Y): ... return np.dot(X, Y.T) ... >>> clf = svm.SVC(kernel=my_kernel)7, điều này đặt tham số >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])4 cho ví dụ thứ i thành >>> clf.predict([[2., 2.]]) array([1])08, điều này sẽ khuyến khích trình phân loại để các mẫu này đúng. Hình dưới đây minh họa ảnh hưởng của trọng số mẫu đối với ranh giới quyết định. Kích thước của các vòng tròn tỷ lệ thuận với trọng số mẫu: 1.4.2. Hồi quy¶Regression¶Phương pháp phân loại vector hỗ trợ có thể được mở rộng để giải quyết các vấn đề hồi quy. Phương pháp này được gọi là hồi quy vector hỗ trợ. Mô hình được sản xuất bởi phân loại vector hỗ trợ (như được mô tả ở trên) chỉ phụ thuộc vào một tập hợp con của dữ liệu đào tạo, bởi vì hàm chi phí để xây dựng mô hình không quan tâm đến các điểm đào tạo nằm ngoài lề. Tương tự, mô hình được tạo ra bởi hồi quy vectơ hỗ trợ chỉ phụ thuộc vào một tập hợp con của dữ liệu đào tạo, bởi vì hàm chi phí bỏ qua các mẫu có dự đoán gần với mục tiêu của chúng. Có ba triển khai khác nhau của hồi quy vectơ hỗ trợ: >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])9, >>> clf.predict([[2., 2.]]) array([1])00 và >>> clf.predict([[2., 2.]]) array([1])02. >>> clf.predict([[2., 2.]]) array([1])02 cung cấp một triển khai nhanh hơn >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])9 nhưng chỉ xem xét hạt nhân tuyến tính, trong khi >>> clf.predict([[2., 2.]]) array([1])00 thực hiện một công thức hơi khác so với >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])9 và >>> clf.predict([[2., 2.]]) array([1])02. Xem chi tiết thực hiện để biết thêm chi tiết.Implementation details for further details. As with classification classes, the fit method will take as argument vectors X, y, only that in this case y is expected to have floating point values instead of integer values: >>> from sklearn import svm >>> X = [[0, 0], [2, 2]] >>> y = [0.5, 2.5] >>> regr = svm.SVR() >>> regr.fit(X, y) SVR() >>> regr.predict([[1, 1]]) array([1.5]) 1.4.3. Density estimation, novelty detection¶The class >>> clf.predict([[2., 2.]]) array([1])03 implements a One-Class SVM which is used in outlier detection. See Novelty and Outlier Detection for the description and usage of OneClassSVM. 1.4.4. Complexity¶Support Vector Machines are powerful tools, but their compute and storage requirements increase rapidly with the number of training vectors. The core of an SVM is a quadratic programming problem (QP), separating support vectors from the rest of the training data. The QP solver used by the libsvm-based implementation scales between \(O(n_{features} \times n_{samples}^2)\) and \(O(n_{features} \times n_{samples}^3)\) depending on how efficiently the libsvm cache is used in practice (dataset dependent). If the data is very sparse \(n_{features}\) should be replaced by the average number of non-zero features in a sample vector. For the linear case, the algorithm used in >>> clf.predict([[2., 2.]]) array([1])8 by the liblinear implementation is much more efficient than its libsvm-based >>> clf.predict([[2., 2.]]) array([1])6 counterpart and can scale almost linearly to millions of samples and/or features. 1.4.5. Tips on Practical Use¶
1.4.6. Chức năng kernelKernel functions¶Hàm kernel có thể là bất kỳ điều nào sau đây:
Các hạt khác nhau được chỉ định bởi tham số >>> # get support vectors >>> clf.support_vectors_ array([[0., 0.], [1., 1.]]) >>> # get indices of support vectors >>> clf.support_ array([0, 1]...) >>> # get number of support vectors for each class >>> clf.n_support_ array([1, 1]...)3: >>> linear_svc = svm.SVC(kernel='linear') >>> linear_svc.kernel 'linear' >>> rbf_svc = svm.SVC(kernel='rbf') >>> rbf_svc.kernel 'rbf' Xem thêm xấp xỉ kernel cho một giải pháp để sử dụng hạt nhân RBF nhanh hơn và có thể mở rộng hơn nhiều.Kernel Approximation for a solution to use RBF kernels that is much faster and more scalable. 1.4.6.1. Các tham số của Kernel LParameters of the RBF Kernel¶Khi đào tạo SVM với hạt nhân cơ sở xuyên tâm (RBF), hai tham số phải được xem xét: >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])4 và >>> clf.predict([[2., 2.]]) array([1])76. Tham số >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])4, phổ biến cho tất cả các hạt nhân SVM, giao dịch phân loại sai các ví dụ đào tạo chống lại sự đơn giản của bề mặt quyết định. Một >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])4 thấp làm cho bề mặt quyết định trơn tru, trong khi >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])4 cao nhằm mục đích phân loại tất cả các ví dụ đào tạo một cách chính xác. >>> clf.predict([[2., 2.]]) array([1])76 Xác định mức độ ảnh hưởng của một ví dụ đào tạo duy nhất. >>> clf.predict([[2., 2.]]) array([1])76 lớn hơn là, các ví dụ khác càng gần bị ảnh hưởng. Lựa chọn thích hợp của >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])4 và >>> clf.predict([[2., 2.]]) array([1])76 là rất quan trọng đối với hiệu suất của SVM. Người ta nên sử dụng >>> clf.predict([[2., 2.]]) array([1])88 với >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])4 và >>> clf.predict([[2., 2.]]) array([1])76 cách xa nhau một cách xa nhau để chọn các giá trị tốt. 1.4.6.2. Hạt nhân tùy chỉnhCustom Kernels¶Bạn có thể xác định hạt nhân của riêng bạn bằng cách cho hạt nhân là hàm Python hoặc bằng cách tính toán trước ma trận Gram. Phân loại với hạt nhân tùy chỉnh hoạt động giống như bất kỳ phân loại nào khác, ngoại trừ điều đó:
1.4.6.2.1. Sử dụng các chức năng Python làm hạt nhânUsing Python functions as kernels¶Bạn có thể sử dụng các hạt nhân được xác định của riêng bạn bằng cách chuyển một hàm cho tham số >>> # get support vectors >>> clf.support_vectors_ array([[0., 0.], [1., 1.]]) >>> # get indices of support vectors >>> clf.support_ array([0, 1]...) >>> # get number of support vectors for each class >>> clf.n_support_ array([1, 1]...)3. Hạt nhân của bạn phải lấy làm đối số hai ma trận hình dạng >>> clf.predict([[2., 2.]]) array([1])97, >>> clf.predict([[2., 2.]]) array([1])98 và trả lại một ma trận hạt nhân hình dạng >>> clf.predict([[2., 2.]]) array([1])99. Mã sau đây xác định một hạt nhân tuyến tính và tạo một thể hiện phân loại sẽ sử dụng hạt nhân đó: >>> import numpy as np >>> from sklearn import svm >>> def my_kernel(X, Y): ... return np.dot(X, Y.T) ... >>> clf = svm.SVC(kernel=my_kernel) 1.4.6.2.2. Sử dụng ma trận gramUsing the Gram matrix¶Bạn có thể vượt qua hạt nhân được tính toán trước bằng cách sử dụng tùy chọn >>> # get support vectors >>> clf.support_vectors_ array([[0., 0.], [1., 1.]]) >>> # get indices of support vectors >>> clf.support_ array([0, 1]...) >>> # get number of support vectors for each class >>> clf.n_support_ array([1, 1]...)00. Sau đó, bạn nên chuyển ma trận Gram thay vì X cho các phương thức >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])2 và >>> linear_svc = svm.SVC(kernel='linear') >>> linear_svc.kernel 'linear' >>> rbf_svc = svm.SVC(kernel='rbf') >>> rbf_svc.kernel 'rbf'4. Các giá trị kernel giữa tất cả các vectơ đào tạo và các vectơ thử nghiệm phải được cung cấp: >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0]) 1.4.7. Công thức toán họcMathematical formulation¶Một máy vectơ hỗ trợ xây dựng một mặt phẳng siêu hoặc tập hợp các mặt phẳng trong một không gian chiều cao hoặc vô hạn, có thể được sử dụng để phân loại, hồi quy hoặc các tác vụ khác. Theo trực giác, một sự phân tách tốt đạt được bằng mặt phẳng siêu lớn có khoảng cách lớn nhất đến các điểm dữ liệu đào tạo gần nhất của bất kỳ lớp nào (được gọi là biên độ chức năng), vì nói chung, tỷ lệ lợi nhuận càng lớn thì lỗi khái quát hóa của phân loại càng thấp. Hình dưới đây cho thấy chức năng quyết định cho một vấn đề có thể tách rời tuyến tính, với ba mẫu trên các ranh giới lề, được gọi là các vectơ hỗ trợ của Hồi giáo: Nói chung, khi vấn đề không có thể tách rời tuyến tính, các vectơ hỗ trợ là các mẫu trong ranh giới lề. Chúng tôi đề xuất [13] và [14] là tài liệu tham khảo tốt cho lý thuyết và thực tiễn của SVM.[13] and [14] as good references for the theory and practicalities of SVMs. 1.4.7.1. Svc¶SVC¶Đã cho các vectơ đào tạo \ (x_i \ in \ mathbb {r}^p \), i = 1, Hồi, n, trong hai lớp và một vectơ \ (y \ in \ {1, -1 \}^n \) , mục tiêu của chúng tôi là tìm \ (w \ in \ mathbb {r}^p \) và \ (b \ in \ mathbb {r} \) sao cho dự đoán được đưa ra bởi \ (\ text {Sign} (w^t \ Phi (x) + b) \) là chính xác cho hầu hết các mẫu.\(x_i \in \mathbb{R}^p\), i=1,…, n, in two classes, and a vector \(y \in \{1, -1\}^n\), our goal is to find \(w \in \mathbb{R}^p\) and \(b \in \mathbb{R}\) such that the prediction given by \(\text{sign} (w^T\phi(x) + b)\) is correct for most samples. SVC giải quyết vấn đề nguyên thủy sau: \ [\ started {align} \ start \\ Begin {split} \ textrm {chủ đề cho} & y_i (w^t \ Phi (x_i) + b) \ geq 1 - \ zeta_i, \\ & \ zeta_i \ geq 0, i = 1, ... n \ end {split} \ end {căn chỉnh} \ end {align} \] Theo trực giác, chúng tôi đang cố gắng tối đa hóa biên độ (bằng cách giảm thiểu \ (| Lý tưởng nhất là giá trị \ (y_i (w^t \ Phi (x_i) + b) \) sẽ là \ (\ geq 1 \) cho tất cả các mẫu, cho thấy một dự đoán hoàn hảo. Nhưng các vấn đề thường không phải lúc nào cũng có thể tách rời hoàn toàn với một siêu phẳng, vì vậy chúng tôi cho phép một số mẫu ở khoảng cách \ (\ zeta_i \) từ ranh giới lề chính xác của chúng. Thuật ngữ hình phạt >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])4 kiểm soát sức mạnh của hình phạt này, và kết quả là, hoạt động như một tham số chính quy nghịch đảo (xem lưu ý bên dưới).\(||w||^2 = w^Tw\)), while incurring a penalty when a sample is misclassified or within the margin boundary. Ideally, the value \(y_i (w^T \phi (x_i) + b)\) would be \(\geq 1\) for all samples, which indicates a perfect prediction. But problems are usually not always perfectly separable with a hyperplane, so we allow some samples to be at a distance \(\zeta_i\) from their correct margin boundary. The penalty term >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])4 controls the strength of this penalty, and as a result, acts as an inverse regularization parameter (see note below). Vấn đề kép đối với nguyên thủy là \ [\ Begin {align} \ start } & y^t \ alpha = 0 \\ & 0 \ leq \ alpha_i \ leq c, i = 1, ..., n \ end {split} \ end {căn chỉnh} trong đó \ (e \) là vectơ của tất cả các kết hợp và \ (q \) là một ma trận semidefinite dương \ (n \) của \ (n \) x_j) \), trong đó \ (k (x_i, x_j) = \ Phi (x_i)^t \ Phi (x_j) \) là hạt nhân. Các thuật ngữ \ (\ alpha_i \) được gọi là các hệ số kép và chúng được giới hạn trên bởi \ (c \). Biểu diễn kép này làm nổi bật thực tế là các vectơ đào tạo được ánh xạ ngầm vào một không gian chiều cao hơn (có thể vô hạn) bằng hàm \ (\ Phi \): Xem Trick Kernel.\(e\) is the vector of all ones, and \(Q\) is an \(n\) by \(n\) positive semidefinite matrix, \(Q_{ij} \equiv y_i y_j K(x_i, x_j)\), where \(K(x_i, x_j) = \phi (x_i)^T \phi (x_j)\) is the kernel. The terms \(\alpha_i\) are called the dual coefficients, and they are upper-bounded by \(C\). This dual representation highlights the fact that training vectors are implicitly mapped into a higher (maybe infinite) dimensional space by the function \(\phi\): see kernel trick. Khi vấn đề tối ưu hóa được giải quyết, đầu ra của Russ_Function cho một mẫu đã cho \ (x \) đã cho:decision_function for a given sample \(x\) becomes: \ [\ sum_ {i \ in sv} y_i \ alpha_i k (x_i, x) + b, \] và lớp dự đoán tương ứng với dấu hiệu của nó. Chúng ta chỉ cần tổng hợp các vectơ hỗ trợ (nghĩa là các mẫu nằm trong lề) vì các hệ số kép \ (\ alpha_i \) bằng không đối với các mẫu khác.\(\alpha_i\) are zero for the other samples. Các tham số này có thể được truy cập thông qua các thuộc tính >>> from sklearn import svm >>> X = [[0, 0], [2, 2]] >>> y = [0.5, 2.5] >>> regr = svm.SVR() >>> regr.fit(X, y) SVR() >>> regr.predict([[1, 1]]) array([1.5])9 chứa sản phẩm \ (y_i \ alpha_i \), >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 44 chứa các vectơ hỗ trợ và >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 48 giữ thuật ngữ độc lập \ (b \)\(y_i \alpha_i\), >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 44 which holds the support vectors, and >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 48 which holds the independent term \(b\) Ghi chú Trong khi các mô hình SVM có nguồn gốc từ LIBSVM và LiBLinear sử dụng >>> import numpy as np >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn import svm >>> X, y = make_classification(n_samples=10, random_state=0) >>> X_train , X_test , y_train, y_test = train_test_split(X, y, random_state=0) >>> clf = svm.SVC(kernel='precomputed') >>> # linear kernel computation >>> gram_train = np.dot(X_train, X_train.T) >>> clf.fit(gram_train, y_train) SVC(kernel='precomputed') >>> # predict on training examples >>> gram_test = np.dot(X_test, X_train.T) >>> clf.predict(gram_test) array([0, 1, 0])4 làm tham số chính quy, hầu hết các công cụ ước tính khác đều sử dụng >>> # get support vectors >>> clf.support_vectors_ array([[0., 0.], [1., 1.]]) >>> # get indices of support vectors >>> clf.support_ array([0, 1]...) >>> # get number of support vectors for each class >>> clf.n_support_ array([1, 1]...)08. Sự tương đương chính xác giữa số lượng chính quy hóa của hai mô hình phụ thuộc vào hàm mục tiêu chính xác được tối ưu hóa bởi mô hình. Ví dụ: khi công cụ ước tính được sử dụng là hồi quy >>> # get support vectors >>> clf.support_vectors_ array([[0., 0.], [1., 1.]]) >>> # get indices of support vectors >>> clf.support_ array([0, 1]...) >>> # get number of support vectors for each class >>> clf.n_support_ array([1, 1]...)09, mối quan hệ giữa chúng được đưa ra là \ (c = \ frac {1} {alpha} \).\(C = \frac{1}{alpha}\). 1.4.7.2. Tuyến tínhLinearSVC¶Vấn đề nguyên thủy có thể được xây dựng tương đương \ [\ min_ {w, b} \ frac {1} {2} w^t w + c \ sum_ {i = 1}^{n} \ max (0, 1 - y_i ) + b)), \] nơi chúng tôi sử dụng mất bản lề. Đây là hình thức được tối ưu hóa trực tiếp bởi >>> clf.predict([[2., 2.]]) array([1])8, nhưng không giống như dạng kép, loại này không liên quan đến các sản phẩm bên trong giữa các mẫu, do đó, thủ thuật hạt nhân nổi tiếng không thể được áp dụng. Đây là lý do tại sao chỉ có hạt nhân tuyến tính được hỗ trợ bởi >>> clf.predict([[2., 2.]]) array([1])8 (\ (\ Phi \) là hàm nhận dạng).\(\phi\) is the identity function). 1.4.7.3. Nusvc¶NuSVC¶Công thức \ (\ nu \)-SVC [15] là một sự sắp xếp lại của \ (c \)-SVC và do đó tương đương về mặt toán học.\(\nu\)-SVC formulation [15] is a reparameterization of the \(C\)-SVC and therefore mathematically equivalent. Chúng tôi giới thiệu một tham số mới \ (\ nu \) (thay vì \ (c \))) để kiểm soát số lượng vectơ hỗ trợ và lỗi lề: \ (\ nu \ in (0, 1] \) là một giới hạn trên trên một phần lỗi lề và giới hạn thấp hơn của tỷ lệ vectơ hỗ trợ. Một lỗi lề tương ứng với một mẫu nằm ở phía sai của ranh giới lề của nó: nó được phân loại sai, hoặc nó được phân loại chính xác nhưng không nằm ngoài lề .\(\nu\) (instead of \(C\)) which controls the number of support vectors and margin errors: \(\nu \in (0, 1]\) is an upper bound on the fraction of margin errors and a lower bound of the fraction of support vectors. A margin error corresponds to a sample that lies on the wrong side of its margin boundary: it is either misclassified, or it is correctly classified but does not lie beyond the margin. 1.4.7.4. Svr¶SVR¶Đã cho các vectơ đào tạo \ (x_i \ in \ mathbb {r}^p \), i = 1, triệt, n và một vector \ (y \ in \ mathbb {r}^n \) \ (\ varepsilon \)- SVR giải quyết vấn đề nguyên thủy sau:\(x_i \in \mathbb{R}^p\), i=1,…, n, and a vector \(y \in \mathbb{R}^n\) \(\varepsilon\)-SVR solves the following primal problem: \ [\ started {align} \ start n} (\ zeta_i + \ zeta_i^*) T \ Phi (x_i) + b - y_i \ leq \ varepsilon + \ zeta_i^*, \\ & \ zeta_i, \ zeta_i^* \ geq 0, i = 1, ..., n \ end {split} \ end {căn chỉnh} \ end {align} \] Ở đây, chúng tôi đang xử phạt các mẫu có dự đoán ít nhất là \ (\ varepsilon \) khỏi mục tiêu thực sự của chúng. Các mẫu này xử phạt mục tiêu bằng \ (\ zeta_i \) hoặc \ (\ zeta_i^*\), tùy thuộc vào việc dự đoán của chúng nằm ở trên hay dưới ống \ (\ varepsilon \).\(\varepsilon\) away from their true target. These samples penalize the objective by \(\zeta_i\) or \(\zeta_i^*\), depending on whether their predictions lie above or below the \(\varepsilon\) tube. Vấn đề kép là \ [\ started {align} \ start ) \ alpha^*) = 0 \\ & 0 \ leq \ alpha_i, \ alpha_i^* \ leq c, i = 1, ..., n \ end {split} \ end {căn chỉnh} \ end Trong đó \ (e \) là vectơ của tất cả các kết hợp, \ (q \) là một ma trận semidefinite dương \ (n \) của \ (n \) \ Phi (x_i)^t \ Phi (x_j) \) là hạt nhân. Ở đây các vectơ đào tạo được ánh xạ ngầm vào một không gian chiều cao hơn (có thể vô hạn) bằng hàm \ (\ Phi \).\(e\) is the vector of all ones, \(Q\) is an \(n\) by \(n\) positive semidefinite matrix, \(Q_{ij} \equiv K(x_i, x_j) = \phi (x_i)^T \phi (x_j)\) is the kernel. Here training vectors are implicitly mapped into a higher (maybe infinite) dimensional space by the function \(\phi\). Dự đoán là: \ [\ sum_ {i \ in sv} (\ alpha_i - \ alpha_i^*) k (x_i, x) + b \] Các tham số này có thể được truy cập thông qua các thuộc tính >>> from sklearn import svm >>> X = [[0, 0], [2, 2]] >>> y = [0.5, 2.5] >>> regr = svm.SVR() >>> regr.fit(X, y) SVR() >>> regr.predict([[1, 1]]) array([1.5])9 chứa sự khác biệt \ (\ alpha_i - \ alpha_i^*\), >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 44 giữ các vectơ hỗ trợ và >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 48 giữ thuật ngữ độc lập \ (b \)\(\alpha_i - \alpha_i^*\), >>> X = [[0], [1], [2], [3]] >>> Y = [0, 1, 2, 3] >>> clf = svm.SVC(decision_function_shape='ovo') >>> clf.fit(X, Y) SVC(decision_function_shape='ovo') >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes: 4*3/2 = 6 6 >>> clf.decision_function_shape = "ovr" >>> dec = clf.decision_function([[1]]) >>> dec.shape[1] # 4 classes 44 which holds the support vectors, and >>> lin_clf = svm.LinearSVC() >>> lin_clf.fit(X, Y) LinearSVC() >>> dec = lin_clf.decision_function([[1]]) >>> dec.shape[1] 48 which holds the independent term \(b\) 1.4.7.5. Tuyến tínhLinearSVR¶Vấn đề nguyên thủy có thể được xây dựng tương đương \]| - \ varepsilon), \] trong đó chúng ta sử dụng tổn thất không nhạy cảm với epsilon, tức là các lỗi nhỏ hơn \ (\ varepsilon \) bị bỏ qua.Đây là hình thức được tối ưu hóa trực tiếp bởi >>> clf.predict([[2., 2.]]) array([1])02.\(\varepsilon\) are ignored. This is the form that is directly optimized by >>> clf.predict([[2., 2.]]) array([1])02. 1.4.8.Chi tiết thực hiện JoImplementation details¶Trong nội bộ, chúng tôi sử dụng libsvm [12] và liblinear [11] để xử lý tất cả các tính toán.Các thư viện này được bọc bằng C và Cython.Để biết mô tả về việc thực hiện và chi tiết của các thuật toán được sử dụng, vui lòng tham khảo các bài báo tương ứng của họ.[12] and liblinear [11] to handle all computations. These libraries are wrapped using C and Cython. For a description of the implementation and details of the algorithms used, please refer to their respective papers. |