matplotlib 连续等值线图

6mw9ycah  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(138)

这是我的代码来寻找异常值。我想在没有异常值的情况下绘制一条连续的等高线。我得到的是两条孤立的等高线。所以,我只想得到一条等高线(连接的等高线),不管形状如何。谢谢。

import numpy as np
from sklearn.svm import OneClassSVM
import matplotlib.pyplot as plt

ss = [[9.0, 1.0],[1.78, 2.14],[7.36, 2.67],[5.95, 2.5 ],[2.59, 2.87],[1.76, 2.45],[1.87, 2.45],[2.15, 2.61],[1.64, 2.17],[1.35, 2.27],
      [2.16, 2.3 ],[1.48, 2.32],[1.73, 2.41],[1.73, 2.39],[3.87, 1.38],[1.81, 2.7 ],[1.92, 2.72],[1.57, 2.62],[1.59, 2.48],[3.1 , 2.56]]
ssa = np.asarray(ss)
X1 = ssa
# Learn a frontier for outlier detection with several classifiers
xx1, yy1 = np.meshgrid(np.linspace(0, 10, 500), np.linspace(0, 6, 500))
model_1 = OneClassSVM(nu=0.25, gamma=0.35)
model_1.fit(X1) # clf.fit(X1)

Z1 = model_1.decision_function(np.c_[xx1.ravel(), yy1.ravel()]) # Z1 = clf.decision_function(np.c_[xx1.ravel(), yy1.ravel()])
Z1 = Z1.reshape(xx1.shape)

plt.figure(1)
plt.contour(xx1, yy1, Z1, levels=[0], linewidths=2, colors='r') #'''
plt.scatter(X1[:, 0], X1[:, 1], color="black")
plt.show()

的复数
我想得到这样一个情节

4nkexdtk

4nkexdtk1#

关键不在于如何让matplotlib绘制,而在于如何让模型(OneClassSVM)生成我们想要的东西。
由于你使用的是无监督学习技术,有时我们不得不调整模型的参数,以获得我们认为是最好的(作为外部“监督者”)。
因此,在您的案例中,通过使用OneClassSVM和默认内核RBF,您可以模拟nugammatol等:

model_1 = OneClassSVM(nu=0.25, gamma=0.12)

相关问题