【问题标题】:Plotting 3D Decision Boundary From MLPClassifier By Using make_classification Dataset使用 make_classification 数据集从 MLPClassifier 绘制 3D 决策边界
【发布时间】:2021-03-07 19:07:08
【问题描述】:

我使用了来自 sklearn 的 make_classification 库和 MLPClassifier。但是,我无法像this screenshot 那样将我的观点分开。而这个screenshot 就是我的情节所显示的。你能帮我分开点还是有什么问题?

我的代码是:

from sklearn.datasets import make_classification
X,y=make_classification(n_samples=550, n_features=10, n_informative=2,random_state=0)

from sklearn.model_selection import train_test_split
X_train,X_test , y_train , y_test = train_test_split(X, y, test_size=0.3, random_state=1)


from sklearn.neural_network import MLPClassifier
mlp= MLPClassifier(hidden_layer_sizes=(), max_iter=300, random_state=0)
    
clf = mlp.fit(X_test, y_test)

z = lambda x,y: (-clf.intercepts_[0]-clf.coefs_[0][0]*x -clf.coefs_[0][1]*y) / clf.coefs_[0][2]
                  
tmp = np.linspace(-5,5,30)
x,y = np.meshgrid(tmp,tmp)

fig = plt.figure()
ax  = fig.add_subplot(111, projection='3d')

ax.scatter(X_test[y_test==0,0]+2, X_test[y_test==0,1]-2, X_test[y_test==0,2]-5, c='b', marker='^')
ax.scatter(X_test[y_test==1,0]-2, X_test[y_test==1,1]+2, X_test[y_test==1,2]+5, c='r', marker='o')

ax.plot_surface(x, y, z(x,y))
ax.view_init(30, 60)
plt.show()

【问题讨论】:

    标签: matplotlib scikit-learn


    【解决方案1】:

    您的代码没有任何(严重)错误。你需要设置一些参数来得到你想要的。首先,您可能需要更大的数字。

    # To set figure size other than the default value, specify width and height
    fig = plt.figure( figsize=(8,8) )
    

    其次,对于scatter()函数中标记的大小:

    # To set marker size, use `s=number` as an option
    ax.scatter(X_test[y_test==0,0]+2, X_test[y_test==0,1]-2, X_test[y_test==0,2]-5, \
        c='b', marker='^', s=3)
    ax.scatter(X_test[y_test==1,0]-2, X_test[y_test==1,1]+2, X_test[y_test==1,2]+5, \
        c='r', marker='o', s=3)
    

    情节应该是这样的:

    【讨论】:

      猜你喜欢
      • 2016-07-13
      • 2017-10-09
      • 2014-02-26
      • 2014-11-27
      • 2012-12-13
      • 2020-09-25
      • 2014-08-07
      • 2013-10-03
      • 2021-07-30
      相关资源
      最近更新 更多