{"id":1115741,"date":"2025-01-08T18:09:43","date_gmt":"2025-01-08T10:09:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1115741.html"},"modified":"2025-01-08T18:09:45","modified_gmt":"2025-01-08T10:09:45","slug":"python%e5%86%b3%e7%ad%96%e6%a0%91%e5%88%86%e7%b1%bb%e5%8f%98%e9%87%8f%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1115741.html","title":{"rendered":"python\u51b3\u7b56\u6811\u5206\u7c7b\u53d8\u91cf\u5982\u4f55\u5904\u7406"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080349\/2e273e21-cac5-4a93-aec4-3d8cade4d59e.webp\" alt=\"python\u51b3\u7b56\u6811\u5206\u7c7b\u53d8\u91cf\u5982\u4f55\u5904\u7406\" \/><\/p>\n<p><p> <strong>\u5728Python\u51b3\u7b56\u6811\u4e2d\u5904\u7406\u5206\u7c7b\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\uff1aLabel Encoding\u3001One-Hot Encoding\u3001\u907f\u514d\u4fe1\u606f\u6cc4\u9732\u3002<\/strong>\u5176\u4e2d\uff0c<strong>Label Encoding<\/strong>\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u901a\u8fc7\u5c06\u5206\u7c7b\u53d8\u91cf\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u4f7f\u5f97\u51b3\u7b56\u6811\u80fd\u591f\u7406\u89e3\u5e76\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u3002\u5728\u4f7f\u7528Label Encoding\u65f6\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5206\u7c7b\u53d8\u91cf\u7684\u6570\u503c\u987a\u5e8f\u4e0d\u4f1a\u5bf9\u6a21\u578b\u7684\u8bad\u7ec3\u548c\u9884\u6d4b\u4ea7\u751f\u8bef\u5bfc\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Label Encoding\u5904\u7406\u51b3\u7b56\u6811\u4e2d\u7684\u5206\u7c7b\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001LABEL ENCODING<\/h3>\n<\/p>\n<p><p>Label Encoding \u662f\u4e00\u79cd\u5c06\u5206\u7c7b\u53d8\u91cf\u8f6c\u6362\u4e3a\u6570\u5b57\u7684\u65b9\u6cd5\u3002\u5b83\u5c06\u6bcf\u4e2a\u7c7b\u522b\u6620\u5c04\u4e3a\u4e00\u4e2a\u552f\u4e00\u7684\u6574\u6570\u503c\uff0c\u8fd9\u4f7f\u5f97\u51b3\u7b56\u6811\u80fd\u591f\u5904\u7406\u8fd9\u4e9b\u5206\u7c7b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528LabelEncoder\u8fdb\u884c\u7f16\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import LabelEncoder<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;Color&#39;: [&#39;Red&#39;, &#39;Blue&#39;, &#39;Green&#39;, &#39;Blue&#39;, &#39;Red&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u521d\u59cb\u5316LabelEncoder<\/strong><\/h2>\n<p>label_encoder = LabelEncoder()<\/p>\n<h2><strong>\u5bf9\u5206\u7c7b\u53d8\u91cf\u8fdb\u884c\u7f16\u7801<\/strong><\/h2>\n<p>df[&#39;Color_Encoded&#39;] = label_encoder.fit_transform(df[&#39;Color&#39;])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u201cColor\u201d\u5217\u4e2d\u7684\u5206\u7c7b\u53d8\u91cf\u8f6c\u6362\u4e3a\u6570\u5b57\u3002LabelEncoder \u4f1a\u5c06\u4e0d\u540c\u7684\u7c7b\u522b\u6620\u5c04\u4e3a\u4e0d\u540c\u7684\u6574\u6570\u503c\uff0c\u5982 \u201cRed\u201d \u6620\u5c04\u4e3a 2\uff0c\u201cBlue\u201d \u6620\u5c04\u4e3a 0\uff0c\u201cGreen\u201d \u6620\u5c04\u4e3a 1\u3002\u8fd9\u6837\uff0c\u51b3\u7b56\u6811\u5c31\u80fd\u591f\u7406\u89e3\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u8003\u8651\u7c7b\u522b\u987a\u5e8f<\/h4>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cLabel Encoding \u4f1a\u4e3a\u6bcf\u4e2a\u7c7b\u522b\u5206\u914d\u4e00\u4e2a\u6574\u6570\u503c\uff0c\u4f46\u8fd9\u4e9b\u6574\u6570\u503c\u7684\u987a\u5e8f\u53ef\u80fd\u4f1a\u5f71\u54cd\u6a21\u578b\u7684\u8bad\u7ec3\u7ed3\u679c\u3002\u5982\u679c\u7c7b\u522b\u672c\u8eab\u6ca1\u6709\u987a\u5e8f\uff0c\u90a3\u4e48\u8fd9\u79cd\u7f16\u7801\u65b9\u5f0f\u53ef\u80fd\u4f1a\u5f15\u5165\u8bef\u5bfc\u4fe1\u606f\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 One-Hot Encoding\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001ONE-HOT ENCODING<\/h3>\n<\/p>\n<p><p>One-Hot Encoding \u662f\u53e6\u4e00\u79cd\u5904\u7406\u5206\u7c7b\u53d8\u91cf\u7684\u65b9\u6cd5\u3002\u5b83\u5c06\u6bcf\u4e2a\u7c7b\u522b\u8f6c\u6362\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u4e8c\u8fdb\u5236\u7279\u5f81\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4f1a\u5f15\u5165\u7c7b\u522b\u4e4b\u95f4\u7684\u987a\u5e8f\u5173\u7cfb\uff0c\u56e0\u6b64\u9002\u7528\u4e8e\u65e0\u5e8f\u7684\u5206\u7c7b\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528pd.get_dummies\u8fdb\u884cOne-Hot Encoding<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;Color&#39;: [&#39;Red&#39;, &#39;Blue&#39;, &#39;Green&#39;, &#39;Blue&#39;, &#39;Red&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5bf9\u5206\u7c7b\u53d8\u91cf\u8fdb\u884cOne-Hot Encoding<\/strong><\/h2>\n<p>df_encoded = pd.get_dummies(df, columns=[&#39;Color&#39;])<\/p>\n<p>print(df_encoded)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>pd.get_dummies<\/code> \u51fd\u6570\u5c06\u201cColor\u201d\u5217\u4e2d\u7684\u6bcf\u4e2a\u7c7b\u522b\u8f6c\u6362\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u4e8c\u8fdb\u5236\u7279\u5f81\u5217\u3002\u4f8b\u5982\uff0c\u201cRed\u201d \u7c7b\u522b\u5c06\u88ab\u8f6c\u6362\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u4e8c\u8fdb\u5236\u5217\u201cColor_Red\u201d\uff0c\u201cBlue\u201d \u7c7b\u522b\u5c06\u88ab\u8f6c\u6362\u4e3a\u201cColor_Blue\u201d\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u8fd9\u6837\uff0c\u51b3\u7b56\u6811\u5c31\u80fd\u591f\u5904\u7406\u8fd9\u4e9b\u7f16\u7801\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2.2 One-Hot Encoding\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p>One-Hot Encoding \u7684\u4f18\u70b9\u662f\u5b83\u4e0d\u4f1a\u5f15\u5165\u7c7b\u522b\u4e4b\u95f4\u7684\u987a\u5e8f\u5173\u7cfb\uff0c\u56e0\u6b64\u9002\u7528\u4e8e\u65e0\u5e8f\u7684\u5206\u7c7b\u53d8\u91cf\u3002\u7136\u800c\uff0c\u5b83\u7684\u7f3a\u70b9\u662f\u5f53\u5206\u7c7b\u53d8\u91cf\u7684\u7c7b\u522b\u6570\u91cf\u8f83\u591a\u65f6\uff0c\u4f1a\u5bfc\u81f4\u7279\u5f81\u6570\u91cf\u6fc0\u589e\uff0c\u4ece\u800c\u589e\u52a0\u6a21\u578b\u7684\u8ba1\u7b97\u590d\u6742\u5ea6\u548c\u5b58\u50a8\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u907f\u514d\u4fe1\u606f\u6cc4\u9732<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5206\u7c7b\u53d8\u91cf\u65f6\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u907f\u514d\u4fe1\u606f\u6cc4\u9732\uff08Data Leakage\uff09\u3002\u4fe1\u606f\u6cc4\u9732\u662f\u6307\u5728\u6a21\u578b\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\uff0c\u4f7f\u7528\u4e86\u6d4b\u8bd5\u6570\u636e\u4e2d\u7684\u4fe1\u606f\uff0c\u4ece\u800c\u5bfc\u81f4\u6a21\u578b\u5728\u6d4b\u8bd5\u6570\u636e\u4e0a\u7684\u8868\u73b0\u4f18\u4e8e\u5b9e\u9645\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528\u8bad\u7ec3\u96c6\u8fdb\u884c\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u4fe1\u606f\u6cc4\u9732\uff0c\u5e94\u786e\u4fdd\u5728\u7f16\u7801\u8fc7\u7a0b\u4e2d\u53ea\u4f7f\u7528\u8bad\u7ec3\u96c6\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u7f16\u7801\uff0c\u800c\u4e0d\u5305\u62ec\u6d4b\u8bd5\u96c6\u4e2d\u7684\u6570\u636e\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u7684\u8868\u73b0\u4e0e\u5b9e\u9645\u60c5\u51b5\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.model_selection import tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_test_split<\/p>\n<p>from sklearn.preprocessing import LabelEncoder<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;Color&#39;: [&#39;Red&#39;, &#39;Blue&#39;, &#39;Green&#39;, &#39;Blue&#39;, &#39;Red&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5212\u5206\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6<\/strong><\/h2>\n<p>train_df, test_df = train_test_split(df, test_size=0.2, random_state=42)<\/p>\n<h2><strong>\u521d\u59cb\u5316LabelEncoder<\/strong><\/h2>\n<p>label_encoder = LabelEncoder()<\/p>\n<h2><strong>\u4f7f\u7528\u8bad\u7ec3\u96c6\u8fdb\u884c\u7f16\u7801<\/strong><\/h2>\n<p>train_df[&#39;Color_Encoded&#39;] = label_encoder.fit_transform(train_df[&#39;Color&#39;])<\/p>\n<h2><strong>\u4f7f\u7528\u8bad\u7ec3\u96c6\u7684\u7f16\u7801\u5bf9\u6d4b\u8bd5\u96c6\u8fdb\u884c\u8f6c\u6362<\/strong><\/h2>\n<p>test_df[&#39;Color_Encoded&#39;] = label_encoder.transform(test_df[&#39;Color&#39;])<\/p>\n<p>print(train_df)<\/p>\n<p>print(test_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u6570\u636e\u96c6\u5212\u5206\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u8bad\u7ec3\u96c6\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u7f16\u7801\uff0c\u5e76\u5c06\u7f16\u7801\u540e\u7684\u8bad\u7ec3\u96c6\u7f16\u7801\u5668\u5e94\u7528\u4e8e\u6d4b\u8bd5\u96c6\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7f16\u7801\u8fc7\u7a0b\u4e2d\u4e0d\u4f1a\u6cc4\u9732\u6d4b\u8bd5\u96c6\u4e2d\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u51b3\u7b56\u6811\u5206\u7c7b\u53d8\u91cf\u5904\u7406\u7684\u5b9e\u8df5\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5206\u7c7b\u53d8\u91cf\u7684\u5904\u7406\u65b9\u6cd5\u53ef\u80fd\u56e0\u5177\u4f53\u60c5\u51b5\u800c\u5f02\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u4e00\u4e2a\u5b9e\u9645\u6570\u636e\u96c6\u4e2d\u5904\u7406\u5206\u7c7b\u53d8\u91cf\u5e76\u6784\u5efa\u51b3\u7b56\u6811\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u548c\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from sklearn.model_selection import train_test_split<\/p>\n<p>from sklearn.preprocessing import LabelEncoder<\/p>\n<p>from sklearn.tree import DecisionTreeClassifier<\/p>\n<p>from sklearn.metrics import accuracy_score<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e\u96c6\uff08\u5047\u8bbe\u6570\u636e\u96c6\u5df2\u7ecf\u5b58\u5728\uff09<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Color&#39;: [&#39;Red&#39;, &#39;Blue&#39;, &#39;Green&#39;, &#39;Blue&#39;, &#39;Red&#39;, &#39;Green&#39;, &#39;Red&#39;, &#39;Blue&#39;],<\/p>\n<p>    &#39;Size&#39;: [&#39;Small&#39;, &#39;Large&#39;, &#39;Medium&#39;, &#39;Large&#39;, &#39;Small&#39;, &#39;Medium&#39;, &#39;Small&#39;, &#39;Large&#39;],<\/p>\n<p>    &#39;Weight&#39;: [1.2, 3.4, 2.1, 3.5, 1.3, 2.2, 1.1, 3.6],<\/p>\n<p>    &#39;Label&#39;: [0, 1, 0, 1, 0, 0, 0, 1]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u5904\u7406\u5206\u7c7b\u53d8\u91cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316LabelEncoder<\/p>\n<p>label_encoder_color = LabelEncoder()<\/p>\n<p>label_encoder_size = LabelEncoder()<\/p>\n<h2><strong>\u5bf9\u5206\u7c7b\u53d8\u91cf\u8fdb\u884c\u7f16\u7801<\/strong><\/h2>\n<p>df[&#39;Color_Encoded&#39;] = label_encoder_color.fit_transform(df[&#39;Color&#39;])<\/p>\n<p>df[&#39;Size_Encoded&#39;] = label_encoder_size.fit_transform(df[&#39;Size&#39;])<\/p>\n<h2><strong>\u5220\u9664\u539f\u59cb\u5206\u7c7b\u53d8\u91cf\u5217<\/strong><\/h2>\n<p>df_encoded = df.drop([&#39;Color&#39;, &#39;Size&#39;], axis=1)<\/p>\n<p>print(df_encoded)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.3 \u5212\u5206\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5212\u5206\u7279\u5f81\u548c\u6807\u7b7e<\/p>\n<p>X = df_encoded.drop(&#39;Label&#39;, axis=1)<\/p>\n<p>y = df_encoded[&#39;Label&#39;]<\/p>\n<h2><strong>\u5212\u5206\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.4 \u6784\u5efa\u548c\u8bad\u7ec3\u51b3\u7b56\u6811\u6a21\u578b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u51b3\u7b56\u6811\u5206\u7c7b\u5668<\/p>\n<p>clf = DecisionTreeClassifier(random_state=42)<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>clf.fit(X_train, y_train)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.5 \u8bc4\u4f30\u6a21\u578b\u6027\u80fd<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9884\u6d4b\u6d4b\u8bd5\u96c6\u6807\u7b7e<\/p>\n<p>y_pred = clf.predict(X_test)<\/p>\n<h2><strong>\u8ba1\u7b97\u51c6\u786e\u7387<\/strong><\/h2>\n<p>accuracy = accuracy_score(y_test, y_pred)<\/p>\n<p>print(f&quot;\u6a21\u578b\u51c6\u786e\u7387: {accuracy}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bf9\u5206\u7c7b\u53d8\u91cf\u8fdb\u884c\u4e86\u7f16\u7801\uff0c\u5e76\u5c06\u6570\u636e\u96c6\u5212\u5206\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u3002\u7136\u540e\uff0c\u6211\u4eec\u6784\u5efa\u5e76\u8bad\u7ec3\u4e86\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u5e76\u8bc4\u4f30\u4e86\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u7684\u6027\u80fd\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u6a21\u578b\u5728\u5904\u7406\u5206\u7c7b\u53d8\u91cf\u65f6\u4e0d\u4f1a\u5f15\u5165\u4fe1\u606f\u6cc4\u9732\uff0c\u5e76\u80fd\u591f\u51c6\u786e\u5730\u8fdb\u884c\u5206\u7c7b\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u591a\u91cd\u5206\u7c7b\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u5904\u7406\u591a\u4e2a\u5206\u7c7b\u53d8\u91cf\u3002\u5904\u7406\u591a\u4e2a\u5206\u7c7b\u53d8\u91cf\u65f6\uff0c\u53ef\u4ee5\u6309\u7167\u4e0a\u8ff0\u65b9\u6cd5\u5206\u522b\u5bf9\u6bcf\u4e2a\u5206\u7c7b\u53d8\u91cf\u8fdb\u884c\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u793a\u4f8b\u6570\u636e\u96c6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;Color&#39;: [&#39;Red&#39;, &#39;Blue&#39;, &#39;Green&#39;, &#39;Blue&#39;, &#39;Red&#39;, &#39;Green&#39;, &#39;Red&#39;, &#39;Blue&#39;],<\/p>\n<p>    &#39;Size&#39;: [&#39;Small&#39;, &#39;Large&#39;, &#39;Medium&#39;, &#39;Large&#39;, &#39;Small&#39;, &#39;Medium&#39;, &#39;Small&#39;, &#39;Large&#39;],<\/p>\n<p>    &#39;Shape&#39;: [&#39;Circle&#39;, &#39;Square&#39;, &#39;Triangle&#39;, &#39;Square&#39;, &#39;Circle&#39;, &#39;Triangle&#39;, &#39;Circle&#39;, &#39;Square&#39;],<\/p>\n<p>    &#39;Label&#39;: [0, 1, 0, 1, 0, 0, 0, 1]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u5904\u7406\u591a\u4e2a\u5206\u7c7b\u53d8\u91cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316LabelEncoder<\/p>\n<p>label_encoder_color = LabelEncoder()<\/p>\n<p>label_encoder_size = LabelEncoder()<\/p>\n<p>label_encoder_shape = LabelEncoder()<\/p>\n<h2><strong>\u5bf9\u5206\u7c7b\u53d8\u91cf\u8fdb\u884c\u7f16\u7801<\/strong><\/h2>\n<p>df[&#39;Color_Encoded&#39;] = label_encoder_color.fit_transform(df[&#39;Color&#39;])<\/p>\n<p>df[&#39;Size_Encoded&#39;] = label_encoder_size.fit_transform(df[&#39;Size&#39;])<\/p>\n<p>df[&#39;Shape_Encoded&#39;] = label_encoder_shape.fit_transform(df[&#39;Shape&#39;])<\/p>\n<h2><strong>\u5220\u9664\u539f\u59cb\u5206\u7c7b\u53d8\u91cf\u5217<\/strong><\/h2>\n<p>df_encoded = df.drop([&#39;Color&#39;, &#39;Size&#39;, &#39;Shape&#39;], axis=1)<\/p>\n<p>print(df_encoded)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5bf9\u201cColor\u201d\u3001\u201cSize\u201d\u548c\u201cShape\u201d\u4e09\u4e2a\u5206\u7c7b\u53d8\u91cf\u5206\u522b\u8fdb\u884c\u4e86\u7f16\u7801\uff0c\u5e76\u5220\u9664\u4e86\u539f\u59cb\u7684\u5206\u7c7b\u53d8\u91cf\u5217\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u5206\u7c7b\u53d8\u91cf\u90fd\u88ab\u6b63\u786e\u7f16\u7801\uff0c\u5e76\u4e14\u4e0d\u4f1a\u5f15\u5165\u4fe1\u606f\u6cc4\u9732\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u51b3\u7b56\u6811\u4e2d\u5904\u7406\u5206\u7c7b\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ecLabel Encoding\u548cOne-Hot Encoding\u3002\u5728\u9009\u62e9\u5177\u4f53\u7684\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u8003\u8651\u5206\u7c7b\u53d8\u91cf\u7684\u7c7b\u522b\u6570\u91cf\u3001\u7c7b\u522b\u4e4b\u95f4\u662f\u5426\u6709\u987a\u5e8f\u5173\u7cfb\uff0c\u4ee5\u53ca\u6a21\u578b\u7684\u8ba1\u7b97\u590d\u6742\u5ea6\u548c\u5b58\u50a8\u9700\u6c42\u3002\u6b64\u5916\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u907f\u514d\u4fe1\u606f\u6cc4\u9732\uff0c\u786e\u4fdd\u5728\u7f16\u7801\u8fc7\u7a0b\u4e2d\u53ea\u4f7f\u7528\u8bad\u7ec3\u96c6\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u7f16\u7801\u3002\u901a\u8fc7\u5408\u7406\u5730\u5904\u7406\u5206\u7c7b\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u66f4\u52a0\u51c6\u786e\u548c\u7a33\u5b9a\u7684\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u63d0\u9ad8\u6a21\u578b\u7684\u9884\u6d4b\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u5206\u7c7b\u53d8\u91cf\u4ee5\u6784\u5efa\u51b3\u7b56\u6811\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u5904\u7406\u5206\u7c7b\u53d8\u91cf\u4ee5\u6784\u5efa\u51b3\u7b56\u6811\u901a\u5e38\u4f7f\u7528\u7f16\u7801\u6280\u672f\uff0c\u5982\u72ec\u70ed\u7f16\u7801\uff08One-Hot Encoding\uff09\u6216\u6807\u7b7e\u7f16\u7801\uff08Label Encoding\uff09\u3002\u72ec\u70ed\u7f16\u7801\u9002\u7528\u4e8e\u65e0\u5e8f\u5206\u7c7b\u53d8\u91cf\uff0c\u5c06\u6bcf\u4e2a\u7c7b\u522b\u8f6c\u6362\u4e3a\u4e00\u4e2a\u65b0\u7684\u4e8c\u8fdb\u5236\u5217\uff0c\u800c\u6807\u7b7e\u7f16\u7801\u5219\u5c06\u6bcf\u4e2a\u7c7b\u522b\u6620\u5c04\u4e3a\u6574\u6570\u3002\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u8fd9\u4e9b\u7f16\u7801\u65b9\u6cd5\uff0c\u4e3a\u51b3\u7b56\u6811\u6a21\u578b\u63d0\u4f9b\u9002\u5408\u7684\u8f93\u5165\u683c\u5f0f\u3002<\/p>\n<p><strong>\u51b3\u7b56\u6811\u6a21\u578b\u5bf9\u5206\u7c7b\u53d8\u91cf\u7684\u5904\u7406\u65b9\u5f0f\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u51b3\u7b56\u6811\u6a21\u578b\u901a\u8fc7\u9009\u62e9\u6700\u4f73\u7684\u5206\u88c2\u7279\u5f81\u6765\u5904\u7406\u5206\u7c7b\u53d8\u91cf\u3002\u8fd9\u4e9b\u6a21\u578b\u901a\u5e38\u4f7f\u7528\u4fe1\u606f\u589e\u76ca\u6216\u57fa\u5c3c\u6307\u6570\u7b49\u6307\u6807\u6765\u8bc4\u4f30\u7279\u5f81\u7684\u5206\u88c2\u6548\u679c\u3002\u5206\u7c7b\u53d8\u91cf\u5728\u6811\u7684\u6bcf\u4e2a\u8282\u70b9\u5904\u90fd\u53ef\u4ee5\u88ab\u7528\u4e8e\u51b3\u7b56\uff0c\u8fd9\u4f7f\u5f97\u51b3\u7b56\u6811\u80fd\u591f\u81ea\u7136\u5730\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u800c\u65e0\u9700\u8fdb\u884c\u590d\u6742\u7684\u8f6c\u6362\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u5904\u7406\u51b3\u7b56\u6811\u5206\u7c7b\u53d8\u91cf\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u5e38\u7528\u7684\u5e93\u5305\u62ecScikit-learn\u3001pandas\u548cNumPy\u3002Scikit-learn\u63d0\u4f9b\u4e86\u5b9e\u73b0\u51b3\u7b56\u6811\u7684\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u800cpandas\u53ef\u4ee5\u5e2e\u52a9\u8fdb\u884c\u6570\u636e\u9884\u5904\u7406\u548c\u5206\u7c7b\u53d8\u91cf\u7684\u7f16\u7801\u3002\u6b64\u5916\uff0c\u4f7f\u7528Matplotlib\u6216Seaborn\u7b49\u53ef\u89c6\u5316\u5e93\u53ef\u4ee5\u5e2e\u52a9\u7406\u89e3\u548c\u5206\u6790\u51b3\u7b56\u6811\u7684\u7ed3\u6784\u4e0e\u7279\u5f81\u91cd\u8981\u6027\u3002\u8fd9\u4e9b\u5de5\u5177\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5206\u7c7b\u53d8\u91cf\u5e76\u6784\u5efa\u9ad8\u6548\u7684\u51b3\u7b56\u6811\u6a21\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u51b3\u7b56\u6811\u4e2d\u5904\u7406\u5206\u7c7b\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\uff1aLabel Encoding\u3001One-Hot Encoding\u3001\u907f [&hellip;]","protected":false},"author":3,"featured_media":1115752,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115741"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1115741"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115741\/revisions"}],"predecessor-version":[{"id":1115756,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115741\/revisions\/1115756"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1115752"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1115741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1115741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1115741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}