我刚刚为支持向量机创建了一个PCA决策面。
我想查看每个点的标签。例如,我想点击一个点并读取它代表的行,比如一个点代表意大利,一个点代表阿尔巴尼亚,依此类推。
这是我的代码(非交互式)。
clf_svm = SVC(random_state=42, C=100, gamma=1)clf_svm.fit(pca_train_scaled, y_train)X_test_pca = pca.transform(X_train_scaled)test_pc1_coords = X_test_pca[:, 0] test_pc2_coords = X_test_pca[:, 1]x_min = test_pc1_coords.min() - 1x_max = test_pc1_coords.max() + 1y_min = test_pc2_coords.min() - 1y_max = test_pc2_coords.max() + 1xx, yy = np.meshgrid(np.arange(start=x_min, stop=x_max, step=0.1), np.arange(start=y_min, stop=y_max, step=0.1))Z = clf_svm.predict(np.column_stack((xx.ravel(), yy.ravel())))Z = Z.reshape(xx.shape)fig, ax = plt.subplots(figsize=(10,10))ax.contourf(xx, yy, Z, alpha=0.1)cmap = colors.ListedColormap(['#e41a1c', '#4daf4a'])scatter = ax.scatter(test_pc1_coords, test_pc2_coords, c=y_train, cmap=cmap, s=100, edgecolors='k', ## 'k' = black alpha=0.7)legend = ax.legend(scatter.legend_elements()[0], scatter.legend_elements()[1], loc="upper right")legend.get_texts()[0].set_text("No Default")legend.get_texts()[1].set_text("Default")ax.set_ylabel('PC2')ax.set_xlabel('PC1')ax.set_title('Decison surface using the PCA transformed/projected features')plt.show()
回答:
为了获得交互式图表,我建议你使用 plotly
这将生成一个交互式图表,每次你将光标放在点上时,你都能看到相应的值。
欲了解更多信息,请访问此 页面
编辑:在你的情况下,我认为它看起来会像这样:
其中 df
是一个包含三列的数据框,分别是 test_pc1_coords
、test_pc2_coords
和 country