在PCA决策面上标记点 – SVM

我刚刚为支持向量机创建了一个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()

enter image description here


回答:

为了获得交互式图表,我建议你使用 plotly

这将生成一个交互式图表,每次你将光标放在点上时,你都能看到相应的值。

欲了解更多信息,请访问此 页面

编辑:在你的情况下,我认为它看起来会像这样:

其中 df 是一个包含三列的数据框,分别是 test_pc1_coordstest_pc2_coordscountry

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注