Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

python - How do I specify areas of 0 probability in a contour plot produced from a Gaussian process classifier?

I am applying a Gaussian process classifier via the following code. I then produce the figure seen below. My x_2 variable cannot be negative, so the 0.1 - 0.4 probability contours physically do not make sense, because they continue into negative x_2 space. I know the Gaussian algorithm does not "know" that a negative x_2 value is unreasonable, so it is just filling in the specified meshgrid based on the training data. I was wondering, however, if there is some way to specify that a given area should have a 0 probability when producing this type of plot. Here I would like to force x_2 to be positive, so the 0.1 - 0.4 contour lines wrap around to run more or less parallel to the x_1 axis. I hope this makes sense — if you would like me to clarify anything, please let me know. Thanks in advance!

# import packages
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF

gpc = GaussianProcessClassifier(1.0*RBF(1.0)).fit(X_train,y_train)
test_score = gpc.score(X_test,y_test)

print('the test score is', round(test_score,4))
# calculate probability meshgrid
xx, yy = np.mgrid[1:18:0.1, 0:5.5:0.1]
grid = np.c_[xx.ravel(), yy.ravel()]
probs = gpc.predict_proba(grid)[:,1].reshape(xx.shape)

# plot figure
fig, ax = plt.subplots(figsize=(10,7))
contour_lines = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
contour = ax.contour(xx, yy, probs,levels=contour_lines,
                     cmap='RdBu',
                     vmin=0, vmax=1,
                     zorder=0)
ax.clabel(contour,fontsize=12)

plt.scatter(data1['x1'],data1['x2'],
            lw=0.3,edgecolor='black',alpha=0.6,c='midnightblue',marker='s')
plt.scatter(data2['x1'],data2['x2'],
            lw=0.3,edgecolor='black',alpha=0.6,c='firebrick',marker='o')
ax.set_xlim([1,17.5])
plt.show()

Contour plot

question from:https://stackoverflow.com/questions/65929485/how-do-i-specify-areas-of-0-probability-in-a-contour-plot-produced-from-a-gaussi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...