Last active
March 5, 2020 10:26
-
-
Save elpsk/bbf2ac838406b7459cee904a836a8463 to your computer and use it in GitHub Desktop.
A sample playground to create a trained model for twitter sentiment analysis
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 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