Problem with add String values on DenseInstance

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with add String values on DenseInstance

KevlmL
Hi, I have de following code:

@Test
    public void shouldAddNewSaleAndGetTheCluster() {
        Sale sale = new Sale();
        sale.setBookingDate("10/07/2017 21:45");
        sale.setGds("Worldspan");
        sale.setPnr("KIUU2S");
        sale.setEngine("Viajala PerĂº");
        sale.setStatus("Pending Payment");
        sale.setDepartureDate("04/11/2017 06:00");
        sale.setArrivalDate("08/11/2017 18:00");
        sale.setOrigin("LIM");
        sale.setDestination("AQP");
        sale.setItinerary("LIM-AQP-LIM");
        sale.setPassengersCount(2);
        sale.setContactPerson("KEVIN NOVOA");
        sale.setValidatingAirlines("P9-P9");
        sale.setPaymentType("AMERICAN EXPRESS");
        sale.setAmount("249.90");

        File file = new File(Objects.requireNonNull(
               
getClass().getClassLoader().getResource("data/sales.csv")).getFile());

        try {
            CSVLoader csvLoader = new CSVLoader();
            csvLoader.setFile(file);
            Instances newData1 = csvLoader.getDataSet();

            double[] values = new double[newData1.numAttributes()];
            values[0] =
newData1.attribute(0).addStringValue(sale.getBookingDate());
            values[1] = newData1.attribute(1).addStringValue(sale.getGds());
            values[2] = newData1.attribute(2).addStringValue(sale.getPnr());
            values[3] =
newData1.attribute(3).addStringValue(sale.getEngine());
            values[4] =
newData1.attribute(4).addStringValue(sale.getStatus());
            values[5] =
newData1.attribute(5).addStringValue(sale.getDepartureDate());
            values[6] =
newData1.attribute(6).addStringValue(sale.getArrivalDate());
            values[7] =
newData1.attribute(7).addStringValue(sale.getOrigin());
            values[8] =
newData1.attribute(8).addStringValue(sale.getDestination());
            values[9] =
newData1.attribute(9).addStringValue(sale.getItinerary());
            values[10] = sale.getPassengersCount();
            values[11] =
newData1.attribute(11).addStringValue(sale.getContactPerson());
            values[12] =
newData1.attribute(12).addStringValue(sale.getValidatingAirlines());
            values[13] =
newData1.attribute(13).addStringValue(sale.getPaymentType());
            values[14] = Double.parseDouble(sale.getAmount());
            newData1.add(new DenseInstance(1.0, values));

            SimpleKMeans simpleKMeans = new SimpleKMeans();
            simpleKMeans.setPreserveInstancesOrder(true);
            simpleKMeans.setNumClusters(7);
            simpleKMeans.setSeed(10);
            simpleKMeans.setDontReplaceMissingValues(true);
            simpleKMeans.buildClusterer(newData1);

            simpleKMeans.setMaxIterations(5000);
            Instances centroids = simpleKMeans.getClusterCentroids();

            for (int i = 0; i < centroids.numInstances(); i++) {
                System.out.println("Centroid " + i + ": " +
centroids.instance(i));
            }

            for (int i = 0; i < newData1.numInstances(); i++) {
                System.out.println(newData1.instance(i) + " is in cluster "
                        +
simpleKMeans.clusterInstance(newData1.instance(i)));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

but all String values of my object return -1 and this throws
IndexOutOfBoundsExceptions. Can anyone help me with this? Thank you.



--
Sent from: http://weka.8497.n7.nabble.com/
_______________________________________________
Wekalist mailing list
Send posts to: [hidden email]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/mailman/listinfo/wekalist
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Reply | Threaded
Open this post in threaded view
|

Re: Problem with add String values on DenseInstance

Peter Reutemann
[...]

> getClass().getClassLoader().getResource("data/sales.csv")).getFile());
>
>         try {
>             CSVLoader csvLoader = new CSVLoader();
>             csvLoader.setFile(file);
>             Instances newData1 = csvLoader.getDataSet();

[...]

> but all String values of my object return -1 and this throws
> IndexOutOfBoundsExceptions. Can anyone help me with this? Thank you.

Have you actually checked that these attributes are actually STRING
attributes and not NOMINAL ones? That would explain the "-1" (= index
not found).

You might have to force them to be string using -S option or
setStringAttributes method:
http://weka.sourceforge.net/doc.dev/weka/core/converters/CSVLoader.html

Cheers, Peter
--
Peter Reutemann
Dept. of Computer Science
University of Waikato, NZ
+64 (7) 858-5174
http://www.cms.waikato.ac.nz/~fracpete/
http://www.data-mining.co.nz/
_______________________________________________
Wekalist mailing list
Send posts to: [hidden email]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/mailman/listinfo/wekalist
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html