double[] CombinedOldAndNewTargets() { double[] loadedTargets = ValfriStatic.Targets; if (ValfriStatic.AddedPoints == null) { return(loadedTargets); } double[][] addedTargets = ListToMatrix(ValfriStatic.AddedPoints); double[] combinedData = new double[loadedTargets.Length + addedTargets.Length]; int i = 0; foreach (var point in loadedTargets) { combinedData[i++] = point; } foreach (var point in addedTargets) { combinedData[i++] = Knn.Classify(point, ValfriStatic.MatrixOfPoints, Dataset.NumberOfFeatures, Dataset.K); } return(combinedData); }
private IEnumerable <Bitmap> DoRecognize(Bitmap b) { List <Bitmap> Result = new List <Bitmap>(); var NotClassifiedFeatures = FeatureExtractor.ExtractFeatures(b, null); List <SURFFeature> FeatureRemain = new List <SURFFeature>(); Parallel.ForEach(NotClassifiedFeatures, (NotClassifiedFeature) => { var KnnResult = knn.Classify(k, NotClassifiedFeature); if (KnnResult.Confidence >= ConfidenceThreshold) { lock (FeatureRemain) FeatureRemain.Add(NotClassifiedFeature); } }); var GroupedFeatureRemains = FeatureRemain.GroupBy(x => x.Tag); Bitmap FinalResult = (Bitmap)b.Clone(); //Draw Features FeaturesMarker featuresMarker = new FeaturesMarker(FeatureRemain.Select(x => x.Original)); Bitmap MarkedBitmap = featuresMarker.Apply(b); //Draw bounding box foreach (var GroupedFeatureRemain in GroupedFeatureRemains) { string Tag = GroupedFeatureRemain.Key; Point2DGrouper point2DGrouper = new Point2DGrouper(MaximumPointGroupDistance) { MinimumGroupCount = MinimumPointGroupCount }; var BoundingBoxes = point2DGrouper .GetGrouped2DPoints(GroupedFeatureRemain.Select(f => f.GetLocation())) .GroupBy(p => p.GroupID) .Select(group => group.GetBoundingBox(MinimumPointGroupArea)) .Where(bbox => bbox != null); var ObjectBox = BoundingBoxes.GetBoundingBox(); Brush brush = SelectBrush(Tag); if (BoundingBoxes != null) { MarkedBitmap.DrawRect(BoundingBoxes.Select(boxes => boxes.ToRectangle()), brush, 3, "Bbox:" + Tag); } if (ObjectBox != null) { FinalResult.DrawRect(ObjectBox.ToRectangle(), brush, 3, "Object:" + Tag); } } FinalResult.Tag = "Final Result"; MarkedBitmap.Tag = "Class Bounding Boxes"; Result.Add(FinalResult); Result.Add(MarkedBitmap); return(Result); }
private void Classify_btn_Click(object sender, EventArgs e) { using (Bitmap ResizedInput = new Bitmap(InputPictureBox.Image, 28, 28)) { RawImage28x28 NewInput = new RawImage28x28(ResizedInput, null); NewInput.Print(); Knn?.Classify(Convert.ToInt32(Knn_KValue_Textbox.Text), NewInput); Kmeans?.Classify(NewInput); } }
List <GameObject> CreatePies(List <List <double> > matrix, bool mark = false) { List <GameObject> pies = new List <GameObject>(); for (int i = 0; i < matrix.Count; i++) { GameObject pie = CreatePie(matrix[i].ToArray(), Knn.Classify(matrix[i].ToArray(), ValfriStatic.MatrixOfPoints, Dataset.NumberOfFeatures, Dataset.K), mark); pies.Add(pie); } return(pies); }
static void Main(string[] args) { var start = new APoint { X = 0, Y = 0 }; var target = new APoint { X = 5, Y = 4 }; //var DFSmatrix = Common.GetRandomMatrix(7, 7); //var dfsMatrix = Algorithms.DFS(DFSmatrix, start, target); //Console.WriteLine("DFS path: "); //Common.PrettyPrint(dfsMatrix, 7, 7); //Console.WriteLine("---------------------------"); //var BFSmatrix = Common.GetRandomMatrix(7, 7); //var bfsMatrix = Algorithms.BFS(BFSmatrix, start, target); //Console.WriteLine("BFS path: "); //Common.PrettyPrint(bfsMatrix, 7, 7); //var idsMatrix = Common.GetRandomMatrix(7, 7); //IDS.Matrix = idsMatrix; //IDS.Run(start, target); //Console.WriteLine("IDS path: "); //Common.PrettyPrint(idsMatrix, 7, 7); //var aStarMatrix = Common.GetRandomMatrix(7, 7); //InformedAlgs.Matrix = aStarMatrix; //InformedAlgs.AStar(start, target); //Console.WriteLine("A-Star path: "); //Console.WriteLine(InformedAlgs.PathLength); //Common.PrettyPrint(aStarMatrix, 7, 7); //int n = 1000; //QueensProblem queensProblem = new QueensProblem(n); //queensProblem.Solve(); //queensProblem.GetSolution(n); //var knapsack = new Knapsack(); //knapsack.InitializeItems(); //Generator generator = new Generator(knapsack); //generator.Solve(); //Console.Read(); var knn = new Knn(); knn.InitializeSets(); int k = 12; knn.Classify(k); Console.WriteLine("Accuracy: " + knn.predictedAnswers * 100 / 20 + "%"); }
public static void KnnMain() { Console.WriteLine("Using demo setup?[y/n]"); string str = Console.ReadLine(); if (str.ToLower().Contains("y") == false) { Console.WriteLine("Input node maximum value:(Default:10000)"); NodesMaxValueSet = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Input node count:(Default:150)"); NodesSet = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Input groups count for random points to divid into:(Default:3, Min:1)"); DataGroupCount = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Input fluctuation ratio:(Default:0.5, Min:0.1)"); FluctuationRatio = Convert.ToSingle(Console.ReadLine()); Console.WriteLine("Input new node count:(Default:10)"); NewNodesCount = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Input k set:(Default:7)"); k = Convert.ToInt32(Console.ReadLine()); } //Gen grouped random nodes Random2DPoints.OnGeneratePointGroups += Random2DPoints_OnGeneratePointGroups; var ClassifiedPoints = Random2DPoints.GenerateRandomPointsGroup(NodesMaxValueSet, NodesSet, DataGroupCount, FluctuationRatio); var Dataset = new VectorCollection <Point2D>(ClassifiedPoints); Dataset.Print(); var knnTrainer = new KnnTrainer <Point2D>(Dataset); Knn <Point2D> knn = new Knn <Point2D>(knnTrainer.Train()); knn.OnClassify += Knn_OnClassify; while (Retry) { var NewNodes = Random2DPoints.GenerateRandomPoints(NodesMaxValueSet, NewNodesCount); foreach (var NewNode in NewNodes) { knn.Classify(k, NewNode); } InputCommand(); } }
public static void NewPrediction() { if (usedFeatures.Count > 0) { NewDataPoints[0][usedFeatures[0]] = newPred[0]; NewDataPoints[0][usedFeatures[1]] = newPred[1]; NewDataPoints[0][usedFeatures[2]] = newPred[2]; } else { NewDataPoints[0][NewDataPoints[0].Keys.ElementAt(1)] = newPred[0]; NewDataPoints[0][NewDataPoints[0].Keys.ElementAt(2)] = newPred[1]; NewDataPoints[0][NewDataPoints[0].Keys.ElementAt(3)] = newPred[2]; } var obj = CreateNewObject(NewDataPoints[0].Keys.Count() - 2, NewDataPoints[0]); var classified = Knn.Classify(obj, DictToMatrix.DictionaryListToMatrix(ListOfPoints), GetNumberOfTargets(GetDataSetTargets()), K); if (classified is - 1) { return; }
public void Save() { List <double> point = scrollScript.GetInputs(); Debug.Log(point.Count); GameObject pieInitiator = GameObject.Find("DataInitiator"); var pieInitiatorScript = (PieController)pieInitiator.GetComponent(typeof(PieController)); int target = Knn.Classify(point.ToArray(), ValfriStatic.MatrixOfPoints, Dataset.NumberOfFeatures, Dataset.K); var canvas = GameObject.Find("Canvas"); var newPie = pieInitiatorScript.CreatePie(point.ToArray(), target); var KPCA = KernalPca.Transform(point.ToArray()); Display.Pie(newPie, canvas); Display.SetPosition(newPie, (float)KPCA[0], (float)KPCA[1], 0, true); Delete(); }