Use Machine Learning to predict if a person is working out based of his/her heart rate.
To run the example project, clone the repo, and run pod install from the Example directory first.
HRLClassifier is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod"HRLClassifier"import HRLClassifier
letfilename="/path/to/archive"letbaseDate=Date(timeIntervalSinceReferenceDate:0)letdayInterval=24*60*60letmaxBPM=200
// Fill data frame
varauxDataFrame=NSKeyedUnarchiver.unarchiveObject(withFile: filename)as?DataFrameif auxDataFrame ==nil{
auxDataFrame =DataFrame()foriin0..<7{for_in0..<80{lettimeInterval= i * dayInterval + Int(arc4random_uniform(UInt32(dayInterval)))letdate= baseDate.addingTimeInterval(TimeInterval(timeInterval))letbpm=Float(arc4random_uniform(UInt32(maxBPM)))letrecord=Record(date: date, bpm: bpm)letisWorkingOut=arc4random_uniform(2)==1?true:false
auxDataFrame!.append(record: record, isWorkingOut: isWorkingOut)}}}letdataFrame= auxDataFrame!
// Make classifier
guardlet classifier =try?ClassifierFactory().makeClassifier(dataFrame: dataFrame)else{print("Given that all records are created in a random fashion, this is expected")return}
// Archive data frame
NSKeyedArchiver.archiveRootObject(dataFrame, toFile: filename)
// Predict
letdate= baseDate.addingTimeInterval(TimeInterval(7* dayInterval))letbpm=Float(arc4random_uniform(UInt32(maxBPM)))letrecord=Record(date: date, bpm: bpm)letprediction= classifier.predictedWorkingOut(for: record)print("At \(date) with \(bpm) bpm, is user working out? \(prediction)")HRLClassifier is available under the MIT license. See the LICENSE file for more info.