This is an old revision of the document!
Table of Contents
Metody Eksploracji Danych: Laboratorium 5
Podczas zajęć będziemy korzystali z funkcji biblioteki Weka wołanych programowo z kodu Java.
Zapoznaj się z treścią
Zbiory danych
Zbiory danych - dwuwymiarowym obserwacjom X1 i X2 przypisana jest etykieta klasy (atrybut nominalny Y)
Weka
Brak specjalnych wymagań
IDE
- W
usr/local/bin
jest dostępne Eclipse Neon - Utwórz projekt
- Wybierz: Project → Properties → Java Build Path. Następnie zakładkę Libraries i opcję Add External JARs. Dodaj bibliotekę
weka.jar
Kod
Fragmenty kodu:
DataSource source = new DataSource("c-001.arff"); Instances data = source.getDataSet(); if (data.classIndex() == -1) data.setClassIndex(data.numAttributes() - 1);
Utworznenie i uczenie klasyfikatora
Classifier cls = new NaiveBayes(); cls.buildClassifier(data);
Instancja do sklasyfikowania
Instance inst = new DenseInstance (3); inst.setDataset (data); inst.setValue(0, 1.1); // wartośc dla X1 inst.setValue(1, 2.2); // wartość dla X2
double[] distrib = cls.distributionForInstance(inst); System.out.printf(Locale.US,"%d->%f %d->%f\n",0,distrib[0],1,distrib[1]);
List<Attribute> atts = Arrays.asList( new Attribute("X1"), new Attribute("X2"), new Attribute("Y",Arrays.asList("tak","nie"))); Instances result = new Instances("some-relation", new ArrayList<> (atts),0); result.setClassIndex(result.numAttributes()-1);
for(double x1=-10;x1<=10;x1+=0.1){ for(double x2=-10;x2<=10;x2+=0.1){ Instance inst = new DenseInstance(3); inst.setValue(0, x1); inst.setValue(1, x2); inst.setDataset(result); double y = cls.classifyInstance(inst); inst.setClassValue(y); result.add(inst); } }