{"id":1066719,"date":"2024-12-31T16:28:53","date_gmt":"2024-12-31T08:28:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1066719.html"},"modified":"2024-12-31T16:28:56","modified_gmt":"2024-12-31T08:28:56","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e7%89%b9%e5%be%81%e8%bd%ac%e6%8d%a2%e4%b8%ba%e7%a8%80%e7%96%8f%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1066719.html","title":{"rendered":"python\u5982\u4f55\u628a\u7279\u5f81\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/e4939c0b-d9de-41da-adb4-2c0a7f507ec8.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u628a\u7279\u5f81\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635\" \/><\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u56de\u7b54\u6807\u9898\u95ee\u9898<\/h3>\n<\/p>\n<p><p><strong>Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Scipy\u5e93\u4e2d\u7684<code>csr_matrix<\/code>\u7c7b\u3001Scikit-learn\u5e93\u4e2d\u7684<code>DictVectorizer<\/code>\u7c7b\u3001Pandas\u5e93\u4e2d\u7684<code>get_dummies<\/code>\u51fd\u6570\u5c06\u7279\u5f81\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528Scikit-learn\u5e93\u4e2d\u7684<code>DictVectorizer<\/code>\u7c7b\u662f\u6700\u5e38\u7528\u4e14\u4fbf\u6377\u7684\u65b9\u6cd5\uff0c\u5b83\u80fd\u591f\u5c06\u5b57\u5178\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635\u3002<strong>\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>DictVectorizer<\/code>\u7c7b\u65f6\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u8be5\u7c7b\uff0c\u5176\u6b21\u5c06\u6570\u636e\u8f6c\u6362\u6210\u5b57\u5178\u7684\u5f62\u5f0f\uff0c\u6700\u540e\u8c03\u7528<code>fit_transform<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u5c55\u5f00\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528Scipy\u5e93<\/strong><\/p>\n<\/p>\n<p><p>Scipy\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u7a00\u758f\u77e9\u9635\u683c\u5f0f\uff0c\u5176\u4e2d<code>csr_matrix<\/code>\u662f\u6700\u5e38\u7528\u7684\u3002\u5b83\u80fd\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u64cd\u4f5c\u7a00\u758f\u77e9\u9635\u3002\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u6570\u636e\u7684\u7a00\u758f\u6027\u662f\u5e38\u89c1\u73b0\u8c61\uff0c\u5c06\u7279\u5f81\u6570\u636e\u8f6c\u6362\u6210\u7a00\u758f\u77e9\u9635\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u5b58\u50a8\u7a7a\u95f4\u548c\u8ba1\u7b97\u6210\u672c\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.sparse import csr_matrix<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2\u3001\u521b\u5efa\u7a00\u758f\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u539f\u59cb\u6570\u636e\u7684\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = np.array([[0, 0, 3], [4, 0, 0], [0, 5, 0]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>csr_matrix<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sparse_matrix = csr_matrix(data)<\/p>\n<p>print(sparse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.3\u3001\u7a00\u758f\u77e9\u9635\u7684\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u7a00\u758f\u77e9\u9635\u540e\uff0c\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u5404\u79cd\u77e9\u9635\u64cd\u4f5c\uff0c\u5982\u77e9\u9635\u4e58\u6cd5\u3001\u8f6c\u7f6e\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transposed_matrix = sparse_matrix.transpose()<\/p>\n<p>print(transposed_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Scikit-learn\u5e93<\/h3>\n<\/p>\n<p><p>Scikit-learn\u5e93\u4e2d\u7684<code>DictVectorizer<\/code>\u7c7b\u662f\u5c06\u7279\u5f81\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635\u7684\u5e38\u7528\u5de5\u5177\u3002\u5b83\u4e3b\u8981\u7528\u4e8e\u5c06\u5b57\u5178\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u7c7b\u522b\u7279\u5f81\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.feature_extraction import DictVectorizer<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2\u3001\u521b\u5efa\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u5982\u4e0b\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [{&#39;feature1&#39;: 1, &#39;feature2&#39;: 2}, {&#39;feature1&#39;: 3, &#39;feature2&#39;: 4}]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.3\u3001\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">vec = DictVectorizer(sparse=True)<\/p>\n<p>sparse_matrix = vec.fit_transform(data)<\/p>\n<p>print(sparse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.4\u3001\u67e5\u770b\u7279\u5f81\u540d\u79f0<\/h4>\n<\/p>\n<p><p><code>DictVectorizer<\/code>\u8fd8\u53ef\u4ee5\u63d0\u4f9b\u7279\u5f81\u540d\u79f0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">feature_names = vec.get_feature_names_out()<\/p>\n<p>print(feature_names)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u4e2d\u7684<code>get_dummies<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u7c7b\u522b\u7279\u5f81\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635\uff0c\u9002\u7528\u4e8e\u5904\u7406\u6570\u636e\u6846\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2\u3001\u521b\u5efa\u6570\u636e\u6846<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.DataFrame({<\/p>\n<p>    &#39;feature1&#39;: [1, 2, 3],<\/p>\n<p>    &#39;feature2&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;A&#39;]<\/p>\n<p>})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.3\u3001\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">sparse_matrix = pd.get_dummies(df, sparse=True)<\/p>\n<p>print(sparse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06\u7279\u5f81\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635\u5728\u6570\u636e\u5904\u7406\u548c\u673a\u5668\u5b66\u4e60\u4e2d\u975e\u5e38\u91cd\u8981\u3002<strong>\u4f7f\u7528Scipy\u5e93\u4e2d\u7684<code>csr_matrix<\/code>\u7c7b\u9002\u5408\u5904\u7406\u6570\u503c\u578b\u6570\u636e<\/strong>\uff0c<strong>\u4f7f\u7528Scikit-learn\u5e93\u4e2d\u7684<code>DictVectorizer<\/code>\u7c7b\u9002\u5408\u5904\u7406\u5b57\u5178\u7c7b\u578b\u7684\u7c7b\u522b\u6570\u636e<\/strong>\uff0c<strong>\u4f7f\u7528Pandas\u5e93\u4e2d\u7684<code>get_dummies<\/code>\u51fd\u6570\u9002\u5408\u5904\u7406\u6570\u636e\u6846\u4e2d\u7684\u7c7b\u522b\u7279\u5f81<\/strong>\u3002\u6839\u636e\u5177\u4f53\u7684\u6570\u636e\u7c7b\u578b\u548c\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u6570\u636e\u5904\u7406\u6548\u7387\u548c\u6a21\u578b\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06\u7279\u5f81\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Scikit-learn\u5e93\u4e2d\u7684<code>scipy.sparse<\/code>\u6a21\u5757\u6765\u5c06\u7279\u5f81\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635\u3002\u5177\u4f53\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\u6570\u636e\u9884\u5904\u7406\u3001\u9009\u62e9\u5408\u9002\u7684\u7a00\u758f\u77e9\u9635\u683c\u5f0f\uff08\u5982CSR\u6216CSC\uff09\uff0c\u7136\u540e\u4f7f\u7528<code>scipy.sparse<\/code>\u4e2d\u7684\u51fd\u6570\u6765\u521b\u5efa\u7a00\u758f\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>csr_matrix<\/code>\u51fd\u6570\u5c06\u5bc6\u96c6\u77e9\u9635\u8f6c\u6362\u4e3a\u7a00\u758f\u683c\u5f0f\u3002<\/p>\n<p><strong>\u7a00\u758f\u77e9\u9635\u4e0e\u5bc6\u96c6\u77e9\u9635\u76f8\u6bd4\u6709\u54ea\u4e9b\u4f18\u52bf\uff1f<\/strong><br \/>\u7a00\u758f\u77e9\u9635\u7684\u4e3b\u8981\u4f18\u52bf\u5728\u4e8e\u5185\u5b58\u4f7f\u7528\u6548\u7387\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u7279\u5f81\u503c\u4e3a\u96f6\u7684\u9ad8\u7ef4\u6570\u636e\uff0c\u7a00\u758f\u77e9\u9635\u53ea\u5b58\u50a8\u975e\u96f6\u5143\u7d20\uff0c\u4ece\u800c\u663e\u8457\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002\u6b64\u5916\uff0c\u7a00\u758f\u77e9\u9635\u5728\u67d0\u4e9b\u8ba1\u7b97\u4e2d\uff08\u5982\u77e9\u9635\u4e58\u6cd5\uff09\u4e5f\u80fd\u591f\u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u65f6\u3002<\/p>\n<p><strong>\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u5e94\u8be5\u4f7f\u7528\u7a00\u758f\u77e9\u9635\uff1f<\/strong><br \/>\u5f53\u5904\u7406\u7684\u6570\u636e\u7279\u5f81\u7ef4\u5ea6\u975e\u5e38\u9ad8\uff0c\u800c\u5927\u90e8\u5206\u7279\u5f81\u7684\u503c\u4e3a\u96f6\u65f6\uff0c\u4f7f\u7528\u7a00\u758f\u77e9\u9635\u662f\u975e\u5e38\u5408\u9002\u7684\u3002\u4f8b\u5982\uff0c\u6587\u672c\u6570\u636e\u7684\u8bcd\u888b\u6a21\u578b\u6216TF-IDF\u8868\u793a\u901a\u5e38\u4f1a\u4ea7\u751f\u5927\u91cf\u96f6\u503c\u7279\u5f81\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7a00\u758f\u77e9\u9635\u80fd\u591f\u6709\u6548\u5730\u5b58\u50a8\u548c\u5904\u7406\u8fd9\u4e9b\u6570\u636e\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5185\u5b58\u5f00\u9500\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u76f4\u63a5\u56de\u7b54\u6807\u9898\u95ee\u9898 Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Scipy\u5e93\u4e2d\u7684csr_matrix\u7c7b\u3001Scikit-learn [&hellip;]","protected":false},"author":3,"featured_media":1066729,"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\/1066719"}],"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=1066719"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1066719\/revisions"}],"predecessor-version":[{"id":1066733,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1066719\/revisions\/1066733"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1066729"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1066719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1066719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1066719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}