{
  "id": 69588,
  "title": "Feature Importance Label Encoding",
  "url": "/competitions/ga-customer-revenue-prediction/discussion/69588",
  "author_name": "Ben Greenwalt",
  "post_date": "2018-10-25T05:47:53.831000",
  "votes": 0,
  "comment_count": 0,
  "views": 0,
  "content": "<p>Hi, I am trying to do some feature importance, but encode labels first. Has anyone tried to do this for all relevant columns in the dataset at once, rather than doing it column by column? I can't seem to find a solution. The code below is sort of getting me in the direction I need to go, and hopefully helps illustrate generally what I am trying to do. I need to remove some irrelevant features and whatnot still.</p>\n\n<pre><code>from sklearn.preprocessing import LabelEncoder, OneHotEncoder\n\ntrain_predictors = train.drop('totals_transactionRevenue', axis=1)\ntrain_predictors_cat = train_predictors.select_dtypes(include=[object])\n\nle = LabelEncoder()\nle.fit(train_predictors_cat.astype(str).values.flatten())\ntrain_predictors_cat_encoded = train_predictors_cat.astype(str).apply(le.fit_transform)\ntrain_predictors_cat_encoded.head()\n</code></pre>",
  "messages": [
    {
      "id": 409959,
      "postDate": "2018-10-25T05:47:53.833Z",
      "content": "<p>Hi, I am trying to do some feature importance, but encode labels first. Has anyone tried to do this for all relevant columns in the dataset at once, rather than doing it column by column? I can't seem to find a solution. The code below is sort of getting me in the direction I need to go, and hopefully helps illustrate generally what I am trying to do. I need to remove some irrelevant features and whatnot still.</p>\n\n<pre><code>from sklearn.preprocessing import LabelEncoder, OneHotEncoder\n\ntrain_predictors = train.drop('totals_transactionRevenue', axis=1)\ntrain_predictors_cat = train_predictors.select_dtypes(include=[object])\n\nle = LabelEncoder()\nle.fit(train_predictors_cat.astype(str).values.flatten())\ntrain_predictors_cat_encoded = train_predictors_cat.astype(str).apply(le.fit_transform)\ntrain_predictors_cat_encoded.head()\n</code></pre>",
      "rawMarkdown": "Hi, I am trying to do some feature importance, but encode labels first. Has anyone tried to do this for all relevant columns in the dataset at once, rather than doing it column by column? I can't seem to find a solution. The code below is sort of getting me in the direction I need to go, and hopefully helps illustrate generally what I am trying to do. I need to remove some irrelevant features and whatnot still.\n\n    from sklearn.preprocessing import LabelEncoder, OneHotEncoder\n    \n    train_predictors = train.drop('totals_transactionRevenue', axis=1)\n    train_predictors_cat = train_predictors.select_dtypes(include=[object])\n    \n    le = LabelEncoder()\n    le.fit(train_predictors_cat.astype(str).values.flatten())\n    train_predictors_cat_encoded = train_predictors_cat.astype(str).apply(le.fit_transform)\n    train_predictors_cat_encoded.head()"
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "409959": "Hi, I am trying to do some feature importance, but encode labels first. Has anyone tried to do this for all relevant columns in the dataset at once, rather than doing it column by column? I can't seem to find a solution. The code below is sort of getting me in the direction I need to go, and hopefully helps illustrate generally what I am trying to do. I need to remove some irrelevant features and whatnot still.\n\n    from sklearn.preprocessing import LabelEncoder, OneHotEncoder\n    \n    train_predictors = train.drop('totals_transactionRevenue', axis=1)\n    train_predictors_cat = train_predictors.select_dtypes(include=[object])\n    \n    le = LabelEncoder()\n    le.fit(train_predictors_cat.astype(str).values.flatten())\n    train_predictors_cat_encoded = train_predictors_cat.astype(str).apply(le.fit_transform)\n    train_predictors_cat_encoded.head()"
  }
}