Last active
April 23, 2018 06:19
-
-
Save cmdr2/26bf314cfd67430744e7a6025d026124 to your computer and use it in GitHub Desktop.
Simple classifier (no libraries) to label RGB colors
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
package main | |
import ( | |
"fmt" | |
"math" | |
"net/http" | |
"strconv" | |
"sort" | |
) | |
type OptFunc func([]float64)(float64, []float64) | |
var training [][]float64 | |
var classes []float64 | |
var thetas [][]float64 | |
func getTrainingData() [][]float64 { | |
return [][]float64 { | |
{0.9372549019607843, 0.027450980392156862, 0.027450980392156862, 1}, // Red | |
{0.6549019607843137, 0, 0, 1}, // Red | |
{0.9176470588235294, 0.3686274509803922, 0.3686274509803922, 1}, // Red | |
{0.6549019607843137, 0.12549019607843137, 0.2, 1}, // Red | |
{0.3803921568627451, 0.07450980392156863, 0.11764705882352941, 1}, // Red | |
{0.9019607843137255, 0.3058823529411765, 0.39215686274509803, 1}, // Red | |
{0.5019607843137255, 0.011764705882352941, 0.12549019607843137, 1}, // Red | |
{0.9372549019607843, 0.9529411764705882, 0.0392156862745098, 2}, // Yellow | |
{0.9921568627450981, 1, 0.6, 2}, // Yellow | |
{1, 0.9764705882352941, 0.27450980392156865, 2}, // Yellow | |
{1, 0.9921568627450981, 0.7725490196078432, 2}, // Yellow | |
{0.8, 0.7215686274509804, 0.1803921568627451, 2}, // Yellow | |
{1, 0.8784313725490196, 0.03137254901960784, 2}, // Yellow | |
{0.9176470588235294, 0.8235294117647058, 0.16470588235294117, 2}, // Yellow | |
{0.24705882352941178, 0.9176470588235294, 0.16470588235294117, 3}, // Green | |
{0.10980392156862745, 1, 0, 3}, // Green | |
{0.6431372549019608, 1, 0.6, 3}, // Green | |
{0.0784313725490196, 0.7098039215686275, 0, 3}, // Green | |
{0.11764705882352941, 0.4823529411764706, 0.07450980392156863, 3}, // Green | |
{0.24705882352941178, 0.5098039215686274, 0.21568627450980393, 3}, // Green | |
{0.43137254901960786, 1, 0, 3}, // Green | |
{0.792156862745098, 1, 0.6352941176470588, 3}, // Green | |
{0.48627450980392156, 0.8549019607843137, 0.21176470588235294, 3}, // Green | |
{0.5725490196078431, 0.807843137254902, 0, 3}, // Green | |
{0.596078431372549, 0.7725490196078432, 0.1568627450980392, 3}, // Green | |
{0.3764705882352941, 0.5176470588235295, 0.03529411764705882, 3}, // Green | |
{0.03529411764705882, 0.3411764705882353, 0.5176470588235295, 4}, // Blue | |
{0.49019607843137253, 0.8156862745098039, 1, 4}, // Blue | |
{0.00784313725490196, 0.6431372549019608, 1, 4}, // Blue | |
{0, 0.27450980392156865, 0.42745098039215684, 4}, // Blue | |
{0.19215686274509805, 0.4, 0.5176470588235295, 4}, // Blue | |
{0.23529411764705882, 0.6313725490196078, 0.8549019607843137, 4}, // Blue | |
{0.10196078431372549, 0.3764705882352941, 0.5333333333333333, 4}, // Blue | |
{0, 0.3058823529411765, 1, 4}, // Blue | |
{0.5137254901960784, 0.6627450980392157, 1, 4}, // Blue | |
{0.18823529411764706, 0.2901960784313726, 0.5254901960784314, 4}, // Blue | |
{0.00784313725490196, 0.1568627450980392, 0.5098039215686274, 4}, // Blue | |
{0.36470588235294116, 0.25098039215686274, 1, 4}, // Blue | |
{0.15294117647058825, 0, 1, 4}, // Blue | |
{0.12941176470588237, 0.043137254901960784, 0.6039215686274509, 4}, // Blue | |
{0, 0.9058823529411765, 1, 4}, // Blue | |
{0.5647058823529412, 0.9607843137254902, 1, 4}, // Blue | |
{0.35294117647058826, 0.7450980392156863, 0.7803921568627451, 4}, // Blue | |
{0.6549019607843137, 0.6352941176470588, 0.12549019607843137, 2}, // Yellow | |
{0.8392156862745098, 0.8156862745098039, 0.20784313725490197, 2}, // Yellow | |
{0.7490196078431373, 0.7254901960784313, 0.08235294117647059, 2}, // Yellow | |
{0.7803921568627451, 0.792156862745098, 0.027450980392156862, 2}, // Yellow | |
{0.8941176470588236, 0.9019607843137255, 0.2784313725490196, 2}, // Yellow | |
{0.6705882352941176, 0.6784313725490196, 0.01568627450980392, 2}, // Yellow | |
{0.8470588235294118, 0.8235294117647058, 0.03137254901960784, 2}, // Yellow | |
{0.7254901960784313, 0.807843137254902, 0.03137254901960784, 3}, // Green | |
{0.796078431372549, 0.8705882352941177, 0.1607843137254902, 3}, // Green | |
{0.6039215686274509, 0.6705882352941176, 0.03529411764705882, 3}, // Green | |
{0.7058823529411765, 0.7490196078431373, 0.0196078431372549, 3}, // Green | |
{0.6392156862745098, 0.6705882352941176, 0.1568627450980392, 3}, // Green | |
{0.8117647058823529, 0.8627450980392157, 0.023529411764705882, 3}, // Green | |
{0.7058823529411765, 0.7490196078431373, 0.027450980392156862, 3}, // Green | |
{0.6, 0.6352941176470588, 0, 3}, // Green | |
{0.22745098039215686, 0.7333333333333333, 0.5215686274509804, 3}, // Green | |
{0.34901960784313724, 0.9764705882352941, 0.7137254901960784, 3}, // Green | |
{0.027450980392156862, 0.5647058823529412, 0.3411764705882353, 3}, // Green | |
{0.24705882352941178, 0.6862745098039216, 0.5019607843137255, 3}, // Green | |
{0.9921568627450981, 0.9882352941176471, 1, 5}, // White | |
{0.9019607843137255, 0.9019607843137255, 0.9019607843137255, 5}, // White | |
{1, 1, 1, 5}, // White | |
{0.9450980392156862, 0.9294117647058824, 0.9294117647058824, 5}, // White | |
{0.9294117647058824, 0.9294117647058824, 0.9450980392156862, 5}, // White | |
{0.9098039215686274, 0.9098039215686274, 0.9098039215686274, 5}, // White | |
{0.9686274509803922, 0.9450980392156862, 0.9450980392156862, 5}, // White | |
{0.9686274509803922, 0.9647058823529412, 0.9450980392156862, 5}, // White | |
{0.9490196078431372, 0.9686274509803922, 0.9450980392156862, 5}, // White | |
{0.9450980392156862, 0.9686274509803922, 0.9607843137254902, 5}, // White | |
{0.9450980392156862, 0.9450980392156862, 0.9450980392156862, 5}, // White | |
{0.9686274509803922, 0.9686274509803922, 0.9686274509803922, 5}, // White | |
{0.803921568627451, 0.9764705882352941, 0.9294117647058824, 4}, // Blue | |
{0.7019607843137254, 0.8862745098039215, 0.8352941176470589, 4}, // Blue | |
{0.7568627450980392, 1, 0.9333333333333333, 4}, // Blue | |
{0.7568627450980392, 1, 0.7764705882352941, 3}, // Green | |
{0.8352941176470589, 0.9607843137254902, 0.8470588235294118, 3}, // Green | |
{0.7137254901960784, 0.8392156862745098, 0.7254901960784313, 3}, // Green | |
{0.7137254901960784, 0.7529411764705882, 0.8392156862745098, 4}, // Blue | |
{0.7137254901960784, 0.7607843137254902, 0.8627450980392157, 4}, // Blue | |
{0.7176470588235294, 0.8627450980392157, 0.7137254901960784, 3}, // Green | |
{0.8627450980392157, 0.8509803921568627, 0.7137254901960784, 2}, // Yellow | |
{0.9764705882352941, 0.9607843137254902, 0.788235294117647, 2}, // Yellow | |
{0.9764705882352941, 0.9686274509803922, 0.8666666666666667, 2}, // Yellow | |
{0.9686274509803922, 0.9411764705882353, 0.8588235294117647, 2}, // Yellow | |
{0.9607843137254902, 0.9098039215686274, 0.7568627450980392, 2}, // Yellow | |
{0.9176470588235294, 0.8745098039215686, 0.7333333333333333, 2}, // Yellow | |
{0.8705882352941177, 1, 0.8823529411764706, 3}, // Green | |
{0.8274509803921568, 0.9254901960784314, 0.8352941176470589, 3}, // Green | |
{0.8, 1, 0.8156862745098039, 3}, // Green | |
{0.8509803921568627, 0.9686274509803922, 0.8588235294117647, 3}, // Green | |
{0.8666666666666667, 1, 0.8745098039215686, 3}, // Green | |
{0.8980392156862745, 0.9921568627450981, 0.9019607843137255, 3}, // Green | |
{0.8470588235294118, 0.9921568627450981, 0.8509803921568627, 3}, // Green | |
{0.24313725490196078, 0.24313725490196078, 0.24313725490196078, 6}, // Black | |
{0.15294117647058825, 0.1450980392156863, 0.1450980392156863, 6}, // Black | |
{0.07450980392156863, 0.06666666666666667, 0.06666666666666667, 6}, // Black | |
{0.01568627450980392, 0.01568627450980392, 0.01568627450980392, 6}, // Black | |
{0, 0, 0, 6}, // Black | |
{0.2196078431372549, 0.18823529411764706, 0.18823529411764706, 6}, // Black | |
{0.12156862745098039, 0.00784313725490196, 0.00784313725490196, 6}, // Black | |
{0.13725490196078433, 0.047058823529411764, 0.047058823529411764, 6}, // Black | |
{0.13725490196078433, 0.07450980392156863, 0.07450980392156863, 6}, // Black | |
{0.07450980392156863, 0.13725490196078433, 0.09019607843137255, 6}, // Black | |
{0.03529411764705882, 0.13725490196078433, 0.06274509803921569, 6}, // Black | |
{0.00392156862745098, 0.09803921568627451, 0.027450980392156862, 6}, // Black | |
{0.023529411764705882, 0.00392156862745098, 0.12156862745098039, 6}, // Black | |
{0.07058823529411765, 0.050980392156862744, 0.16862745098039217, 6}, // Black | |
{0.12549019607843137, 0.11372549019607843, 0.1843137254901961, 6}, // Black | |
{0.11372549019607843, 0.08627450980392157, 0.011764705882352941, 6}, // Black | |
{0.011764705882352941, 0.11372549019607843, 0.08627450980392157, 6}, // Black | |
{0.10196078431372549, 0.011764705882352941, 0.11372549019607843, 6}, // Black | |
{0.03529411764705882, 0.027450980392156862, 0.35294117647058826, 4}, // Blue | |
{0.10980392156862745, 0.10196078431372549, 0.403921568627451, 4}, // Blue | |
{0.35294117647058826, 0.027450980392156862, 0.027450980392156862, 1}, // Red | |
{0.37254901960784315, 0.07450980392156863, 0.07450980392156863, 1}, // Red | |
{0.3803921568627451, 0.1450980392156863, 0.1450980392156863, 1}, // Red | |
{0.08627450980392157, 0.2901960784313726, 0.10196078431372549, 3}, // Green | |
{0.027450980392156862, 0.2901960784313726, 0.047058823529411764, 3}, // Green | |
{0.00392156862745098, 0.2901960784313726, 0.027450980392156862, 3}, // Green | |
{0.19607843137254902, 0.2901960784313726, 0.20392156862745098, 3}, // Green | |
{1, 0.23921568627450981, 0.011764705882352941, 7}, // Orange | |
{0.9254901960784314, 0.4666666666666667, 0.32941176470588235, 7}, // Orange | |
{0.6627450980392157, 0.39215686274509803, 0.1607843137254902, 7}, // Orange | |
{0.9450980392156862, 0.7137254901960784, 0.5176470588235295, 7}, // Orange | |
{0.9921568627450981, 0.8627450980392157, 0.7529411764705882, 7}, // Orange | |
{0.7176470588235294, 0.5019607843137255, 0.3137254901960784, 7}, // Orange | |
{0.7176470588235294, 0.4980392156862745, 0, 7}, // Orange | |
{0.8627450980392157, 0.6431372549019608, 0.1411764705882353, 7}, // Orange | |
{0.984313725490196, 0.796078431372549, 0.37254901960784315, 7}, // Orange | |
{1, 0.6470588235294118, 0.21176470588235294, 7}, // Orange | |
{0.9607843137254902, 0.7647058823529411, 0.06274509803921569, 2}, // Yellow | |
{0.6196078431372549, 0.011764705882352941, 0.6, 8}, // Purple | |
{0.44313725490196076, 0.15294117647058825, 0.43529411764705883, 8}, // Purple | |
{0.3803921568627451, 0.00784313725490196, 0.3686274509803922, 8}, // Purple | |
{0.611764705882353, 0.3843137254901961, 0.6039215686274509, 8}, // Purple | |
{0.3333333333333333, 0.07058823529411765, 0.4196078431372549, 8}, // Purple | |
{0.48627450980392156, 0, 0.6470588235294118, 8}, // Purple | |
{0.803921568627451, 0.21568627450980393, 1, 8}, // Purple | |
{0.7490196078431373, 0, 1, 8}, // Purple | |
{0.9450980392156862, 0.7843137254901961, 1, 8}, // Purple | |
{0.9686274509803922, 0.8705882352941177, 1, 8}, // Purple | |
{0.8, 0.6862745098039216, 0.8392156862745098, 8}, // Purple | |
{0.596078431372549, 0.43529411764705883, 0.6549019607843137, 8}, // Purple | |
{0.35294117647058826, 0.19215686274509805, 0.4980392156862745, 8}, // Purple | |
{0.3058823529411765, 0.047058823529411764, 0.5411764705882353, 8}, // Purple | |
{0.5843137254901961, 0.11764705882352941, 0.7019607843137254, 8}, // Purple | |
{0.9254901960784314, 0.6274509803921569, 1, 9}, // Pink | |
{0.9686274509803922, 0.8352941176470589, 1, 9}, // Pink | |
{0.9019607843137255, 0.7294117647058823, 0.9450980392156862, 9}, // Pink | |
{0.8862745098039215, 0.6235294117647059, 0.9019607843137255, 9}, // Pink | |
{0.8745098039215686, 0.5058823529411764, 0.8941176470588236, 9}, // Pink | |
{0.8313725490196079, 0.36470588235294116, 0.8549019607843137, 9}, // Pink | |
{0.9058823529411765, 0.13333333333333333, 0.9450980392156862, 9}, // Pink | |
{0.9372549019607843, 0, 0.984313725490196, 9}, // Pink | |
{0.8823529411764706, 0.2901960784313726, 0.9098039215686274, 9}, // Pink | |
{0.9098039215686274, 0.2901960784313726, 0.7490196078431373, 9}, // Pink | |
{1, 0.054901960784313725, 0.7568627450980392, 9}, // Pink | |
{1, 0.8156862745098039, 0.9529411764705882, 9}, // Pink | |
{1, 0.4, 0.8470588235294118, 9}, // Pink | |
{0.7725490196078432, 0.16862745098039217, 0.6196078431372549, 9}, // Pink | |
{0.7098039215686275, 0.10196078431372549, 0.5568627450980392, 9}, // Pink | |
{0.807843137254902, 0.42745098039215684, 0.7098039215686275, 9}, // Pink | |
{0.7333333333333333, 0.45098039215686275, 0.6588235294117647, 9}, // Pink | |
{1, 0.5607843137254902, 0.7647058823529411, 9}, // Pink | |
{1, 0.2627450980392157, 0.6039215686274509, 9}, // Pink | |
{0.9686274509803922, 0.1450980392156863, 0.5254901960784314, 9}, // Pink | |
{0.8862745098039215, 0, 0.40784313725490196, 9}, // Pink | |
{0.7411764705882353, 0.3137254901960784, 0.5098039215686274, 9}, // Pink | |
{0.5647058823529412, 0.13725490196078433, 0.3333333333333333, 1}, // Red | |
{0.4823529411764706, 0.0196078431372549, 0.23137254901960785, 1}, // Red | |
{0.596078431372549, 0.3333333333333333, 0.4549019607843137, 9}, // Pink | |
{1, 0.7803921568627451, 0.8823529411764706, 9}, // Pink | |
{0.8470588235294118, 0.7803921568627451, 0.8117647058823529, 10}, // Gray | |
{0.7647058823529411, 0.7647058823529411, 0.7647058823529411, 10}, // Gray | |
{0.36470588235294116, 0.36470588235294116, 0.36470588235294116, 10}, // Gray | |
{0.6784313725490196, 0.6196078431372549, 0.6196078431372549, 10}, // Gray | |
{0.807843137254902, 0.807843137254902, 0.807843137254902, 10}, // Gray | |
{0.3215686274509804, 0.3215686274509804, 0.3215686274509804, 10}, // Gray | |
{0.403921568627451, 0.36470588235294116, 0.36470588235294116, 10}, // Gray | |
{0.36470588235294116, 0.403921568627451, 0.3843137254901961, 10}, // Gray | |
{0.6078431372549019, 0.6705882352941176, 0.6392156862745098, 10}, // Gray | |
{0.8235294117647058, 0.8392156862745098, 0.8313725490196079, 10}, // Gray | |
{0.8274509803921568, 0.8235294117647058, 0.8392156862745098, 10}, // Gray | |
{0.30980392156862746, 0.30196078431372547, 0.33725490196078434, 10}, // Gray | |
{0.5019607843137255, 0.4980392156862745, 0.5254901960784314, 10}, // Gray | |
{0.5254901960784314, 0.4980392156862745, 0.5098039215686274, 10}, // Gray | |
{0.7725490196078432, 0.7372549019607844, 0.7529411764705882, 10}, // Gray | |
{0.8392156862745098, 0.8392156862745098, 0.8392156862745098, 10}, // Gray | |
{0.8549019607843137, 0.8470588235294118, 0.8156862745098039, 10}, // Gray | |
{0.6039215686274509, 0.596078431372549, 0.5529411764705883, 10}, // Gray | |
{0.33725490196078434, 0.33725490196078434, 0.3215686274509804, 10}, // Gray | |
{0.3215686274509804, 0.33725490196078434, 0.32941176470588235, 10}, // Gray | |
{0.30196078431372547, 0.3058823529411765, 0.3058823529411765, 10}, // Gray | |
{0.39215686274509803, 0.42745098039215684, 0.42745098039215684, 10}, // Gray | |
{0.5725490196078431, 0.3686274509803922, 0, 11}, // Brown | |
{0.6784313725490196, 0.4823529411764706, 0.13333333333333333, 11}, // Brown | |
{0.4823529411764706, 0.30980392156862746, 0, 11}, // Brown | |
{0.5254901960784314, 0.38823529411764707, 0.1450980392156863, 11}, // Brown | |
{0.5490196078431373, 0.4235294117647059, 0.2, 11}, // Brown | |
{0.7176470588235294, 0.5372549019607843, 0.2196078431372549, 11}, // Brown | |
{0.6941176470588235, 0.3686274509803922, 0.13725490196078433, 11}, // Brown | |
{0.6784313725490196, 0.2823529411764706, 0, 11}, // Brown | |
{0.37254901960784315, 0.16862745098039217, 0.023529411764705882, 11}, // Brown | |
{0.37254901960784315, 0.2235294117647059, 0.12156862745098039, 11}, // Brown | |
{0.5803921568627451, 0.403921568627451, 0.2823529411764706, 11}, // Brown | |
{0.7019607843137254, 0.43137254901960786, 0.24705882352941178, 11}, // Brown | |
{0.37254901960784315, 0.17254901960784313, 0.00392156862745098, 11}, // Brown | |
{0.6705882352941176, 0.3607843137254902, 0.10196078431372549, 11}, // Brown | |
{0.7490196078431373, 0.5333333333333333, 0.35294117647058826, 11}, // Brown | |
{0.807843137254902, 0.5411764705882353, 0.3215686274509804, 11}, // Brown | |
{0.6549019607843137, 0.4392156862745098, 0.2627450980392157, 11}, // Brown | |
{0.7647058823529411, 0.5450980392156862, 0.36470588235294116, 11}, // Brown | |
{0.5568627450980392, 0.396078431372549, 0.26666666666666666, 11}, // Brown | |
{0.6784313725490196, 0.5254901960784314, 0.4, 11}, // Brown | |
{0.5647058823529412, 0.43137254901960786, 0.3176470588235294, 11}, // Brown | |
{0.4588235294117647, 0.35294117647058826, 0.2627450980392157, 11}, // Brown | |
{0.7490196078431373, 0.611764705882353, 0.49411764705882355, 11}, // Brown | |
{0.7647058823529411, 0.6588235294117647, 0.5647058823529412, 11}, // Brown | |
{0.5725490196078431, 0.49019607843137253, 0.41568627450980394, 11}, // Brown | |
{0.403921568627451, 0.3333333333333333, 0.26666666666666666, 11}, // Brown | |
{0.8392156862745098, 0.49411764705882355, 0.19215686274509805, 7}, // Orange | |
{0.8470588235294118, 0.5372549019607843, 0.2627450980392157, 7}, // Orange | |
{0.596078431372549, 0.3803921568627451, 0.1843137254901961, 11}, // Brown | |
{0.8156862745098039, 0.38823529411764707, 0, 7}, // Orange | |
{0.8392156862745098, 0.4666666666666667, 0.12549019607843137, 7}, // Orange | |
{0.9529411764705882, 0.5647058823529412, 0.20784313725490197, 7}, // Orange | |
{1, 0.7647058823529411, 0.5490196078431373, 7}, // Orange | |
{1, 0.8549019607843137, 0.7215686274509804, 7}, // Orange | |
{0.7098039215686275, 0.5254901960784314, 0.35294117647058826, 11}, // Brown | |
{1, 0.7176470588235294, 0.4588235294117647, 7}, // Orange | |
{0.6352941176470588, 0.12156862745098039, 0.5058823529411764, 8}, // Purple | |
{0.6470588235294118, 0.2549019607843137, 0.5450980392156862, 8}, // Purple | |
{0.5254901960784314, 0.23529411764705882, 0.45098039215686275, 8}, // Purple | |
{0.42745098039215684, 0.11764705882352941, 0.34901960784313724, 8}, // Purple | |
{0.5098039215686274, 0.20784313725490197, 0.43137254901960786, 8}, // Purple | |
{0.6627450980392157, 0.3686274509803922, 0.5843137254901961, 8}, // Purple | |
{0.6862745098039216, 0.4745098039215686, 0.6, 9}, // Pink | |
{0.9254901960784314, 0.7098039215686275, 0.8392156862745098, 9}, // Pink | |
{0.7254901960784313, 0.5568627450980392, 0.6588235294117647, 9}, // Pink | |
{0.596078431372549, 0.4196078431372549, 0.5254901960784314, 9}, // Pink | |
{0.5333333333333333, 0.3137254901960784, 0.44313725490196076, 9}, // Pink | |
{0.8352941176470589, 0.32941176470588235, 0.03529411764705882, 11}, // Brown | |
{0.9294117647058824, 0.8, 0.5411764705882353, 11}, // Brown | |
{0.8823529411764706, 0.6705882352941176, 0.5137254901960784, 11}, // Brown | |
{0.8313725490196079, 0.29411764705882354, 0.07058823529411765, 11}, // Brown | |
{0.8352941176470589, 0.32941176470588235, 0.03529411764705882, 11}, // Brown | |
{0.9215686274509803, 0.25882352941176473, 0.07450980392156863, 7}, // Orange | |
{0.8784313725490196, 0.6901960784313725, 0.3686274509803922, 11}, // Brown | |
{0.5843137254901961, 0.4745098039215686, 0.6823529411764706, 8}, // Purple | |
{0.8509803921568627, 0.3254901960784314, 0.09803921568627451, 7}, // Orange | |
{0.6235294117647059, 0.27058823529411763, 0.43529411764705883, 9}, // Pink | |
{0.8705882352941177, 0.2823529411764706, 0.1843137254901961, 7}, // Orange | |
{0.8823529411764706, 0.7490196078431373, 0.47058823529411764, 11}, // Brown | |
{0.5803921568627451, 0.1843137254901961, 0.6352941176470588, 8}, // Purple | |
{0.6196078431372549, 0.13725490196078433, 0.7568627450980392, 8}, // Purple | |
{0.8745098039215686, 0.596078431372549, 0.9411764705882353, 8}, // Purple | |
{0.796078431372549, 0.44313725490196076, 0.8313725490196079, 8}, // Purple | |
{0.792156862745098, 0.2784313725490196, 0.8901960784313725, 8}, // Purple | |
{0.7294117647058823, 0.4627450980392157, 0.49019607843137253, 9}, // Pink | |
{0.7529411764705882, 0.34901960784313724, 0.3843137254901961, 9}, // Pink | |
{0.7725490196078432, 0.6901960784313725, 0.17254901960784313, 2}, // Yellow | |
{0.9686274509803922, 0.1607843137254902, 0.2235294117647059, 1}, // Red | |
{0.9294117647058824, 0.12156862745098039, 0.2980392156862745, 1}, // Red | |
{0.7450980392156863, 0.4588235294117647, 0.8980392156862745, 8}, // Purple | |
{0.6313725490196078, 0.7058823529411765, 0.7725490196078432, 10}, // Gray | |
{0.8313725490196079, 0.8431372549019608, 0.9058823529411765, 10}, // Gray | |
{0.03137254901960784, 0.5725490196078431, 0.4823529411764706, 3}, // Green | |
{0.3254901960784314, 0.09019607843137255, 0.13333333333333333, 11}, // Brown | |
{0.4392156862745098, 0.2235294117647059, 0.7647058823529411, 8}, // Purple | |
{0.5803921568627451, 0.09411764705882353, 0.4, 9}, // Pink | |
{0.592156862745098, 0.5568627450980392, 0.011764705882352941, 3}, // Green | |
{0.396078431372549, 0.4, 0.1568627450980392, 3}, // Green | |
{0.7176470588235294, 0.5411764705882353, 0.8117647058823529, 8}, // Purple | |
{0.6941176470588235, 0.22745098039215686, 0.7098039215686275, 8}, // Purple | |
{0.5568627450980392, 0.34901960784313724, 0.4196078431372549, 9}, // Pink | |
{0.6862745098039216, 0.2235294117647059, 0.1803921568627451, 1}, // Red | |
{0.12549019607843137, 0.2627450980392157, 0.12941176470588237, 3}, // Green | |
{0.7529411764705882, 0.6901960784313725, 0.6352941176470588, 11}, // Brown | |
{0.3176470588235294, 0.3215686274509804, 0.3411764705882353, 10}, // Gray | |
{0.12549019607843137, 0.2627450980392157, 0.12941176470588237, 3}, // Green | |
{0.8274509803921568, 0.00392156862745098, 1, 8}, // Purple | |
{0.5294117647058824, 0.4980392156862745, 0.6431372549019608, 8}, // Purple | |
{0.5490196078431373, 0.6, 0.47058823529411764, 3}, // Green | |
{0.40784313725490196, 0.3137254901960784, 0.45098039215686275, 8}, // Purple | |
{0.6352941176470588, 0.6352941176470588, 0.5843137254901961, 11}, // Brown | |
{0.7490196078431373, 0.8196078431372549, 0.8235294117647058, 10}, // Gray | |
{0.8431372549019608, 0.788235294117647, 0.6509803921568628, 11}, // Brown | |
{0.5843137254901961, 0.5137254901960784, 0.33725490196078434, 11}, // Brown | |
{0.45098039215686275, 0.21568627450980393, 0.28627450980392155, 1}, // Red | |
{0.7411764705882353, 0.4117647058823529, 0.4196078431372549, 1}, // Red | |
{0.7843137254901961, 0.8352941176470589, 0.7019607843137254, 3}, // Green | |
{0.5686274509803921, 0.3764705882352941, 0.36470588235294116, 11}, // Brown | |
{0.8431372549019608, 0.8588235294117647, 0.6823529411764706, 3}, // Green | |
{0.47058823529411764, 0.4235294117647059, 0.5411764705882353, 8}, // Purple | |
{0.7411764705882353, 0.5529411764705883, 0.5607843137254902, 11}, // Brown | |
{0.28627450980392155, 0.17647058823529413, 0.30196078431372547, 8}, // Purple | |
{0.5686274509803921, 0.37254901960784315, 0.4980392156862745, 9}, // Pink | |
{0.4588235294117647, 0.45098039215686275, 0.5176470588235295, 10}, // Gray | |
{0.5568627450980392, 0.6313725490196078, 0.596078431372549, 3}, // Green | |
{0.9019607843137255, 0.6823529411764706, 0.5725490196078431, 7}, // Orange | |
{0.6745098039215687, 0.38823529411764707, 0.4588235294117647, 9}, // Pink | |
{0.8235294117647058, 0.792156862745098, 0.6549019607843137, 11}, // Brown | |
{0.3176470588235294, 0.3058823529411765, 0.43137254901960786, 4}, // Blue | |
{0.6196078431372549, 0.6235294117647059, 0.5568627450980392, 11}, // Brown | |
{0.3058823529411765, 0.058823529411764705, 0.12549019607843137, 11}, // Brown | |
} | |
} | |
func maskTrainingClass(classToMask float64) { | |
training = getTrainingData() | |
for _, row := range training { | |
m := len(row) | |
if row[m - 1] == classToMask { | |
row[m - 1] = 1 | |
} else { | |
row[m - 1] = 0 | |
} | |
} | |
} | |
func getClasses(data [][]float64) []float64 { | |
set := map[float64]bool{} | |
var classes []float64 | |
for _, row := range data { | |
n := len(row) | |
set[row[n-1]] = true | |
} | |
for classLabel, _ := range set { | |
classes = append(classes, classLabel) | |
} | |
sort.Float64s(classes) | |
return classes | |
} | |
func predict(x, t []float64) float64 { | |
var v float64 | |
n := len(t) | |
for i := 0; i < n; i++ { | |
v += x[i] * t[i] | |
} | |
v = 1.0 / (1.0 + math.Exp(-v)) | |
return v | |
} | |
func fminunc(opt OptFunc, t []float64, iters int) []float64 { | |
var cost float64 | |
var gradient []float64 | |
alpha := 0.2 | |
for i := 0; i < iters; i++ { | |
cost, gradient = opt(t) | |
for j := 0; j < len(gradient); j++ { | |
t[j] = t[j] - alpha * gradient[j] | |
} | |
// fmt.Printf("%d) cost with (%v): %f\n", i, t, c) | |
} | |
fmt.Printf("%d iterations, alpha: %f. Cost with (%v): %f\n", iters, alpha, t, cost) | |
return t | |
} | |
// cost | |
// y = 1: cost = -log(pY) | |
// y = 0: cost = -log(1 - pY) | |
func costFunc(t []float64) (float64, []float64) { | |
ONE := []float64{1.0} | |
m := float64(len(training)) | |
var cost float64 | |
var predictedY float64 | |
var x []float64 | |
var y float64 | |
nFeatures := len(t)-1 | |
gradient := make([]float64, nFeatures+1) | |
for _, row := range training { | |
x = row[:nFeatures] | |
x = append(ONE, x...) | |
y = row[nFeatures] | |
predictedY = predict(x, t) | |
cost += y * math.Log(predictedY) + (1 - y) * math.Log(1 - predictedY) | |
for i := 0; i <= nFeatures; i++ { | |
gradient[i] += -(y - predictedY) * x[i] | |
} | |
} | |
for i := 0; i < len(gradient); i++ { | |
gradient[i] /= m | |
} | |
cost = -cost / m | |
return cost, gradient | |
} | |
func s(pts [][]float64) string { | |
out := "" | |
for _, p := range pts { | |
out += fmt.Sprintf("(%f, %f), ", p[0], p[1] * 10) | |
} | |
return out | |
} | |
func predictColorHandler(w http.ResponseWriter, r *http.Request) { | |
q := r.URL.Query() | |
fmt.Printf("q: %v\n", q) | |
rStr := q["r"][0] | |
gStr := q["g"][0] | |
bStr := q["b"][0] | |
red, _ := strconv.ParseFloat(rStr, 64) | |
green, _ := strconv.ParseFloat(gStr, 64) | |
blue, _ := strconv.ParseFloat(bStr, 64) | |
fmt.Printf("rgb: %f, %f, %f\n", red, green, blue) | |
x := []float64{red, green, blue} | |
classLabel, classScore := getPredictedClass(x) | |
fmt.Fprintf(w, "{\"classLabel\": %d, \"classScore\": %f}", classLabel, classScore) | |
} | |
func getPredictedClass(x []float64) (int, float64) { | |
// x := []float64{0, 1, 0} | |
var scores []float64 | |
for idx, _ := range classes { | |
t := thetas[idx] | |
y := predict(append([]float64{1.0}, x...), t) | |
scores = append(scores, y) | |
} | |
classSelected := -1.0 | |
classScore := -1000.0 | |
for idx, classLabel := range classes { | |
score := scores[idx] | |
if score > classScore { | |
classScore = score | |
classSelected = classLabel | |
} | |
} | |
fmt.Printf("Predicted class %f (%f) for x=%v. All scores: %v\n\n", classSelected, classScore, x, scores) | |
return int(classSelected), classScore | |
} | |
func cacheControlWrapper(h http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Cache-Control", "max-age=0") // no cache | |
h.ServeHTTP(w, r) | |
}) | |
} | |
func printModel(thetas [][]float64) { | |
s := "[" | |
m := len(thetas) | |
n := len(thetas[0]) | |
for i, t := range thetas { | |
s += "[" | |
for j, w := range t { | |
s += strconv.FormatFloat(w, 'f', -1, 64) //fmt.Sprintf("%f", w) | |
if j < n - 1 { | |
s += "," | |
} | |
} | |
s += "]" | |
if i < m - 1 { | |
s += "," | |
} | |
} | |
s += "]" | |
fmt.Printf("\nmodel = %s;\n\n", s) | |
} | |
func main() { | |
fmt.Println("e2m4: multi-class logistic regression (classification) for colors\n") | |
training = getTrainingData() | |
fmt.Printf("Training with %d samples\n\n", len(training)) | |
iters := 10000 | |
nFeatures := len(training[0]) - 1 | |
classes = getClasses(training) | |
for _, classLabel := range classes { | |
t := make([]float64, nFeatures + 1) | |
// train | |
maskTrainingClass(classLabel) | |
t = fminunc(costFunc, t, iters) | |
thetas = append(thetas, t) | |
} | |
printModel(thetas) | |
// start server | |
fmt.Printf("\nServer started at http://localhost:8080\n") | |
fmt.Printf("\nExample: http://localhost:8080/predictColor?r=0.343&g=0.454&b=0.3523\n\n") | |
var SITE_DIR = "html" | |
filesHandler := http.FileServer(http.Dir(SITE_DIR)) | |
http.Handle("/", cacheControlWrapper(filesHandler)) | |
http.HandleFunc("/predictColor", predictColorHandler) | |
http.ListenAndServe("127.0.0.1:8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment