java - 使用 PCA 或类似的东西从文本文件中获取集群分配的可视化?
问题描述
我正在尝试执行 PCA、TSne 或其他某种降维技术,以从以下格式的文本文件中获取集群分配的可视化(其中显示的第一列是实例编号,显示的第二列是哪个集群实例属于。这可以做到吗?关于我将如何做到这一点的任何建议都会很棒。
0 1
1 0
2 1
3 0
4 0
5 0
6 1
7 0
8 1
9 0
10 1
11 1
12 1
13 1
14 1
15 0
解决方案
我在stackoverflow上的第一个答案(ahhh ^^')
对于 PCA 实现,请参见此处: Java 中的 PCA 实现
为了便于在 Java 中进行任何类型的绘图,我推荐 JavaFX ( https://openjfx.io/ )
这是一些使用 JavaFX 创建绘图的示例代码:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Screen;
import javafx.stage.Stage;
/*
* DIFFERENT CHART TYPES IN JAVA FX:
*
* JavaFX PieChart
JavaFX LineChart
JavaFX Area Chart
JavaFX BubbleChart
JavaFX BarChart
JavaFX ScatterChart
*
*/
public class MyCharts extends Application {
private double screenFac = 1.25;
private Rectangle2D screenSize;
private double width;
private double height;
private Pane root;
private Parent showMyCharts() {
root = createRoot();
/* we have to use NEW NumberAxis-Objects
* for each BarChart
* it is not possible to share the same NumberAxis-Object
* with different Bar charts
*/
final NumberAxis xAxis1 = new NumberAxis();
final NumberAxis xAxis2 = new NumberAxis();
final NumberAxis xAxis3 = new NumberAxis();
final CategoryAxis yAxis1 = new CategoryAxis();
final CategoryAxis yAxis2 = new CategoryAxis();
final CategoryAxis yAxis3 = new CategoryAxis();
final BarChart<Number, String> bc1 = new BarChart<Number, String>(xAxis1, yAxis1);
// here use other NumberAxis-Object
// final BarChart<Number, String> bc2 = new BarChart<Number, String>(xAxis2, yAxis2);
LineChart<Number, String> l1 = new LineChart<>(xAxis2, yAxis2);
// define BarChart settings
bc1.setTitle("myBarChart");
xAxis1.setLabel("Value");
xAxis1.setTickLabelRotation(90);
yAxis1.setLabel("Item");
l1.setTitle("myLineChart");
xAxis2.setLabel("Value");
// create some Data series
XYChart.Series<Number, String> series1 = new XYChart.Series<>();
final String itemA = "A";
final String itemB = "B";
final String itemC = "F";
series1.setName("2005");
series1.getData().add(new XYChart.Data<Number, String>(45.0D, itemA));
series1.getData().add(new XYChart.Data<Number, String>(55.0D, itemB));
series1.getData().add(new XYChart.Data<Number, String>(75.0D, itemC));
// series1.getData().add(new XYChart.Data(44, itemB));
// series1.getData().add(new XYChart.Data(18, itemC));
bc1.getData().add(series1);
// also XYChart.Series cannot be shared; we have to create a new Object for the line chart
XYChart.Series<Number, String> series2 = new XYChart.Series<>();
series2.setName("2005");
series2.getData().add(new XYChart.Data<Number, String>(45.0D, itemA));
series2.getData().add(new XYChart.Data<Number, String>(55.0D, itemB));
series2.getData().add(new XYChart.Data<Number, String>(75.0D, itemC));
l1.getData().add(series2);
/* piechart seems to be a bit special; you cann add the observeabel list directly if you create
* the piechart or you have to create PieChart.Data(..) if you want to add the data after creating the piechart-object
*/
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Grapefruit", 13),
new PieChart.Data("Oranges", 25),
new PieChart.Data("Plums", 10),
new PieChart.Data("Pears", 22),
new PieChart.Data("Apples", 30));
PieChart p2 = new PieChart(pieChartData);
PieChart p1 = new PieChart();
PieChart.Data slice1 = new PieChart.Data("Desktop", 213);
PieChart.Data slice2 = new PieChart.Data("Phone" , 67);
PieChart.Data slice3 = new PieChart.Data("Tablet" , 36);
p1.getData().add(slice1);
p1.getData().add(slice2);
p1.getData().add(slice3);
HBox hbox = new HBox();
VBox vBox1 = new VBox();
VBox vBox2 = new VBox();
vBox1.getChildren().addAll(l1, bc1);
vBox2.getChildren().addAll(p1, p2);
hbox.getChildren().addAll(vBox1, vBox2);
// bc.getData().addAll(series1, series2, series3);
// XYChart<>.Series series1 = new XYChart.Series();
// series1.setName("2003");
// series1.getData().add(new XYChart.Data(2, itemA));
// series1.getData().add(new XYChart.Data(20, itemB));
// series1.getData().add(new XYChart.Data(10, itemC));
//
root.getChildren().add(hbox);
return root;
}
private Pane createRoot() {
screenSize = Screen.getPrimary().getBounds();
width = screenSize.getWidth();
height = screenSize.getHeight();
root = new Pane(); // Pane is a layout without any grid o.s.e.
root.setPrefSize(width / screenFac, height / screenFac);
return root;
}
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(showMyCharts());
stage.setScene(scene);
stage.show();
}
public static void main(String[] charts) {
launch(charts);
}
}
推荐阅读
- javascript - Gantt Highcharts 布局截止
- node.js - 需要 babel "^7.0.0-0",但加载的是 "6.26.3"
- java - JNA 调用 SetTcpEntry 总是返回 317
- ocr - 尝试翻译成另一种语言时,Textblob OCR 引发 404 错误
- javascript - 在 tomcat web.xml 中设置 Httponly 和安全标志后会话过期问题
- apache-spark - Spark 中区分大小写的联接
- python - 是否可以将各种状态代码设置为单个烧瓶错误处理函数?
- angular - 运行“ng extract-i18n”命令后的 Angular 12 XLF 文件添加了 node_modules 跨单元字符串
- playwright - Playwright - 针对不同的环境和不同的变量进行测试
- python - 使用 Python 中的列表理解读取 GPX 文件时出现 UTF 错误