Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/mltk/core/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,15 @@ public String toString() {
}
} else {
double[] values = getValues();
sb.append(values[0]);
if (Double.isNaN(values[0]))
sb.append("?");
else
sb.append(values[0]);
for (int i = 1; i < values.length; i++) {
sb.append("\t").append(values[i]);
if (Double.isNaN(values[i]))
sb.append("\t?");
else
sb.append("\t").append(values[i]);
}
if (!Double.isNaN(getTarget())) {
sb.append("\t").append(getTarget());
Expand Down
10 changes: 10 additions & 0 deletions src/mltk/core/Instances.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Iterator;
import java.util.List;

import mltk.core.io.AttrInfo;
import mltk.util.Random;

/**
Expand Down Expand Up @@ -48,6 +49,15 @@ public Instances(List<Attribute> attributes, Attribute targetAtt) {
this(attributes, targetAtt, 1000);
}

/**
* Constructs a dataset from attributes and target attribute.
*
* @param ainfo container for attributes and target attribute.
*/
public Instances(AttrInfo ainfo) {
this(ainfo.attributes, ainfo.clsAttr);
}

/**
* Constructs a dataset from attributes and target attribute, with specified capacity.
*
Expand Down
46 changes: 46 additions & 0 deletions src/mltk/core/io/AttrInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package mltk.core.io;

import java.util.ArrayList;
import java.util.List;

import mltk.core.Attribute;

/**
* Structure for information on attributes in the data:
* list of active attributes, class attributes,
* columns corresponding to the active attributes in the data file
*
* @author Daria Sorokina
*
*/

public class AttrInfo {
public List<Attribute> attributes;
public Attribute clsAttr;
public List<Integer> columns;

/**
* Default constructor
*/
public AttrInfo() {
this.attributes = new ArrayList<Attribute>();
this.columns = new ArrayList<Integer>();
}

/**
* Maps names of attributes into their ids
*
* @param names names of attributes
* @return a list of corresponding attribute ids.
*/
public List<Integer> getIds(List<String> names) {
List<Integer> ids = new ArrayList<>();
for(String name: names)
for(Attribute att: attributes)
if(att.getName().equals(name)) {
ids.add(att.getIndex());
break;
}
return ids;
}
}
62 changes: 44 additions & 18 deletions src/mltk/core/io/AttributesReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

import mltk.core.Attribute;
import mltk.core.BinnedAttribute;
import mltk.core.NominalAttribute;
import mltk.core.NumericalAttribute;
import mltk.util.tuple.Pair;


/**
* Class for reading attributes. It only reads in a list of attributes from the attribute file.
* Class for reading attributes information.
* It reads in a list of attributes, list of inactive attributes and a class attribute
* from the attribute file.
*
* @author Yin Lou
* @author Yin Lou, modified by Daria Sorokina
*
*/
public class AttributesReader {
Expand All @@ -24,18 +26,40 @@ public class AttributesReader {
* Reads attributes and class attribute from attribute file.
*
* @param attFile the attribute file.
* @return a pair of attributes and class attribute (null if no class attribute).
* @return attribute information instance.
* @throws IOException
*/
public static Pair<List<Attribute>, Attribute> read(String attFile) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(attFile), 65535);
List<Attribute> attributes = new ArrayList<Attribute>();
Attribute clsAttr = null;
for (int i = 0;; i++) {
String line = br.readLine();
if (line == null) {
public static AttrInfo read(String attFile) throws IOException {

Set<String> neverNames = new HashSet<String>();
AttrInfo ainfo = new AttrInfo();

BufferedReader br1 = new BufferedReader(new FileReader(attFile), 65535);
String line = br1.readLine();
while ((line != null) && (line.indexOf("contexts:") == -1)) {
line = br1.readLine();
}
if (line != null) {
line = br1.readLine();
}
while (line != null) {
neverNames.add(line.split(" ")[0]);
line = br1.readLine();
}
br1.close();

BufferedReader br2 = new BufferedReader(new FileReader(attFile), 65535);
ainfo.clsAttr = null;
for (int i = 0, col = 0;; i++, col++) {
line = br2.readLine();
if ((line == null) || (line.indexOf("contexts:") != -1)) {
break;
}
String aname = line.split(": ")[0];
if(neverNames.contains(aname)) {
i--;
continue;
}
Attribute att = null;
if (line.indexOf("binned") != -1) {
att = BinnedAttribute.parse(line);
Expand All @@ -44,17 +68,19 @@ public static Pair<List<Attribute>, Attribute> read(String attFile) throws IOExc
} else {
att = NumericalAttribute.parse(line);
}
att.setIndex(i);
if (line.indexOf("(class)") != -1) {
clsAttr = att;
att.setIndex(col);
ainfo.clsAttr = att;
i--;
} else {
attributes.add(att);
att.setIndex(i);
ainfo.attributes.add(att);
ainfo.columns.add(col);
}
}
br.close();
br2.close();

return new Pair<List<Attribute>, Attribute>(attributes, clsAttr);
return ainfo;
}

}
Loading