private IEnumerable <ClusterableVector> GetVectorsFromFile(string relativePath) { string[] fileLines = File.ReadAllLines(relativePath); ClusterableVector[] vectorArray = new ClusterableVector[fileLines.Length]; for (int i = 0; i < fileLines.Length; i++) { var strVector = fileLines[i].Split(new char[] { ' ', }, StringSplitOptions.RemoveEmptyEntries); vectorArray[i] = new ClusterableVector(strVector); } return(vectorArray); }
public void IrrelevantArgsTest() { ClusterableVector[] defaultVectorCollection = new ClusterableVector[defaultCentroidCount]; for (int i = 0; i < defaultVectorCollection.Length; i++) { defaultVectorCollection[i] = new ClusterableVector(new double[] { 1.0 }); } Assert.ThrowsException <ArgumentException>(() => { new KMeansPPClusterizer <ClusterableVector>(0, defaultVectorCollection, Metrics.Euclidian); }); Assert.ThrowsException <ArgumentException>(() => { new KMeansPPClusterizer <ClusterableVector>(defaultCentroidCount, defaultVectorCollection, Metrics.Euclidian, 0); }); Assert.ThrowsException <ArgumentException>(() => { new KMeansPPClusterizer <ClusterableVector>(defaultCentroidCount + 1, defaultVectorCollection, Metrics.Euclidian); }); }
public void MarginalArgsTest() { ClusterableVector[] defaultVectorCollection = new ClusterableVector[defaultCentroidCount]; for (int i = 0; i < defaultVectorCollection.Length; i++) { defaultVectorCollection[i] = new ClusterableVector(new double[] { (double)i }); } var clusterizer1 = new KMeansPPClusterizer <ClusterableVector>(1, defaultVectorCollection, Metrics.Euclidian); Assert.IsNotNull(clusterizer1.GetClusters()); ClusterableVector[] trivialVectorCollection = new ClusterableVector[defaultCentroidCount]; for (int i = 0; i < trivialVectorCollection.Length; i++) { trivialVectorCollection[i] = new ClusterableVector(new double[] { 1.0 }); } var clusterizer2 = new KMeansPPClusterizer <ClusterableVector>(defaultCentroidCount, trivialVectorCollection, Metrics.Euclidian); Assert.IsNotNull(clusterizer2.GetClusters()); }
public void EuclidianTest() { var oneDimVector1 = new ClusterableVector(oneDimVectorCoordinates1); var oneDimVector2 = new ClusterableVector(oneDimVectorCoordinates2); var oneDimVector3 = new ClusterableVector(oneDimVectorCoordinates3); var oneDimDistance1To1 = Metrics.Euclidian(oneDimVector1, oneDimVector1); var oneDimDistance2To2 = Metrics.Euclidian(oneDimVector2, oneDimVector2); var oneDimDistance3To3 = Metrics.Euclidian(oneDimVector3, oneDimVector3); var oneDimDistance1To2 = Metrics.Euclidian(oneDimVector1, oneDimVector2); var oneDimDistance2To3 = Metrics.Euclidian(oneDimVector2, oneDimVector3); var oneDimDistance1To3 = Metrics.Euclidian(oneDimVector1, oneDimVector3); var oneDimDistance2To1 = Metrics.Euclidian(oneDimVector1, oneDimVector2); var oneDimDistance3To2 = Metrics.Euclidian(oneDimVector2, oneDimVector3); var oneDimDistance3To1 = Metrics.Euclidian(oneDimVector1, oneDimVector3); //axiom of identity Assert.AreEqual(oneDimDistance1To1, 0.0); Assert.AreEqual(oneDimDistance2To2, 0.0); Assert.AreEqual(oneDimDistance3To3, 0.0); //axiom of symmetry Assert.AreEqual(oneDimDistance1To2, oneDimDistance2To1); Assert.AreEqual(oneDimDistance2To3, oneDimDistance3To2); Assert.AreEqual(oneDimDistance1To3, oneDimDistance3To1); //axiom of triangle Assert.IsTrue(oneDimDistance1To3 <= oneDimDistance1To2 + oneDimDistance2To3); //expectation Assert.AreEqual(Math.Round(oneDimDistance1To2, 9), expectedOneDimDistance1To2); Assert.AreEqual(Math.Round(oneDimDistance2To3, 9), expectedOneDimDistance2To3); Assert.AreEqual(Math.Round(oneDimDistance1To3, 9), expectedOneDimDistance1To3); var threeDimVector1 = new ClusterableVector(threeDimVectorCoordinates1); var threeDimVector2 = new ClusterableVector(threeDimVectorCoordinates2); var threeDimVector3 = new ClusterableVector(threeDimVectorCoordinates3); var threeDimDistance1To1 = Metrics.Euclidian(threeDimVector1, threeDimVector1); var threeDimDistance2To2 = Metrics.Euclidian(threeDimVector2, threeDimVector2); var threeDimDistance3To3 = Metrics.Euclidian(threeDimVector3, threeDimVector3); var threeDimDistance1To2 = Metrics.Euclidian(threeDimVector1, threeDimVector2); var threeDimDistance2To3 = Metrics.Euclidian(threeDimVector2, threeDimVector3); var threeDimDistance1To3 = Metrics.Euclidian(threeDimVector1, threeDimVector3); var threeDimDistance2To1 = Metrics.Euclidian(threeDimVector1, threeDimVector2); var threeDimDistance3To2 = Metrics.Euclidian(threeDimVector2, threeDimVector3); var threeDimDistance3To1 = Metrics.Euclidian(threeDimVector1, threeDimVector3); //axiom of identity Assert.AreEqual(threeDimDistance1To1, 0.0); Assert.AreEqual(threeDimDistance2To2, 0.0); Assert.AreEqual(threeDimDistance3To3, 0.0); //axiom of symmetry Assert.AreEqual(threeDimDistance1To2, threeDimDistance2To1); Assert.AreEqual(threeDimDistance2To3, threeDimDistance3To2); Assert.AreEqual(threeDimDistance1To3, threeDimDistance3To1); //axiom of triangle Assert.IsTrue(threeDimDistance1To3 <= threeDimDistance1To2 + threeDimDistance2To3); //expectation Assert.AreEqual(Math.Round(threeDimDistance1To2, precision), expectedThreeDimEuclidian1To2); Assert.AreEqual(Math.Round(threeDimDistance2To3, precision), expectedThreeDimEuclidian2To3); Assert.AreEqual(Math.Round(threeDimDistance1To3, precision), expectedThreeDimEuclidian1To3); }