Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Src/numl.Tests/CollectionTests/CollectionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void NSortedList_Size_Test()

var contains = (dict.Where(w => w.Value == 0 && sorted.Contains(w.Key)));

bool result = (contains.Count() == 0);
bool result = (!contains.Any());

Assert.True(result, "Length was not equal to the elements in the collection");

Expand Down
6 changes: 3 additions & 3 deletions Src/numl.Tests/DataTests/GraphTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ public void RemoveVertexTest()

g.RemoveVertex(vertex1);

Assert.Equal(0, g.GetVertices().Where(v => v.Id == 1).Count());
Assert.Equal(0, g.GetEdges().Where(e => e.ParentId == 1 || e.ChildId == 1).Count());
Assert.Equal(0, g.GetVertices().Count(v => v.Id == 1));
Assert.Equal(0, g.GetEdges().Count(e => e.ParentId == 1 || e.ChildId == 1));
}

[Fact]
Expand All @@ -190,7 +190,7 @@ public void RemoveEdgeTest()

g.RemoveEdge(edge);

Assert.Equal(0, g.GetEdges().Where(e => e.ParentId == 1 && e.ChildId == 5).Count());
Assert.Equal(0, g.GetEdges().Count(e => e.ParentId == 1 && e.ChildId == 5));

}

Expand Down
2 changes: 1 addition & 1 deletion Src/numl/AI/Search/AdversarialSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal Node GetBestChildNode(Node node, double v)
.Where(n => n.Cost == v && n.State.IsTerminal);

Node r;
if (q.Count() > 0) // favor terminal nodes first
if (q.Any()) // favor terminal nodes first
r = q.Random();
else
r = node.Children
Expand Down
2 changes: 1 addition & 1 deletion Src/numl/Learner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private static LearningModel GenerateModel(IGenerator generator, Matrix x, Vecto

Score score = new Score();

if (testingSlice.Count() > 0)
if (testingSlice.Any())
{
// testing
object[] test = GetTestExamples(testingSlice, examples);
Expand Down
4 changes: 2 additions & 2 deletions Src/numl/Math/LinearAlgebra/MatrixStatics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,8 @@ public static Matrix Slice(Matrix m, IEnumerable<int> indices, VectorType t)
{
var q = indices.Distinct();

int rows = t == VectorType.Row ? q.Where(j => j < m.Rows).Count() : m.Rows;
int cols = t == VectorType.Col ? q.Where(j => j < m.Cols).Count() : m.Cols;
int rows = t == VectorType.Row ? q.Count(j => j < m.Rows) : m.Rows;
int cols = t == VectorType.Col ? q.Count(j => j < m.Cols) : m.Cols;

Matrix n = new Matrix(rows, cols);

Expand Down
9 changes: 5 additions & 4 deletions Src/numl/Math/LinearAlgebra/VectorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ public static Vector ToVector(this double[] array)
/// <returns>e as a Matrix.</returns>
public static Matrix ToMatrix(this IEnumerable<Vector> source, VectorType vectorType = VectorType.Row)
{
var c = source.Count();
if (c == 0)
if (!source.Any())
throw new InvalidOperationException("Cannot create matrix from an empty set.");

return Matrix.Stack(vectorType, source.ToArray());
Expand Down Expand Up @@ -333,7 +332,7 @@ group i by i into g
select new
{
key = g.Key,
count = source.Where(d => d == g.Key).Count()
count = source.Count(d => d == g.Key)
};

double mode = -1;
Expand Down Expand Up @@ -801,7 +800,9 @@ public static bool IsNaN(this Vector vector)
public static bool IsBinary(this Vector vector)
{
var v = vector.Distinct();
return ((v.Count() == 2 && (v.Contains(1d) && (v.Contains(0d) || v.Contains(-1d)))) || (v.Count() == 1 && (v.Contains(1d) || v.Contains(0d) || v.Contains(-1d))));
var count = v.Count();

return ((count == 2 && (v.Contains(1d) && (v.Contains(0d) || v.Contains(-1d)))) || (count == 1 && (v.Contains(1d) || v.Contains(0d) || v.Contains(-1d))));
}

/// <summary>
Expand Down
5 changes: 1 addition & 4 deletions Src/numl/Model/Descriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ public Property this[string name]
{
get
{
if (Features.Where(p => p.Name == name).Count() == 1)
return Features.Where(p => p.Name == name).First();
else
return null;
return Features.Count(p => p.Name == name) == 1 ? Features.First(p => p.Name == name) : null;
}
}
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Src/numl/Recommendation/CofiRecommenderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public override IModel Generate(Matrix X, Vector y)
// The mean needs to be values within rating range only.
Vector mean = X.GetRows().Select(s =>
s.Where(w => this.Ratings.Test(w)).Sum() /
s.Where(w => this.Ratings.Test(w)).Count()
s.Count(w => this.Ratings.Test(w))
).ToVector();

// update feature averages before preprocessing features.
Expand Down
6 changes: 3 additions & 3 deletions Src/numl/Reinforcement/ReinforcementGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public virtual void Preprocess(Matrix X1, Vector y, Matrix X2, Vector r)
/// <returns>IReinforcementModel.</returns>
public IReinforcementModel Generate(Descriptor description, IEnumerable<object> examples)
{
if (examples.Count() == 0) throw new InvalidOperationException("Empty example set.");
if (!examples.Any()) throw new InvalidOperationException("Empty example set.");

Descriptor = description;
if (Descriptor.Features == null || Descriptor.Features.Length == 0)
Expand Down Expand Up @@ -169,7 +169,7 @@ public IReinforcementModel Generate<T>(Descriptor descriptor, IEnumerable<T> exa
/// <returns>IReinforcementModel.</returns>
public IReinforcementModel Generate(IEnumerable<object> examples1, IEnumerable<object> examples2)
{
if (examples1.Count() == 0) throw new InvalidOperationException("Empty example set.");
if (!examples1.Any()) throw new InvalidOperationException("Empty example set.");

if (this.Descriptor == null)
throw new InvalidOperationException("Descriptor is null");
Expand All @@ -188,7 +188,7 @@ public IReinforcementModel Generate(IEnumerable<object> examples1, IEnumerable<o
/// <returns>IReinforcementModel.</returns>
public IReinforcementModel Generate(Descriptor descriptor, IEnumerable<object> examples1, Descriptor transitionDescriptor, IEnumerable<object> examples2)
{
if (examples1.Count() == 0) throw new InvalidOperationException("Empty example set.");
if (!examples1.Any()) throw new InvalidOperationException("Empty example set.");

bool hasTransitionStates = (examples2 != null && examples2.Any());

Expand Down
2 changes: 1 addition & 1 deletion Src/numl/Supervised/Classification/MultiClassLearner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private static Tuple<IClassifier, Score, object> GenerateModel(IGenerator genera

Score score = new Score();

if (testingSlice.Count() > 0)
if (testingSlice.Any())
{
object[] testExamples = examples.Slice(testingSlice).ToArray();
var testing = generator.Descriptor.Convert(testExamples, true).ToExamples();
Expand Down
2 changes: 1 addition & 1 deletion Src/numl/Supervised/DecisionTree/DecisionTreeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private Node BuildTree(Matrix x, Vector y, int depth, List<int> used, Tree tree)
// if this number is 0 then this edge
// leads to a dead end - the edge will
// not be built
if (slice.Count() > 0)
if (slice.Any())
{
Vector ySlice = y.Slice(slice);
// only one answer, set leaf
Expand Down
4 changes: 2 additions & 2 deletions Src/numl/Supervised/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public virtual Matrix ToEncoded(Vector y)
/// <returns>Model.</returns>
public IModel Generate(IEnumerable<object> examples)
{
if (examples.Count() == 0) throw new InvalidOperationException("Empty example set.");
if (!examples.Any()) throw new InvalidOperationException("Empty example set.");

if (Descriptor == null)
throw new InvalidOperationException("Descriptor is null");
Expand All @@ -146,7 +146,7 @@ public IModel Generate(IEnumerable<object> examples)
/// <returns>Model.</returns>
public IModel Generate(Descriptor description, IEnumerable<object> examples)
{
if (examples.Count() == 0) throw new InvalidOperationException("Empty example set.");
if (!examples.Any()) throw new InvalidOperationException("Empty example set.");

Descriptor = description;
if (Descriptor.Features == null || Descriptor.Features.Length == 0)
Expand Down
5 changes: 3 additions & 2 deletions Src/numl/Supervised/NeuralNetwork/NetworkOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ public static Network Create(this Network network, int inputLayer, int outputLay
/// <returns></returns>
public static Network AddConnections(this Network network, Neuron node, IEnumerable<Neuron> parentNodes, IEnumerable<Neuron> childNodes, double epsilon = double.NaN)
{
if (epsilon == double.NaN)
//episilon == double.Nan will be always false, even if epsilon is Nan
if (double.IsNaN(epsilon))
epsilon = Edge.GetEpsilon(node.ActivationFunction.Minimum, node.ActivationFunction.Maximum, parentNodes.Count(), childNodes.Count());

if (parentNodes != null)
Expand Down Expand Up @@ -257,7 +258,7 @@ public static Vector GetBiases(this Network network, int layer = 0)

var nodes = network.GetNodes(layer + 1).ToArray();

Vector biases = Vector.Zeros(nodes.Where(w => !w.IsBias).Count());
Vector biases = Vector.Zeros(nodes.Count(w => !w.IsBias));

for (int i = 0; i < nodes.Length; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions Src/numl/Supervised/Score.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ public static Score ScorePredictions(Vector predictions, Vector actual,
{
var score = new numl.Supervised.Score()
{
TotalPositives = actual.Where(w => w == truthLabel).Count(),
TotalNegatives = actual.Where(w => (w == falseLabel || w != truthLabel)).Count(),
TotalPositives = actual.Count(w => w == truthLabel),
TotalNegatives = actual.Count(w => (w == falseLabel || w != truthLabel)),

TruePositives = actual.Where((i, idx) => i == truthLabel && i == predictions[idx]).Count(),
FalsePositives = actual.Where((i, idx) => (i == falseLabel || i != truthLabel) && predictions[idx] == truthLabel).Count(),
Expand Down