Skip to content

Commit

Permalink
Refactoring the scalers.
Browse files Browse the repository at this point in the history
  • Loading branch information
datumbox committed Dec 27, 2016
1 parent eac571e commit 4dc9911
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 72 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
@@ -1,7 +1,7 @@
CHANGELOG
=========

Version 0.8.0-SNAPSHOT - Build 20161226
Version 0.8.0-SNAPSHOT - Build 20161227
---------------------------------------

- Initial Updates:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -16,7 +16,7 @@ The code is licensed under the [Apache License, Version 2.0](./LICENSE).
Version
-------

The latest version is 0.8.0-SNAPSHOT (Build 20161226).
The latest version is 0.8.0-SNAPSHOT (Build 20161227).

The [devel branch](https://github.com/datumbox/datumbox-framework/tree/devel) is the development branch (default github branch). The [master branch](https://github.com/datumbox/datumbox-framework/tree/master) contains the latest stable version of the framework. All the stable releases are marked with [tags](https://github.com/datumbox/datumbox-framework/releases).

Expand Down
Expand Up @@ -138,30 +138,15 @@ protected void _transform(Dataframe newData) {

Double maxAbsolute = entry.getValue();

double normalizedValue;
if(maxAbsolute.equals(0.0)) {
normalizedValue = Math.signum(value);
}
else {
normalizedValue = value/maxAbsolute;
}

xData.put(column, normalizedValue);
xData.put(column, scale(value, maxAbsolute));
modified = true;
}

if(scaleResponse && yData != null) {
Double maxAbsolute = maxAbsoluteColumnValues.get(Dataframe.COLUMN_NAME_Y);

Double value = TypeInference.toDouble(yData);
Double maxAbsolute = maxAbsoluteColumnValues.get(Dataframe.COLUMN_NAME_Y);

if(maxAbsolute.equals(0.0)) {
yData = Math.signum(value);
}
else {
yData = TypeInference.toDouble(yData)/maxAbsolute;
}

yData = scale(value, maxAbsolute);
modified = true;
}

Expand All @@ -175,4 +160,19 @@ protected void _transform(Dataframe newData) {
});
}

/**
* Performs the actual rescaling handling corner cases.
*
* @param value
* @param maxAbsolute
* @return
*/
private double scale(Double value, Double maxAbsolute) {
if(maxAbsolute.equals(0.0)) {
return Math.signum(value);
}
else {
return value/maxAbsolute;
}
}
}
Expand Up @@ -167,31 +167,16 @@ protected void _transform(Dataframe newData) {
Double min = entry.getValue();
Double max = maxColumnValues.get(column);

double normalizedValue;
if(min.equals(max)) {
normalizedValue = (value>max || (value==max && value!=0.0))?1.0:0.0;
}
else {
normalizedValue = (value-min)/(max-min);
}

xData.put(column, normalizedValue);
xData.put(column, scale(value, min, max));
modified = true;
}

if(scaleResponse && yData != null) {
Double value = TypeInference.toDouble(yData);
Double min = minColumnValues.get(Dataframe.COLUMN_NAME_Y);
Double max = maxColumnValues.get(Dataframe.COLUMN_NAME_Y);

Double value = TypeInference.toDouble(yData);

if(min.equals(max)) {
yData = (value>max || (value==max && value!=0.0))?1.0:0.0;
}
else {
yData = (value-min)/(max-min);
}

yData = scale(value, min, max);
modified = true;
}

Expand All @@ -205,4 +190,29 @@ protected void _transform(Dataframe newData) {
});
}

/**
* Performs the actual rescaling handling corner cases.
*
* @param value
* @param min
* @param max
* @return
*/
private double scale(Double value, Double min, Double max) {
if(min.equals(max)) {
if(value>max) {
return 1.0;
}
else if(value==max && value!=0.0) {
return 1.0;
}
else {
return 0.0;
}
}
else {
return (value-min)/(max-min);
}
}

}
Expand Up @@ -167,47 +167,16 @@ protected void _transform(Dataframe newData) {
Double mean = entry.getValue();
Double std = stdColumnValues.get(column);

double normalizedValue;
if(std.equals(0.0)) {
if(value == 0.0) {
normalizedValue = 0.0;
}
else if(value >= mean) {
normalizedValue = 1.0;
}
else {
normalizedValue = -1.0;
}
}
else {
normalizedValue = (value-mean)/std;
}

xData.put(column, normalizedValue);
xData.put(column, scale(value, mean, std));
modified = true;
}

if(scaleResponse && yData != null) {
Double value = TypeInference.toDouble(yData);
Double mean = meanColumnValues.get(Dataframe.COLUMN_NAME_Y);
Double std = stdColumnValues.get(Dataframe.COLUMN_NAME_Y);

Double value = TypeInference.toDouble(yData);

if(std.equals(0.0)) {
if(value == 0.0) {
yData = 0.0;
}
else if(value >= mean) {
yData = 1.0;
}
else {
yData = -1.0;
}
}
else {
yData = (value-mean)/std;
}

yData = scale(value, mean, std);
modified = true;
}

Expand All @@ -221,4 +190,29 @@ else if(value >= mean) {
});
}

/**
* Performs the actual rescaling handling corner cases.
*
* @param value
* @param mean
* @param std
* @return
*/
private double scale(Double value, Double mean, Double std) {
if(std.equals(0.0)) {
if(value > mean) {
return 1.0;
}
else if(value < mean) {
return -1.0;
}
else {
return Math.signum(value);
}
}
else {
return (value-mean)/std;
}
}

}

0 comments on commit 4dc9911

Please sign in to comment.