{"id":190898,"date":"2022-08-03T15:23:43","date_gmt":"2022-08-03T07:23:43","guid":{"rendered":"https:\/\/www.idc.net\/help\/190898\/"},"modified":"2022-08-03T15:23:43","modified_gmt":"2022-08-03T07:23:43","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91ios%e6%95%99%e7%a8%8b%ef%bc%9a%e5%88%9b%e5%bb%ba%e6%96%87%e6%9c%ac%e5%88%86%e7%b1%bb%e5%99%a8%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/190898\/","title":{"rendered":"\u540e\u6d6a\u4e91IOS\u6559\u7a0b\uff1a\u521b\u5efa\u6587\u672c\u5206\u7c7b\u5668\u6a21\u578b"},"content":{"rendered":"<h2> \u6982\u89c8 <\/h2>\n<p> \u6587\u672c\u5206\u7c7b\u5668\u662f\u4e00\u79cd\u673a\u5668\u5b66\u4e60\u6a21\u578b\uff0c\u7ecf\u8fc7\u8bad\u7ec3\u5c06\u80fd\u591f\u8bc6\u522b\u81ea\u7136\u8bed\u8a00\u6587\u672c\u4e2d\u7684\u89c4\u5f8b\uff0c\u4f8b\u5982\u53e5\u5b50\u6240\u8868\u8fbe\u7684\u60c5\u7eea\u3002 <\/p>\n<\/p>\n<p> \u8bad\u7ec3\u6587\u672c\u5206\u7c7b\u5668\u7684\u65b9\u6cd5\u662f\u5411\u5b83\u5c55\u793a\u5927\u91cf\u5df2\u6807\u8bb0\u7684\u6587\u672c\u793a\u4f8b\uff0c\u4f8b\u5982\u4f60\u5df2\u6807\u8bb0\u4e3a\u597d\u8bc4\u3001\u5dee\u8bc4\u6216\u4e2d\u7acb\u7684\u5f71\u8bc4\u3002 <\/p>\n<\/p>\n<h3> \u5bfc\u5165\u6570\u636e <\/h3>\n<p> \u9996\u5148\uff0c\u6536\u96c6\u6587\u672c\u6570\u636e\u5e76\u5bfc\u5165\u5230 <code>MLDataTable<\/code> <span>(\u82f1\u6587)<\/span> \u5b9e\u4f8b\u4e2d\u3002\u4f60\u53ef\u4ee5\u4ece JSON \u548c CSV \u683c\u5f0f\u521b\u5efa\u6570\u636e\u8868\u3002\u5982\u679c\u6587\u672c\u6570\u636e\u5728\u4e00\u7cfb\u5217\u6587\u4ef6\u91cc\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5c06\u5b83\u4eec\u6574\u7406\u5230\u6587\u4ef6\u5939\u4e2d\uff0c\u5e76\u4f7f\u7528\u6587\u4ef6\u5939\u540d\u79f0\u4f5c\u4e3a\u6807\u7b7e\uff0c\u7c7b\u4f3c\u4e8e\u201c\u521b\u5efa\u56fe\u50cf\u5206\u7c7b\u5668\u6a21\u578b\u201d\u4e2d\u4f7f\u7528\u7684\u56fe\u50cf\u6570\u636e\u6e90\u3002 <\/p>\n<p> \u4f8b\u5982\uff0c\u5047\u8bbe\u4e00\u4e2a JSON \u6587\u4ef6\u4e2d\u5305\u542b\u4f60\u6309\u60c5\u7eea\u5206\u7c7b\u7684\u5f71\u8bc4\u3002\u6bcf\u4e2a\u6761\u76ee\u90fd\u5305\u542b <code>text<\/code> \u548c <code>label<\/code> \u8fd9\u6837\u4e00\u5bf9\u5c5e\u6027\u3002\u8fd9\u4e9b\u5c5e\u6027\u503c\u5c31\u662f\u7528\u6765\u8bad\u7ec3\u6a21\u578b\u7684\u8f93\u5165\u6837\u672c\u3002\u4e0b\u9762\u7684 JSON \u6bb5\u843d\u663e\u793a\u4e86\u4e09\u5bf9\u53e5\u5b50\u4ee5\u53ca\u5bf9\u5e94\u7684\u60c5\u7eea\u6807\u7b7e\u3002 <\/p>\n<pre><code><span><span><\/span> <span><span>\/\/ JSON file<\/span><\/span><\/span><span><span><\/span> <span>[<\/span><\/span><span><span><\/span> <span> {<\/span><\/span><span><span><\/span> <span> <span>\"text\"<\/span>: <span>\"The movie was fantastic!\"<\/span>,<\/span><\/span><span><span><\/span> <span> <span>\"label\"<\/span>: <span>\"positive\"<\/span><\/span><\/span><span><span><\/span> <span> }, {<\/span><\/span><span><span><\/span> <span> <span>\"text\"<\/span>: <span>\"Very boring. Fell asleep.\"<\/span>,<\/span><\/span><span><span><\/span> <span> <span>\"label\"<\/span>: <span>\"negative\"<\/span><\/span><\/span><span><span><\/span> <span> }, {<\/span><\/span><span><span><\/span> <span> <span>\"text\"<\/span>: <span>\"It was just OK.\"<\/span>,<\/span><\/span><span><span><\/span> <span> <span>\"label\"<\/span>: <span>\"neutral\"<\/span><\/span><\/span><span><span><\/span> <span> } ...<\/span><\/span><span><span><\/span> <span>]<\/span><\/span><\/code>\n\t\t\t\t\t\t<\/pre>\n<p> \u5728 macOS Playground \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>MLDataTable<\/code> <span>(\u82f1\u6587)<\/span> \u7684 <code>init(contentsOf:options:)<\/code> <span>(\u82f1\u6587)<\/span> \u65b9\u6cd5\u6765\u521b\u5efa\u6570\u636e\u8868\u3002 <\/p>\n<pre><code><span><span><\/span> <span><span>import<\/span> CreateML<\/span><\/span><span><span><\/span> <span> <\/span><\/span><span><span><\/span> <span><span>let<\/span> data = <span>try<\/span> <span>MLDataTable<\/span>(contentsOf: <span>URL<\/span>(fileURLWithPath: <span>\"&lt;#\/path\/to\/read\/data.json#&gt;\"<\/span>))<\/span><\/span><\/code>\n\t\t\t\t\t\t<\/pre>\n<p> \u5f97\u5230\u7684\u6570\u636e\u8868\u5305\u542b\u4e24\u5217\uff0c\u5206\u522b\u540d\u4e3a <em>text<\/em> \u548c <em>label<\/em>\uff0c\u8fd9\u4e24\u5217\u4ece JSON \u6587\u4ef6\u4e2d\u7684\u952e\u6d3e\u751f\u800c\u6765\u3002\u5217\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u540d\u79f0\uff0c\u53ea\u8981\u5bf9\u4f60\u6709\u610f\u4e49\u5373\u53ef\uff0c\u56e0\u4e3a\u4f60\u4f1a\u5728\u5176\u4ed6\u65b9\u6cd5\u4e2d\u5c06\u5217\u540d\u7528\u4f5c\u53c2\u6570\u3002 <\/p>\n<h3> \u51c6\u5907\u8bad\u7ec3\u548c\u8bc4\u4f30\u6570\u636e <\/h3>\n<p> \u4f60\u7528\u4e8e\u8bad\u7ec3\u6a21\u578b\u7684\u6570\u636e\u5fc5\u987b\u4e0e\u7528\u6765\u8bc4\u4f30\u6a21\u578b\u7684\u6570\u636e\u6709\u6240\u5dee\u522b\u3002\u4f7f\u7528 <code>MLDataTable<\/code> <span>(\u82f1\u6587)<\/span> \u7684 <code>randomSplit(by:seed:)<\/code> <span>(\u82f1\u6587)<\/span> \u65b9\u6cd5\u5c06\u6570\u636e\u62c6\u5206\u5230\u4e24\u4e2a\u8868\u4e2d\uff0c\u5206\u522b\u7528\u4e8e\u8bad\u7ec3\u548c\u6d4b\u8bd5\u3002\u8bad\u7ec3\u6570\u636e\u8868\u4f1a\u5305\u542b\u5927\u90e8\u5206\u6570\u636e\uff0c\u6d4b\u8bd5\u6570\u636e\u8868\u5219\u5305\u542b\u5176\u4f59 10% - 20% \u7684\u6570\u636e\u3002 <\/p>\n<pre><code><span><span><\/span> <span><span>let<\/span> (trainingData, testingData) = data.randomSplit(by: <span>0.8<\/span>, seed: <span>5<\/span>)<\/span><\/span><\/code>\n\t\t\t\t\t\t<\/pre>\n<h3> \u521b\u5efa\u548c\u8bad\u7ec3\u6587\u672c\u5206\u7c7b\u5668 <\/h3>\n<p> \u4f7f\u7528\u8bad\u7ec3\u6570\u636e\u8868\u548c\u5217\u540d\u6765\u521b\u5efa <code>MLTextClassifier<\/code> <span>(\u82f1\u6587)<\/span> \u5b9e\u4f8b\u3002\u8bad\u7ec3\u4fbf\u4f1a\u7acb\u5373\u5f00\u59cb\u3002 <\/p>\n<pre><code><span><span><\/span> <span><span>let<\/span> sentimentClassifier = <span>try<\/span> <span>MLTextClassifier<\/span>(trainingData: trainingData,<\/span><\/span><span><span><\/span> <span> textColumn: <span>\"text\"<\/span>,<\/span><\/span><span><span><\/span> <span> labelColumn: <span>\"label\"<\/span>)<\/span><\/span><\/code>\n\t\t\t\t\t\t<\/pre>\n<p> \u5728\u8bad\u7ec3\u671f\u95f4\uff0cCreate ML \u4f1a\u5206\u79bb\u51fa\u4e00\u5c0f\u90e8\u5206\u8bad\u7ec3\u6570\u636e\uff0c\u4f9b\u5728\u8bad\u7ec3\u9636\u6bb5\u7528\u6765\u9a8c\u8bc1\u6a21\u578b\u7684\u8fdb\u5ea6\u3002\u9a8c\u8bc1\u6570\u636e\u8ba9\u8bad\u7ec3\u8fc7\u7a0b\u80fd\u591f\u4f7f\u7528\u6a21\u578b\u672a\u8bad\u7ec3\u8fc7\u7684\u793a\u4f8b\u6765\u8861\u91cf\u6a21\u578b\u7684\u8868\u73b0\u3002\u6839\u636e\u9a8c\u8bc1\u51c6\u786e\u6027\uff0c\u8bad\u7ec3\u7b97\u6cd5\u53ef\u4ee5\u8c03\u6574\u6a21\u578b\u5185\u90e8\u7684\u503c\uff0c\u751a\u81f3\u80fd\u5728\u51c6\u786e\u6027\u8db3\u591f\u9ad8\u65f6\u505c\u6b62\u8bad\u7ec3\u8fc7\u7a0b\u3002\u7531\u4e8e\u62c6\u5206\u662f\u968f\u673a\u7684\uff0c\u56e0\u6b64\u6bcf\u6b21\u8bad\u7ec3\u6a21\u578b\u65f6\u53ef\u80fd\u4f1a\u5f97\u5230\u4e0d\u540c\u7684\u7ed3\u679c\u3002 <\/p>\n<p> \u8981\u4e86\u89e3\u6a21\u578b\u5728\u8bad\u7ec3\u548c\u9a8c\u8bc1\u6570\u636e\u65f6\u7684\u51c6\u786e\u5ea6\uff0c\u8bf7\u4f7f\u7528\u6a21\u578b\u91cc <code>trainingMetrics<\/code> <span>(\u82f1\u6587)<\/span> \u548c <code>validationMetrics<\/code> <span>(\u82f1\u6587)<\/span> \u5c5e\u6027\u4e2d\u7684 <code>classificationError<\/code> <span>(\u82f1\u6587)<\/span> \u5c5e\u6027\u3002 <\/p>\n<pre><code><span><span><\/span> <span><span>\/\/ Training accuracy as a percentage<\/span><\/span><\/span><span><span><\/span> <span><span>let<\/span> trainingAccuracy = (<span>1.0<\/span> - sentimentClassifier.trainingMetrics.classificationError) * <span>100<\/span><\/span><\/span><span><span><\/span> <span> <\/span><\/span><span><span><\/span> <span><span>\/\/ Validation accuracy as a percentage<\/span><\/span><\/span><span><span><\/span> <span><span>let<\/span> validationAccuracy = (<span>1.0<\/span> - sentimentClassifier.validationMetrics.classificationError) * <span>100<\/span><\/span><\/span><\/code>\n\t\t\t\t\t\t<\/pre>\n<\/p>\n<h3> \u8bc4\u4f30\u5206\u7c7b\u5668\u7684\u51c6\u786e\u6027 <\/h3>\n<p> \u63a5\u7740\uff0c\u4f7f\u7528\u65b0\u7684\u53e5\u5b50\u5bf9\u7ecf\u8fc7\u8bad\u7ec3\u7684\u6a21\u578b\u8fdb\u884c\u6d4b\u8bd5\uff0c\u8bc4\u4f30\u6a21\u578b\u7684\u8868\u73b0\u60c5\u51b5\u3002\u5c06\u4f60\u7684\u6d4b\u8bd5\u6570\u636e\u8868\u4f20\u9012\u5230 <code>evaluation(on:)<\/code> <span>(\u82f1\u6587)<\/span> \u65b9\u6cd5\uff0c\u8fd9\u5c06\u8fd4\u56de <code>MLClassifierMetrics<\/code> <span>(\u82f1\u6587)<\/span> \u5b9e\u4f8b\u3002 <\/p>\n<pre><code><span><span><\/span> <span><span>let<\/span> evaluationMetrics = sentimentClassifier.evaluation(on: testingData)<\/span><\/span><\/code>\n\t\t\t\t\t\t<\/pre>\n<p> \u8981\u83b7\u53d6\u8bc4\u4f30\u51c6\u786e\u6027\uff0c\u8bf7\u4f7f\u7528\u6240\u8fd4\u56de <code>MLClassifierMetrics<\/code> <span>(\u82f1\u6587)<\/span> \u5b9e\u4f8b\u7684 <code>classificationError<\/code> <span>(\u82f1\u6587)<\/span> \u5c5e\u6027\u3002 <\/p>\n<pre><code><span><span><\/span> <span><span>\/\/ Evaluation accuracy as a percentage<\/span><\/span><\/span><span><span><\/span> <span><span>let<\/span> evaluationAccuracy = (<span>1.0<\/span> - evaluationMetrics.classificationError) * <span>100<\/span><\/span><\/span><\/code>\n\t\t\t\t\t\t<\/pre>\n<p> \u5982\u679c\u8bc4\u4f30\u8868\u73b0\u4e0d\u7406\u60f3\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u66f4\u591a\u6570\u636e\u91cd\u65b0\u8bad\u7ec3\u6216\u8fdb\u884c\u5176\u4ed6\u8c03\u6574\u3002\u6709\u5173\u63d0\u5347\u6a21\u578b\u6027\u80fd\u7684\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u201c\u63d0\u9ad8\u6a21\u578b\u51c6\u786e\u6027\u201d\u3002 <\/p>\n<h3> \u5b58\u50a8 Core ML \u6a21\u578b <\/h3>\n<p> \u5982\u679c\u4f60\u5bf9\u6a21\u578b\u8868\u73b0\u6ee1\u610f\uff0c\u5c31\u53ef\u4ee5\u5b58\u50a8\u6a21\u578b\u4ee5\u4fbf\u5728 App \u4e2d\u4f7f\u7528\u4e86\u3002\u4f7f\u7528 <code>write(to:metadata:)<\/code> <span>(\u82f1\u6587)<\/span> \u65b9\u6cd5\u5c06 Core ML \u6a21\u578b\u6587\u4ef6 (<code>SentimentClassifier.mlmodel<\/code>) \u5199\u5165\u78c1\u76d8\u3002\u5728 <code>MLModelMetadata<\/code> <span>(\u82f1\u6587)<\/span> \u5b9e\u4f8b\u4e2d\u63d0\u4f9b\u6709\u5173\u6a21\u578b\u7684\u4efb\u4f55\u4fe1\u606f\uff0c\u4f8b\u5982\u4f5c\u8005\u3001\u7248\u672c\u6216\u63cf\u8ff0\u7b49\u3002 <\/p>\n<pre><code><span><span><\/span> <span><span>let<\/span> metadata = <span>MLModelMetadata<\/span>(author: <span>\"John Appleseed\"<\/span>,<\/span><\/span><span><span><\/span> <span> shortDescription: <span>\"A model trained to classify movie review sentiment\"<\/span>,<\/span><\/span><span><span><\/span> <span> version: <span>\"1.0\"<\/span>)<\/span><\/span><span><span><\/span> <span> <\/span><\/span><span><span><\/span> <span><span>try<\/span> sentimentClassifier.write(to: <span>URL<\/span>(fileURLWithPath: <span>\"&lt;#\/path\/to\/save\/SentimentClassifier.mlmodel#&gt;\"<\/span>),<\/span><\/span><span><span><\/span> <span> metadata: metadata)<\/span><\/span><\/code>\n\t\t\t\t\t\t<\/pre>\n<\/p>\n<h3> \u5c06\u6a21\u578b\u6dfb\u52a0\u5230 App <\/h3>\n<p> \u5728 Xcode \u4e2d\u6253\u5f00 App \u540e\uff0c\u5c06 <code>SentimentClassifier.mlmodel<\/code> \u6587\u4ef6\u62d6\u5230\u5bfc\u822a\u9762\u677f\u4e2d\u3002Xcode \u4f1a\u7f16\u8bd1\u6a21\u578b\u5e76\u751f\u6210 <code>SentimentClassifier<\/code> \u7c7b\uff0c\u4f9b\u4f60\u5728 App \u4e2d\u4f7f\u7528\u3002\u5728 Xcode \u4e2d\u9009\u62e9 <code>SentimentClassifier.mlmodel<\/code> \u6587\u4ef6\u53ef\u4ee5\u67e5\u770b\u5173\u4e8e\u6a21\u578b\u7684\u66f4\u591a\u4fe1\u606f\u3002 <\/p>\n<p> \u5728\u81ea\u7136\u8bed\u8a00\u6846\u67b6\u4e2d\uff0c\u4ece <code>SentimentClassifier<\/code> \u521b\u5efa <span><code>NLModel<\/code><\/span>\uff0c\u786e\u4fdd\u6807\u8bb0\u5316\u5728\u8bad\u7ec3\u548c\u90e8\u7f72\u4e2d\u662f\u4e00\u81f4\u7684\u3002\u7136\u540e\uff0c\u4f7f\u7528 <code>predictedLabel(for:)<\/code> <span>(\u82f1\u6587)<\/span> \u6765\u751f\u6210\u65b0\u6587\u672c\u8f93\u5165\u7684\u9884\u6d4b\u3002 <\/p>\n<pre><code><span><span><\/span> <span><span>import<\/span> NaturalLanguage<\/span><\/span><span><span><\/span> <span><span>import<\/span> CoreML<\/span><\/span><span><span><\/span> <span> <\/span><\/span><span><span><\/span> <span><span>let<\/span> mlModel = <span>try<\/span> <span>SentimentClassifier<\/span>(configuration: <span>MLModelConfiguration<\/span>()).model<\/span><\/span><span><span><\/span> <span> <\/span><\/span><span><span><\/span> <span><span>let<\/span> sentimentPredictor = <span>try<\/span> <span>NLModel<\/span>(mlModel: mlModel)<\/span><\/span><span><span><\/span> <span>sentimentPredictor.predictedLabel(<span>for<\/span>: <span>\"It was the best I've ever seen!\"<\/span>)<\/span><\/span><\/code>\n\t\t\t\t\t\t<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u89c8 \u6587\u672c\u5206\u7c7b\u5668\u662f\u4e00\u79cd\u673a\u5668\u5b66\u4e60\u6a21\u578b\uff0c\u7ecf\u8fc7\u8bad\u7ec3\u5c06\u80fd\u591f\u8bc6\u522b\u81ea\u7136\u8bed\u8a00\u6587\u672c\u4e2d\u7684\u89c4\u5f8b\uff0c\u4f8b\u5982\u53e5\u5b50\u6240\u8868\u8fbe\u7684\u60c5\u7eea\u3002 \u8bad\u7ec3\u6587\u672c\u5206 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":190899,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[189686],"tags":[],"class_list":["post-190898","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/190898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=190898"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/190898\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/190899"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=190898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=190898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=190898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}