attribute selection assigned for each attribute - java

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));

Related

Putting Jsoup abs:href results into an array and looping through the array

I am having some trouble understanding how to properly add to an array. What I am trying to accomplish is adding the results from the a method into an array and then run through the array.
Here is an example of what I need, or assume I need:
array{"websiteaddress","websiteaddress","websiteaddress","websiteaddress","websiteaddress"}
but instead I'm getting:
websiteaddress
websiteaddress
websiteaddress
websiteaddress
websiteaddress
websiteaddress
Here is the code I am working with:
private static final String webSiteURL = "https://websitename.com/";
//The path of the folder that you want to save the images to
private static final String folderPath = "C://path/";
private static final ArrayList<String> webPages = new ArrayList<>();
public static String[] thisIsAStringArray = {"https://websitename.com/"};
public static String[] tempArray = new String[ thisIsAStringArray.length + 1 ];
/**
*
* Method description:
* Date: Mar 17, 2018
* #param args
* #return void
*/
public static void main(String[] args) {
String path = folderPath + getPageTitle(webSiteURL).replaceAll(" ", "-");
pageLinks(webSiteURL);
System.out.println(thisIsAStringArray);
for(String web : thisIsAStringArray)
{
for(int n = 0; n < thisIsAStringArray.length - 1; n++)
{
System.out.println(thisIsAStringArray[n]);
getPageTitle(web);
pageLinks(web);
creatDirectory(folderPath, getPageTitle(web));
getsImagesAndSaves(path, web);
n++;
}
}
}
/**
*
* Method description: Get all the links on the page and put them into an array
* Date: Mar 16, 2018
* #param src
* #return void
*/
public static void pageLinks(String src)
{
try
{
URL url = new URL(src);
Document doc = Jsoup.parse(url, 3*1000);
Elements links = doc.select("a[href]"); // a with href
for (Element link : links)
{
System.out.println(link.attr("abs:href"));
String noHref = link.attr("abs:href");
for(int i = 0; i < thisIsAStringArray.length; i++)
{
tempArray[i] = thisIsAStringArray[i];
}
//thisIsAStringArray[i] = noHref;
tempArray[thisIsAStringArray.length] = noHref;
}
thisIsAStringArray = tempArray;
}
catch(Exception error)
{
System.out.println(error + " Something went wrong getting the links!");
}
}
}
Any help would be greatly appreciated and thank you in advance!
You have 2 arrays: thisIsAStringArray with size 1 and tempArray with size 2. Their size is fixed and cannot be changed! Now you have a loop:
for (Element link : links)
{
...
for(int i = 0; i < thisIsAStringArray.length; i++)
{
tempArray[i] = thisIsAStringArray[i];
}
}
which reads - for each link you've found, i loops from zero to one (which means that inside the inner loop i will have only the value 0) and than adds the link to the first place (with index 0).
You cannot change the size of an array in runtime. If you cannot tell ahead how many items you will have, you must use a List. Try something like this:
ArrayList<String> myList = new ArrayList<>();
for (Element link : links)
myList.add(link);

JavaFX tableView content from wekaapi is missing

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);
}

MOA's StreamKM clustering doesn't return any result

I'm currently trying to cluster a great amount of data points into a given amount of clusters and I wanted to try MOA's streaming based k-means StreamKM. A very simple example of what I'm trying to do using random data looks as follows:
StreamKM streamKM = new StreamKM();
streamKM.numClustersOption.setValue(5); // default setting
streamKM.widthOption.setValue(100000); // default setting
streamKM.prepareForUse();
for (int i = 0; i < 150000; i++) {
streamKM.trainOnInstanceImpl(randomInstance(2));
}
Clustering result = streamKM.getClusteringResult();
System.out.println("size = " + result.size());
System.out.println("dimension = " + result.dimension());
The random instances are created as follows:
static DenseInstance randomInstance(int size) {
DenseInstance instance = new DenseInstance(size);
for (int idx = 0; idx < size; idx++) {
instance.setValue(idx, Math.random());
}
return instance;
}
However, when running the given code, no clusters seem to be created:
System.out.println("size = " + result.size()); // size = 0
System.out.println("dimension = " + result.dimension()); // NPE
Is there anything else I need to take care of, or do I have a fundamental misunderstanding of the MOA clustering concepts?
I think prepareForUse() method is not the correct method that initialize the algorithm.
Instead of streamKM.prepareForUse(); , you should use streamKM.resetLearning();.
In short, your code should be like:
StreamKM streamKM = new StreamKM();
streamKM.numClustersOption.setValue(5); // default setting
streamKM.widthOption.setValue(100000); // default setting
streamKM. resetLearning(); // UPDATED CODE LINE !!!
for (int i = 0; i < 150000; i++) {
streamKM.trainOnInstanceImpl(randomInstance(2));
}
Clustering result = streamKM.getClusteringResult();
System.out.println("size = " + result.size());
System.out.println("dimension = " + result.dimension());

Why list is replacing the old object with new object

I am trying to read the object through xml using JAXB and updating some info the saving back.
here is the read and update code-
dbf = DocumentBuilderFactory.newInstance();
dbuilder = dbf.newDocumentBuilder();
document = dbuilder.parse(file);
jc = JAXBContext.newInstance(OutletWisePlanningListContainer.class);
Binder<Node> binder = jc.createBinder();
owpLContainer = (OutletWisePlanningListContainer)
binder.unmarshal(document);
owpLContList = owpLContainer.getOwpList();
then - updating the objects
for (OutletWisePlanningList owpl1 : owpLContList) {
owpl = owpl1.getOwpList();
owpList = new OutletWisePlanningList();
owpList = owpl1;
skuList.add(owpl1.getSkuId());
for (i = 1; i < sku; i++) {
if (owpl1.getSkuId().trim().equals(request.getParameter("skuId" + i).trim())) {
owpl1.getSkuId();
ArrayList idList = new ArrayList();
int j = 1, cnt = 1;
al1.clear();
int perf = Integer.parseInt(request.getParameter("hdnPerf" + i));
for (OutletWisePlanning owps : owpl) {
owp = new OutletWisePlanning();
for (j = 1; j < perf; j++) { //used only when Planned outlets is changed
if (owp.getMarketIntId().trim().equals(request.getParameter("UID" + i + '-' + j).trim()) && !request.getParameter("txtTARGET_SETvolume" + i + '-' + j).trim().equals("0")) {
//some updation in owp object
al1.add(owp);
}
}
if (request.getParameter("selPlanUnplanned").equals("0")) { //used when Unplanned in selected
al1.add(owp);
}
}
owpList.setOwpList(al1);
owpList.setSkuId(owpl1.getSkuId());
}
}
owpList1.add(owpList);
Iterator itr = owpList1.iterator();
while (itr.hasNext()) {
OutletWisePlanningList op1 = (OutletWisePlanningList) itr.next();
for (OutletWisePlanning op2 : op1.getOwpList()) {
System.out.println("Party id in the owpList1" + op2.getPartyId());
}
}
}
al.addAll(owpList1);
And the xml from where i am reading data and updating the same-
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<OutletWisePlanningListContainer>
<OutletWisePlanningList skuId="4214">
<OutletWisePlanning partyId="14560">
<marketIntId>14002</marketIntId>
<perfAllocId>818</perfAllocId>
<relPartyName>Aangan (Indian North)</relPartyName>
<addrLine1>Bhikaji Cama Place</addrLine1>
<locationName>-NA-</locationName>
<planQty>30</planQty>
<planCpc>10</planCpc>
<agreedQty>0</agreedQty>
<agreedCpc>0</agreedCpc>
<executedQty>0</executedQty>
<executedCpc>0</executedCpc>
<actualQty>0</actualQty>
<actualCpc>0</actualCpc>
<claimedQty>0</claimedQty>
<claimedCpc>0</claimedCpc>
<genCpc>0</genCpc>
<eligibility></eligibility>
<eligibleItem>0</eligibleItem>
</OutletWisePlanning>
</OutletWisePlanningList>
<OutletWisePlanningList skuId="4215">
<OutletWisePlanning partyId="14554">
<marketIntId>14105</marketIntId>
<perfAllocId>819</perfAllocId>
<relPartyName>Dhaba (Indian North)</relPartyName>
<addrLine1>Aurangzeb Road</addrLine1>
<locationName>Aurangzeb Road</locationName>
<planQty>44</planQty>
<planCpc>10</planCpc>
<agreedQty>0</agreedQty>
<agreedCpc>0</agreedCpc>
<executedQty>0</executedQty>
<executedCpc>0</executedCpc>
<actualQty>0</actualQty>
<actualCpc>0</actualCpc>
<claimedQty>0</claimedQty>
<claimedCpc>0</claimedCpc>
<genCpc>0</genCpc>
<eligibility></eligibility>
<eligibleItem>0</eligibleItem>
</OutletWisePlanning>
</OutletWisePlanningList>
I am getting the xml with different skuId(i.e. ok) but partyId = 14560 is replacing by partyId = 14554 with its whole data.
Please help
This code has lot of unnecessary initializations and assignments. You should have to clear all those.
However to answer your question, my best guess is that the issue is with this for loop.
for (OutletWisePlanning owps : owpl) {
owp = new OutletWisePlanning();
for (j = 1; j < perf; j++) { //used only when Planned outlets is changed
if (owp.getMarketIntId().trim().equals(request.getParameter("UID" + i + '-' + j).trim()) && !request.getParameter("txtTARGET_SETvolume" + i + '-' + j).trim().equals("0")) {
//some updation in owp object
al1.add(owp);
}
}
if (request.getParameter("selPlanUnplanned").equals("0")) { //used when Unplanned in selected
al1.add(owp);
}
}
First you are initializing the owp inside the for-each loop. owp = new OutletWisePlanning(); This is never been assigned any values still you are using it on the if condition. Here either the code is incomplete or you might have some initializations inside the constructor. Anyway within the inner for loop the "SAME OBJECT REFERENCE" gets modified and added to the list al1 when the if conditions(both inside and outside the loop) satisfies. So no matter how many times you add the object to this list all will have the same value.

How to get array data with CATIA using com4j API to get point coordinate?

I try to retreive information from catia usig com4j. Some methods require to pass an array in argument to retreive information but the array is never populated. In this example is to get coordinate from a point in catia.
The declaration of the method generated by com4j
public interface Point extends com4j.catia.HybridShape {
...
void getCoordinates(java.lang.Object[] oCoordinates);
...
}
my code to get the coordinate
public static void testGetPointCoordinates() {
String catiafile="E:\\test.CATPart";
Application app =null;
app = COM4J.createInstance(Application.class, "CATIA.Application");
Documents documents = app.documents();
Document oDocument = (Document) documents.open(new Holder<>(catiaFile.getAbsolutePath()));
PartDocument partDocument = oDocument.queryInterface(PartDocument.class);
Part part = partDocument.part();
Factory HSFactory = part.hybridShapeFactory();
HybridShapeFactory HSF = HSFactory.queryInterface(HybridShapeFactory.class);
HybridBodies hbodies = part.hybridBodies();
int n = hbodies.count();
for (int i = 1; i <= n; i++) {
HybridBody hbody = null;
hbody = hbodies.item(i);
int nbitems = hbody.hybridShapes().count();
for (int j = 1; j <= nbitems; j++) {
String name = hbody.hybridShapes().item(j).name();
System.out.println("name=" + name);
//Object tab[]=new Object[3];
if (name.startsWith("Point.12")) {//true point
HybridShape hs = hbody.hybridShapes().item(j);
Reference reference = part.createReferenceFromObject(hs);
HybridShapePointCoord p3 = hs.queryInterface(HybridShapePointCoord.class);
//works
System.out.println("point name = " + p3.name());
System.out.println(p3.y().value());//display 50.0
//doesn't work
Variant tab[] = new Variant[3];
for (int k = 0; k < 3; k++) {
Variant variant = new Variant();
variant.set(k);
tab[k] = variant;
}
p3.getCoordinates(tab);
System.out.println(tab[1].getParseableString()); //display 1 (value not modified)
//doesn't work
tab = new Variant[3];
for (int k = 0; k < 3; k++) {
Variant variant = new Variant(Variant.Type.VT_EMPTY);//tested with VT_R4 VT_R8,...
tab[k] = variant;
}
System.out.println(tab[1].getJavaCode()); //display null (Don't know how to print VT_EMPTY as an Java literal)
//doesn't work with this other solution but ok in VBA
tab = new Variant[3];
//Variant v = new Variant(Type.VT_EMPTY);
tab[0] = new Variant(Variant.Type.VT_EMPTY);
tab[1] = new Variant(Variant.Type.VT_EMPTY);
tab[2] = new Variant(Variant.Type.VT_EMPTY);
HybridShapePointExplicit point = HSF.addNewPointDatum(reference);
point.getCoordinates(tab);
System.out.println(tab[1].doubleValue() + " " + tab[2].toString()); //display 0
//doesn't work
//crash JVM
// tab = new Variant[3];
// p3.getCoordinates(tab);
break;
}
}
}
}
the code below works in VBA with CATIA
Dim P1
Dim coordonnees(2)
Dim coordonnees2(100, 3)
coordonnees(0) = 0
coordonnees(1) = 0
coordonnees(2) = 0
Set P1 = HSF.AddNewPointDatum(hbody.HybridShapes.Item(i))
'fonction to get coordinates
P1.GetCoordinates coordonnees
'set name and coordinates
coordonnees2(Y, 0) = hbody.HybridShapes.Item(i).name
coordonnees2(Y, 1) = Round(coordonnees(0), 3)
coordonnees2(Y, 2) = Round(coordonnees(1), 3)
coordonnees2(Y, 3) = Round(coordonnees(2), 3)
I'm not familiar with com4j, but Point.GetCoordinates is a restricted method:
.
The solution to this problem in VB is you need to create a new variant and set it equal to p3. Then, call GetCoordinates from the variant's context. Intellisense will not work well on the variant, but the call to GetCoordinates will work.
In VB the code would look something like this:
...
Dim p3 As Variant
Dim arrayOfCoord(2) As Variant
Set p3 = hybridShapePointCoord1 'set variant = strongly typed point object
p3.GetCoordinates arrayOfCoord
...
In your case, non-VB, your code might look like this
...
Object p3Obj; //used in vb:Variant p3Var;
p3Obj = p3;
p3Obj.GetCoordinates(tab);//Intellisense will not work, but the call to this method should
...
Let me know if this works.

Categories

Resources