{"id":6045,"date":"2024-05-24T13:02:30","date_gmt":"2024-05-24T05:02:30","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6045"},"modified":"2024-05-24T13:05:34","modified_gmt":"2024-05-24T05:05:34","slug":"mltextclassifier-%e5%a6%82%e4%bd%95%e6%8f%90%e5%8d%87%e6%96%87%e6%9c%ac%e5%88%86%e7%b1%bb%e4%bb%bb%e5%8a%a1%e5%87%86%e7%a1%ae%e6%80%a7","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6045","title":{"rendered":"MLTextClassifier \u5982\u4f55\u63d0\u5347\u6587\u672c\u5206\u7c7b\u4efb\u52a1\u51c6\u786e\u6027"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><cite>\u6765\u81ea ChatGPT \u56de\u7b54\uff0c\u975e\u539f\u521b\uff0c\u6709\u70b9\u8dd1\u9898\uff0c\u4f46\u6709\u4e00\u4e9b\u53c2\u8003<\/cite><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4f7f\u7528 MacOS \u4e0a\u7684 <code>MLTextClassifier<\/code> \u6846\u67b6\u65f6\uff0c\u63d0\u5347\u6a21\u578b\u7684\u51c6\u786e\u7387\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u65b9\u9762\u7684\u4f18\u5316\u548c\u7b56\u7565\u6765\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u6570\u636e\u6e05\u7406\u4e0e\u9884\u5904\u7406<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u786e\u4fdd\u6570\u636e\u6e05\u6d01\u548c\u89c4\u8303\u5316\uff0c\u8fd9\u5bf9\u4e8e\u4efb\u4f55\u6587\u672c\u5206\u7c7b\u4efb\u52a1\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6570\u636e\u6e05\u7406<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53bb\u9664\u566a\u97f3<\/strong>\uff1a\u53bb\u9664HTML\u6807\u7b7e\u3001\u7279\u6b8a\u5b57\u7b26\u3001\u91cd\u590d\u5185\u5bb9\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u6807\u51c6\u5316<\/strong>\uff1a\u5c06\u6587\u672c\u7edf\u4e00\u4e3a\u5c0f\u5199\uff0c\u53bb\u9664\u591a\u4f59\u7a7a\u683c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\n<code><code>pythonCopy codeimport re\n\ndef clean_text(text):\n    text = re.sub(r'&lt;&#91;^>]+>', '', text)  # \u53bb\u9664HTML\u6807\u7b7e\n    text = re.sub(r'\\s+', ' ', text)     # \u53bb\u9664\u591a\u4f59\u7a7a\u683c\n    text = re.sub(r'&#91;^a-zA-Z0-9\\s]', '', text)  # \u53bb\u9664\u7279\u6b8a\u5b57\u7b26\n    return text.lower().strip()\n\ndf&#91;'textColumn'] = df&#91;'textColumn'].apply(clean_text)<\/code><\/code><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u6570\u636e\u589e\u5f3a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u540c\u4e49\u8bcd\u66ff\u6362<\/strong>\uff1a\u589e\u52a0\u6570\u636e\u7684\u591a\u6837\u6027\u3002<\/li>\n\n\n\n<li><strong>\u7ffb\u8bd1\u56de\u8bd1<\/strong>\uff1a\u5c06\u6587\u672c\u7ffb\u8bd1\u6210\u53e6\u4e00\u79cd\u8bed\u8a00\u518d\u7ffb\u8bd1\u56de\u6765\uff0c\u4ee5\u751f\u6210\u65b0\u7684\u6837\u672c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u7279\u5f81\u5de5\u7a0b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4f7f\u7528 <code>MLTextClassifier<\/code> \u65f6\uff0c\u53ef\u80fd\u6ca1\u6709\u76f4\u63a5\u64cd\u4f5c\u8bcd\u5411\u91cf\u7684\u80fd\u529b\uff0c\u4f46\u4f60\u53ef\u4ee5\u5728\u9884\u5904\u7406\u9636\u6bb5\u8fdb\u884c\u7279\u5f81\u5de5\u7a0b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bcd\u6c47\u8868\u793a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>N-gram<\/strong>\uff1a\u4e0d\u4ec5\u8003\u8651\u5355\u4e2a\u8bcd\uff0c\u8fd8\u53ef\u4ee5\u8003\u8651\u8bcd\u7ec4\uff08n-gram\uff09\uff0c\u5982\u4e8c\u5143\u7ec4\uff08bigrams\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>from sklearn.feature_extraction.text import CountVectorizer\n\nvectorizer = CountVectorizer(ngram_range=(1, 2))\nX = vectorizer.fit_transform(df['textColumn'])\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">TF-IDF<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>TF-IDF<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u7a81\u51fa\u91cd\u8981\u8bcd\u6c47\uff0c\u6291\u5236\u5e38\u89c1\u8bcd\u6c47\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>from sklearn.feature_extraction.text import TfidfVectorizer\n\nvectorizer = TfidfVectorizer(ngram_range=(1, 2), max_features=5000)\nX = vectorizer.fit_transform(df['textColumn'])\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u6a21\u578b\u4f18\u5316<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u53c2\u6570\u8c03\u6574<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8d85\u53c2\u6570\u8c03\u6574<\/strong>\uff1a\u5c1d\u8bd5\u4e0d\u540c\u7684\u8d85\u53c2\u6570\u7ec4\u5408\uff0c\u627e\u5230\u6700\u4f73\u53c2\u6570\u914d\u7f6e\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u589e\u52a0\u6570\u636e\u91cf<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u6269\u5145<\/strong>\uff1a\u5c3d\u53ef\u80fd\u589e\u52a0\u8bad\u7ec3\u6570\u636e\u91cf\uff0c\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u6570\u636e\u96c6\u5e73\u8861<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5904\u7406\u7c7b\u522b\u4e0d\u5e73\u8861<\/strong>\uff1a\u4f7f\u7528\u6b20\u91c7\u6837\u6216\u8fc7\u91c7\u6837\u6280\u672f\u5904\u7406\u7c7b\u522b\u4e0d\u5e73\u8861\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>from imblearn.over_sampling import SMOTE\n\nsm = SMOTE(random_state=42)\nX_res, y_res = sm.fit_resample(X, y)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u867d\u7136 <code>MLTextClassifier<\/code> \u662f\u4e00\u4e2a\u9ad8\u5c42API\uff0c\u4e0d\u80fd\u76f4\u63a5\u66ff\u6362\u5e95\u5c42\u6a21\u578b\uff0c\u4f46\u4f60\u53ef\u4ee5\u8003\u8651\u9884\u8bad\u7ec3\u7684\u8bcd\u5d4c\u5165\u6a21\u578b\u6765\u589e\u5f3a\u7279\u5f81\u8868\u793a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u4ea4\u53c9\u9a8c\u8bc1\u4e0e\u6a21\u578b\u8bc4\u4f30<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u786e\u4fdd\u6a21\u578b\u5728\u4e0d\u540c\u6570\u636e\u5b50\u96c6\u4e0a\u8868\u73b0\u4e00\u81f4\uff0c\u907f\u514d\u8fc7\u62df\u5408\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4ea4\u53c9\u9a8c\u8bc1<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>K\u6298\u4ea4\u53c9\u9a8c\u8bc1<\/strong>\uff1a\u786e\u4fdd\u6a21\u578b\u5728\u4e0d\u540c\u6570\u636e\u5b50\u96c6\u4e0a\u8868\u73b0\u4e00\u81f4\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>from sklearn.model_selection import cross_val_score\nfrom sklearn.ensemble import RandomForestClassifier\n\nclf = RandomForestClassifier(n_estimators=100, max_depth=10, random_state=42)\nscores = cross_val_score(clf, X_res, y_res, cv=5)\nprint(scores)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u4f7f\u7528 <code>MLTextClassifier<\/code> \u7684\u589e\u5f3a\u529f\u80fd<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>MLTextClassifier<\/code> \u53ef\u80fd\u6709\u4e00\u4e9b\u7279\u5b9a\u7684\u589e\u5f3a\u529f\u80fd\u6216\u53c2\u6570\u914d\u7f6e\uff0c\u53c2\u8003\u6846\u67b6\u6587\u6863\uff0c\u786e\u4fdd\u4f60\u5229\u7528\u4e86\u6240\u6709\u4f18\u5316\u9009\u9879\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7efc\u5408\u5e94\u7528<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u6e05\u7406\u548c\u9884\u5904\u7406<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u6e05\u7406\u566a\u97f3\uff0c\u7edf\u4e00\u6587\u672c\u683c\u5f0f\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528N-gram\u548cTF-IDF\u8fdb\u884c\u7279\u5f81\u5de5\u7a0b\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u6570\u636e\u96c6\u5e73\u8861<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528SMOTE\u7b49\u6280\u672f\u5e73\u8861\u7c7b\u522b\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u6a21\u578b\u8bad\u7ec3\u548c\u9a8c\u8bc1<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u4ea4\u53c9\u9a8c\u8bc1\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u3002<\/li>\n\n\n\n<li>\u5c1d\u8bd5\u4e0d\u540c\u7684\u53c2\u6570\u548c\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>import pandas as pd\nimport re\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.model_selection import train_test_split\nfrom imblearn.over_sampling import SMOTE\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import cross_val_score\n\n# \u6570\u636e\u6e05\u7406\u51fd\u6570\ndef clean_text(text):\n    text = re.sub(r'&lt;[^&gt;]+&gt;', '', text)\n    text = re.sub(r'\\s+', ' ', text)\n    text = re.sub(r'[^a-zA-Z0-9\\s]', '', text)\n    return text.lower().strip()\n\n# \u52a0\u8f7d\u6570\u636e\ndf = pd.read_csv('data\/dataset.csv')\n\n# \u6570\u636e\u6e05\u7406\ndf['textColumn'] = df['textColumn'].apply(clean_text)\n\n# \u7279\u5f81\u5de5\u7a0b\nvectorizer = TfidfVectorizer(ngram_range=(1, 2), max_features=5000)\nX = vectorizer.fit_transform(df['textColumn'])\ny = df['labelColumn']\n\n# \u6570\u636e\u5206\u5272\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# \u6570\u636e\u5e73\u8861\nsm = SMOTE(random_state=42)\nX_res, y_res = sm.fit_resample(X_train, y_train)\n\n# \u6a21\u578b\u8bad\u7ec3\u4e0e\u8bc4\u4f30\nclf = RandomForestClassifier(n_estimators=100, max_depth=10, random_state=42)\nscores = cross_val_score(clf, X_res, y_res, cv=5)\nprint(scores)\n\n# \u6700\u7ec8\u6a21\u578b\u8bad\u7ec3\nclf.fit(X_res, y_res)\n\n# \u6a21\u578b\u8bc4\u4f30\naccuracy = clf.score(X_test, y_test)\nprint(f'Test Accuracy: {accuracy}')\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u7b56\u7565\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u5347 <code>MLTextClassifier<\/code> \u7684\u51c6\u786e\u7387\u3002\u5177\u4f53\u91c7\u7528\u54ea\u4e9b\u65b9\u6cd5\uff0c\u9700\u6839\u636e\u6570\u636e\u7684\u7279\u70b9\u548c\u5b9e\u9645\u95ee\u9898\u8fdb\u884c\u8c03\u6574\u548c\u4f18\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6765\u81ea ChatGPT \u56de\u7b54\uff0c\u975e\u539f\u521b\uff0c\u6709\u70b9\u8dd1\u9898\uff0c\u4f46\u6709\u4e00\u4e9b\u53c2\u8003 \u5728\u4f7f\u7528 MacOS \u4e0a\u7684 MLTextClassi&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6045\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[53,54,52,51],"class_list":["post-6045","post","type-post","status-publish","format-standard","hentry","category-5","tag-llm","tag-54","tag-52","tag-51"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6045"}],"version-history":[{"count":2,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6045\/revisions"}],"predecessor-version":[{"id":6048,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6045\/revisions\/6048"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}