使用 rapidminer 5.2 设计了一个决策树模型 我想使用 java 实现和查看结果 我使用从 rapidminer 生成的 xml 并在 java 中作为 process.run() 运行过程 但我想使用 java 查看我的决策树
ravi
问问题
2410 次
1 回答
1
试试这个例子:
package rm.rapidi.com;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import com.rapidminer.Process;
import com.rapidminer.RapidMiner;
import com.rapidminer.RapidMiner.ExecutionMode;
import com.rapidminer.RepositoryProcessLocation;
import com.rapidminer.gui.renderer.Renderer;
import com.rapidminer.gui.renderer.RendererService;
import com.rapidminer.operator.IOContainer;
import com.rapidminer.operator.IOObject;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.report.Renderable;
import com.rapidminer.report.Reportable;
import com.rapidminer.repository.Entry;
import com.rapidminer.repository.ProcessEntry;
import com.rapidminer.repository.RepositoryException;
import com.rapidminer.repository.RepositoryLocation;
import com.rapidminer.tools.XMLException;
public class PlotterTest {
public static void main(String[] args) {
RapidMiner.setExecutionMode(ExecutionMode.EMBEDDED_WITHOUT_UI);
RapidMiner.init();
JFrame frame = new JFrame("Decision Tree Renderer Test");
frame.setLocation(0, 0);
JLabel label = new JLabel();
frame.add(new JScrollPane(label));
try {
RepositoryLocation location;
RapidMiner.setExecutionMode(ExecutionMode.COMMAND_LINE);
RapidMiner.init();
location = new RepositoryLocation("//RapidMinerRepository/PlotterTest");
Entry entry = location.locateEntry();
if (entry instanceof ProcessEntry) {
Process process = new RepositoryProcessLocation(location).load(null);
IOContainer ioResult = process.run();
IOObject result = ioResult.getElementAt(0); // or whatever index you need
String name = RendererService.getName(result.getClass());
List<Renderer> renderers = RendererService.getRenderers(name);
for (Renderer renderer : renderers) { // you don't really need to
// iterate over this, it's
// probably only one anyway
// ;)
IOContainer dummy = new IOContainer();
// edit size of image here
int imgWidth = 1600;
int imgHeight = 950;
Reportable reportable = renderer.createReportable(result, dummy, imgWidth, imgHeight);
if (reportable instanceof Renderable) {
Renderable renderable = (Renderable) reportable;
renderable.prepareRendering();
int preferredWidth = renderable.getRenderWidth(800);
int preferredHeight = renderable.getRenderHeight(800);
final BufferedImage img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D) img.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, imgWidth, imgHeight);
// scale to whatever you need
graphics.scale(2, 2);
renderable.render(graphics, preferredWidth, preferredHeight);
label.setIcon(new ImageIcon(img));
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XMLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperatorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
使用以下 XML(或类似的):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<process version="5.3.005">
<context>
<input/>
<output/>
<macros/>
</context>
<operator activated="true" class="process" compatibility="5.3.005" expanded="true" name="Process">
<process expanded="true">
<operator activated="true" class="retrieve" compatibility="5.3.005" expanded="true" height="60" name="Retrieve" width="90" x="45" y="75">
<parameter key="repository_entry" value="//Samples/data/Sonar"/>
</operator>
<operator activated="true" class="decision_tree" compatibility="5.3.005" expanded="true" height="76" name="Decision Tree" width="90" x="179" y="75"/>
<connect from_op="Retrieve" from_port="output" to_op="Decision Tree" to_port="training set"/>
<connect from_op="Decision Tree" from_port="model" to_port="result 1"/>
<portSpacing port="source_input 1" spacing="0"/>
<portSpacing port="sink_result 1" spacing="0"/>
<portSpacing port="sink_result 2" spacing="0"/>
</process>
</operator>
</process>
于 2013-03-11T15:11:08.277 回答