Skip to content

Instantly share code, notes, and snippets.

@elpsk
Last active March 5, 2020 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elpsk/bbf2ac838406b7459cee904a836a8463 to your computer and use it in GitHub Desktop.
Save elpsk/bbf2ac838406b7459cee904a836a8463 to your computer and use it in GitHub Desktop.
A sample playground to create a trained model for twitter sentiment analysis
import Cocoa
import CreateML
// tha path of your csv file
let baseFolder = "<#path#>"
let data = try! MLDataTable(contentsOf: URL(fileURLWithPath: "\(baseFolder)/twitter-sanders-apple3.csv"))
let (trainingData, testingData) = data.randomSplit(by: 0.8, seed: 5)
// classifier
let sentimentClassifier = try MLTextClassifier(trainingData: trainingData, textColumn: "text", labelColumn: "class")
// metrics
let evaluationMetrics = sentimentClassifier.evaluation(on: testingData, textColumn: "text", labelColumn: "class")
// accuracy
let evaluationAccuract = (1.0 - evaluationMetrics.classificationError) * 100
// metadata to export
let metadata = MLModelMetadata(
author: "Alberto Pasca",
shortDescription: "A model to classify tweets",
license: "MIT",
version: "1,0"
)
// save the generated *.mlmodel
try sentimentClassifier.write(to: URL(fileURLWithPath: "\(baseFolder)/TweetSentimentClassifier.mlmodel"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment