Skip to content

Instantly share code, notes, and snippets.

@fversnel
Last active November 9, 2017 15:24
Show Gist options
  • Save fversnel/12f9e1b3602039975efc96691a51af2d to your computer and use it in GitHub Desktop.
Save fversnel/12f9e1b3602039975efc96691a51af2d to your computer and use it in GitHub Desktop.
Following along in Scala with the Coursera Machine Learning course
import spire.implicits._
import spire.math._
class MachineLearning1[Number](implicit fractional: Fractional[Number]) {
import fractional.pow
private def number(integer: Int): Number = fractional.fromInt(integer)
type Hypothesis = Number => Number
type Cost = Hypothesis => Number
type TrainingSet = Set[(Number, Number)]
def hypothesis(constant: Number)(weightX: Number): Hypothesis = {
x => constant + weightX * x
}
def cost(trainingSet: TrainingSet)(hypothesis: Hypothesis): Number = {
val averager = number(1) / (number(2) * number(trainingSet.size))
val totalError = trainingSet.foldLeft(number(0)) { case (error, (input, actual)) =>
val prediction = hypothesis(input)
error + pow(prediction - actual, 2)
}
val averageError = averager * totalError
averageError
}
def gradientDescent(learningRate: Number)(cost: Cost): Number = {
// Generate hypotheses and feed them to cost
}
def minimize(cost: Cost): Number = {
???
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment