Изменения

Перейти к: навигация, поиск

Примеры кода на Java

407 байт добавлено, 8 апрель
Нет описания правки
'''import''' weka.core.Instances;
<font color="green">//Load Data set</font>
'''var''' data = new Instances(new BufferedReader(new FileReader("dataset/house.arff")));
data.setClassIndex(data.numAttributes() - 1);
<font color="green">//Build model</font>
'''var''' model = new LinearRegression();
'''try''' { model.buildClassifier(data); }
'''catch''' (Exception e) { e.printStackTrace(); }
<font color="green">//output model</font>
System.out.printf("model parameters: %s%n", model);
<font color="green">// Now Predicting the cost</font>
'''var''' myHouse = data.lastInstance();
'''var''' price = model.classifyInstance(myHouse);
System.out.printf("predicted price = %s%n", price);
====Логистическая регрессиия====
{{main|Логистическая регрессия}}
'''import''' java.io.File;
<font color="green">// read train & test datasets and build MLP classifier</font>
'''var''' trainds = new DataSource("etc/train.csv");
'''var''' train = trainds.getDataSet();
'''var''' mlp = new MultilayerPerceptron();
mlp.buildClassifier(train);
<font color="green">// Test the model</font>
'''var''' eTest = new Evaluation(train);
eTest.evaluateModel(mlp, test);
<font color="green">// Print the result à la Weka explorer:</font>
'''var''' strSummary = eTest.toSummaryString();
System.out.println(strSummary);
'''import''' java.util.Arrays;
<font color="green">// read train & test dataset</font>
'''var''' parser = new DelimitedTextParser();
parser.setResponseIndex(new NominalAttribute("class"), 0);
'''var''' test = parser.parse("USPS Test", this.getClass().getResourceAsStream("/smile/data/usps/zip.test"));
'''var''' classes = Arrays.stream(test.labels()).max().orElse(0) + 1;
<font color="green">// build SVM classifier</font>
'''var''' svm = new SVM<>(new GaussianKernel(8.0), 5.0, classes, SVM.Multiclass.ONE_VS_ONE);
svm.learn(train.x(), train.labels());
svm.finish();
<font color="green">// calculate test error rate</font>
'''var''' error = 0;
for (int i = 0; i < test.x().length; i++) {
'''import''' java.util.Random;
<font color="green">//load data</font>
'''var''' data = new Instances(new BufferedReader(new FileReader("data/bank-data.arff")));
<font color="green">// new instance of clusterer</font>
'''var''' model = new EM();
<font color="green">// build the clusterer</font>
model.buildClusterer(data);
System.out.println(model);
Анонимный участник

Навигация