簡體   English   中英

繪制高維數據的決策邊界

[英]Plotting decision boundary for High Dimension Data

我正在構建二進制分類問題的模型,其中我的每個數據點都是300維 (我使用300個特征)。 我正在使用sklearnPassiveAggressiveClassifier 該模型表現非常好。

我希望繪制模型的決策邊界。 我怎么能這樣做?

為了了解數據,我使用TSNE在2D中繪制它。 我通過兩個步驟減少了數據的維度 - 從300到50,然后從50減少到2(這是一個常見的推薦)。 以下是相同的代碼段:

from sklearn.manifold import TSNE
from sklearn.decomposition import TruncatedSVD

X_Train_reduced = TruncatedSVD(n_components=50, random_state=0).fit_transform(X_train)
X_Train_embedded = TSNE(n_components=2, perplexity=40, verbose=2).fit_transform(X_Train_reduced)

#some convert lists of lists to 2 dataframes (df_train_neg, df_train_pos) depending on the label - 

#plot the negative points and positive points
scatter(df_train_neg.val1, df_train_neg.val2, marker='o', c='red')
scatter(df_train_pos.val1, df_train_pos.val2, marker='x', c='green')

數據圖

我得到了一個體面的圖表。

有沒有辦法可以為這個圖添加一個決策邊界,它代表我模型在300昏暗空間中的實際決策邊界?

一種方法是在2D繪圖上施加Voronoi tesselation,即基於與2D數據點的接近度(每個預測類標簽的不同顏色)對其進行着色。 參見Migut等人最近的論文,2015年

這比使用meshgrid和scikit的KNeighborsClassifier(這是一個帶有Iris數據集的端到端示例;用模型/代碼替換前幾行)要容易得多:

import numpy as np, matplotlib.pyplot as plt
from sklearn.neighbors.classification import KNeighborsClassifier
from sklearn.datasets.base import load_iris
from sklearn.manifold.t_sne import TSNE
from sklearn.linear_model.logistic import LogisticRegression

# replace the below by your data and model
iris = load_iris()
X,y = iris.data, iris.target
X_Train_embedded = TSNE(n_components=2).fit_transform(X)
print X_Train_embedded.shape
model = LogisticRegression().fit(X,y)
y_predicted = model.predict(X)
# replace the above by your data and model

# create meshgrid
resolution = 100 # 100x100 background pixels
X2d_xmin, X2d_xmax = np.min(X_Train_embedded[:,0]), np.max(X_Train_embedded[:,0])
X2d_ymin, X2d_ymax = np.min(X_Train_embedded[:,1]), np.max(X_Train_embedded[:,1])
xx, yy = np.meshgrid(np.linspace(X2d_xmin, X2d_xmax, resolution), np.linspace(X2d_ymin, X2d_ymax, resolution))

# approximate Voronoi tesselation on resolution x resolution grid using 1-NN
background_model = KNeighborsClassifier(n_neighbors=1).fit(X_Train_embedded, y_predicted) 
voronoiBackground = background_model.predict(np.c_[xx.ravel(), yy.ravel()])
voronoiBackground = voronoiBackground.reshape((resolution, resolution))

#plot
plt.contourf(xx, yy, voronoiBackground)
plt.scatter(X_Train_embedded[:,0], X_Train_embedded[:,1], c=y)
plt.show()

請注意,這不會精確地繪制您的決策邊界,而只是粗略估計邊界應該位於何處(特別是在數據點較少的區域,真正的邊界可能與此不同)。 它將在屬於不同類的兩個數據點之間繪制一條線,但是將它放在中間(在這種情況下確實確保這些點之間存在決策邊界,但它不一定必須在中間) 。

還有一些實驗方法可以更好地逼近真正的決策邊界,例如github上的這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM