python - '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)
解决方案
如果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。
推荐阅读
- haskell - 这种类型的孔怎么塞?
- elasticsearch - Kibana error on startup (incompatible nodes)
- javascript - 检查函数值而不是返回值时显示警告
- python - Importing modules that import other modules from other directories in Python 3.6
- reactjs - React 开发工具空白组件
- git - 创建新分支
- r - 如何使用具有匹配变量的不同长度的两个不同数据帧减去一个值
- java - 使用“mimeType contains”查询的 Google Drive API 搜索无法正常工作
- c# - 如何将具有相同名称的 XML 元素和属性值获取到类对象?
- excel - 如何创建一个 VBA 循环来根据列中未知数量的重复值求和?