I've got big problem with visualiation of arff files in javaFX table view. My code :
ArffLoader loader = new ArffLoader();
loader.setFile(arff);
Instances data = loader.getDataSet();
List<TableColumn<Instance, String>> atrributes = new ArrayList<>();
for (int i = 0; i < data.numAttributes(); i++) {
atrributes.add(new TableColumn<Instance,String>` (data.attribute(i).name()));
}
List <Instance> instances = new ArrayList<>();
for (int i =0;i<data.size();i++)
{
instances.add(data.get(i));
}
ObservableList<Instance> tableContent = FXCollections.observableArrayList(instances);
table.getColumns().removeAll();
table.getColumns().addAll(atrributes);
table.setItems(tableContent);
table.setVisible(true);
Names of columns(attributes) are set properly but contest is not shwon(tableContent variable)
You need to set a cell value factory for each table column. This is a function that maps the Instance in each row to the value to be displayed in the corresponding cell in that column.
I am not familiar with Weka, but looking at the Javadocs, I think you need
for (int i = 0; i < data.numAttributes(); i++) {
TableColumn<Instance, String> column
= new TableColumn<Instance,String>(data.attribute(i).name());
final int attIndex = i ;
column.setCellValueFactory(cellData ->
new SimpleStringProperty(cellData.getValue().toString(attIndex)));
atrributes.add(column);
}
Related
Lets say I have a public class called GameBoard that will be a two dimensional array with 4 rows and 5 columns. The spaces in the array are filed with String values from 1 to 20. A card will be drawn that has a name (King of Spades for example) . If the user inputs 15 I will store it in a String variable called userLocation. What would be the most efficient way to create a method that takes the input location and updates the array with the name of the Card? Would a for loop be most efficient?
public GameBoard() {
square = new String[4][5];
square[0][0] = new String("1");
square[0][1] = new String("2");
square[0][2] = new String("3");
square[0][3] = new String("4");
square[0][4] = new String("5");
square[1][0] = new String("6");
square[1][1] = new String("7");
square[1][2] = new String("8");
square[1][3] = new String("9");
square[1][4] = new String("10");
square[2][1] = new String("11");
square[2][2] = new String("12");
square[2][3] = new String("13");
square[3][1] = new String("14");
square[3][2] = new String("15");
square[3][3] = new String("16");
square[2][0] = new String(17);
square[3][0] = new String(18);
square[2][4] = new String(19);
square[3][4] = new String(20);
}
My preferred method as of now would look something like this but it gives me the error code "type mismatch:cannot convert string to boolean" under userLocation = board[i][j]
public String[][] updateBoard(String userLocation, Card card, String[][] board) {
for (int i = 0; i <4; i++)
{
for (int j = 0; j < 5; j++)
{
if(userLocation = board[i][j]) {
board[i][j] = card.name;
}
}
}
return board;
}
So the reason it will not compile is your = does not return a boolean expression. == would, but it's still not what you want, since you want to check if the String contents are the same, not if they're the same object, so use .equals.
But, no, I think you don't want to depend on strings to identify locations. What if you want to replace a card? And why look through everything when you need not?
Rather if i is some number between 1 and 20, identify the corresponding spot in the array by square[(i-1)/5][(i-1)%5]
That should bypass the issue you are having with matching strings.
So for example, your constructor becomes:
public GameBoard() {
square = new String[4][5];
for (int i=1; i<=20;i++){
square[(i-1)/5][(i-1)%5]=""+i;//initialize with 1 to 20 if you like
}
and userLocation is an int.
I am using weka with java by using Eclipse IDE for Java Development. Version: Neon 4.6.
I would like to know how could I extract the values like:
correlation ranking assigned for each attribute.
SVM-RFE ranking attribute and weight values assigned for each attribute
I would like to see these values on the screen.
I am using weka:
I tried with this code:
public class AttributeSelectionTest {
protected static void useRanker(Instances data) throws Exception {
SVMAttributeEval eval = new SVMAttributeEval();
eval.buildEvaluator(data);
Evaluation evaluation = new Evaluation(data);
System.out.println(eval.getPercentToEliminatePerIteration());
System.out.println(eval.attsToEliminatePerIterationTipText());
eval.getPercentToEliminatePerIteration();
for (int classInd = 0; classInd < data.numAttributes(); classInd++)
System.out.println(eval.rankBySVM(classInd,data));
System.out.println(evaluation.toSummaryString());
}
public static void main(String[] args) throws Exception {
// load data
System.out.println("\n0. Loading data");
DataSource source = new DataSource("data.arff");
Instances data = source.getDataSet();
if (data.classIndex() == -1)
data.setClassIndex(data.numAttributes() - 1);
useRanker(data);
}
}
To get SVM-RFE ranking use the following code.Use your file data and load it using fileHandler
Dataset dataSet = FileHandler.loadDataset(new File("sample.data"), 4, ",");
RecursiveFeatureEliminationSVM svmrfe = new RecursiveFeatureEliminationSVM(0.2);
svmrfe.build(dataSet);
for (int i = 0; i < svmrfe.noAttributes(); i++)
System.out.println(svmrfe.rank(i));
for the same data you can get the attribute selection ranking as
ASEvaluation eval = new GainRatioAttributeEval();
ASSearch search = new Ranker();
WekaAttributeSelection attributeSelection = new WekaAttributeSelection(eval,search);
wekaattrsel.build(dataSet);
for (int i = 0; i < attributeSelection.noAttributes(); i++)
System.out.println("Attribute : " + i + " Ranks : " + attributeSelection.rank(i));
I am working in list view. In it, there are two lists such that if I select a value from base list, it will add to the child list and remove the item from base list, all are working fine. My query is delete list item from child list it will re-add to the base list.
contacts = jsonObj.getJSONArray(TAG_PRODUCTLIST);
for (int i = 0; i < contacts.length(); i++)
{
JSONObject c = contacts.getJSONObject(i);
Object id = c.get(TAG_CATID);
Object em = c.get(TAG_CAT);
contact1 = new HashMap<>();
contact1.put(TAG_CATID, id);
contact1.put(TAG_CAT, em);
JSONArray productss = c.getJSONArray(TAG_PRODUCT);
worldpopulationlist.add(new QuoteSectionItem(em.toString()));//listview section
for (int j = 0; j < productss.length(); j++)
{
JSONObject d = productss.getJSONObject(j);
Object pid = d.get(TAG_PID);
Object price = d.get(TAG_PRICE);
Object pn = d.get(TAG_PNAME);
contact = new HashMap<>();
contact.put(TAG_PID, pid);
contact.put(TAG_PRICE, price);
contact.put(TAG_PNAME, pn);
contact.put(TAG_PDESC, pn);
Quote_Products worldpopulation = new Quote_Products(id,em,pid,pn,price);//listview item
worldpopulationlist.add(worldpopulation);
} }
this is base list creation and i create child list like` SparseBooleanArray selected = listviewadapter.getSelectedIds();
for (int i = (selected.size() - 1); i >= 0; i--)
{
if (selected.valueAt(i))
{
QuoteItem selectedI = listviewadapter.getItem(selected.keyAt(i));
Quote_Products selecteditem = (Quote_Products) selectedI;
System.out.println("Selected Items : "+selecteditem.getCountry());
System.out.println("Selected Items : "+selecteditem.getRank());
System.out.println("Selected Items : "+selecteditem.getPopulation());
System.out.println("Products:"+product);
productList.add(product);
Quote_SelectedProducts worldpopulation1 = new Quote_SelectedProducts(
selecteditem.getCidid(),
selecteditem.getCname(),
selecteditem.getRank(),
selecteditem.getCountry(),
selecteditem.getPopulation());
worldpopulationlist1.add(worldpopulation1);
listviewadapter.remove(selecteditem);
}
}
`
after this are successfully done i need to to how to to reinsert the list value once delete value form child list
SparseBooleanArray selected = listviewadapter1.getSelectedIds();
for (int i = (selected.size() - 1); i >= 0; i--)
{
if (selected.valueAt(i))
{
Quote_SelectedProducts selecteditem = listviewadapter1.getItem(selected.keyAt(i));
listviewadapter1.remove(selecteditem);
System.out.println("Delete after ignore "+selecteditem.getCidid());
System.out.println("Delete after ignore "+selecteditem.getCname());
System.out.println("Delete after ignore "+selecteditem.getRank());
System.out.println("Delete after ignore "+selecteditem.getCountry());
System.out.println("Delet after ignore "+selecteditem.getPopulation());
Quote_Products worldpopulation = new Quote_Products(selecteditem.getCidid(),
selecteditem.getCname(),selecteditem.getRank(),
selecteditem.getRank(),selecteditem.getPopulation());
System.out.println(" EEE "+String.valueOf(worldpopulation));
worldpopulationlist.add(worldpopulation);
System.out.println(" EEE "+String.valueOf(worldpopulationlist));
}
the value delete from child list how to re insert the child list value in the base list
Thanks in advance
This method should add an entire array of books into the library object books array.
The method must add the new array at the end of the current library and avoid overwriting any books already in the library. Make sure to include one copy of each book. [For simplicity no books added through this method will ever be duplicates of each other or previous books].
The method I tried to wrote hasn't been adding arrays correctly. Here's the shortened Library class.
public class Library {
Book [] books;
int [] copies;
int [] checkedOut;
int numBooks;
public Library() {
books = new Book[400];// Array to hold each book in element
copies = new int[400];// Array to hold number of "book" copies corresponding to the element in the books array.
checkedOut = new int[400];// Array to hold number of checked out books corresponding to the elemtn in the books array.
numBooks = 0;// Number of unique books.
}
public void addMultipleBooks( Book [] b ) {
for(int k = 0; k < b.length; k++) {
for(int i = 0; i < books.length; i++) {
if(books[i] == null) { numBooks = i; }
books[numBooks] = b[k];
copies[numBooks] = copies[numBooks] + 1;
}
}
}
}// End Library Class
addMultipleBooks should do the following:
1)Add each of the new books in the right spot in the array.
2)Set the number of copies to 1 for each book.
3)Update numBooks.
And here's the new array of book objects in the main I am trying to pass through the method.
Book [] buildLibrary = new Book[10];
buildLibrary[0] = new Book( "Ulysses", new Author("Joyce","James") );
buildLibrary[1] = new Book( "The Great Gatsby", new Author("Fitzgerald","F. Scott") );
buildLibrary[2] = new Book( "A Portrait of the Artist as a Young Man", new Author("Joyce","James") );
buildLibrary[3] = new Book( "Lolita", new Author("Nobokov","Vladimir") );
buildLibrary[4] = new Book( "Brave New World", new Author("Huxley","Aldous") );
buildLibrary[5] = new Book( "The Sound and the Fury", new Author("Faulkner","William") );
buildLibrary[6] = new Book( "Catch-22", new Author("Heller","Joseph") );
buildLibrary[7] = new Book( "Darkness at Noon", new Author("Koestler","Arthur") );
buildLibrary[8] = new Book( "Sons and Lovers", new Author("Lawrence","D.H.") );
buildLibrary[9] = new Book( "The Grapes of Wrath", new Author("Steinbeck","John") );
Without using arraylists you can use the following code, but I agree arraylists are the simplest way.
public void addMultipleBooks(Book [] b) {
for(int i = 0; i < b.length; i++) {
books[numBooks] = b[i];
copies[numBooks]++;
numBooks++;
}
}
The size of Arrays cannot be changed so either you would need to use an ArrayList instead like Rohit Jain says as these can be expanded and shortened when you add and remove items.
Either that or you would need to create an entirely new array with a length one greater than the original array if you wanted to add an item to it, then move all the original array items into the new one and then the new item.
So yes, use an ArrayList.
Try modifying your code for adding this way :
for (int k = 0; k < b.length; k++) {
for (int i = 0; i < books.length; i++) {
if (books[i] == null) {
numBooks = i;
books[numBooks] = b[k];
copies[numBooks] = copies[numBooks] + 1;
break;
}
}
}
This way, the adding will only happen if the current slot of the book storage in your library is empty (null), then proceed to the next book item to be added (break out from searching of empty slot in library's book array since the current book to be stored has found its proper slot)
I am generating a box and whisker chart with one item per category.I also want to generate a report with the mean, median and all the values per item in the BoxPlot. So, after i create the dataset, defaultboxandwhiskercategorydataset based on the categoryType, I call the method convertReportData to fetch each item in the defaultboxandwhiskercategorydataset and save the mean, median etc into another data object later for report generation. But it just prints only one category. Could anyone please help me to figure out what is wrong?
My boxplot
Code:
public static BoxAndWhiskerCategoryDataset createDataset() {
startTime = inputData.getItimeFrom();
endTime = inputData.getItimeTo();
List<String> categorylist = new ArrayList<>();
categorylist.add("Distance 0-20");
categorylist.add("Distance 20-40");
categorylist.add("Distance 40-60");
categorylist.add("Distance 60-80");
categorylist.add("Distance 80-100");
categorylist.add("Distance >100");
Map<String, List<Double>> map = new HashMap<String, List<Double>>();
map = addDistance(values_list);
DefaultBoxAndWhiskerCategoryDataset defaultboxandwhiskercategorydataset = new DefaultBoxAndWhiskerCategoryDataset();
for (String categoryType : categorylist) {
map.remove(null);
for (Map.Entry<String, List<Double>> entry : map.entrySet()) {
if (entry.getKey().equalsIgnoreCase(categoryType)) {
defaultboxandwhiskercategorydataset.add(entry.getValue(),
categoryType, " ");
}
}
}
convertReportData(defaultboxandwhiskercategorydataset, categorylist);
return defaultboxandwhiskercategorydataset;
}
private static void convertReportData(DefaultBoxAndWhiskerCategoryDataset boxandwhiskercategorydataset, List<String> latencyTypelist) {
report = new HashMap<>();
for (int i = 0; i < boxandwhiskercategorydataset.getColumnKeys().size(); i++) {
BoxAndWhiskerItem item = boxandwhiskercategorydataset.getItem(i, 0);
ReportData data = new ReportData();
data.setMean(item.getMean());
data.setMedian(item.getMedian());
data.setQ1(item.getQ1());
data.setQ3(item.getQ3());
data.setMaxOutlier(item.getMaxOutlier());
data.setMaxRegularNumber(item.getMaxRegularValue());
data.setMinOutlier(item.getMinOutlier());
data.setMinRegularNumber(item.getMinRegularValue());
data.setOutliers(item.getOutliers());
report.put(boxandwhiskercategorydataset.getRowKey(i).toString(),
data);
}
}
The problem is with
for (int i = 0; i < boxandwhiskercategorydataset.getColumnKeys().size(); i++) {
you are using getColumnKeys whereas you have only one Column. It should have been,
for (int i = 0; i < boxandwhiskercategorydataset.getRowKeys().size(); i++) {