Skip to content

Commit

Permalink
add Spectral
Browse files Browse the repository at this point in the history
  • Loading branch information
xin-pu committed Jun 16, 2022
1 parent 80836db commit 1fb4921
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/ML.Core.Test/KMeansTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ML.Core.Data.DataStructs;
using ML.Core.Data.Loader;
using ML.Core.Models;
using ML.Core.Transform;
using Numpy;
using Numpy.Models;
using Xunit;
Expand Down Expand Up @@ -60,5 +61,72 @@ public void Test()
var max = np.argmax(sumDis, 0);
print(max);
}


[Fact]
public void TestAdjacentMatrix()
{
var input = np.array(new[,] {{1, 1, 1}, {1, 2, 1}, {2, 2.5, 2}, {3.1, 3, 3}});
var (W, D) = getAdjacentMatrix(input);
print(W);
print(D);

var Lrw = np.eye(input.shape[0]) - np.linalg.inv(D) * W;
print(Lrw);
}


[Fact]
public void TestAdjacentMatrix2()
{
var path = Path.Combine(dataFolder, "data_cluster.txt");
var data = TextLoader.LoadDataSet<LinearData>(path, new[] {','}, false);
var input = data.ToFeatureNDarray();
var (W, D) = getAdjacentMatrix(input);
print(W);
print(D);

var Lrw = np.eye(input.shape[0]) - np.linalg.inv(D) * W;
print(Lrw);

var (l, v) = np.linalg.eig(Lrw);
print(l);
print(v);
}

/// <summary>
/// epsilon
/// </summary>
/// <param name="input"></param>
/// <param name="esplion"></param>
private (NDarray, NDarray) getAdjacentEpsilon(NDarray input, double epsilon)
{
var batch = input.shape[0];
var features = input.shape[1];
var inputH = np.reshape(np.tile(input, np.array(batch)), new Shape(batch, batch, features));

var inputV = np.reshape(np.tile(input, np.array(batch, 1)), new Shape(batch, batch, features));

var dis = np.linalg.norm(inputV - inputH, axis: -1, ord: 2);

var W = np.where(dis < epsilon, np.array(epsilon), np.array(0));
//np.fill_diagonal(W, 0);

var D = np.eye(batch) * np.sum(W, 0);

return (W, D);
}

/// <summary>
/// epsilon
/// </summary>
/// <param name="input"></param>
/// <param name="esplion"></param>
private (NDarray, NDarray) getAdjacentMatrix(NDarray input)
{
var W = new Gaussian().Call(input);
var D = np.eye(input.shape[0]) * np.sum(W, 0);
return (W, D);
}
}
}
1 change: 1 addition & 0 deletions src/ML.Core/ML.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
<Compile Include="Metrics\Regression\RSquared.cs" />
<Compile Include="Models\Cluster\Cluster.cs" />
<Compile Include="Models\Cluster\KMeans.cs" />
<Compile Include="Models\Cluster\Spectral.cs" />
<Compile Include="Models\Supervised\BinaryLogicClassify.cs" />
<Compile Include="Models\Interface\IModel.cs" />
<Compile Include="Models\Interface\IModelGD.cs" />
Expand Down
5 changes: 5 additions & 0 deletions src/ML.Core/Models/Cluster/Cluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ public int K
}

public abstract NDarray Call(NDarray input);

public static NDarray GetAdjacentMatrixEpsilon(double epsilon)
{
return null;
}
}
}
6 changes: 6 additions & 0 deletions src/ML.Core/Models/Cluster/KMeans.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public class KMeans : Cluster
private int _iterationLimit;
private KMeansAlgorithm _kMeansAlgorithm;

/// <summary>
/// KMeans 聚类算法
/// </summary>
/// <param name="k"></param>
/// <param name="iterationLimit">limit for iteration</param>
/// <param name="kMeansAlgorithm">KMeans and KMeans++ </param>
public KMeans(
int k,
int iterationLimit = 100,
Expand Down
21 changes: 21 additions & 0 deletions src/ML.Core/Models/Cluster/Spectral.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using Numpy;

namespace ML.Core.Models
{
public class Spectral : Cluster
{
/// <summary>
/// 谱聚类
/// </summary>
/// <param name="k"></param>
public Spectral(int k) : base(k)
{
}

public override NDarray Call(NDarray input)
{
throw new NotImplementedException();
}
}
}

0 comments on commit 1fb4921

Please sign in to comment.