• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Sorting A 2D ArrayList Based On Columns

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am in the process of implementing a decision tree for a machine learning course. I am given a 2D ArrayList where each row describes an instance (set of attributes with a given classification) for training my decision tree. Each column is a particular attribute, all attributes are real-valued doubles, and the last column is a binary classification (either a 0 or a 1). What I need help with is sorting my 2D ArrayList by 1 attribute at a time. I want the entire row to be moved along with the attribute it's being sorted by.

For example:

[ 9, 8, ... , 0]
[ 3, 2, ... , 1]
[ 4, 1, ... , 1]
[ 5, 6, ... , 0]

first time it's sorted (i.e sort by the first attribute):

[ 3, 2, ... , 1]
[ 4, 1, ... , 1]
[ 5, 6, ... , 0]
[ 9, 8, ... , 0]

after sorting on the first attribute (column) I will perform a set of operations with this data. Then I need to sort it by the second attribute, third attribute, ... nth attribute. I do not want to sort by the final column because this stores classifications, not attributes.

If anyone could lead me in the correct direction for the sorting behavior it would be greatly appreciated! Please let me know if you need any further clarifications.
 
Ranch Hand
Posts: 146
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Defining a Comparator for the Arrays class's sort method should work.
 
John Carmicahel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you point me towards a source that could potentially help me implement that? Or could you possibly outline the steps. The method in question looks like this:

public void rootInfoGain(ArrayList<ArrayList<Double>> dataSet, ArrayList<String> trainAttributeNames, int minLeafNumber) {
this.mTrainAttributes = trainAttributeNames;
this.mTrainDataSet = dataSet;
this.minLeafNumber = minLeafNumber;

}

I'm trying to sort this.mTrainDataSet.
 
John Carmicahel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is the solution that I am working towards. The problem here is that I can't seem to figure out how to replace the .get(0) with .get(i) without running into errors. Some further direction would be greatly appreciated.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Comparator class's compare method would take two rows to compare and return a value indicating which is greater.
Then use something like:  Arrays.sort(arrayName, yourComparator...
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

without running into errors.


Can you copy the full text of the error messages and paste it here.

The posted code looks like it always sorts on the first column (index == 0).
That needs to be parametized to allow the setting of the column to sort on.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain why there is a loop in the code at line 2?
 
John Carmicahel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I want that loop so that I can iterate through the different attributes (colums) to sort by. So I want ot sort by the first column, then I need to find the split threshold that will give me the optimal information gain for my decision tree based off of the sorted list. After I find that information, I need repeat this proces for the second attribute, which requires me to sort by the second column/attribute. I need to repeat this process for every attribute/column to find the optimal split threshold for each attribute/column. Most of that information is kind of irrelevant to you. I just need to know how to sort by a column, perform my operations, then move to the next column for sorting, etc.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you compile and execute the code?

Note: Instead of hardcoding a 0 in the get methods, the value used should be a variable that can change for the column you want sorted.
 
John Carmicahel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


NOTE: I change the values in the get() to i. The problem with this is I am not passing i to the compare method. So if I go one step further:



Now I have passed i to my overriden compare function; however, my IDE is giving me this error:

The method compare(ArrayList<Double>, ArrayList<Double>, int) of type new Comparator<ArrayList<Double>>(){} must override or implement a supertype method
 
John Carmicahel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize, there is an error in the second piece of code I linked to use, the get() calls should still contain (i) not (0).
 
John Carmicahel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is the entire class that I am implementing currently. The stuff we are focusing on is inside rootInfoGain
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the sort work for you?
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can not change the parameters for the compare method.  They must match what is defined in the interface.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is easier to work on a small program that tests only the problem you are working on.  For this problem about 25 lines of  code should be enough.
 
John Carmicahel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't even run that code, mostly because there are compiler errors from what I have included. In the entire class that I included, I believe I can utilize the private class DataBinder to help with my problem here, but I'm not exactly sure how to do so.
 
John Carmicahel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


coupled with some form of



is where my answer is I believe, I'm just not entirely sure how to use the DataBinder
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I prefer working on one problem at a time.  I thought the current problem was sorting a 2 dim ArrayList of doubles.  Have you tried creating a small program of 20-30 lines that defines a 2 dim ArrayList and sorts it on different columns?  Get that to work, then merge the code into the larger program and move to the next problem.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

there are compiler errors  


You need to post the code and its errors if you want help.
 
John Carmicahel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Alright I have isolated the problem we are looking at. This code properly sorts by the first column. I need to figure out how to implement the behavior I described above.
 
Saloon Keeper
Posts: 10924
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to define a Comparator class whose constructor takes a column number. Something along these lines...

 
John Carmicahel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're a life saver! With some tweaking that worked perfectly. Much appreciated!
 
Carey Brown
Saloon Keeper
Posts: 10924
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Carmicahel wrote:You're a life saver! With some tweaking that worked perfectly. Much appreciated!


Glad I could be of  help.
 
Bartender
Posts: 5546
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is indeed an elegant comparator.

Another way would be to use the Comparator.comparing method from the Comparator interface, something like:

and t could be used as in:
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic