{"id":761,"date":"2024-05-04T22:41:09","date_gmt":"2024-05-04T14:41:09","guid":{"rendered":"http:\/\/madapapa.com\/wordpress\/?p=761"},"modified":"2024-05-04T22:43:04","modified_gmt":"2024-05-04T14:43:04","slug":"yi-ge-jue-ce-shu-xiang-mu-zhong-de-shu-ju-chu-li","status":"publish","type":"post","link":"http:\/\/madapapa.com\/wordpress\/?p=761","title":{"rendered":"\u4e00\u4e2a\u51b3\u7b56\u6811\u9879\u76ee\u4e2d\u7684\u6570\u636e\u5904\u7406"},"content":{"rendered":"<p>\u56e0\u4e3a\u8981\u4f7f\u7528\u51b3\u7b56\u6811\uff0c\u6ca1\u6cd5\u5bf9\u975e\u6570\u503c\u8fdb\u884c\u5904\u7406\uff0c\u6240\u4ee5\u9700\u8981\u628adataframe\u4e2d\u7684\u4e00\u4e9b\u6570\u636e\u8f6c\u6362\u6210\u6570\u503c\uff0c\u4f7f\u7528\u4e86\u5982\u4e0bcode<\/p>\n<pre><code class=\"language-plain_text\">from sklearn.preprocessing import LabelEncoder\nfrom collections import defaultdict\nd = defaultdict(LabelEncoder)\n\nX_trans = X.apply(lambda x: d[x.name].fit_transform(x))\nX_trans.head()\n<\/code><\/pre>\n<h2><a id=\"chatgpt4%E8%A7%A3%E9%87%8A%E5%A6%82%E4%B8%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>ChatGPT4\u89e3\u91ca\u5982\u4e0b<\/h2>\n<p>The expression <code>lambda x: d[x.name].fit_transform(x)<\/code> is a compact way of applying a function to each column of a pandas DataFrame. Let&#8217;s dissect this expression further to understand how it works in the context of transforming categorical data into numerical format using <code>LabelEncoder<\/code> in a Python environment.<\/p>\n<h3><a id=\"the-lambda-function\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>The <code>lambda<\/code> Function<\/h3>\n<p>A <code>lambda<\/code> function in Python is a small anonymous function defined with the keyword <code>lambda<\/code>. The syntax is:<\/p>\n<pre><code class=\"language-python\">lambda arguments: expression\n<\/code><\/pre>\n<p>In this case, the <code>lambda<\/code> function has one argument <code>x<\/code> (representing a column of the DataFrame) and the expression involves applying the <code>fit_transform<\/code> method from a <code>LabelEncoder<\/code> instance to <code>x<\/code>.<\/p>\n<h3><a id=\"how-the-lambda-function-is-applied\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>How the <code>lambda<\/code> Function is Applied<\/h3>\n<ul>\n<li><strong><code>x<\/code><\/strong>: Within the context of the <code>apply()<\/code> method, <code>x<\/code> represents each column in the DataFrame one at a time. For instance, if the DataFrame has columns &quot;Color&quot; and &quot;Size&quot;, <code>x<\/code> will be the &quot;Color&quot; column data in one iteration and the &quot;Size&quot; column data in the next.<\/li>\n<\/ul>\n<h3><a id=\"usage-of-d-x-name\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Usage of <code>d[x.name]<\/code><\/h3>\n<ul>\n<li>\n<p><strong><code>d<\/code><\/strong>: This is a <code>defaultdict<\/code> containing instances of <code>LabelEncoder<\/code>. The use of <code>defaultdict<\/code> ensures that if a <code>LabelEncoder<\/code> for a particular column name does not exist, it will automatically create a new <code>LabelEncoder<\/code> instance without raising an error.<\/p>\n<\/li>\n<li>\n<p><strong><code>x.name<\/code><\/strong>: This attribute fetches the name of the column currently being processed (like &quot;Color&quot; or &quot;Size&quot;).<\/p>\n<\/li>\n<li>\n<p><strong><code>d[x.name]<\/code><\/strong>: This retrieves the <code>LabelEncoder<\/code> instance for the column named <code>x.name<\/code>. If it&#8217;s the first time this column is being accessed, <code>defaultdict<\/code> initializes a new <code>LabelEncoder<\/code> for this key.<\/p>\n<\/li>\n<\/ul>\n<h3><a id=\"the-fit-transform-method\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>The <code>fit_transform<\/code> Method<\/h3>\n<ul>\n<li><strong><code>fit_transform()<\/code><\/strong>: This method is part of <code>LabelEncoder<\/code> and serves a dual purpose:\n<ul>\n<li><strong><code>fit<\/code><\/strong>: It learns the mapping from the labels in the column to integers. For example, for a column with labels [&quot;Red&quot;, &quot;Blue&quot;, &quot;Green&quot;], it might learn the mapping Red -&gt; 0, Blue -&gt; 1, Green -&gt; 2.<\/li>\n<li><strong><code>transform<\/code><\/strong>: It then transforms the labels into these integers based on the mapping it has learned.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><a id=\"putting-it-all-together\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Putting It All Together<\/h3>\n<p>When you call <code>X.apply(lambda x: d[x.name].fit_transform(x))<\/code>, it performs the following steps for each column in DataFrame <code>X<\/code>:<\/p>\n<ul>\n<li>Accesses each column as <code>x<\/code>.<\/li>\n<li>Fetches the <code>LabelEncoder<\/code> for that column by <code>x.name<\/code>.<\/li>\n<li>Applies <code>fit_transform<\/code> to the column, converting text labels into integers.<\/li>\n<li>Returns a new DataFrame where each text label has been replaced by an integer.<\/li>\n<\/ul>\n<p>This method is particularly useful when preparing data for machine learning models, which generally require numeric input features rather than text labels. The transformation is done in-place and efficiently for each column, utilizing the flexibility of <code>defaultdict<\/code> and the functionality of <code>LabelEncoder<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u56e0\u4e3a\u8981\u4f7f\u7528\u51b3\u7b56\u6811\uff0c\u6ca1\u6cd5\u5bf9\u975e\u6570\u503c\u8fdb\u884c\u5904\u7406\uff0c\u6240\u4ee5\u9700\u8981\u628adataframe\u4e2d\u7684\u4e00\u4e9b\u6570\u636e\u8f6c\u6362\u6210\u6570\u503c\uff0c\u4f7f\u7528\u4e86\u5982\u4e0bcode from sklearn.preprocessing import LabelEncoder from collections import defaultdict d = defaultdict(LabelEncoder) X_trans = X.apply(lambda x: d[x.name].fit_transform(x)) X_trans.head() ChatGPT4\u89e3\u91ca\u5982\u4e0b The expression lambda x: d[x.name].fit_transform(x) is a compact way of applying a function to each column of a pandas DataFrame. Let&#8217;s dissect this expression further to understand how it works in the context of transforming categorical &hellip; <a href=\"http:\/\/madapapa.com\/wordpress\/?p=761\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">\u4e00\u4e2a\u51b3\u7b56\u6811\u9879\u76ee\u4e2d\u7684\u6570\u636e\u5904\u7406<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[48,44],"tags":[],"class_list":["post-761","post","type-post","status-publish","format-standard","hentry","category-artificial-intelligence","category-flask"],"_links":{"self":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=761"}],"version-history":[{"count":1,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/761\/revisions"}],"predecessor-version":[{"id":762,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/761\/revisions\/762"}],"wp:attachment":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=761"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}