Skip to content

Instantly share code, notes, and snippets.

View rofr's full-sized avatar

Robert Friberg rofr

View GitHub Profile
@rofr
rofr / gist:4110189
Created November 19, 2012 11:16
Nearest Neighbor test driver
public static void main(String[] args) {
int[] sizes = new int[] { 5000, 10000, 50000, 100000,
1000000, 5000000, 10000000 };
for (int numInstances : sizes) {
List<LoanApplication> data = getRandomData(numInstances);
List<LoanApplication> testData = extractTestData(data, 100);
NearestNeighborClassifier<LoanApplication> classifier;
@rofr
rofr / gist:4110451
Created November 19, 2012 12:45
LoanApplication data class
enum Gender{
Male,
Female
}
enum Carrier {
Telenor,
Telia
}
@rofr
rofr / gist:4110513
Created November 19, 2012 12:59
Distance Function interface
public interface DistanceFunction<T> {
double calculateDistance(T item, T item2);
}
@rofr
rofr / gist:4110520
Created November 19, 2012 13:01
LoanApplication distance function
public double calculateDistance(LoanApplication item, LoanApplication item2) {
double distance = 0;
//nominal attributes
if (item.getCarrier() != item2.getCarrier()) distance += 1;
if (item.getGender() != item2.getGender()) distance += 1;
int dayDiff = Math.abs(item.getDayOfWeek() - item2.getDayOfWeek());
if (dayDiff > 3) {
dayDiff -= 1 + 2 * (dayDiff - 4);
@rofr
rofr / gist:4110536
Created November 19, 2012 13:05
kNN Classifier
public double classify(T item) {
int numItems = items.size();
// calculated distances in same order as the items list
double[] distances = new double[numItems];
//calculate the distance to each instance
for (int i = 0; i < numItems; i++) {
distances[i] = distanceFunction.calculateDistance(item, items.get(i));
@rofr
rofr / gist:4110637
Created November 19, 2012 13:29
Multithreaded kNN
/**
* Thread pool from the Java 1.5 Executor Framework
*/
private ExecutorService executorService;
/**
* Initialize the thread pool
*/
private void init() {
int numThreads = Runtime.getRuntime().availableProcessors();
@rofr
rofr / gist:4162362
Created November 28, 2012 16:29
Sql Server Object Explorer in VS12
/*
* The Sql Server Object Explorer Window of Visual Studio 2012
* doesn't release connections properly. Here is a workaround
*
*/
-- 1. Open a query window, either VS or SSMS will do
-- 2. Run the following select, replacing 'MyDB' with the name of your DB
@rofr
rofr / gist:4172177
Created November 29, 2012 21:57
Async journaling with LiveDB
// With Asyncronous journaling enabled, commands are queued and written
// to the journal by a background thread. This increases query and command throughput
// but there is risk of losing commands in the case of a system failure
var config = new EngineConfiguration();
config.AsyncronousJournaling = true;
var engine = Engine.LoadOrCreate<MyModel>(config);
@rofr
rofr / gist:4333472
Created December 19, 2012 00:49
Having a hard time writing clean code with the modern LINQ, functional and fluent styles. How would you improve this code?
public class FrontierEvaluator : Evaluator
{
public override float Evaluate(Disc playerToMove, Board board)
{
int frontierDiscComparison = Square.All
.Where(square => board[(Square) square] != Disc.None)
.Sum(square => square.Neighbors
.Count(neighbor => neighbor != null && board[neighbor] == Disc.None) *
(board[square] == playerToMove ? -1 : 1));
return frontierDiscComparison;
@rofr
rofr / output.txt
Created January 29, 2013 19:41
comparing some compression algorithms...
---------LiveDomain.Core.Compression.DeflateStreamCompressor---------
Compress: 1266235
Decompress: 1007630
Compression: 0,377149519090644
---------LiveDomain.Core.Compression.GzipCompressor---------
Compress: 513028
Decompress: 417533
Compression: 0,382395802972894
---------LiveDomain.Core.Compression.LzfCompressionAdapter---------
Compress: 486846