Last active
November 9, 2017 15:24
-
-
Save fversnel/12f9e1b3602039975efc96691a51af2d to your computer and use it in GitHub Desktop.
Following along in Scala with the Coursera Machine Learning course
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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