首页 > 解决方案 > 'y.values == 0,1' 的计算结果是什么,y 是熊猫系列的 0 和 1?

问题描述

y.values是一个熊猫系列,它的每个元素要么是 1,要么是零。

我的问题是什么逻辑y.values == 0,1评估为真或假?

我正在尝试绘制逻辑回归模型的决策边界和我正在使用的代码:

def plot_mushroom_boundary(X, y, fitted_model):

    plt.figure(figsize=(9.8,5), dpi=100)

    for i, plot_type in enumerate(['Decision Boundary', 'Decision Probabilities']):
        plt.subplot(1,2,i+1)

        mesh_step_size = 0.01  # step size in the mesh
        x_min, x_max = X[:, 0].min() - .1, X[:, 0].max() + .1
        y_min, y_max = X[:, 1].min() - .1, X[:, 1].max() + .1
        xx, yy = np.meshgrid(np.arange(x_min, x_max, mesh_step_size), np.arange(y_min, y_max, mesh_step_size))
        if i == 0:
            Z = fitted_model.predict(np.c_[xx.ravel(), yy.ravel()])
        else:
            try:
                Z = fitted_model.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:,1]
            except:
                plt.text(0.4, 0.5, 'Probabilities Unavailable', horizontalalignment='center',
                 verticalalignment='center', transform = plt.gca().transAxes, fontsize=12)
                plt.axis('off')
                break
        Z = Z.reshape(xx.shape)
        plt.scatter(X[y.values==0,0], X[y.values==0,1], alpha=0.4, label='Edible', s=5)
        plt.scatter(X[y.values==1,0], X[y.values==1,1], alpha=0.4, label='Posionous', s=5)
        plt.imshow(Z, interpolation='nearest', cmap='RdYlBu_r', alpha=0.15, 
               extent=(x_min, x_max, y_min, y_max), origin='lower')
        plt.title(plot_type + '\n' + 
              str(fitted_model).split('(')[0]+ ' Test Accuracy: ' + 
str(np.round(fitted_model.score(X, y), 5)))
        plt.gca().set_aspect('equal');
    
    plt.tight_layout()
    plt.subplots_adjust(top=0.9, bottom=0.08, wspace=0.02)

标签: pythonpandas

解决方案


如果X是一个二维数组和y一个熊猫系列,那么

y.values`    # 1d array
y.values==0    # boolean 1d array, True where y.values is 0

那么这个表达式:

X[y.values==0, 0]

X对 的第一个维度进行布尔索引和列的 0 标量索引是有意义的。X即where的第一列y是 0。


推荐阅读