2017-05-19 95 views
0

我有一个数据矩阵X它是n x 2和相应的二元标签阵列y,说第i个人是否是赢家。我试图用散热图创建一个散点图,其中显示了图上每个点的预测概率,即赢家。这是到目前为止我的代码2d scatter matplotlib背后的热图

import matplotlib.pyplot as plt 
from sklearn.externals import joblib 
from sklearn.linear_model import LogisticRegression 

X = joblib.load('X.pkl') 
y = joblib.load('y.pkl') 
lr = LogisticRegression() 
lr.fit(X, y) 
plt.scatter(X[y == 1, 0], X[y == 1, 1], color='r', label='winners', s=1) 
plt.scatter(X[y == 0, 0], X[y == 0, 1], color='b', label='losers', s=1) 
plt.legend() 
# Want to add a heatmap in the background for predicted probabilities here 
plt.show() 

从本质上讲,我希望背景是越红,其中预测的概率很高,更蓝的地方低。我可以使用lr.predict_proba(X)[:0]获得一组点的概率。

如何给背景着色,使得图中的每个点(x1,x2)根据其预测的获胜概率获得颜色?

+0

有你在这[问题]阅读解决方案(http://stackoverflow.com/questions/2369492/generate-a-heatmap-in-matplotlib-using-a-scatter-data-set)?作为热图的散射可能与您想要的不一样,但这可能对您有所帮助。 –

回答

0

你正在寻找的点被指定C值是什么,以及颜色表:

props = lr.predict_proba(X) 
plt.scatter(X[y == 1, 0], X[y == 1, 1], c=props[:, 1], cmap='Reds', label='winners', s=1) 
plt.scatter(X[y == 0, 0], X[y == 0, 1], c=props[:, 0], cmap='Blues', label='losers', s=1) 

如果你想不同的色彩映射表,检查该链接的完整列表: https://matplotlib.org/examples/color/colormaps_reference.html

+0

这几乎适用于我,但我在这个问题上有点不清楚。我想要为图上的每个点(不仅仅是X中的那个点)分配一种颜色(即整个图是彩色的) – michaelsnowden

+0

您可以尝试plt.pcolormesh(x,y,道具)。您将需要根据自己的意图重塑阵列... –