码迷,mamicode.com
首页 > 其他好文 > 详细

如何用matplotlib绘制决策边界

时间:2019-01-19 19:57:30      阅读:464      评论:0      收藏:0      [点我收藏+]

标签:amp   lin   UNC   alt   width   with   red   add   str   

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绘制决策边界

标签:amp   lin   UNC   alt   width   with   red   add   str   

原文地址:https://www.cnblogs.com/douzujun/p/10292689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!