📄 instance.java
字号:
rtrn = rtrn + separator;
}
else{
//System.out.println("Values.length = " + values.length);
//System.out.println("attrNum = "+attrNum);
if(values[attrNum] == null)System.out.println("Error");
String data = ai.attrValue_to_string(values[attrNum]);
if(ai.can_cast_to_nominal())// && protectChars)
;// data = protect_chars(data);
rtrn = rtrn + data;
if(data.equals("")) rtrn = rtrn + "?";
if(attrNum < (schema.num_attr()-1))
rtrn = rtrn + separator;
}
}
if(is_labelled()){
if(schema.num_attr() > 0)
rtrn = rtrn + ", ";
AttrInfo ai = schema.label_info();
String data = ai.attrValue_to_string(labelValue);
//if(protectChars)
// data = protect_chars(data);
rtrn = rtrn + data;
}
rtrn = rtrn + "\n";
return(rtrn);
}
/** Returns the information about the attribute specified.
* @return The information about the attribute.
* @param attrNum The index of the attribute specified.
*/
public AttrInfo attr_info(int attrNum) {
return schema.attr_info(attrNum);
}
/** Checks if this Instance is equal to the specified Instance. Compares the
* attributes values, the weight, and the label of this Instance.
* @return TRUE if these Instances are equal, FALSE otherwise.
* @param i_instance The Instance to be compared to.
*/
public boolean equals(Instance i_instance) {
return equal(i_instance,true,true,false);
}
/** Compares this Instance to the specified Instance. This function may
* optionally compare the label and the weight as part of this process. Setting
* fatalOnFalse will cause equal() to abort with a clear error message
* if the equality test fails.
* @return TRUE if these Instances are equal, FALSE otherwise.
* @param instance The Instance to be compared to.
* @param compLabel TRUE if labels are to be compared, FALSE otherwise.
* @param compWeight TRUE if weights are to be compared, FALSE otherwise.
* @param fatalOnFalse TRUE if an Error message should be displayed if
* the Instances are not equal, FALSE otherwise.
*/
boolean equal(Instance instance,
boolean compLabel, boolean compWeight,
boolean fatalOnFalse) {
// if one instance is labelled and the other is not AND we're
// comparing labels, its a mismatch.
if( compLabel && (is_labelled() != instance.is_labelled())) {
if (fatalOnFalse) {
Error.fatalErr("Instance::equal: equalilty failed (only one of the"
+"two instances has label).");
}
else
return false;
}
// compare labels if compLabel is set
else if(compLabel && is_labelled()) {
// DBGSLOW(schema.label_info().equal(instance.get_schema().label_info(),
// TRUE));
if(!schema.label_info().equal_value(labelValue,instance.get_label())) {
if(fatalOnFalse) {
Error.fatalErr("Instance::equal: equality failed (label values "
+"differ).");
}
return false;
}
}
// compare weights if compWeight is set
if(compWeight) {
if(!MLJ.approx_equal((float)get_weight(),
(float)(instance.get_weight()))) {
if(fatalOnFalse) {
Error.fatalErr("Instance::equal: equality failed (weights do not match: "
+get_weight() +" vs " +instance.get_weight()
+").");
}
return false;
}
}
// check schemas and number of values (DBG only)
// DBGSLOW(get_schema().equal_no_label(instance.get_schema(), TRUE));
// DBG(if (instance.values.size() != values.size())
// err << "Instance::equal: number of values in instance "
// "do not match. class=" << values.size()
// << " and rhs=" << instance.values.size() << fatal_error);
// compare attribute values
for (int i = 0; i < schema.num_attr(); i++) {
if (!attr_info(i).equal_value( values[i], instance.values[i] )) {
if( fatalOnFalse ) {
Error.fatalErr("Instance::equal: equality failed (differed on "
+attr_info(i).name() +").");
}
return false;
}
}
// comparison succeeded
return true;
}
/** Returns a String representation of the Instance.
* @param displayWeight TRUE if the Instance weight is to be displayed, FALSE otherwise.
* @param normalizeReals TRUE if the real values in the Instance are to be normalized, FALSE otherwise.
* @return A String representation of the Instance.
*/
public String out(boolean displayWeight, boolean normalizeReals) {
String rtrn = new String();
rtrn = rtrn + display_unlabelled_out(displayWeight, normalizeReals);
if(is_labelled()){
if(schema.num_attr() > 0)
rtrn = rtrn + ", ";
AttrInfo ai = schema.label_info();
String data = ai.attrValue_to_string(labelValue);
//if(protectChars)
// data = protect_chars(data);
rtrn = rtrn + data;
}
rtrn = rtrn + "\n";
return rtrn;
}
/** Returns a String representation of the Instance.
* @param displayWeight TRUE if the Instance weight is to be displayed, FALSE otherwise.
* @param normalizeReal TRUE if the real values in the Instance are to be normalized, FALSE otherwise.
* @return A String representation of the Instance.
*/
public String display_unlabelled_out(boolean displayWeight, boolean normalizeReal) {
String rtrn = new String();
String separator = new String(", ");
if(displayWeight)
rtrn = rtrn + (weight+separator);
for(int attrNum = 0;attrNum<schema.num_attr();attrNum++){
AttrInfo ai = schema.attr_info(attrNum);
if(normalizeReal && ai.can_cast_to_real()){
RealAttrInfo rai = ai.cast_to_real();
if(rai.is_unknown(values[attrNum]))
rtrn = rtrn + AttrInfo.UNKNOWN_VAL_STR;
else
rtrn = rtrn + rai.normalized_value(values[attrNum]);
if(attrNum < schema.num_attr() -1)
rtrn = rtrn + separator;
}
else{
//System.out.println("Values.length = " + values.length);
//System.out.println("attrNum = "+attrNum);
if(values[attrNum] == null)System.out.println("Error");
String data = ai.attrValue_to_string(values[attrNum]);
if(ai.can_cast_to_nominal())// && protectChars)
;// data = protect_chars(data);
rtrn = rtrn + data;
if((attrNum < (schema.num_attr()-1))&&(!(data.equals(""))))
rtrn = rtrn + separator;
}
}
return rtrn;
}
/** Returns the AttrValue at the specified index number.
* @return The AttrValue at the specified index.
* @param index The index specified.
*/
public AttrValue index(int index) {
return values[index];
}
/** Copies the supplied Instance object into this Instance object.
* @param other The Instance object to be copied.
* @throws CloneNotSupportedException if the cloning process for contained data members encounters a
* CloneNotSupportedException.
*/
public void copy(Instance other) throws CloneNotSupportedException{
values = new AttrValue[other.schema.num_attr()];
for(int i = 0; i < values.length; values[i++] = new AttrValue());
schema = other.schema;
refCount = other.refCount;
set_weight(weight);
set_label((AttrValue)labelValue.clone());
for(int i=0;i<values.length;i++)
values[i] = (AttrValue)other.get_value(i).clone();
}
/** Remove an Attribute from an Instance. When doing many projections, it is more
* efficient to supply a Schema with the deleted attribute.
* @param attrNum The number of the attribute to be removed from this InstanceObject.
* @param schemaWithDelAttr Schema with the attribute deleted.
* @return The modified Instance.
*/
public Instance remove_attr(int attrNum, Schema schemaWithDelAttr) {
if ( attrNum < 0 || attrNum >= num_attr() )
Error.fatalErr("Instance.remove_attr(int,Schema): illegal attrNum \n"
+attrNum+" is passed but the proper range is \n"+" 0 to "+(num_attr() - 1)
+".");
Instance instance = new Instance(schemaWithDelAttr);
for (int i = 0; i < schema.num_attr(); i++)
if (i != attrNum){
index(i - ((i > attrNum)?1:0)).copy(index(i));
}
if (schema.is_labelled()) // both are labelled becauseof
// equal_except_del_attr
instance.set_label(get_label());
return instance;
}
/** Remove an Attribute from an Instance. When doing many projections, it is more
* efficient to supply a Schema with the deleted attribute.
* @param attrNum The number of the attribute to be removed from this InstanceObject.
* @return The modified Instance.
*/
public Instance remove_attr(int attrNum) {
if ( attrNum < 0 || attrNum >= num_attr() )
Error.fatalErr("Instance.remove_attr(int): illegal attrNum \n"
+attrNum+" is passed but the proper range is \n"+" 0 to "+(num_attr() - 1)
+".");
Schema schemaNew = schema.remove_attr(attrNum);
return remove_attr(attrNum, schemaNew);
}
/** Returns the number of values for the specified attribute.
* @param attrNum The number of the attribute for which the number of values is requested.
* @return The number of attribute values.
*/
public int num_attr_values(int attrNum){
return schema.num_attr_values(attrNum);
}
/** Returns the number of possible label values in the Schema of this Instance.
* @return The number of possible label values.
*/
public int num_label_values() {
return nominal_label_info().num_values();
}
/** Returns the information on the label attribute.
* @return Information on the label attribute.
*/
public NominalAttrInfo nominal_label_info() {
return label_info().cast_to_nominal();
}
/** Returns the attribute name.
* @param attrNum The number of the attribute.
* @return The name of the attribute.
*/
public String attr_name(int attrNum){
return schema.attr_name(attrNum);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -