private Vector Normalise(Vector y) { for (int i = 0; i < y.Length; i++) { y[i] = PreProcessing.FeatureNormalizer.FeatureScale(y[i], this.FeatureAverages[i], this.FeatureStandardDeviations[i]); } return y.Insert(0, 1.0d); }
/// <summary> /// Create a prediction based on the learned Theta values and the supplied test item. /// </summary> /// <param name="x">Training record</param> /// <returns></returns> public override double Predict(Vector x) { this.Preprocess(x); return x.Insert(0, 1.0, false).Dot(Theta); }
/// <summary> /// Computes the probability of the prediction being True. /// </summary> /// <param name="x"></param> /// <returns></returns> public double PredictRaw(Vector x) { x = IncreaseDimensions(x, this.PolynomialFeatures); this.Preprocess(x); return LogisticFunction.Compute(x.Insert(0, 1.0, false).Dot(Theta)); }
/// <summary> /// Converts the experience pair into their equivalent math forms. /// </summary> /// <param name="state">IMDPState instance.</param> /// <param name="nodes">List of nodes added to the result set.</param> /// <param name="states">Matrix to store contained successor state vectors.</param> /// <param name="actions">Vector to store the contained action values.</param> /// <param name="statesP">Matrix to store all contained successor transition state vectors.</param> /// <param name="rewards">Vector to store all contained reward values.</param> /// <returns>HashSet<string></returns> private static void Convert(this IMDPState state, ref List<string> nodes, ref Matrix states, ref Vector actions, ref Matrix statesP, ref Vector rewards) { if (state != null) { foreach (IMDPSuccessor successor in state.GetSuccessors()) { if (state.Features.Length != states.Cols) states = Matrix.Reshape(states, states.Rows, state.Features.Length); if (state.Features.Length != statesP.Cols) statesP = Matrix.Reshape(statesP, statesP.Rows, ((IMDPState) successor.State).Features.Length); string id = $"{state.Id}:{successor.State.Id}"; if (!nodes.Contains(id)) { states = states.Insert(state.ToVector(), states.Rows - 1, VectorType.Row); actions = actions.Insert(actions.Length - 1, successor.Action.Id); statesP = statesP.Insert(((IMDPState) successor.State).ToVector(), statesP.Rows - 1, VectorType.Row); rewards = rewards.Insert(rewards.Length - 1, successor.Reward); nodes.Add(id); } if (!successor.State.IsTerminal) { var successorState = ((IMDPState) successor.State); if (successorState.Id != state.Id) successorState.Convert(ref nodes, ref states, ref actions, ref statesP, ref rewards); } } } }