Skip to content

Instantly share code, notes, and snippets.

@amitkgupta
Last active August 29, 2015 14:06
Show Gist options
  • Save amitkgupta/30d1f6c8f49ba8d23124 to your computer and use it in GitHub Desktop.
Save amitkgupta/30d1f6c8f49ba8d23124 to your computer and use it in GitHub Desktop.
kNN Classifications on a large dataset with different packages.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
package main
import (
"fmt"
"github.com/amitkgupta/goodlearn/classifier/knn"
"github.com/amitkgupta/goodlearn/csvparse"
"github.com/amitkgupta/goodlearn/data/row"
"runtime"
"flag"
"log"
"os"
"runtime/pprof"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
runtime.GOMAXPROCS(runtime.NumCPU())
println("parsing test")
validationSample, _ := csvparse.DatasetFromPath("many_features_test.csv", 0, 1)
println("parsed test")
println("parsing training")
trainingSample, _ := csvparse.DatasetFromPath("many_features_training.csv", 0, 1)
println("parsed training")
c, _ := knn.NewKNNClassifier(1)
c.Train(trainingSample)
var totalCorrect float32 = 0
successChannel := make(chan float32, 10000) //len(validationSample))
for i := 0; i < 10000; i++ {
test, _ := validationSample.Row(i)
go func(t row.Row, j int) {
if j%5 == 0 {
println("classifying", j)
}
cl, _ := c.Classify(test)
if cl.Equals(test.Target()) {
successChannel <- 1
} else {
successChannel <- 0
}
if j%5 == 0 {
println("classified", j)
}
}(test, i)
}
for i := 0; i < 10000; i++ { //len(validationSample); i++ {
totalCorrect += <-successChannel
}
fmt.Println(float32(totalCorrect)) // / float32(len(validationSample)))
}
// Takes about 50s on the beefy machine, *with* goodlearn optimized
// - replace math.Pow(x,2) with x*x in distance.go
// - SliceFromStrings was super slow, but assuming contiguous features and all of them are floats, much faster
// Outputs unreasonable answer: 10000
Display the source blob
Display the rendered blob
Raw
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.36.0 (20140111.2315)
-->
<!-- Title: bbb; 556 samples Pages: 1 -->
<svg width="100%" height="100%"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<script type="text/ecmascript"><![CDATA[
// SVGPan
// http://www.cyberz.org/blog/2009/12/08/svgpan-a-javascript-svg-panzoomdrag-library/
// Local modification: if(true || ...) below to force panning, never moving.
// Local modification: add clamping to fix bug in handleMouseWheel.
/**
* SVGPan library 1.2
* ====================
*
* Given an unique existing element with id "viewport", including the
* the library into any SVG adds the following capabilities:
*
* - Mouse panning
* - Mouse zooming (using the wheel)
* - Object dargging
*
* Known issues:
*
* - Zooming (while panning) on Safari has still some issues
*
* Releases:
*
* 1.2, Sat Mar 20 08:42:50 GMT 2010, Zeng Xiaohui
* Fixed a bug with browser mouse handler interaction
*
* 1.1, Wed Feb 3 17:39:33 GMT 2010, Zeng Xiaohui
* Updated the zoom code to support the mouse wheel on Safari/Chrome
*
* 1.0, Andrea Leofreddi
* First release
*
* This code is licensed under the following BSD license:
*
* Copyright 2009-2010 Andrea Leofreddi <[email protected]>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of Andrea Leofreddi.
*/
var root = document.documentElement;
var state = 'none', stateTarget, stateOrigin, stateTf;
setupHandlers(root);
/**
* Register handlers
*/
function setupHandlers(root){
setAttributes(root, {
"onmouseup" : "add(evt)",
"onmousedown" : "handleMouseDown(evt)",
"onmousemove" : "handleMouseMove(evt)",
"onmouseup" : "handleMouseUp(evt)",
//"onmouseout" : "handleMouseUp(evt)", // Decomment this to stop the pan functionality when dragging out of the SVG element
});
if(navigator.userAgent.toLowerCase().indexOf('webkit') >= 0)
window.addEventListener('mousewheel', handleMouseWheel, false); // Chrome/Safari
else
window.addEventListener('DOMMouseScroll', handleMouseWheel, false); // Others
var g = svgDoc.getElementById("svg");
g.width = "100%";
g.height = "100%";
}
/**
* Instance an SVGPoint object with given event coordinates.
*/
function getEventPoint(evt) {
var p = root.createSVGPoint();
p.x = evt.clientX;
p.y = evt.clientY;
return p;
}
/**
* Sets the current transform matrix of an element.
*/
function setCTM(element, matrix) {
var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
element.setAttribute("transform", s);
}
/**
* Dumps a matrix to a string (useful for debug).
*/
function dumpMatrix(matrix) {
var s = "[ " + matrix.a + ", " + matrix.c + ", " + matrix.e + "\n " + matrix.b + ", " + matrix.d + ", " + matrix.f + "\n 0, 0, 1 ]";
return s;
}
/**
* Sets attributes of an element.
*/
function setAttributes(element, attributes){
for (i in attributes)
element.setAttributeNS(null, i, attributes[i]);
}
/**
* Handle mouse move event.
*/
function handleMouseWheel(evt) {
if(evt.preventDefault)
evt.preventDefault();
evt.returnValue = false;
var svgDoc = evt.target.ownerDocument;
var delta;
if(evt.wheelDelta)
delta = evt.wheelDelta / 3600; // Chrome/Safari
else
delta = evt.detail / -90; // Mozilla
var z = 1 + delta; // Zoom factor: 0.9/1.1
// Clamp to reasonable values.
// The 0.1 check is important because
// a very large scroll can turn into a
// negative z, which rotates the image 180 degrees.
if(z < 0.1)
z = 0.1;
if(z > 10.0)
z = 10.0;
var g = svgDoc.getElementById("viewport");
var p = getEventPoint(evt);
p = p.matrixTransform(g.getCTM().inverse());
// Compute new scale matrix in current mouse position
var k = root.createSVGMatrix().translate(p.x, p.y).scale(z).translate(-p.x, -p.y);
setCTM(g, g.getCTM().multiply(k));
stateTf = stateTf.multiply(k.inverse());
}
/**
* Handle mouse move event.
*/
function handleMouseMove(evt) {
if(evt.preventDefault)
evt.preventDefault();
evt.returnValue = false;
var svgDoc = evt.target.ownerDocument;
var g = svgDoc.getElementById("viewport");
if(state == 'pan') {
// Pan mode
var p = getEventPoint(evt).matrixTransform(stateTf);
setCTM(g, stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y));
} else if(state == 'move') {
// Move mode
var p = getEventPoint(evt).matrixTransform(g.getCTM().inverse());
setCTM(stateTarget, root.createSVGMatrix().translate(p.x - stateOrigin.x, p.y - stateOrigin.y).multiply(g.getCTM().inverse()).multiply(stateTarget.getCTM()));
stateOrigin = p;
}
}
/**
* Handle click event.
*/
function handleMouseDown(evt) {
if(evt.preventDefault)
evt.preventDefault();
evt.returnValue = false;
var svgDoc = evt.target.ownerDocument;
var g = svgDoc.getElementById("viewport");
if(true || evt.target.tagName == "svg") {
// Pan mode
state = 'pan';
stateTf = g.getCTM().inverse();
stateOrigin = getEventPoint(evt).matrixTransform(stateTf);
} else {
// Move mode
state = 'move';
stateTarget = evt.target;
stateTf = g.getCTM().inverse();
stateOrigin = getEventPoint(evt).matrixTransform(stateTf);
}
}
/**
* Handle mouse button release event.
*/
function handleMouseUp(evt) {
if(evt.preventDefault)
evt.preventDefault();
evt.returnValue = false;
var svgDoc = evt.target.ownerDocument;
if(state == 'pan' || state == 'move') {
// Quit pan mode
state = '';
}
}
]]></script>
<g id="viewport" transform="translate(0,0)">
<g id="viewport" class="graph" transform="scale(1 1) rotate(0) translate(4 1716)">
<title>bbb; 556 samples</title>
<polygon fill="white" stroke="none" points="-4,4 -4,-1716 1149,-1716 1149,4 -4,4"/>
<!-- Legend -->
<g id="node1" class="node"><title>Legend</title>
<text text-anchor="start" x="8" y="-1688.8" font-family="Times,serif" font-size="24.00">bbb</text>
<text text-anchor="start" x="8" y="-1662.8" font-family="Times,serif" font-size="24.00">Total samples: 556</text>
<text text-anchor="start" x="8" y="-1636.8" font-family="Times,serif" font-size="24.00">Focusing on: 556</text>
<text text-anchor="start" x="8" y="-1610.8" font-family="Times,serif" font-size="24.00">Dropped nodes with &lt;= 2 abs(samples)</text>
<text text-anchor="start" x="8" y="-1584.8" font-family="Times,serif" font-size="24.00">Dropped edges with &lt;= 0 samples</text>
</g>
<!-- N1 -->
<g id="node2" class="node"><title>N1</title>
<polygon fill="none" stroke="black" points="482.25,-1660.5 409.75,-1660.5 409.75,-1625.5 482.25,-1625.5 482.25,-1660.5"/>
<text text-anchor="middle" x="446" y="-1650.1" font-family="Times,serif" font-size="8.00">runtime.gosched0</text>
<text text-anchor="end" x="474.5" y="-1641.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="474.5" y="-1632.1" font-family="Times,serif" font-size="8.00">of 551 (99.1%)</text>
</g>
<!-- N2 -->
<g id="node3" class="node"><title>N2</title>
<polygon fill="none" stroke="black" points="478.25,-1521.5 413.75,-1521.5 413.75,-1486.5 478.25,-1486.5 478.25,-1521.5"/>
<text text-anchor="middle" x="446" y="-1511.1" font-family="Times,serif" font-size="8.00">main.func·001</text>
<text text-anchor="end" x="470.5" y="-1502.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="470.5" y="-1493.1" font-family="Times,serif" font-size="8.00">of 488 (87.8%)</text>
</g>
<!-- N1&#45;&gt;N2 -->
<g id="edge2" class="edge"><title>N1&#45;&gt;N2</title>
<path fill="none" stroke="black" stroke-width="2" d="M446,-1625.47C446,-1602.23 446,-1559.61 446,-1531.8"/>
<polygon fill="black" stroke="black" stroke-width="2" points="449.5,-1531.58 446,-1521.58 442.5,-1531.58 449.5,-1531.58"/>
<text text-anchor="middle" x="456.5" y="-1544.3" font-family="Times,serif" font-size="14.00">488</text>
</g>
<!-- N7 -->
<g id="node8" class="node"><title>N7</title>
<polygon fill="none" stroke="black" points="719.5,-1521.5 658.5,-1521.5 658.5,-1486.5 719.5,-1486.5 719.5,-1521.5"/>
<text text-anchor="middle" x="689" y="-1511.1" font-family="Times,serif" font-size="8.00">runtime.main</text>
<text text-anchor="end" x="712" y="-1502.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="712" y="-1493.1" font-family="Times,serif" font-size="8.00">of 63 (11.3%)</text>
</g>
<!-- N1&#45;&gt;N7 -->
<g id="edge32" class="edge"><title>N1&#45;&gt;N7</title>
<path fill="none" stroke="black" d="M453.364,-1625.48C460.989,-1610.14 474.274,-1587.66 492,-1574 538.808,-1537.94 605.909,-1519.82 648.463,-1511.42"/>
<polygon fill="black" stroke="black" points="649.284,-1514.83 658.463,-1509.54 647.988,-1507.95 649.284,-1514.83"/>
<text text-anchor="middle" x="558" y="-1544.3" font-family="Times,serif" font-size="14.00">63</text>
</g>
<!-- N3 -->
<g id="node4" class="node"><title>N3</title>
<polygon fill="none" stroke="black" points="531,-1434 361,-1434 361,-1342 531,-1342 531,-1434"/>
<text text-anchor="middle" x="446" y="-1409.92" font-family="Times,serif" font-size="25.10">main.classify</text>
<text text-anchor="end" x="523" y="-1381.92" font-family="Times,serif" font-size="25.10">65 (11.7%)</text>
<text text-anchor="end" x="523" y="-1353.92" font-family="Times,serif" font-size="25.10">of 485 (87.2%)</text>
</g>
<!-- N2&#45;&gt;N3 -->
<g id="edge16" class="edge"><title>N2&#45;&gt;N3</title>
<path fill="none" stroke="black" stroke-width="2" d="M446,-1486.18C446,-1475.11 446,-1459.92 446,-1444.84"/>
<polygon fill="black" stroke="black" stroke-width="2" points="449.5,-1444.48 446,-1434.48 442.5,-1444.48 449.5,-1444.48"/>
<text text-anchor="middle" x="456.5" y="-1456.3" font-family="Times,serif" font-size="14.00">485</text>
</g>
<!-- N4 -->
<g id="node5" class="node"><title>N4</title>
<polygon fill="none" stroke="black" points="781.5,-1290 110.5,-1290 110.5,-1172 781.5,-1172 781.5,-1290"/>
<text text-anchor="middle" x="446" y="-1246.32" font-family="Times,serif" font-size="49.60">main.squareDistanceWithBailout</text>
<text text-anchor="end" x="774" y="-1191.32" font-family="Times,serif" font-size="49.60">384 (69.1%)</text>
</g>
<!-- N3&#45;&gt;N4 -->
<g id="edge3" class="edge"><title>N3&#45;&gt;N4</title>
<path fill="none" stroke="black" stroke-width="2" d="M446,-1341.75C446,-1328.74 446,-1314.24 446,-1300.17"/>
<polygon fill="black" stroke="black" stroke-width="2" points="449.5,-1300.02 446,-1290.02 442.5,-1300.02 449.5,-1300.02"/>
<text text-anchor="middle" x="456.5" y="-1312.3" font-family="Times,serif" font-size="14.00">384</text>
</g>
<!-- N8 -->
<g id="node9" class="node"><title>N8</title>
<polygon fill="none" stroke="black" points="802,-1120 640,-1120 640,-1066 802,-1066 802,-1120"/>
<text text-anchor="middle" x="721" y="-1099.28" font-family="Times,serif" font-size="20.90">runtime.duffcopy</text>
<text text-anchor="end" x="794" y="-1076.28" font-family="Times,serif" font-size="20.90">37 (6.7%)</text>
</g>
<!-- N3&#45;&gt;N8 -->
<g id="edge26" class="edge"><title>N3&#45;&gt;N8</title>
<path fill="none" stroke="black" d="M531.306,-1374.82C622.684,-1359.83 758.821,-1331.19 790,-1290 821.654,-1248.19 808.579,-1221.04 790,-1172 783.682,-1155.32 771.989,-1139.92 760,-1127.28"/>
<polygon fill="black" stroke="black" points="762.474,-1124.8 752.946,-1120.17 757.505,-1129.73 762.474,-1124.8"/>
<text text-anchor="middle" x="816" y="-1227.3" font-family="Times,serif" font-size="14.00">36</text>
</g>
<!-- N5 -->
<g id="node6" class="node"><title>N5</title>
<polygon fill="none" stroke="black" points="852.5,-1405.5 791.5,-1405.5 791.5,-1370.5 852.5,-1370.5 852.5,-1405.5"/>
<text text-anchor="middle" x="822" y="-1395.1" font-family="Times,serif" font-size="8.00">main.main</text>
<text text-anchor="end" x="845" y="-1386.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="845" y="-1377.1" font-family="Times,serif" font-size="8.00">of 63 (11.3%)</text>
</g>
<!-- N6 -->
<g id="node7" class="node"><title>N6</title>
<polygon fill="none" stroke="black" points="945.25,-1251.5 850.75,-1251.5 850.75,-1210.5 945.25,-1210.5 945.25,-1251.5"/>
<text text-anchor="middle" x="898" y="-1239.42" font-family="Times,serif" font-size="10.10">main.parseCSVFile</text>
<text text-anchor="end" x="937.5" y="-1228.42" font-family="Times,serif" font-size="10.10">1 (0.2%)</text>
<text text-anchor="end" x="937.5" y="-1217.42" font-family="Times,serif" font-size="10.10">of 63 (11.3%)</text>
</g>
<!-- N5&#45;&gt;N6 -->
<g id="edge12" class="edge"><title>N5&#45;&gt;N6</title>
<path fill="none" stroke="black" d="M830.133,-1370.41C842.904,-1344.37 867.832,-1293.53 883.662,-1261.24"/>
<polygon fill="black" stroke="black" points="886.999,-1262.39 888.259,-1251.87 880.714,-1259.31 886.999,-1262.39"/>
<text text-anchor="middle" x="867" y="-1312.3" font-family="Times,serif" font-size="14.00">63</text>
</g>
<!-- N6&#45;&gt;N8 -->
<g id="edge6" class="edge"><title>N6&#45;&gt;N8</title>
<path fill="none" stroke="black" d="M888.197,-1210.2C877.106,-1189.63 857.254,-1157.72 832,-1138 825.681,-1133.07 818.739,-1128.62 811.505,-1124.62"/>
<polygon fill="black" stroke="black" points="812.7,-1121.3 802.212,-1119.8 809.476,-1127.51 812.7,-1121.3"/>
<text text-anchor="middle" x="850.5" y="-1142.3" font-family="Times,serif" font-size="14.00">1</text>
</g>
<!-- N9 -->
<g id="node10" class="node"><title>N9</title>
<polygon fill="none" stroke="black" points="975.25,-1116.5 820.75,-1116.5 820.75,-1069.5 975.25,-1069.5 975.25,-1116.5"/>
<text text-anchor="middle" x="898" y="-1103.14" font-family="Times,serif" font-size="11.70">main.NewLabelWithFeatures</text>
<text text-anchor="end" x="967.5" y="-1090.14" font-family="Times,serif" font-size="11.70">3 (0.5%)</text>
<text text-anchor="end" x="967.5" y="-1077.14" font-family="Times,serif" font-size="11.70">of 32 (5.8%)</text>
</g>
<!-- N6&#45;&gt;N9 -->
<g id="edge19" class="edge"><title>N6&#45;&gt;N9</title>
<path fill="none" stroke="black" d="M898,-1210.22C898,-1188.34 898,-1152.72 898,-1126.68"/>
<polygon fill="black" stroke="black" points="901.5,-1126.52 898,-1116.52 894.5,-1126.52 901.5,-1126.52"/>
<text text-anchor="middle" x="905" y="-1142.3" font-family="Times,serif" font-size="14.00">32</text>
</g>
<!-- N10 -->
<g id="node11" class="node"><title>N10</title>
<polygon fill="none" stroke="black" points="1051.5,-1110.5 994.5,-1110.5 994.5,-1075.5 1051.5,-1075.5 1051.5,-1110.5"/>
<text text-anchor="middle" x="1023" y="-1100.1" font-family="Times,serif" font-size="8.00">bytes.Split</text>
<text text-anchor="end" x="1044" y="-1091.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="1044" y="-1082.1" font-family="Times,serif" font-size="8.00">of 29 (5.2%)</text>
</g>
<!-- N6&#45;&gt;N10 -->
<g id="edge14" class="edge"><title>N6&#45;&gt;N10</title>
<path fill="none" stroke="black" d="M916.176,-1210.22C938.513,-1185.92 976.41,-1144.69 1000.55,-1118.43"/>
<polygon fill="black" stroke="black" points="1003.37,-1120.53 1007.56,-1110.8 998.217,-1115.79 1003.37,-1120.53"/>
<text text-anchor="middle" x="986" y="-1142.3" font-family="Times,serif" font-size="14.00">29</text>
</g>
<!-- N7&#45;&gt;N5 -->
<g id="edge23" class="edge"><title>N7&#45;&gt;N5</title>
<path fill="none" stroke="black" d="M708.339,-1486.42C731.093,-1466.92 769.022,-1434.41 794.748,-1412.36"/>
<polygon fill="black" stroke="black" points="797.323,-1414.76 802.637,-1405.6 792.767,-1409.45 797.323,-1414.76"/>
<text text-anchor="middle" x="754" y="-1456.3" font-family="Times,serif" font-size="14.00">63</text>
</g>
<!-- N12 -->
<g id="node13" class="node"><title>N12</title>
<polygon fill="none" stroke="black" points="911,-1003.5 815,-1003.5 815,-968.5 911,-968.5 911,-1003.5"/>
<text text-anchor="middle" x="863" y="-993.1" font-family="Times,serif" font-size="8.00">main.byteSliceTofloat64</text>
<text text-anchor="end" x="903" y="-984.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="903" y="-975.1" font-family="Times,serif" font-size="8.00">of 26 (4.7%)</text>
</g>
<!-- N9&#45;&gt;N12 -->
<g id="edge25" class="edge"><title>N9&#45;&gt;N12</title>
<path fill="none" stroke="black" d="M890.492,-1069.47C884.984,-1052.95 877.491,-1030.47 871.719,-1013.16"/>
<polygon fill="black" stroke="black" points="875.03,-1012.02 868.547,-1003.64 868.389,-1014.23 875.03,-1012.02"/>
<text text-anchor="middle" x="889" y="-1036.3" font-family="Times,serif" font-size="14.00">26</text>
</g>
<!-- N28 -->
<g id="node29" class="node"><title>N28</title>
<polygon fill="none" stroke="black" points="1005,-899.5 929,-899.5 929,-864.5 1005,-864.5 1005,-899.5"/>
<text text-anchor="middle" x="967" y="-889.1" font-family="Times,serif" font-size="8.00">runtime.makeslice</text>
<text text-anchor="end" x="997" y="-880.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="997" y="-871.1" font-family="Times,serif" font-size="8.00">of 7 (1.3%)</text>
</g>
<!-- N9&#45;&gt;N28 -->
<g id="edge5" class="edge"><title>N9&#45;&gt;N28</title>
<path fill="none" stroke="black" d="M905.516,-1069.24C918.395,-1030.22 944.615,-950.803 958.279,-909.416"/>
<polygon fill="black" stroke="black" points="961.655,-910.354 961.466,-899.761 955.008,-908.16 961.655,-910.354"/>
<text text-anchor="middle" x="944.5" y="-982.3" font-family="Times,serif" font-size="14.00">3</text>
</g>
<!-- N11 -->
<g id="node12" class="node"><title>N11</title>
<polygon fill="none" stroke="black" points="1073.25,-1014 972.75,-1014 972.75,-958 1073.25,-958 1073.25,-1014"/>
<text text-anchor="middle" x="1023" y="-998.24" font-family="Times,serif" font-size="14.70">bytes.genSplit</text>
<text text-anchor="end" x="1065.5" y="-982.24" font-family="Times,serif" font-size="14.70">10 (1.8%)</text>
<text text-anchor="end" x="1065.5" y="-966.24" font-family="Times,serif" font-size="14.70">of 29 (5.2%)</text>
</g>
<!-- N10&#45;&gt;N11 -->
<g id="edge10" class="edge"><title>N10&#45;&gt;N11</title>
<path fill="none" stroke="black" d="M1023,-1075.13C1023,-1061.5 1023,-1041.79 1023,-1024.47"/>
<polygon fill="black" stroke="black" points="1026.5,-1024.07 1023,-1014.07 1019.5,-1024.07 1026.5,-1024.07"/>
<text text-anchor="middle" x="1030" y="-1036.3" font-family="Times,serif" font-size="14.00">29</text>
</g>
<!-- N15 -->
<g id="node16" class="node"><title>N15</title>
<polygon fill="none" stroke="black" points="1100.25,-905.5 1023.75,-905.5 1023.75,-858.5 1100.25,-858.5 1100.25,-905.5"/>
<text text-anchor="middle" x="1062" y="-892.14" font-family="Times,serif" font-size="11.70">bytes.Count</text>
<text text-anchor="end" x="1092.5" y="-879.14" font-family="Times,serif" font-size="11.70">3 (0.5%)</text>
<text text-anchor="end" x="1092.5" y="-866.14" font-family="Times,serif" font-size="11.70">of 15 (2.7%)</text>
</g>
<!-- N11&#45;&gt;N15 -->
<g id="edge17" class="edge"><title>N11&#45;&gt;N15</title>
<path fill="none" stroke="black" d="M1033.35,-957.934C1038.36,-944.826 1044.42,-928.99 1049.67,-915.259"/>
<polygon fill="black" stroke="black" points="1053.02,-916.293 1053.32,-905.702 1046.48,-913.793 1053.02,-916.293"/>
<text text-anchor="middle" x="1052" y="-928.3" font-family="Times,serif" font-size="14.00">15</text>
</g>
<!-- N11&#45;&gt;N28 -->
<g id="edge22" class="edge"><title>N11&#45;&gt;N28</title>
<path fill="none" stroke="black" d="M1008.14,-957.934C999.714,-942.587 989.235,-923.5 980.962,-908.431"/>
<polygon fill="black" stroke="black" points="984.015,-906.719 976.134,-899.637 977.879,-910.088 984.015,-906.719"/>
<text text-anchor="middle" x="999.5" y="-928.3" font-family="Times,serif" font-size="14.00">4</text>
</g>
<!-- N13 -->
<g id="node14" class="node"><title>N13</title>
<polygon fill="none" stroke="black" points="796.25,-904 697.75,-904 697.75,-860 796.25,-860 796.25,-904"/>
<text text-anchor="middle" x="747" y="-891.2" font-family="Times,serif" font-size="11.00">strconv.ParseFloat</text>
<text text-anchor="end" x="788.5" y="-879.2" font-family="Times,serif" font-size="11.00">2 (0.4%)</text>
<text text-anchor="end" x="788.5" y="-867.2" font-family="Times,serif" font-size="11.00">of 18 (3.2%)</text>
</g>
<!-- N12&#45;&gt;N13 -->
<g id="edge28" class="edge"><title>N12&#45;&gt;N13</title>
<path fill="none" stroke="black" d="M844.104,-968.385C826.457,-952.868 799.769,-929.401 778.835,-910.993"/>
<polygon fill="black" stroke="black" points="780.945,-908.188 771.124,-904.213 776.323,-913.445 780.945,-908.188"/>
<text text-anchor="middle" x="815" y="-928.3" font-family="Times,serif" font-size="14.00">18</text>
</g>
<!-- N23 -->
<g id="node24" class="node"><title>N23</title>
<polygon fill="none" stroke="black" points="911,-899.5 815,-899.5 815,-864.5 911,-864.5 911,-899.5"/>
<text text-anchor="middle" x="863" y="-889.1" font-family="Times,serif" font-size="8.00">runtime.slicebytetostring</text>
<text text-anchor="end" x="903" y="-880.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="903" y="-871.1" font-family="Times,serif" font-size="8.00">of 8 (1.4%)</text>
</g>
<!-- N12&#45;&gt;N23 -->
<g id="edge15" class="edge"><title>N12&#45;&gt;N23</title>
<path fill="none" stroke="black" d="M863,-968.157C863,-952.264 863,-928.181 863,-909.676"/>
<polygon fill="black" stroke="black" points="866.5,-909.53 863,-899.53 859.5,-909.53 866.5,-909.53"/>
<text text-anchor="middle" x="866.5" y="-928.3" font-family="Times,serif" font-size="14.00">8</text>
</g>
<!-- N14 -->
<g id="node15" class="node"><title>N14</title>
<polygon fill="none" stroke="black" points="788.5,-805.5 705.5,-805.5 705.5,-758.5 788.5,-758.5 788.5,-805.5"/>
<text text-anchor="middle" x="747" y="-792.14" font-family="Times,serif" font-size="11.70">strconv.atof32</text>
<text text-anchor="end" x="781" y="-779.14" font-family="Times,serif" font-size="11.70">3 (0.5%)</text>
<text text-anchor="end" x="781" y="-766.14" font-family="Times,serif" font-size="11.70">of 16 (2.9%)</text>
</g>
<!-- N13&#45;&gt;N14 -->
<g id="edge30" class="edge"><title>N13&#45;&gt;N14</title>
<path fill="none" stroke="black" d="M747,-859.735C747,-846.87 747,-830.202 747,-815.689"/>
<polygon fill="black" stroke="black" points="750.5,-815.587 747,-805.587 743.5,-815.587 750.5,-815.587"/>
<text text-anchor="middle" x="754" y="-828.3" font-family="Times,serif" font-size="14.00">16</text>
</g>
<!-- N19 -->
<g id="node20" class="node"><title>N19</title>
<polygon fill="none" stroke="black" points="807.25,-685 686.75,-685 686.75,-645 807.25,-645 807.25,-685"/>
<text text-anchor="middle" x="747" y="-669.24" font-family="Times,serif" font-size="14.70">strconv.readFloat</text>
<text text-anchor="end" x="799.5" y="-653.24" font-family="Times,serif" font-size="14.70">10 (1.8%)</text>
</g>
<!-- N14&#45;&gt;N19 -->
<g id="edge18" class="edge"><title>N14&#45;&gt;N19</title>
<path fill="none" stroke="black" d="M747,-758.28C747,-740.275 747,-714.982 747,-695.428"/>
<polygon fill="black" stroke="black" points="750.5,-695.238 747,-685.238 743.5,-695.238 750.5,-695.238"/>
<text text-anchor="middle" x="754" y="-718.3" font-family="Times,serif" font-size="14.00">10</text>
</g>
<!-- N17 -->
<g id="node18" class="node"><title>N17</title>
<polygon fill="none" stroke="black" points="1099,-799.5 1031,-799.5 1031,-764.5 1099,-764.5 1099,-799.5"/>
<text text-anchor="middle" x="1065" y="-789.1" font-family="Times,serif" font-size="8.00">bytes.IndexByte</text>
<text text-anchor="end" x="1091" y="-780.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="1091" y="-771.1" font-family="Times,serif" font-size="8.00">of 12 (2.2%)</text>
</g>
<!-- N15&#45;&gt;N17 -->
<g id="edge29" class="edge"><title>N15&#45;&gt;N17</title>
<path fill="none" stroke="black" d="M1062.7,-858.271C1063.14,-843.893 1063.71,-825.23 1064.17,-810.086"/>
<polygon fill="black" stroke="black" points="1067.68,-809.894 1064.49,-799.791 1060.68,-809.679 1067.68,-809.894"/>
<text text-anchor="middle" x="1070" y="-828.3" font-family="Times,serif" font-size="14.00">12</text>
</g>
<!-- N16 -->
<g id="node17" class="node"><title>N16</title>
<polygon fill="none" stroke="black" points="940.25,-503.5 829.75,-503.5 829.75,-450.5 940.25,-450.5 940.25,-503.5"/>
<text text-anchor="middle" x="885" y="-488.94" font-family="Times,serif" font-size="13.20">runtime.mallocgc</text>
<text text-anchor="end" x="932.5" y="-473.94" font-family="Times,serif" font-size="13.20">6 (1.1%)</text>
<text text-anchor="end" x="932.5" y="-458.94" font-family="Times,serif" font-size="13.20">of 14 (2.5%)</text>
</g>
<!-- N21 -->
<g id="node22" class="node"><title>N21</title>
<polygon fill="none" stroke="black" points="931.25,-397.5 838.75,-397.5 838.75,-362.5 931.25,-362.5 931.25,-397.5"/>
<text text-anchor="middle" x="885" y="-387.1" font-family="Times,serif" font-size="8.00">runtime.MCache_Refill</text>
<text text-anchor="end" x="923.5" y="-378.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="923.5" y="-369.1" font-family="Times,serif" font-size="8.00">of 8 (1.4%)</text>
</g>
<!-- N16&#45;&gt;N21 -->
<g id="edge4" class="edge"><title>N16&#45;&gt;N21</title>
<path fill="none" stroke="black" d="M885,-450.041C885,-436.993 885,-421.195 885,-408.005"/>
<polygon fill="black" stroke="black" points="888.5,-407.656 885,-397.656 881.5,-407.656 888.5,-407.656"/>
<text text-anchor="middle" x="888.5" y="-420.3" font-family="Times,serif" font-size="14.00">8</text>
</g>
<!-- N18 -->
<g id="node19" class="node"><title>N18</title>
<polygon fill="none" stroke="black" points="1144.5,-686 987.5,-686 987.5,-644 1144.5,-644 1144.5,-686"/>
<text text-anchor="middle" x="1066" y="-669.76" font-family="Times,serif" font-size="15.30">runtime.indexbytebody</text>
<text text-anchor="end" x="1137" y="-652.76" font-family="Times,serif" font-size="15.30">12 (2.2%)</text>
</g>
<!-- N17&#45;&gt;N18 -->
<g id="edge11" class="edge"><title>N17&#45;&gt;N18</title>
<path fill="none" stroke="black" d="M1065.15,-764.031C1065.3,-746.205 1065.55,-717.805 1065.74,-696.151"/>
<polygon fill="black" stroke="black" points="1069.24,-696.168 1065.82,-686.138 1062.24,-696.108 1069.24,-696.168"/>
<text text-anchor="middle" x="1072" y="-718.3" font-family="Times,serif" font-size="14.00">12</text>
</g>
<!-- N20 -->
<g id="node21" class="node"><title>N20</title>
<polygon fill="none" stroke="black" points="933.25,-221.5 836.75,-221.5 836.75,-174.5 933.25,-174.5 933.25,-221.5"/>
<text text-anchor="middle" x="885" y="-207.74" font-family="Times,serif" font-size="12.20">MCentral_Grow</text>
<text text-anchor="end" x="925.5" y="-194.74" font-family="Times,serif" font-size="12.20">4 (0.7%)</text>
<text text-anchor="end" x="925.5" y="-181.74" font-family="Times,serif" font-size="12.20">of 8 (1.4%)</text>
</g>
<!-- N29 -->
<g id="node30" class="node"><title>N29</title>
<polygon fill="none" stroke="black" points="929.25,-121.5 840.75,-121.5 840.75,-86.5 929.25,-86.5 929.25,-121.5"/>
<text text-anchor="middle" x="885" y="-111.1" font-family="Times,serif" font-size="8.00">runtime.MHeap_Alloc</text>
<text text-anchor="end" x="921.5" y="-102.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="921.5" y="-93.1" font-family="Times,serif" font-size="8.00">of 4 (0.7%)</text>
</g>
<!-- N20&#45;&gt;N29 -->
<g id="edge7" class="edge"><title>N20&#45;&gt;N29</title>
<path fill="none" stroke="black" d="M885,-174.258C885,-161.411 885,-145.278 885,-131.816"/>
<polygon fill="black" stroke="black" points="888.5,-131.705 885,-121.706 881.5,-131.706 888.5,-131.705"/>
<text text-anchor="middle" x="888.5" y="-144.3" font-family="Times,serif" font-size="14.00">4</text>
</g>
<!-- N22 -->
<g id="node23" class="node"><title>N22</title>
<polygon fill="none" stroke="black" points="942,-309.5 828,-309.5 828,-274.5 942,-274.5 942,-309.5"/>
<text text-anchor="middle" x="885" y="-299.1" font-family="Times,serif" font-size="8.00">runtime.MCentral_CacheSpan</text>
<text text-anchor="end" x="934" y="-290.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="934" y="-281.1" font-family="Times,serif" font-size="8.00">of 8 (1.4%)</text>
</g>
<!-- N21&#45;&gt;N22 -->
<g id="edge8" class="edge"><title>N21&#45;&gt;N22</title>
<path fill="none" stroke="black" d="M885,-362.418C885,-350.283 885,-333.564 885,-319.575"/>
<polygon fill="black" stroke="black" points="888.5,-319.531 885,-309.531 881.5,-319.531 888.5,-319.531"/>
<text text-anchor="middle" x="888.5" y="-332.3" font-family="Times,serif" font-size="14.00">8</text>
</g>
<!-- N22&#45;&gt;N20 -->
<g id="edge27" class="edge"><title>N22&#45;&gt;N20</title>
<path fill="none" stroke="black" d="M885,-274.126C885,-262.271 885,-246.032 885,-231.684"/>
<polygon fill="black" stroke="black" points="888.5,-231.66 885,-221.66 881.5,-231.66 888.5,-231.66"/>
<text text-anchor="middle" x="888.5" y="-244.3" font-family="Times,serif" font-size="14.00">8</text>
</g>
<!-- N25 -->
<g id="node26" class="node"><title>N25</title>
<polygon fill="none" stroke="black" points="890.25,-739.5 835.75,-739.5 835.75,-704.5 890.25,-704.5 890.25,-739.5"/>
<text text-anchor="middle" x="863" y="-729.1" font-family="Times,serif" font-size="8.00">gostringsize</text>
<text text-anchor="end" x="882.5" y="-720.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="882.5" y="-711.1" font-family="Times,serif" font-size="8.00">of 7 (1.3%)</text>
</g>
<!-- N23&#45;&gt;N25 -->
<g id="edge13" class="edge"><title>N23&#45;&gt;N25</title>
<path fill="none" stroke="black" d="M863,-864.39C863,-837.198 863,-782.93 863,-750.104"/>
<polygon fill="black" stroke="black" points="866.5,-749.863 863,-739.863 859.5,-749.863 866.5,-749.863"/>
<text text-anchor="middle" x="866.5" y="-828.3" font-family="Times,serif" font-size="14.00">7</text>
</g>
<!-- N24 -->
<g id="node25" class="node"><title>N24</title>
<polygon fill="none" stroke="black" points="948,-591.5 894,-591.5 894,-556.5 948,-556.5 948,-591.5"/>
<text text-anchor="middle" x="921" y="-581.1" font-family="Times,serif" font-size="8.00">cnew</text>
<text text-anchor="end" x="940" y="-572.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="940" y="-563.1" font-family="Times,serif" font-size="8.00">of 7 (1.3%)</text>
</g>
<!-- N24&#45;&gt;N16 -->
<g id="edge31" class="edge"><title>N24&#45;&gt;N16</title>
<path fill="none" stroke="black" d="M914.729,-556.452C910.201,-544.504 903.92,-527.927 898.296,-513.088"/>
<polygon fill="black" stroke="black" points="901.538,-511.764 894.721,-503.654 894.992,-514.245 901.538,-511.764"/>
<text text-anchor="middle" x="910.5" y="-526.3" font-family="Times,serif" font-size="14.00">7</text>
</g>
<!-- N25&#45;&gt;N16 -->
<g id="edge21" class="edge"><title>N25&#45;&gt;N16</title>
<path fill="none" stroke="black" d="M864.512,-704.298C868.052,-665.193 876.944,-566.985 881.767,-513.709"/>
<polygon fill="black" stroke="black" points="885.259,-513.959 882.675,-503.684 878.287,-513.327 885.259,-513.959"/>
<text text-anchor="middle" x="875.5" y="-614.3" font-family="Times,serif" font-size="14.00">7</text>
</g>
<!-- N26 -->
<g id="node27" class="node"><title>N26</title>
<polygon fill="none" stroke="black" points="971,-799.5 917,-799.5 917,-764.5 971,-764.5 971,-799.5"/>
<text text-anchor="middle" x="944" y="-789.1" font-family="Times,serif" font-size="8.00">makeslice1</text>
<text text-anchor="end" x="963" y="-780.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="963" y="-771.1" font-family="Times,serif" font-size="8.00">of 7 (1.3%)</text>
</g>
<!-- N27 -->
<g id="node28" class="node"><title>N27</title>
<polygon fill="none" stroke="black" points="969,-682.5 891,-682.5 891,-647.5 969,-647.5 969,-682.5"/>
<text text-anchor="middle" x="930" y="-672.1" font-family="Times,serif" font-size="8.00">runtime.cnewarray</text>
<text text-anchor="end" x="961" y="-663.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="961" y="-654.1" font-family="Times,serif" font-size="8.00">of 7 (1.3%)</text>
</g>
<!-- N26&#45;&gt;N27 -->
<g id="edge1" class="edge"><title>N26&#45;&gt;N27</title>
<path fill="none" stroke="black" d="M941.934,-764.031C939.646,-745.239 935.928,-714.694 933.249,-692.691"/>
<polygon fill="black" stroke="black" points="936.712,-692.168 932.029,-682.664 929.763,-693.014 936.712,-692.168"/>
<text text-anchor="middle" x="941.5" y="-718.3" font-family="Times,serif" font-size="14.00">7</text>
</g>
<!-- N27&#45;&gt;N24 -->
<g id="edge24" class="edge"><title>N27&#45;&gt;N24</title>
<path fill="none" stroke="black" d="M928.307,-647.26C927.012,-634.456 925.201,-616.547 923.706,-601.761"/>
<polygon fill="black" stroke="black" points="927.174,-601.262 922.685,-591.665 920.209,-601.967 927.174,-601.262"/>
<text text-anchor="middle" x="929.5" y="-614.3" font-family="Times,serif" font-size="14.00">7</text>
</g>
<!-- N28&#45;&gt;N26 -->
<g id="edge20" class="edge"><title>N28&#45;&gt;N26</title>
<path fill="none" stroke="black" d="M963.098,-864.376C959.596,-849.452 954.391,-827.276 950.3,-809.842"/>
<polygon fill="black" stroke="black" points="953.613,-808.642 947.921,-799.706 946.798,-810.241 953.613,-808.642"/>
<text text-anchor="middle" x="959.5" y="-828.3" font-family="Times,serif" font-size="14.00">7</text>
</g>
<!-- N31 -->
<g id="node32" class="node"><title>N31</title>
<polygon fill="none" stroke="black" points="930.25,-34 839.75,-34 839.75,-0 930.25,-0 930.25,-34"/>
<text text-anchor="middle" x="885" y="-20.64" font-family="Times,serif" font-size="11.70">runtime.memclr</text>
<text text-anchor="end" x="922.5" y="-7.64" font-family="Times,serif" font-size="11.70">3 (0.5%)</text>
</g>
<!-- N29&#45;&gt;N31 -->
<g id="edge9" class="edge"><title>N29&#45;&gt;N31</title>
<path fill="none" stroke="black" d="M885,-86.2067C885,-74.2957 885,-58.0696 885,-44.4319"/>
<polygon fill="black" stroke="black" points="888.5,-44.1816 885,-34.1817 881.5,-44.1817 888.5,-44.1816"/>
<text text-anchor="middle" x="888.5" y="-56.3" font-family="Times,serif" font-size="14.00">3</text>
</g>
<!-- N30 -->
<g id="node31" class="node"><title>N30</title>
<polygon fill="none" stroke="black" points="555,-1660.5 501,-1660.5 501,-1625.5 555,-1625.5 555,-1660.5"/>
<text text-anchor="middle" x="528" y="-1650.1" font-family="Times,serif" font-size="8.00">System</text>
<text text-anchor="end" x="547" y="-1641.1" font-family="Times,serif" font-size="8.00">0 (0.0%)</text>
<text text-anchor="end" x="547" y="-1632.1" font-family="Times,serif" font-size="8.00">of 3 (0.5%)</text>
</g>
</g>
</g></svg>
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"math"
"os"
"runtime"
"runtime/pprof"
"strconv"
)
type LabelWithFeatures struct {
Label []byte
Features []float64
}
func NewLabelWithFeatures(parsedLine [][]byte) LabelWithFeatures {
label := parsedLine[0]
features := make([]float64, len(parsedLine)-1)
for i, feature := range parsedLine {
// skip label
if i == 0 {
continue
}
features[i-1] = byteSliceTofloat64(feature)
}
return LabelWithFeatures{label, features}
}
var newline = []byte("\n")
var comma = []byte(",")
func byteSliceTofloat64(b []byte) float64 {
x, _ := strconv.ParseFloat(string(b), 32)
return float64(x)
}
func parseCSVFile(filePath string) []LabelWithFeatures {
fileContent, _ := ioutil.ReadFile(filePath)
lines := bytes.Split(fileContent, newline)
numRows := len(lines)
labelsWithFeatures := make([]LabelWithFeatures, numRows-2)
for i, line := range lines {
// skip headers
if i == 0 || i == numRows-1 {
continue
}
labelsWithFeatures[i-1] = NewLabelWithFeatures(bytes.Split(line, comma))
}
return labelsWithFeatures
}
func squareDistanceWithBailout(features1, features2 []float64, bailout float64) (d float64) {
for i := 0; i < len(features1); i++ {
x := features1[i] - features2[i]
d += x * x
if d > bailout {
break
}
}
return
}
var trainingSample = parseCSVFile("many_features_training.csv")
func classify(features []float64) (label []byte) {
label = trainingSample[0].Label
d := squareDistanceWithBailout(features, trainingSample[0].Features, math.MaxFloat32)
for _, row := range trainingSample {
dNew := squareDistanceWithBailout(features, row.Features, d)
if dNew < d {
label = row.Label
d = dNew
}
}
return
}
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
runtime.GOMAXPROCS(runtime.NumCPU())
println("parsing test")
validationSample := parseCSVFile("many_features_test.csv")
println("parsed test")
var totalCorrect float64 = 0
successChannel := make(chan float64, 10000) //len(validationSample))
for i := 0; i < 10000; i++ {
test := validationSample[i]
go func(t LabelWithFeatures, j int) {
if j%5 == 0 {
println("classifying", j)
}
if string(t.Label) == string(classify(t.Features)) {
successChannel <- 1
} else {
successChannel <- 0
}
if j%5 == 0 {
println("classified", j)
}
}(test, i)
}
for i := 0; i < 10000; i++ { //len(validationSample); i++ {
totalCorrect += <-successChannel
}
fmt.Println(float64(totalCorrect)) // / float64(len(validationSample)))
}
// Runs in about 10s on the beefy machine
// Has some reasonable number of correct, like 98k
import numpy
from sklearn.neighbors import KNeighborsClassifier
f = open("many_features_test.csv")
f.readline() # ignore headers
test = numpy.loadtxt(f, delimiter=',')
ff = open("many_features_training.csv")
ff.readline() # ignore headers
training = numpy.loadtxt(ff, delimiter=',')
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(training[:, [1,128]], numpy.ravel(training[:, [0]]))
numpy.sum(knn.predict(test[:, [1,128]]) == numpy.ravel(test[:, [0]]))
# Lightning fast, but...
# Something shitty, like 534 (out of ~43000)
knn = KNeighborsClassifier(n_neighbors=1, algorithm='brute')
knn.fit(training[:, [1,128]], numpy.ravel(training[:, [0]]))
numpy.sum(knn.predict(test[:, [1,128]]) == numpy.ravel(test[:, [0]]))
# Takes a while, and then...
# Something shittier, like MemoryError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment