{"id":3072,"date":"2020-05-18T07:21:55","date_gmt":"2020-05-18T07:21:55","guid":{"rendered":""},"modified":"2020-05-18T15:23:41","modified_gmt":"2020-05-18T07:23:41","slug":"%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%e7%9a%845%e7%a7%8d%e9%87%87%e6%a0%b7%e6%96%b9%e6%b3%95%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"http:\/\/www.szryc.com\/?p=3072","title":{"rendered":"\u673a\u5668\u5b66\u4e60\u76845\u79cd\u91c7\u6837\u65b9\u6cd5\u4ecb\u7ecd"},"content":{"rendered":"

\n\t\u4ee5\u4e0b\u662f\u5173\u4e8eRahul Agarwal \u5206\u4eab\u7684\u5185\u5bb9\uff0c\u7f16\u8bd1\u6574\u7406\u5982\u4e0b\u3002<\/p>\n

\n\t\u6570\u636e\u79d1\u5b66\u5b9e\u9645\u4e0a\u662f\u5c31\u662f\u7814\u7a76\u7b97\u6cd5\u3002<\/p>\n

\n\t <\/div>\n

\n\t\u6211\u6bcf\u5929\u90fd\u5728\u52aa\u529b\u5b66\u4e60\u8bb8\u591a\u7b97\u6cd5\uff0c\u6240\u4ee5\u6211\u60f3\u5217\u51fa\u4e00\u4e9b\u6700\u5e38\u89c1\u548c\u6700\u5e38\u7528\u7684\u7b97\u6cd5\u3002<\/p>\n

\n\t\u672c\u6587\u4ecb\u7ecd\u4e86\u5728\u5904\u7406\u6570\u636e\u65f6\u53ef\u4ee5\u4f7f\u7528\u7684\u4e00\u4e9b\u6700\u5e38\u89c1\u7684\u91c7\u6837\u6280\u672f\u3002<\/p>\n

\n\t\u7b80\u5355\u968f\u673a\u62bd\u6837<\/p>\n

\n\t\u5047\u8bbe\u60a8\u8981\u9009\u62e9\u4e00\u4e2a\u7fa4\u4f53\u7684\u5b50\u96c6\uff0c\u5176\u4e2d\u8be5\u5b50\u96c6\u7684\u6bcf\u4e2a\u6210\u5458\u88ab\u9009\u62e9\u7684\u6982\u7387\u90fd\u76f8\u7b49\u3002<\/p>\n

\n\t\u4e0b\u9762\u6211\u4eec\u4ece\u4e00\u4e2a\u6570\u636e\u96c6\u4e2d\u9009\u62e9 100 \u4e2a\u91c7\u6837\u70b9\u3002<\/p>\n

\n\tsample_df = df.sample\uff08100\uff09<\/p>\n

\n\t\u5206\u5c42\u91c7\u6837<\/p>\n

\n\t\"\u673a\u5668\u5b66\u4e60\u76845\u79cd\u91c7\u6837\u65b9\u6cd5\u4ecb\u7ecd\"<\/p>\n

\n\t\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4f30\u8ba1\u9009\u4e3e\u4e2d\u6bcf\u4e2a\u5019\u9009\u4eba\u7684\u5e73\u5747\u7968\u6570\u3002\u73b0\u5047\u8bbe\u8be5\u56fd\u6709 3 \u4e2a\u57ce\u9547\uff1a<\/p>\n

\n\tA \u9547\u6709 100 \u4e07\u5de5\u4eba\uff0c<\/p>\n

\n\tB \u9547\u6709 200 \u4e07\u5de5\u4eba\uff0c\u4ee5\u53ca<\/p>\n

\n\tC \u9547\u6709 300 \u4e07\u9000\u4f11\u4eba\u5458\u3002<\/p>\n

\n\t\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u5728\u6574\u4e2a\u4eba\u53e3\u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a 60 \u5927\u5c0f\u7684\u6837\u672c\uff0c\u4f46\u5728\u8fd9\u4e9b\u57ce\u9547\u4e2d\uff0c\u968f\u673a\u6837\u672c\u53ef\u80fd\u4e0d\u592a\u5e73\u8861\uff0c\u56e0\u6b64\u4f1a\u4ea7\u751f\u504f\u5dee\uff0c\u5bfc\u81f4\u4f30\u8ba1\u8bef\u5dee\u5f88\u5927\u3002<\/p>\n

\n\t\u76f8\u53cd\uff0c\u5982\u679c\u6211\u4eec\u9009\u62e9\u4ece A\u3001B \u548c C \u9547\u5206\u522b\u62bd\u53d6 10\u300120 \u548c 30 \u4e2a\u968f\u673a\u6837\u672c\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5728\u603b\u6837\u672c\u5927\u5c0f\u76f8\u540c\u7684\u60c5\u51b5\u4e0b\uff0c\u4ea7\u751f\u8f83\u5c0f\u7684\u4f30\u8ba1\u8bef\u5dee\u3002<\/p>\n

\n\t\u4f7f\u7528 python<\/u> \u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u505a\u5230\u8fd9\u4e00\u70b9\uff1a<\/p>\n

\n\tfrom<\/u> sklearn.model_selecti<\/u>on import trai<\/u>n_te<\/u>st_splitX_train\uff0c X_test\uff0c y_train\uff0c y_test = train_test_split\uff08X\uff0c y\uff0c stratif<\/u>y=y\uff0c test_size=0.25\uff09<\/p>\n

\n\t\u6c34\u5858\u91c7\u6837<\/p>\n

\n\t\u6211\u559c\u6b22\u8fd9\u4e2a\u95ee\u9898\u9648\u8ff0\uff1a<\/p>\n

\n\t\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u9879\u76ee\u6d41\uff0c\u5b83\u957f\u5ea6\u8f83\u5927\u4e14\u672a\u77e5\u4ee5\u81f3\u4e8e\u6211\u4eec\u53ea\u80fd\u8fed\u4ee3\u4e00\u6b21\u3002<\/p>\n

\n\t\u521b\u5efa\u4e00\u4e2a\u7b97\u6cd5\uff0c\u4ece\u8fd9\u4e2a\u6d41\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u9879\u76ee\uff0c\u8fd9\u6837\u6bcf\u4e2a\u9879\u76ee\u90fd\u6709\u76f8\u540c\u7684\u53ef\u80fd\u88ab\u9009\u4e2d\u3002<\/p>\n

\n\t\u6211\u4eec\u600e\u4e48\u80fd\u505a\u5230\u8fd9\u4e00\u70b9\uff1f<\/p>\n

\n\t\u5047\u8bbe\u6211\u4eec\u5fc5\u987b\u4ece\u65e0\u9650\u5927\u7684\u6d41\u4e2d\u62bd\u53d6 5 \u4e2a\u5bf9\u8c61\uff0c\u4e14\u6bcf\u4e2a\u5143\u7d20\u88ab\u9009\u4e2d\u7684\u6982\u7387\u90fd\u76f8\u7b49\u3002<\/p>\n

\n\timport randomdef generator\uff08max\uff09\uff1a<\/p>\n

\n\tnumber = 1<\/p>\n

\n\twhile number \u300a max\uff1a<\/p>\n

\n\tnumber += 1<\/p>\n

\n\tyield number# Create as stream generator<\/p>\n

\n\tstream = generator\uff0810000\uff09# Doing Reservoir Sampling from the stream<\/p>\n

\n\tk=5<\/p>\n

\n\treservoir = \uff3b\uff3d<\/p>\n

\n\tfor i\uff0c element in enumerate\uff08stream\uff09\uff1a<\/p>\n

\n\tif i+1\u300a= k\uff1a<\/p>\n

\n\treservoir.append\uff08element\uff09<\/p>\n

\n\telse\uff1a<\/p>\n

\n\tprobability = k\/\uff08i+1\uff09<\/p>\n

\n\tif random.random\uff08\uff09 \u300a probability\uff1a<\/p>\n

\n\t# Select item in stream and remove one of the k items already selected<\/p>\n

\n\treservoir\uff3brandom.choice\uff08range\uff080\uff0ck\uff09\uff09\uff3d = elementprint\uff08reservoir\uff09<\/p>\n

\n\t------------------------------------<\/p>\n

\n\t\uff3b1369\uff0c 4108\uff0c 9986\uff0c 828\uff0c 5589\uff3d<\/p>\n

\n\t\u4ece\u6570\u5b66\u4e0a\u53ef\u4ee5\u8bc1\u660e\uff0c\u5728\u6837\u672c\u4e2d\uff0c\u6d41\u4e2d\u6bcf\u4e2a\u5143\u7d20\u88ab\u9009\u4e2d\u7684\u6982\u7387\u76f8\u540c\u3002\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f<\/p>\n

\n\t\u5f53\u6d89\u53ca\u5230\u6570\u5b66\u95ee\u9898\u65f6\uff0c\u4ece\u4e00\u4e2a\u5c0f\u95ee\u9898\u5f00\u59cb\u601d\u8003\u603b\u662f\u6709\u5e2e\u52a9\u7684\u3002<\/p>\n

\n\t\u6240\u4ee5\uff0c\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u53ea\u6709 3 \u4e2a\u9879\u76ee\u7684\u6d41\uff0c\u6211\u4eec\u5fc5\u987b\u4fdd\u7559\u5176\u4e2d 2 \u4e2a\u3002<\/p>\n

\n\t\u5f53\u6211\u4eec\u770b\u5230\u7b2c\u4e00\u4e2a\u9879\u76ee\uff0c\u6211\u4eec\u628a\u5b83\u653e\u5728\u6e05\u5355\u4e0a\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u6c34\u5858\u6709\u7a7a\u95f4\u3002\u5728\u6211\u4eec\u770b\u5230\u7b2c\u4e8c\u4e2a\u9879\u76ee\u65f6\uff0c\u6211\u4eec\u628a\u5b83\u653e\u5728\u5217\u8868\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u6c34\u5858\u8fd8\u662f\u6709\u7a7a\u95f4\u3002<\/p>\n

\n\t\u73b0\u5728\u6211\u4eec\u770b\u5230\u7b2c\u4e09\u4e2a\u9879\u76ee\u3002\u8fd9\u91cc\u662f\u4e8b\u60c5\u5f00\u59cb\u53d8\u5f97\u6709\u8da3\u7684\u5730\u65b9\u3002\u6211\u4eec\u6709 2\/3 \u7684\u6982\u7387\u5c06\u7b2c\u4e09\u4e2a\u9879\u76ee\u653e\u5728\u6e05\u5355\u4e2d\uff0c<\/p>\n

\n\t\u73b0\u5728\u8ba9\u6211\u4eec\u770b\u770b\u7b2c\u4e00\u4e2a\u9879\u76ee\u88ab\u9009\u4e2d\u7684\u6982\u7387\uff1a<\/p>\n

\n\t\u79fb\u9664\u7b2c\u4e00\u4e2a\u9879\u76ee\u7684\u6982\u7387\u662f\u9879\u76ee 3 \u88ab\u9009\u4e2d\u7684\u6982\u7387\u4e58\u4ee5\u9879\u76ee 1 \u88ab\u968f\u673a\u9009\u4e3a\u6c34\u5858\u4e2d 2 \u4e2a\u8981\u7d20\u7684\u66ff\u4ee3\u5019\u9009\u7684\u6982\u7387\u3002\u8fd9\u4e2a\u6982\u7387\u662f\uff1a<\/p>\n

\n\t2\/3*1\/2 = 1\/3<\/p>\n

\n\t\u56e0\u6b64\uff0c\u9009\u62e9\u9879\u76ee 1 \u7684\u6982\u7387\u4e3a\uff1a<\/p>\n

\n\t1–1\/3=2\/3<\/p>\n

\n\t\u6211\u4eec\u53ef\u4ee5\u5bf9\u7b2c\u4e8c\u4e2a\u9879\u76ee\u4f7f\u7528\u5b8c\u5168\u76f8\u540c\u7684\u53c2\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u5c06\u5176\u6269\u5c55\u5230\u591a\u4e2a\u9879\u76ee\u3002<\/p>\n

\n\t\u56e0\u6b64\uff0c\u6bcf\u4e2a\u9879\u76ee\u88ab\u9009\u4e2d\u7684\u6982\u7387\u76f8\u540c\uff1a2\/3 \u6216\u8005\u7528\u4e00\u822c\u7684\u516c\u5f0f\u8868\u793a\u4e3a K\/N<\/p>\n

\n\t\u968f\u673a\u6b20\u91c7\u6837\u548c\u8fc7\u91c7\u6837<\/p>\n

\n\t\"\u673a\u5668\u5b66\u4e60\u76845\u79cd\u91c7\u6837\u65b9\u6cd5\u4ecb\u7ecd\"<\/p>\n

\n\t\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e0d\u5e73\u8861\u7684\u6570\u636e\u96c6\u3002<\/p>\n

\n\t\u4e00\u79cd\u5e7f\u6cdb\u91c7\u7528\u7684\u5904\u7406\u9ad8\u5ea6\u4e0d\u5e73\u8861\u6570\u636e\u96c6\u7684\u6280\u672f\u79f0\u4e3a\u91cd\u91c7\u6837\u3002\u5b83\u5305\u62ec\u4ece\u591a\u6570\u7c7b\uff08\u6b20\u91c7\u6837\uff09\u4e2d\u5220\u9664\u6837\u672c\u6216\u5411\u5c11\u6570\u7c7b\uff08\u8fc7\u91c7\u6837\uff09\u4e2d\u6dfb\u52a0\u66f4\u591a\u793a\u4f8b\u3002<\/p>\n

\n\t\u8ba9\u6211\u4eec\u5148\u521b\u5efa\u4e00\u4e9b\u4e0d\u5e73\u8861\u6570\u636e\u793a\u4f8b\uff0c<\/p>\n

\n\tfrom sklearn.datasets import make_classificaTIonX\uff0c y = make_classificaTIon\uff08 n_classes=2\uff0c class_sep=1.5\uff0c weights=\uff3b0.9\uff0c 0.1\uff3d\uff0c n_informaTIve=3\uff0c n_redundant=1\uff0c flip_y=0\uff0c n_features=20\uff0c n_clusters<\/u>_per_class=1\uff0c n_samples=100\uff0c random_state=10\uff09X = pd.DataFram<\/u>e\uff08X\uff09X\uff3b target \uff3d = y<\/p>\n

\n\t\u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u968f\u673a\u8fc7\u91c7\u6837\u548c\u6b20\u91c7\u6837\uff1a<\/p>\n

\n\tnum_0 = len\uff08X\uff3bX\uff3b target \uff3d==0\uff3d\uff09num_1 = len\uff08X\uff3bX\uff3b target \uff3d==1\uff3d\uff09print\uff08num_0\uff0cnum_1\uff09# random undersampleundersampled<\/u>_data = pd.concat\uff08\uff3b X\uff3bX\uff3b target \uff3d==0\uff3d.sample\uff08num_1\uff09 \uff0c X\uff3bX\uff3b target \uff3d==1\uff3d \uff3d\uff09print\uff08len\uff08undersampled_data\uff09\uff09# random oversampleoversampled_data = pd.concat\uff08\uff3b X\uff3bX\uff3b target \uff3d==0\uff3d \uff0c X\uff3bX\uff3b target \uff3d==1\uff3d.sample\uff08num_0\uff0c replace=True\uff09 \uff3d\uff09print\uff08len\uff08oversampled_data\uff09\uff09------------------------------------------------------------OUTPUT:90 1020180<\/p>\n

\n\t\u4f7f\u7528 imbalanced-learn \u8fdb\u884c\u6b20\u91c7\u6837\u548c\u8fc7\u91c7\u6837<\/p>\n

\n\timbalanced-learn\uff08imblearn\uff09\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u51b3\u4e0d\u5e73\u8861\u6570\u636e\u96c6\u95ee\u9898\u7684 python \u5305\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8fdb\u884c\u6b20\u91c7\u6837\u548c\u8fc7\u91c7\u6837\u3002<\/p>\n

\n\ta. \u4f7f\u7528 Tomek Links \u8fdb\u884c\u6b20\u91c7\u6837\uff1a<\/p>\n

\n\timbalanced-learn \u63d0\u4f9b\u7684\u4e00\u79cd\u65b9\u6cd5\u53eb\u505a Tomek Links\u3002Tomek Links \u662f\u90bb\u8fd1\u7684\u4e24\u4e2a\u76f8\u53cd\u7c7b\u7684\u4f8b\u5b50\u3002<\/p>\n

\n\t\u5728\u8fd9\u4e2a\u7b97\u6cd5\u4e2d\uff0c\u6211\u4eec\u6700\u7ec8\u4ece Tomek Links \u4e2d\u5220\u9664\u4e86\u5927\u591a\u6570\u5143\u7d20\uff0c\u8fd9\u4e3a\u5206\u7c7b\u5668\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u597d\u7684\u51b3\u7b56\u8fb9\u754c\u3002<\/p>\n

\n\t\"\u673a\u5668\u5b66\u4e60\u76845\u79cd\u91c7\u6837\u65b9\u6cd5\u4ecb\u7ecd\"<\/p>\n

\n\tfrom imblearn.under_sampling import TomekLinks<\/p>\n

\n\ttl = TomekLinks\uff08return_indices=True\uff0c raTIo= majority \uff09<\/p>\n

\n\tX_tl\uff0c y_tl\uff0c id_tl = tl.fit_sample\uff08X\uff0c y\uff09<\/p>\n

\n\tb. \u4f7f\u7528 SMOTE \u8fdb\u884c\u8fc7\u91c7\u6837\uff1a<\/p>\n

\n\t\u5728 SMOE\uff08Synthetic Mi<\/u>nority Oversampling Technique\uff09\u4e2d\uff0c\u6211\u4eec\u5728\u73b0\u6709\u5143\u7d20\u9644\u8fd1\u5408\u5e76\u5c11\u6570\u7c7b\u7684\u5143\u7d20\u3002<\/p>\n

\n\tfrom imblearn.over_sampling import SMOTE<\/p>\n

\n\tsmote = SMOTE\uff08ratio= minority \uff09<\/p>\n

\n\tX_sm\uff0c y_sm = smote.fit_sample\uff08X\uff0c y\uff09<\/p>\n

\n\timbLearn \u5305\u4e2d\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u6b20\u91c7\u6837\uff08Cluster Centroids\uff0c NearM<\/u>iss \u7b49\uff09\u548c\u8fc7\u91c7\u6837\uff08ADASYN \u548c bSMOTE\uff09\u3002<\/p>\n

\n\t\u7ed3\u8bba<\/p>\n

\n\t\u7b97\u6cd5\u662f\u6570\u636e\u79d1\u5b66\u7684\u751f\u547d\u7ebf\u3002<\/p>\n

\n\t\u62bd\u6837\u662f\u6570\u636e\u79d1\u5b66\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u8bfe\u9898\uff0c\u4f46\u6211\u4eec\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u8ba8\u8bba\u5f97\u8db3\u591f\u591a\u3002<\/p>\n

\n\t\u6709\u65f6\uff0c\u4e00\u4e2a\u597d\u7684\u62bd\u6837\u7b56\u7565\u4f1a\u5927\u5927\u63a8\u8fdb\u9879\u76ee\u7684\u8fdb\u5c55\u3002\u9519\u8bef\u7684\u62bd\u6837\u7b56\u7565\u53ef\u80fd\u4f1a\u7ed9\u6211\u4eec\u5e26\u6765\u9519\u8bef\u7684\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u5728\u9009\u62e9\u62bd\u6837\u7b56\u7565\u65f6\u5e94\u8be5\u5c0f\u5fc3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4ee5\u4e0b\u662f\u5173\u4e8eRahul Agarwal \u5206\u4eab\u7684\u5185\u5bb9\uff0c\u7f16\u8bd1\u6574\u7406\u5982\u4e0b\u3002 \u6570\u636e\u79d1\u5b66\u5b9e\u9645\u4e0a\u662f\u5c31\u662f\u7814\u7a76\u7b97\u6cd5\u3002 \u6211\u6bcf\u5929\u90fd\u5728\u52aa\u529b\u5b66\u4e60\u8bb8\u591a\u7b97\u6cd5\uff0c\u6240\u4ee5\u6211\u60f3\u5217\u51fa\u4e00\u4e9b\u6700\u5e38\u89c1\u548c\u6700\u5e38\u7528\u7684\u7b97\u6cd5\u3002 \u672c\u6587\u4ecb\u7ecd\u4e86\u5728\u5904\u7406\u6570<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"_links":{"self":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts\/3072"}],"collection":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3072"}],"version-history":[{"count":0,"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts\/3072\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3072"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}