调用上面定义的plot_decision_boundary函数来绘制决策边界和数据点。 python # 绘制决策边界 plot_decision_boundary(model, X, y) 通过上述步骤,你可以在Python中成功绘制出机器学习模型的决策边界。这不仅有助于理解模型的工作原理,还能为数据分析提供更直观的展示。
decision_function_shape="ovo") clf.fit(X, y) plot_decision_boundary(lambda x: clf.predict(x)) plt.title("SVM-Nonlinearity") plt.show() #预测测试集数据 y_train_pred = svmmodel.predict(X_train) print("SVM模型预测训练集300例数据结果如下:\n",y_train_pred) y_test_pred = svmmodel.pr...
X_moon, Y_moon = get_moons_data(500, 0.5) plot_decision_boundary(X_moon, Y_moon, net=True) 用sklearn中的LogisticRegressionCV 利用nn.Module 不足之处 如果要验证复杂的算法,那么生成的数据就二维是不是不太行?利用插值(sklearn中的PolynomialFeatures)映射一下? 的两边如何用颜色覆盖?还没尝试出来。
np.arange(y_min, y_max, h)) # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, m_max]x[y_min, y_max]. Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Put the result into a color plot Z = Z.reshape(xx.shape) p...
defplot_decision_boundary(model,axis): x0,x1 = np.meshgrid( np.linspace(axis[0],axis[1],int((axis[1]-axis[0])*100)), np.linspace(axis[2],axis[3],int((axis[3]-axis[2])*100)) ) X_new = np.c_[x0.ravel(),x1.ravel()] ...
plt.show() 得到图如下: 定义决策边界函数: # 咱们先顶一个一个函数来画决策边界 def plot_decision_boundary(pred_func): # 设定最大最小值,附加一点点边缘填充 x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5 y_min, y_max = X[:, 1].min() - .5, X[:, 1].ma...
def plot_decision_boundary(model,axis): x0,x1 = np.meshgrid( np.linspace(axis[0],axis[1],int((axis[1]-axis[0])*100)), np.linspace(axis[2],axis[3],int((axis[3]-axis[2])*100)) ) X_new = np.c_[x0.ravel(),x1.ravel()] ...
defplot_decision_boundary(X, theta):# X --> Inputs# theta --> parameters# The Line is y=mx+c# So, Equate mx+c = theta0.X0 + theta1.X1 + theta2.X2# Solving we find m and cx1 = [min(X[:,0]),max(X[:,0])]
plot_decision_boundary(rbf,[-1.5,2.5,-1.0,1.5]) plt.scatter(x[y==0,0],x[y==0,1]) plt.scatter(x[y==1,0],x[y==1,1]) plt.show() 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.
Note: You can also reduce the data dimensions to 2 with a method such as PCA and then plot the model's decision boundary. Doing this would result in the same code as the previous one, just substituting X_train_cols for the resulting principal components. Plotting Decision Boundaries - Addit...