gpt4 book ai didi

python - 如何使用 matplotlib 绘制回归的决策边界?

转载 作者:行者123 更新时间:2023-11-28 21:57:17 24 4
gpt4 key购买 nike

如何将逻辑回归结果的等高线图添加到我的散点图中?我想要彩色 0/1 区域,它描绘了分类器的决策边界。

import pandas as pd
import numpy as np
import pylab as pl
import statsmodels.api as sm

# Build X, Y from file
f = open('ex2data2.txt')
lines = f.readlines()
x1 = []
x2 = []
y = []
for line in lines:
line = line.replace("\n", "")
vals = line.split(",")
x1.append(float(vals[0]))
x2.append(float(vals[1]))
y.append(int(vals[2]))

x1 = np.array(x1)
x2 = np.array(x2)
y = np.array(y)

x = np.vstack([x1, x2]).T

# Scatter plot 0/1s
pos_mask = y == 1
neg_mask = y == 0
pos_x1 = x1[pos_mask]
neg_x1 = x1[neg_mask]
pos_x2 = x2[pos_mask]
neg_x2 = x2[neg_mask]
pl.clf()
pl.scatter(pos_x1, pos_x2, c='r')
pl.scatter(neg_x1, neg_x2, c='g')

# Run logistic regression
logit = sm.Logit(y, x)
result = logit.fit()
result.params
result.predict([1.0, 1.0])

# Now I want to add a countour for 0/1 regression results to the scatter plot.

最佳答案

我会尽力回答,但是您必须了解我的回答中的一些假设,这些假设可能适用于您的代码,也可能不适用于您的代码:

我的导入:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

X 包含您的功能,它看起来像这样:

print type(X)
<type 'numpy.ndarray'>

如图所示为102,2:

print X
[[-13.15490196 -23. ]
[-22.95490196 -25. ]
[-12.75490196 -8. ]
[ 0.14509804 -6. ]
.
.
.

ytrain 包含基本事实,在本例中为 bool 值,但您也可以同样执行 0/1。

print type(ytrain)
<type 'numpy.ndarray'>

是51,

print (train)
[False False False False True True True True True True False True
False True True True False False False True True True True True
False False False False True True True True True True False True
False True True True False False False False False True True True
False True False]

最后 clf 包含你的模型,在我的例子中是一个拟合模型我还使用 scikit learn 中的 LogisticRegression,这依赖于我的 clf.predict_proba提供构建标签和轮廓所需的信息。我不熟悉您正在使用的确切包,但请记住这一点。

# evenly sampled points
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 50),
np.linspace(y_min, y_max, 50))
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())

#plot background colors
ax = plt.gca()
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
Z = Z.reshape(xx.shape)
cs = ax.contourf(xx, yy, Z, cmap='RdBu', alpha=.5)
cs2 = ax.contour(xx, yy, Z, cmap='RdBu', alpha=.5)
plt.clabel(cs2, fmt = '%2.1f', colors = 'k', fontsize=14)

# Plot the points
ax.plot(Xtrain[ytrain == 0, 0], Xtrain[ytrain == 0, 1], 'ro', label='Class 1')
ax.plot(Xtrain[ytrain == 1, 0], Xtrain[ytrain == 1, 1], 'bo', label='Class 2')

# make legend
plt.legend(loc='upper left', scatterpoints=1, numpoints=1)

您的结果将如下所示:

enter image description here

关于python - 如何使用 matplotlib 绘制回归的决策边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20045994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com