2017-05-19 94 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,道具)。您將需要根據自己的意圖重塑陣列... –