如何用matplotlib绘制决策边界
Posted douzujun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用matplotlib绘制决策边界相关的知识,希望对你有一定的参考价值。
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import sklearn.datasets
import sklearn.linear_model
def plot_decision_boundary(model, X, y):
# Set min and max values and give it some padding
x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1
y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1
h = 0.01
# Generate a grid of points with distance h between them
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
# Predict the function value for the whole grid
Z = model(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# Plot the contour and training examples
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
plt.ylabel(‘x2‘)
plt.xlabel(‘x1‘)
plt.scatter(X[0, :], X[1, :], c=y.ravel(), cmap=plt.cm.Spectral)
# Train the logistic regression classifier clf = sklearn.linear_model.LogisticRegressionCV(); clf.fit(X.T, Y.T);
# Plot the decision boundary for logistic regression plot_decision_boundary(lambda x: clf.predict(x), X, Y) # 预测X, Y对应坐标 plt.title("Logistic Regression") # Print accuracy LR_predictions = clf.predict(X.T) print (‘Accuracy of logistic regression: %d ‘ % float((np.dot(Y,LR_predictions) + np.dot(1-Y,1-LR_predictions))/float(Y.size)*100) + ‘% ‘ + "(percentage of correctly labelled datapoints)")
以上是关于如何用matplotlib绘制决策边界的主要内容,如果未能解决你的问题,请参考以下文章
使用 Matplotlib 的 pyplot 绘制分隔 2 个类的决策边界