machine-learning - 如何在python中绘制正确的超平面
问题描述
我的代码:
我的绘图功能:
def draw_hyper_plane(coef,intercept,y_max,y_min):
points=np.array([[((-coef*y_min - intercept)/coef), y_min],[((-coef*y_max - intercept)/coef), y_max]])
plt.plot(points[:,0], points[:,1])
实际输出:
期望的输出:
通过我的代码,我无法找到正确的超平面,该超平面可以正确地将点分类为所需的输出图中。任何人都可以帮助我吗
解决方案
一种方法是使用decision_function
分类器中的 并绘制一些水平线(level=0 对应于您的超平面)。这是一些代码。
def plot_2d_separator(classifier, X, fill=False, ax=None, eps=None):
if eps is None:
eps = X.std() / 2.
x_min, x_max = X[:, 0].min() - eps, X[:, 0].max() + eps
y_min, y_max = X[:, 1].min() - eps, X[:, 1].max() + eps
xx = np.linspace(x_min, x_max, 100)
yy = np.linspace(y_min, y_max, 100)
X1, X2 = np.meshgrid(xx, yy)
X_grid = np.c_[X1.ravel(), X2.ravel()]
try:
decision_values = classifier.decision_function(X_grid)
levels = [0]
fill_levels = [decision_values.min(), 0, decision_values.max()]
except AttributeError:
# no decision_function
decision_values = classifier.predict_proba(X_grid)[:, 1]
levels = [.5]
fill_levels = [0, .5, 1]
if ax is None:
ax = plt.gca()
if fill:
ax.contourf(X1, X2, decision_values.reshape(X1.shape),
levels=fill_levels, colors=['tab:blue', 'tab:orange'],
alpha=0.5)
else:
ax.contour(X1, X2, decision_values.reshape(X1.shape), levels=levels,
colors="black")
ax.set_xlim(x_min, x_max)
ax.set_ylim(y_min, y_max)
ax.set_xticks(())
ax.set_yticks(())
这段代码是在那里开发的
推荐阅读
- javascript - data-ng-repeat 标记为注释
- r - 从 R 中的 SQLite 中的表中收集()
- sql-server-data-tools - 使用 DacServices.Deploy 发布 DACPAC (SSDT) 期间的执行超时错误
- php - PHPUnit 未定义的方法调用
- python - python中的文件路径错误
- java - 在数据框 spark 1.6 java 中将字符串转换为日期或整数
- python - 如何对 csv.writer 返回的对象进行类型注释?
- javascript - 单击子菜单页面时如何发出ajax请求?
- sql - regexp_replace 和 regexp_substr 变量的比较
- raspberry-pi3 - 在 RPi B+ 上构建 Yocto 镜像