{"id":6053,"date":"2024-05-28T12:49:00","date_gmt":"2024-05-28T04:49:00","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6053"},"modified":"2024-05-28T12:49:00","modified_gmt":"2024-05-28T04:49:00","slug":"%e6%95%b0%e6%8d%ae%e5%9c%a8%e9%9d%9e%e5%85%b3%e9%94%ae%e7%89%b9%e5%be%81%e4%b8%8a%e7%9b%b8%e4%bc%bc%e7%9a%84%e5%a4%aa%e5%a4%9a%ef%bc%8c%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6053","title":{"rendered":"\u6570\u636e\u5728\u975e\u5173\u952e\u7279\u5f81\u4e0a\u76f8\u4f3c\u7684\u592a\u591a\uff0c\u5982\u4f55\u5904\u7406"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><\/p>\n<cite>\u975e\u539f\u521b\uff0c\u7b54\u6848\u6765\u81ea ChatGPT<\/cite><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6a21\u578b\u8bad\u7ec3\u6570\u636e\u96c6\u4e2d\uff0c\u5982\u679c\u6570\u636e\u5728\u975e\u5173\u952e\u7279\u5f81\u4e0a\u76f8\u4f3c\u5ea6\u592a\u9ad8\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6a21\u578b\u7684\u8fc7\u62df\u5408\uff0c\u964d\u4f4e\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b\u3002\u5904\u7406\u8fd9\u4e9b\u5197\u4f59\u6570\u636e\u53ef\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u548c\u8bad\u7ec3\u6548\u7387\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u6570\u636e\u53bb\u91cd<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u786e\u4fdd\u6570\u636e\u96c6\u4e2d\u6ca1\u6709\u5b8c\u5168\u91cd\u590d\u7684\u6837\u672c\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u7684<code>drop_duplicates<\/code>\u65b9\u6cd5\u8fdb\u884c\u53bb\u91cd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\n\n# \u5047\u8bbedf\u662f\u4f60\u7684\u6570\u636e\u96c6\ndf = df.drop_duplicates()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u805a\u7c7b\u53bb\u91cd<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u5728\u975e\u5173\u952e\u7279\u5f81\u4e0a\u76f8\u4f3c\u7684\u6837\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u805a\u7c7b\u7b97\u6cd5\u6765\u8bc6\u522b\u548c\u5408\u5e76\u76f8\u4f3c\u6837\u672c\u3002\u5e38\u7528\u7684\u805a\u7c7b\u7b97\u6cd5\u6709K-Means\u3001DBSCAN\u7b49\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528DBSCAN\u805a\u7c7b\u53bb\u91cd<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">DBSCAN\u53ef\u4ee5\u81ea\u52a8\u627e\u5230\u6570\u636e\u4e2d\u7684\u9ad8\u5bc6\u5ea6\u533a\u57df\uff0c\u5e76\u5c06\u4f4e\u5bc6\u5ea6\u533a\u57df\u89c6\u4e3a\u566a\u58f0\u3002\u9002\u7528\u4e8e\u5904\u7406\u975e\u7ebf\u6027\u8fb9\u754c\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.cluster import DBSCAN\nfrom sklearn.preprocessing import StandardScaler\n\n# \u5047\u8bbedf\u662f\u4f60\u7684\u6570\u636e\u96c6\uff0c\u5e76\u4e14\u5df2\u7ecf\u63d0\u53d6\u4e86\u7279\u5f81\u5217\u5230features\nfeatures = df&#91;&#91;'feature1', 'feature2', 'feature3']].values  # \u66ff\u6362\u4e3a\u5b9e\u9645\u7684\u975e\u5173\u952e\u7279\u5f81\u5217\n\n# \u6807\u51c6\u5316\u7279\u5f81\u503c\nscaler = StandardScaler()\nfeatures_scaled = scaler.fit_transform(features)\n\n# \u5e94\u7528DBSCAN\ndbscan = DBSCAN(eps=0.5, min_samples=5)\nclusters = dbscan.fit_predict(features_scaled)\n\n# \u5c06\u7c07\u6807\u7b7e\u6dfb\u52a0\u5230\u6570\u636e\u96c6\u4e2d\ndf&#91;'cluster'] = clusters\n\n# \u9009\u62e9\u6bcf\u4e2a\u7c07\u4e2d\u7684\u4e00\u4e2a\u6837\u672c\u4f5c\u4e3a\u4ee3\u8868\uff0c\u53bb\u9664\u5176\u4f59\u6837\u672c\ndf_unique = df.drop_duplicates(subset='cluster', keep='first')\ndf_unique = df_unique.drop(columns='cluster')<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4f7f\u7528PCA\u964d\u7ef4<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u7279\u5f81\u7ef4\u5ea6\u8f83\u9ad8\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e3b\u6210\u5206\u5206\u6790\uff08PCA\uff09\u6765\u964d\u4f4e\u7ef4\u5ea6\uff0c\u4fdd\u7559\u4e3b\u8981\u4fe1\u606f\uff0c\u51cf\u5c11\u5197\u4f59\u7279\u5f81\u7684\u5f71\u54cd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.decomposition import PCA\n\n# \u5047\u8bbedf\u662f\u4f60\u7684\u6570\u636e\u96c6\uff0c\u5e76\u4e14\u5df2\u7ecf\u63d0\u53d6\u4e86\u7279\u5f81\u5217\u5230features\nfeatures = df&#91;&#91;'feature1', 'feature2', 'feature3']].values  # \u66ff\u6362\u4e3a\u5b9e\u9645\u7684\u7279\u5f81\u5217\n\n# \u5e94\u7528PCA\npca = PCA(n_components=2)  # \u5c06\u7ef4\u5ea6\u964d\u4f4e\u52302\u7ef4\nfeatures_pca = pca.fit_transform(features)\n\n# \u5c06\u964d\u7ef4\u540e\u7684\u7279\u5f81\u52a0\u5165\u6570\u636e\u96c6\u4e2d\ndf&#91;'pca1'] = features_pca&#91;:, 0]\ndf&#91;'pca2'] = features_pca&#91;:, 1]\n\n# \u7136\u540e\u53ef\u4ee5\u4f7f\u7528\u805a\u7c7b\u6216\u5176\u4ed6\u65b9\u6cd5\u8fdb\u4e00\u6b65\u53bb\u91cd<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u6570\u636e\u589e\u5f3a\u548c\u5408\u6210<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u5173\u952e\u7279\u5f81\u6837\u672c\u8f83\u5c11\uff0c\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u589e\u5f3a\u548c\u5408\u6210\u6765\u589e\u52a0\u6837\u672c\u591a\u6837\u6027\u3002\u8fd9\u5728\u56fe\u50cf\u548c\u6587\u672c\u6570\u636e\u5904\u7406\u4e2d\u5c24\u5176\u5e38\u89c1\u3002\u4f8b\u5982\uff0c\u4f7f\u7528SMOTE\uff08Synthetic Minority Over-sampling Technique\uff09\u6765\u751f\u6210\u5408\u6210\u6837\u672c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from imblearn.over_sampling import SMOTE\n\n# \u5047\u8bbedf\u662f\u4f60\u7684\u6570\u636e\u96c6\uff0cX\u662f\u7279\u5f81\uff0cy\u662f\u6807\u7b7e\nX = df.drop(columns='label').values\ny = df&#91;'label'].values\n\nsmote = SMOTE()\nX_resampled, y_resampled = smote.fit_resample(X, y)\n\n# \u5c06\u751f\u6210\u7684\u6570\u636e\u8f6c\u6362\u56deDataFrame\ndf_resampled = pd.DataFrame(X_resampled, columns=df.columns&#91;:-1])\ndf_resampled&#91;'label'] = y_resampled<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u6837\u672c\u52a0\u6743<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u67d0\u4e9b\u573a\u666f\uff0c\u53ef\u4ee5\u5bf9\u6837\u672c\u8fdb\u884c\u52a0\u6743\uff0c\u4f7f\u5f97\u6a21\u578b\u66f4\u5173\u6ce8\u5173\u952e\u6837\u672c\u3002\u8fd9\u6837\uff0c\u867d\u7136\u76f8\u4f3c\u6837\u672c\u5b58\u5728\uff0c\u4f46\u5b83\u4eec\u7684\u5f71\u54cd\u88ab\u51cf\u5f31\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.utils.class_weight import compute_sample_weight\n\n# \u8ba1\u7b97\u6bcf\u4e2a\u6837\u672c\u7684\u6743\u91cd\nsample_weights = compute_sample_weight(class_weight='balanced', y=df&#91;'label'])\n\n# \u5728\u8bad\u7ec3\u6a21\u578b\u65f6\u4f7f\u7528\u6837\u672c\u6743\u91cd\nmodel.fit(X, y, sample_weight=sample_weights)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u7279\u5f81\u9009\u62e9<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u975e\u5173\u952e\u7279\u5f81\u8fc7\u591a\uff0c\u53ef\u4ee5\u901a\u8fc7\u7279\u5f81\u9009\u62e9\u65b9\u6cd5\u6765\u9009\u62e9\u6700\u91cd\u8981\u7684\u7279\u5f81\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u57fa\u4e8e\u6811\u6a21\u578b\u7684\u7279\u5f81\u91cd\u8981\u6027\u6216\u57fa\u4e8e\u7edf\u8ba1\u65b9\u6cd5\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.ensemble import RandomForestClassifier\nfrom sklearn.feature_selection import SelectFromModel\n\n# \u5047\u8bbedf\u662f\u4f60\u7684\u6570\u636e\u96c6\uff0cX\u662f\u7279\u5f81\uff0cy\u662f\u6807\u7b7e\nX = df.drop(columns='label').values\ny = df&#91;'label'].values\n\n# \u4f7f\u7528\u968f\u673a\u68ee\u6797\u8fdb\u884c\u7279\u5f81\u9009\u62e9\nrf = RandomForestClassifier()\nrf.fit(X, y)\n\n# \u9009\u62e9\u91cd\u8981\u7279\u5f81\nselector = SelectFromModel(rf, threshold='median')\nX_important = selector.transform(X)\n\n# \u5c06\u9009\u62e9\u7684\u7279\u5f81\u8f6c\u6362\u56deDataFrame\ndf_important = pd.DataFrame(X_important, columns=df.columns&#91;selector.get_support()])\ndf_important&#91;'label'] = y<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u6570\u636e\u96c6\u4e2d\u5728\u975e\u5173\u952e\u7279\u5f81\u4e0a\u7684\u76f8\u4f3c\u5ea6\uff0c\u4ece\u800c\u63d0\u9ad8\u6a21\u578b\u7684\u8bad\u7ec3\u6548\u679c\u548c\u6cdb\u5316\u80fd\u529b\u3002\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u5177\u4f53\u60c5\u51b5\u548c\u4efb\u52a1\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\uff0c\u7b54\u6848\u6765\u81ea ChatGPT \u5728\u6a21\u578b\u8bad\u7ec3\u6570\u636e\u96c6\u4e2d\uff0c\u5982\u679c\u6570\u636e\u5728\u975e\u5173\u952e\u7279\u5f81\u4e0a\u76f8\u4f3c\u5ea6\u592a\u9ad8\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6a21\u578b\u7684\u8fc7\u62df&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6053\">\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":[55,56,51],"class_list":["post-6053","post","type-post","status-publish","format-standard","hentry","category-5","tag-55","tag-56","tag-51"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6053","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=6053"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6053\/revisions"}],"predecessor-version":[{"id":6054,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6053\/revisions\/6054"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}