Skip to content

Commit

Permalink
Refactor and add Cluster Model.
Browse files Browse the repository at this point in the history
  • Loading branch information
xin-pu committed Jun 14, 2022
1 parent 62e0fc0 commit 3b744b0
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 34 deletions.
32 changes: 8 additions & 24 deletions src/ML.Core.Test/ModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using ML.Core.Data.Loader;
using ML.Core.Metrics;
using ML.Core.Metrics.Categorical;
using ML.Core.Metrics.Regression;
using ML.Core.Models;
using ML.Core.Optimizers;
using ML.Core.Trainers;
Expand Down Expand Up @@ -183,33 +184,16 @@ private Dataset<DataView> GetIris<T>(string path) where T : DataView
public void TestKNN()
{
var trainDataset = GetIris<IrisData>("iris-train.txt").ToDatasetNDarray();
var knn = new KNN(5);
var knn = new KNN(2);
knn.LoadDataView(trainDataset.Feature, trainDataset.Label);

var x = trainDataset.Feature;
var y = trainDataset.Label;

var input = new Dataset<IrisData>(new[]
{
new IrisData
{
SepalLength = 6.6,
SepalWidth = 2.9,
PetalLength = 4.6,
PetalWidth = 1.3
},
new IrisData
{
SepalLength = 7.2,
SepalWidth = 3.5,
PetalLength = 6.1,
PetalWidth = 2.4
}
}).ToDatasetNDarray().Feature;


var res = knn.Call(input);
print(res);
var testDataset = GetIris<IrisData>("iris-test.txt").ToDatasetNDarray();
var Y_pred = knn.Call(testDataset.Feature);
print(Y_pred);
print(testDataset.Label);
var categoricalAcc = new MeanSquaredError().Call(testDataset.Label, Y_pred);
print(categoricalAcc);
}
}
}
12 changes: 7 additions & 5 deletions src/ML.Core/ML.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,16 @@
<Compile Include="Metrics\Regression\MeanSquaredError.cs" />
<Compile Include="Metrics\Regression\MeanSquaredLogarithmicError.cs" />
<Compile Include="Metrics\Regression\RSquared.cs" />
<Compile Include="Models\BinaryLogicClassify.cs" />
<Compile Include="Models\Cluster\Cluster.cs" />
<Compile Include="Models\Cluster\KMeans.cs" />
<Compile Include="Models\Supervised\BinaryLogicClassify.cs" />
<Compile Include="Models\Interface\IModel.cs" />
<Compile Include="Models\Interface\IModelGD.cs" />
<Compile Include="Models\KNN.cs" />
<Compile Include="Models\ModelGD.cs" />
<Compile Include="Models\MultipleLinearRegression.cs" />
<Compile Include="Models\Perceptron.cs" />
<Compile Include="Models\PolynomialRegression.cs" />
<Compile Include="Models\Supervised\ModelGD.cs" />
<Compile Include="Models\Supervised\MultipleLinearRegression.cs" />
<Compile Include="Models\Supervised\Perceptron.cs" />
<Compile Include="Models\Supervised\PolynomialRegression.cs" />
<Compile Include="Optimizers\AdaDelta.cs" />
<Compile Include="Optimizers\AdaGrad.cs" />
<Compile Include="Optimizers\Adam.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/ML.Core/ML.Core.csproj.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=losses_005Cbinarylosses/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=losses_005Ccategoricallosses/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=losses_005Cregressionlosses/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Ccluster/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cinterface/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Csupervised/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=optimizers_005Cannealings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=optimizer_005Cannealings/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
27 changes: 27 additions & 0 deletions src/ML.Core/Models/Cluster/Cluster.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using GalaSoft.MvvmLight;
using Numpy;

namespace ML.Core.Models
{
public abstract class Cluster : ViewModelBase
{
private int _k;

/// <summary>
/// 聚类算法抽象类
/// </summary>
/// <param name="k"></param>
protected Cluster(int k)
{
K = k;
}

public int K
{
get => _k;
set => Set(ref _k, value);
}

public abstract NDarray Call(NDarray input);
}
}
17 changes: 17 additions & 0 deletions src/ML.Core/Models/Cluster/KMeans.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Numpy;

namespace ML.Core.Models
{
public class KMeans : Cluster
{
public KMeans(int k) : base(k)
{
}

public override NDarray Call(NDarray input)
{
throw new NotImplementedException();
}
}
}
9 changes: 4 additions & 5 deletions src/ML.Core/Models/KNN.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using ML.Core.Data;
Expand All @@ -21,13 +20,13 @@ public KNN(int k, bool regression = false)
public NDarray Features { get; set; }

public NDarray Labels { get; set; }
public string Name => GetType().Name;
public NDarray Weights { get; set; }

public string WeightFile { get; }
public string WeightFile => $"{Name}.txt";

public double[] GetWeightArray()
{
throw new NotImplementedException();
return null;
}


Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 3b744b0

Please sign in to comment.