24 December 2014

Data Mining Classification with weka

My final year project based on data mining and data classification.So i spent lot of time to find how data classification done by using weka. So In this article i will show how data classification/prediction using weka API.In This tutorial i will show how to predict data using weka API.First you need data set.For that i will suggest it is better to install weka (http://www.cs.waikato.ac.nz/ml/weka/downloading.html )software in your computer. After installing you can see in the installed location data folder.For this tutorial i will use iris.ARFF to predict result.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@RELATION iris

@ATTRIBUTE sepallength REAL

@ATTRIBUTE sepalwidth  REAL

@ATTRIBUTE petallength  REAL

@ATTRIBUTE petalwidth REAL

@ATTRIBUTE class  {Iris-setosa,Iris-versicolor,Iris-virginica}

@DATA

5.1,3.5,1.4,0.2,Iris-setosa

4.9,3.0,1.4,0.2,Iris-setosa

4.7,3.2,1.3,0.2,Iris-setosa

4.6,3.1,1.5,0.2,Iris-setosa

5.0,3.6,1.4,0.2,Iris-setosa

5.4,3.9,1.7,0.4,Iris-setosa

4.6,3.4,1.4,0.3,Iris-setosa

5.0,3.4,1.5,0.2,Iris-setosa

4.4,2.9,1.4,0.2,Iris-setosa

4.9,3.1,1.5,0.1,Iris-setosa

5.4,3.7,1.5,0.2,Iris-setosa

4.8,3.4,1.6,0.2,Iris-setosa

4.8,3.0,1.4,0.1,Iris-setosa

This is the sample data set.Here you can see data set realation name is iris and there are four Attribute to predict class.For predict the class there are several classification algorithm available in  weka API.In here i have used decision tree classification.

First you need to open weka and select iris.arff file and then go to classification tab and select J48 algorithm.Then start classification process.Then you can see this result.


Then you can see the decision tree generated in weka.For the write click the j48 panel (blue color) and visualize.



 Then we have to predict the class value based on our four attributes.In this example i will predict the  class value for this attribute data.     
 4.8,2.1,3.7,1.4,?

1 Step :

Need to build model file for using data set.As a first step you have to add weka jar file to your project and import necessary classes. For building model file we need the data set and using the data set based on the classification algorithm we build model here.I have used cross validation as evaluation technique.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public void buildIrisModel() {

        try {

            ConverterUtils.DataSource source = new ConverterUtils.DataSource("C://Users//Sajith//Desktop//iris.arff");

            Instances train = source.getDataSet();

            train.setClassIndex(train.numAttributes() - 1);

            J48 j48 = new J48();

            j48.setUnpruned(true);

            Evaluation eval = new Evaluation(train);

            eval.crossValidateModel(j48, train, 10, new Random(1));

            System.out.println("Percent correct: " + Double.toString(eval.pctCorrect()));

            System.out.println("Correct : " + Double.toString(eval.correct()));

            System.out.println("Incorrect : " + Double.toString(eval.incorrect()));

            System.out.println("Error Rate : " + Double.toString(eval.errorRate()));

            System.out.println("Mean Absolute Error  : " + Double.toString(eval.meanAbsoluteError()));


            j48.buildClassifier(train);

            ObjectOutputStream oos = new ObjectOutputStream(

                    new FileOutputStream("C://Users//Sajith//Desktop//iris.model"));

            oos.writeObject(j48);

            oos.flush();

            oos.close();

            System.out.println("Graph   : " + j48.graph());

        } catch (Exception e) {

            e.printStackTrace();

        }

    }
2 Step :
This method used to generate Instance of Arff data set.It means we create instance of data set and using that Instance predict class value. This CreateInstance method get the four attributes and returns the Instance.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
private Instances data;

    public Instances CreateInstance(double sepallength,double sepalwidth,double petallength,double petalwidth){

        double result = -1;

        String prediction=null;

        try {

          ArrayList<Attribute> attributeList = new ArrayList<Attribute>(2);

          Attribute attribute_sepallength = new Attribute("sepallength");

          Attribute attribute_sepalwidth = new Attribute("sepalwidth");

          Attribute attribute_petallength = new Attribute("petallength");

          Attribute attribute_petalwidth = new Attribute("petalwidth");

            ArrayList<String> classVal = new ArrayList<String>();

            classVal.add("Iris-setosa");

            classVal.add("Iris-versicolor");

            classVal.add("Iris-virginica"); 

            attributeList.add(attribute_sepallength);

            attributeList.add(attribute_sepalwidth);

            attributeList.add(attribute_petallength);

            attributeList.add(attribute_petalwidth);

            attributeList.add(new Attribute("class",classVal));


            data = new Instances("Iris",attributeList,0);

            data.setClassIndex(data.numAttributes() - 1);


            Instance inst_co = new DenseInstance(data.numAttributes());


            inst_co.setValue(attribute_sepallength, sepallength);

            inst_co.setValue(attribute_sepalwidth, sepalwidth);

            inst_co.setValue(attribute_petallength, petallength);

            inst_co.setValue(attribute_petalwidth, petalwidth);

            data.add(inst_co);

            System.out.println(data);

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        return data;

    }

3  Step:
This is the last step.In this step we parse the data and create Instance and then predict class value based on Instance.

1
2
3
4
5
// parse sepallength,sepalwidth,petallength,petalwidth  values
Instances inst = new wekaWriteArfFile().CreateInstance(4.8,2.1,3.7,1.4);
Classifier j48_classifier = (Classifier)weka.core.SerializationHelper.read("C://Users//Sajith//Desktop//ModelFiles//j48_iris_model.model");
 value = j48_classifier.classifyInstance(inst.instance(linereadFromfile));
 prediction = inst.classAttribute().value((int) value);

And now you got the result.Later i will upload source code in to github..Thanks

05 December 2014

Java swing chart library

First You need to download library.(http://www.jfree.org/jfreechart/download.html).Then it supports different type of chart types and it is easy to implement.Here i will describe how to implement PieChart and save chart as Image.Before You start you have to import jcommon and jfreechart to your project.

public class PieChartExampleDemo extends JFrame {

    public PieChartExampleDemo(String applicationTitle, String chartTitle) {

        super(applicationTitle);

        PieDataset dataset = createDataset();

        JFreeChart chart = createChart(dataset, chartTitle);

        String filename = "F:\\pichartImage.jpg";

        try {

            ChartUtilities.saveChartAsJPEG(new File(filename), chart, 600, 400);

        } catch (IOException e) {

            e.printStackTrace();

        }

        ChartPanel chartPanel = new ChartPanel(chart);

        chartPanel.setPreferredSize(new java.awt.Dimension(600, 400));

        setContentPane(chartPanel);


    }


    private  PieDataset createDataset() {

        DefaultPieDataset result = new DefaultPieDataset();

        result.setValue("Blackberry", 24);

        result.setValue("Apple", 22);

        result.setValue("Windows", 44);

        result.setValue("Android", 35);

        result.setValue("Symbian", 5);

        result.setValue("Other", 4);

        return result;


    }


    private JFreeChart createChart(PieDataset dataset, String title) {

        JFreeChart chart = ChartFactory.createPieChart3D(title, dataset,

                true,

                true,

                false);

        PiePlot3D plot = (PiePlot3D) chart.getPlot();

        plot.setStartAngle(290);

        plot.setDirection(Rotation.CLOCKWISE);

        plot.setForegroundAlpha(0.5f);

        return chart;

    }

}

From main class you can call to this class.You can save chart as jpg image.

PieChartExampleDemo pieChartExampleDemo = new PieChartExampleDemo("Mobile OS", "Mobile operating System Usage 2014");
        pieChartExampleDemo.pack();
        pieChartExampleDemo.setVisible(true);


And This is the result.