简体   繁体   English

绘制具有3个特征的Binary Logistic回归

[英]Plotting Binary Logistic regression with 3 features

I have built a logistic regression model for binary classification. 我已经建立了用于二进制分类的逻辑回归模型。 I am trying to develop a visualisation but unable to. 我正在尝试开发可视化效果,但是无法。 I can do it with two features but my model uses 3 features and hence struggling to convert it into a 3d graph. 我可以使用两个功能来做到这一点,但是我的模型使用了三个功能,因此很难将其转换为3d图形。 Below is the code i have tried but doesnt work. 以下是我尝试过但无法正常工作的代码。 I would be grateful if someone could guide me in this please. 如果有人可以指导我,我将不胜感激。 I want to be able to visualise my results. 我希望能够可视化我的结果。 i am still new to visualising graph. 我还是可视化图形的新手。

from matplotlib.colors import ListedColormap
X_set, y_set = X_train, y_train
X1, X2, X3 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
                     np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01),
                     np.arange(start = X_set[:, 2].min() - 1, stop = X_set[:, 2].max() + 1, step = 0.01))
plt.contourf(X1, X2, X3, classifier.predict(np.array([X1.ravel(), X2.ravel(), X3.ravel()]).T).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
plt.ylim(X3.min(), X3.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
                c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('Logistic Regression (Training set)')
plt.xlabel('Recency')
plt.ylabel('Frequency')
plt.zlabel('Monetary')
plt.legend()
plt.show()

plt.contourf(x, y, z) would work fine for visualising two features (x, y) and a prediction function, which depends on these two features. plt.contourf(x,y,z)对于可视化两个特征(x,y)和一个取决于这两个特征的预测函数会很好地工作。 The predictions z must be a 2D array. 预测z必须是2D数组。 With three features you could fix one of them and then make a 2D contour plots using plt.contourf(). 使用三个功能,您可以修复其中之一,然后使用plt.contourf()进行2D等高线图绘制。 To get a more complete picture, repeat this with the feature fixed to another value and/or fixing one of the other features. 要获得更完整的图片,请重复此操作,将功能固定为另一个值和/或固定其他功能之一。

Or plot your training set in 3D with a scatter plot: https://matplotlib.org/2.1.1/gallery/mplot3d/scatter3d.html 或使用散点图绘制3D训练集: https//matplotlib.org/2.1.1/gallery/mplot3d/scatter3d.html

True and predicted labels could be visualised using different markers for the four possible outcomes: (true=1, pred=1), (true=1, pred=0), (true=0, pred=1) and (true=0, pred=0). 可以使用针对四个可能结果的不同标记来显示真实标签和预测标签:(true = 1,pred = 1),(true = 1,pred = 0),(true = 0,pred = 1)和(true = 0 ,pred = 0)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM