-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from sanity/regressionTrees
Regression trees
- Loading branch information
Showing
96 changed files
with
5,608 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/quickml/data/instances/RegressionInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package quickml.data.instances; | ||
|
||
import quickml.data.AttributesMap; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Created by alexanderhawk on 4/14/15. | ||
*/ | ||
public class RegressionInstance extends InstanceWithAttributesMap<Double> { | ||
public RegressionInstance(AttributesMap attributes, Double label) { | ||
super(attributes, label, 1.0); | ||
} | ||
public RegressionInstance(AttributesMap attributes, Double label, double weight) { | ||
super(attributes, label, weight); | ||
} | ||
public RegressionInstance(AttributesMap attributes, Double label, double weight, double alternativeTarget) { | ||
super(attributes, label, weight); | ||
this.alternativeTarget = alternativeTarget; | ||
} | ||
public double alternativeTarget; | ||
public long id; | ||
|
||
} | ||
|
84 changes: 84 additions & 0 deletions
84
src/main/java/quickml/data/instances/SparseRegressionInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package quickml.data.instances; | ||
|
||
import org.javatuples.Pair; | ||
import quickml.data.AttributesMap; | ||
|
||
import java.io.Serializable; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by alexanderhawk on 10/12/15. | ||
*/ | ||
public class SparseRegressionInstance extends RegressionInstance { | ||
private int[] indicesOfCorrespondingWeights; | ||
private double[] values; | ||
|
||
public SparseRegressionInstance(AttributesMap attributes, Double label, Map<String, Integer> nameToValueIndexMap) { | ||
super(attributes, label); | ||
setIndicesAndValues(attributes, nameToValueIndexMap); | ||
} | ||
|
||
public SparseRegressionInstance(AttributesMap attributes, Double label, double weight, Map<String, Integer> nameToValueIndexMap) { | ||
super(attributes, label, weight); | ||
setIndicesAndValues(attributes, nameToValueIndexMap); | ||
} | ||
|
||
private void setIndicesAndValues(AttributesMap attributes, Map<String, Integer> nameToIndexMap) { | ||
indicesOfCorrespondingWeights = new int[attributes.size()+1]; | ||
values = new double[attributes.size()+1]; | ||
//add bias term | ||
indicesOfCorrespondingWeights[0] = 0; | ||
values[0] = 1.0; | ||
//add non bias terms | ||
int i = 1; | ||
for (Map.Entry<String, Serializable> entry : attributes.entrySet()) { | ||
if (!(entry.getValue() instanceof Double)) { | ||
throw new RuntimeException("wrong type of values in attributes"); | ||
} | ||
int valueIndex = nameToIndexMap.get(entry.getKey()); | ||
indicesOfCorrespondingWeights[i] = valueIndex; | ||
values[i] = (Double)entry.getValue(); | ||
i++; | ||
} | ||
} | ||
|
||
public static double[] getArrayOfValues(RegressionInstance regressionInstance, Map<String, Integer> nameToIndexMap, boolean useBias){ | ||
int numAttributes = regressionInstance.getAttributes().size(); | ||
AttributesMap attributesMap = regressionInstance.getAttributes(); | ||
double[] valuesArray; | ||
int attributeIndex = 0; | ||
|
||
if (useBias) { | ||
valuesArray = new double[numAttributes + 1]; | ||
valuesArray[0] = 1.0; | ||
attributeIndex++; | ||
} else { | ||
valuesArray = new double[numAttributes]; | ||
} | ||
for (Map.Entry<String, Serializable> attributeEntry : attributesMap.entrySet()) { | ||
attributeIndex = nameToIndexMap.get(attributeEntry.getKey()); | ||
valuesArray[attributeIndex] = (Double)attributeEntry.getValue(); | ||
} | ||
return valuesArray; | ||
} | ||
|
||
@Override | ||
public AttributesMap getAttributes() { | ||
return super.getAttributes(); | ||
} | ||
|
||
public Pair<int[], double[]> getSparseAttributes(){ | ||
return new Pair<>(indicesOfCorrespondingWeights, values); | ||
} | ||
|
||
public double dotProduct(double[] omega) { | ||
double result = 0; | ||
for (int i = 0; i< indicesOfCorrespondingWeights.length; i++) { | ||
int indexOfFeature = indicesOfCorrespondingWeights[i]; | ||
double valueOfFeature = values[i]; | ||
result+= omega[indexOfFeature]* valueOfFeature; | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package quickml.experiments; | ||
|
||
/** | ||
* Created by alexanderhawk on 4/9/16. | ||
* | ||
import java.util.*; | ||
import java.lang.*; | ||
import java.io.*; | ||
*/ | ||
public class GeoDistance { | ||
|
||
/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ | ||
/*:: :*/ | ||
/*:: This routine calculates the distance between two points (given the :*/ | ||
/*:: latitude/longitude of those points). It is being used to calculate :*/ | ||
/*:: the distance between two locations using GeoDataSource (TM) prodducts :*/ | ||
/*:: :*/ | ||
/*:: Definitions: :*/ | ||
/*:: South latitudes are negative, east longitudes are positive :*/ | ||
/*:: :*/ | ||
/*:: Passed to function: :*/ | ||
/*:: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) :*/ | ||
/*:: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) :*/ | ||
/*:: unit = the unit you desire for results :*/ | ||
/*:: where: 'M' is statute miles (default) :*/ | ||
/*:: 'K' is kilometers :*/ | ||
/*:: 'N' is nautical miles :*/ | ||
/*:: Worldwide cities and other features databases with latitude longitude :*/ | ||
/*:: are available at http://www.geodatasource.com :*/ | ||
/*:: :*/ | ||
/*:: For enquiries, please contact [email protected] :*/ | ||
/*:: :*/ | ||
/*:: Official Web site: http://www.geodatasource.com :*/ | ||
/*:: :*/ | ||
/*:: GeoDataSource.com (C) All Rights Reserved 2015 :*/ | ||
/*:: :*/ | ||
/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ | ||
|
||
|
||
public static void main (String[] args) throws java.lang.Exception | ||
{ | ||
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, "M") + " Miles\n"); | ||
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, "K") + " Kilometers\n"); | ||
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, "N") + " Nautical Miles\n"); | ||
} | ||
|
||
public static double distance(double lat1, double lon1, double lat2, double lon2, String unit) { | ||
double theta = lon1 - lon2; | ||
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta)); | ||
dist = Math.acos(dist); | ||
dist = rad2deg(dist); | ||
dist = dist * 60 * 1.1515; | ||
if (unit == "K") { | ||
dist = dist * 1.609344; | ||
} else if (unit == "N") { | ||
dist = dist * 0.8684; | ||
} | ||
|
||
return (dist); | ||
} | ||
|
||
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ | ||
/*:: This function converts decimal degrees to radians :*/ | ||
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ | ||
private static double deg2rad(double deg) { | ||
return (deg * Math.PI / 180.0); | ||
} | ||
|
||
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ | ||
/*:: This function converts radians to decimal degrees :*/ | ||
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ | ||
private static double rad2deg(double rad) { | ||
return (rad * 180 / Math.PI); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.