.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/svm/plot_oneclass.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_svm_plot_oneclass.py: ========================================== 使用非线性核(RBF)的单类SVM ========================================== 使用单类SVM进行新颖性检测的示例。 :ref:`单类SVM ` 是一种无监督算法, 它学习一个用于新颖性检测的决策函数:将新数据分类为与训练集相似或不同。 .. GENERATED FROM PYTHON SOURCE LINES 14-37 .. code-block:: Python import numpy as np from sklearn import svm # 生成训练数据 X = 0.3 * np.random.randn(100, 2) X_train = np.r_[X + 2, X - 2] # 生成一些常规的新颖观察 X = 0.3 * np.random.randn(20, 2) X_test = np.r_[X + 2, X - 2] # 生成一些异常的新观测值 X_outliers = np.random.uniform(low=-4, high=4, size=(20, 2)) # 拟合模型 clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1) clf.fit(X_train) y_pred_train = clf.predict(X_train) y_pred_test = clf.predict(X_test) y_pred_outliers = clf.predict(X_outliers) n_error_train = y_pred_train[y_pred_train == -1].size n_error_test = y_pred_test[y_pred_test == -1].size n_error_outliers = y_pred_outliers[y_pred_outliers == 1].size .. GENERATED FROM PYTHON SOURCE LINES 38-102 .. code-block:: Python import matplotlib.font_manager import matplotlib.lines as mlines import matplotlib.pyplot as plt from sklearn.inspection import DecisionBoundaryDisplay _, ax = plt.subplots() # 生成边界显示的网格 xx, yy = np.meshgrid(np.linspace(-5, 5, 10), np.linspace(-5, 5, 10)) X = np.concatenate([xx.reshape(-1, 1), yy.reshape(-1, 1)], axis=1) DecisionBoundaryDisplay.from_estimator( clf, X, response_method="decision_function", plot_method="contourf", ax=ax, cmap="PuBu", ) DecisionBoundaryDisplay.from_estimator( clf, X, response_method="decision_function", plot_method="contourf", ax=ax, levels=[0, 10000], colors="palevioletred", ) DecisionBoundaryDisplay.from_estimator( clf, X, response_method="decision_function", plot_method="contour", ax=ax, levels=[0], colors="darkred", linewidths=2, ) s = 40 b1 = ax.scatter(X_train[:, 0], X_train[:, 1], c="white", s=s, edgecolors="k") b2 = ax.scatter(X_test[:, 0], X_test[:, 1], c="blueviolet", s=s, edgecolors="k") c = ax.scatter(X_outliers[:, 0], X_outliers[:, 1], c="gold", s=s, edgecolors="k") plt.legend( [mlines.Line2D([], [], color="darkred"), b1, b2, c], [ "learned frontier", "training observations", "new regular observations", "new abnormal observations", ], loc="upper left", prop=matplotlib.font_manager.FontProperties(size=11), ) ax.set( xlabel=( f"error train: {n_error_train}/200 ; errors novel regular: {n_error_test}/40 ;" f" errors novel abnormal: {n_error_outliers}/40" ), title="Novelty Detection", xlim=(-5, 5), ylim=(-5, 5), ) plt.show() .. image-sg:: /auto_examples/svm/images/sphx_glr_plot_oneclass_001.png :alt: Novelty Detection :srcset: /auto_examples/svm/images/sphx_glr_plot_oneclass_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.063 seconds) .. _sphx_glr_download_auto_examples_svm_plot_oneclass.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/svm/plot_oneclass.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_oneclass.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_oneclass.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_oneclass.zip ` .. include:: plot_oneclass.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_