{
  "id": 21957,
  "title": "gridsearch for feature weighting",
  "url": "/competitions/facebook-v-predicting-check-ins/discussion/21957",
  "author_name": "simonm3",
  "post_date": "2016-06-29T13:27:18.137000",
  "votes": 0,
  "comment_count": 0,
  "views": 204,
  "content": "<p>Would be useful to run gridearch to help optimise feature weights prior to KNN. I have got this to work...sort of....but does anyone have a better way?</p>\n\n<p>The current solution requires pasting the feature list into the class every time the features change. Also it has to be first in the pipeline so I can pass a dataframe in to match up the column names with the weights. After the first pipeline step it becomes a np.array.</p>\n\n<pre><code>class FeatureWeights(TransformerMixin, BaseEstimator):\n&quot;&quot;&quot; base class for applying weights to features &quot;&quot;&quot;    \n\ndef __init__(self):\n    &quot;&quot;&quot; Override to explicitly list params \n        BaseEstimator does not allow *args, **kwargs &quot;&quot;&quot;\n    pass\n\ndef transform(self, x):\n    &quot;&quot;&quot; NOTE assumes 1st in pipeline and passed pandas dataframe \n    otherwise cannot match numpy array columns to unordered\n    feature weights dict.\n    sklearn does not allow pass **params or set class variables after init\n    &quot;&quot;&quot;\n    cols = x.columns\n    x = x.values.copy()\n    for i, col in enumerate(cols):\n        x[:,i] = x[:,i] * self.__dict__[col]\n    return x\n\ndef fit(self, x, y=None):\n    return self\n\nclass FBFeatureWeights(FeatureWeights):\n&quot;&quot;&quot; weights features for models such as KNN &quot;&quot;&quot;\n# copy features into __init__ parameters \ndef __init__(self, x, y, time, accuracy, dayofweek, hour, month):\n    self.__dict__ = locals().copy()\n    del self.__dict__[&quot;self&quot;]\n</code></pre>",
  "messages": [
    {
      "id": 125440,
      "postDate": "2016-06-29T13:27:18.137Z",
      "content": "<p>Would be useful to run gridearch to help optimise feature weights prior to KNN. I have got this to work...sort of....but does anyone have a better way?</p>\n\n<p>The current solution requires pasting the feature list into the class every time the features change. Also it has to be first in the pipeline so I can pass a dataframe in to match up the column names with the weights. After the first pipeline step it becomes a np.array.</p>\n\n<pre><code>class FeatureWeights(TransformerMixin, BaseEstimator):\n&quot;&quot;&quot; base class for applying weights to features &quot;&quot;&quot;    \n\ndef __init__(self):\n    &quot;&quot;&quot; Override to explicitly list params \n        BaseEstimator does not allow *args, **kwargs &quot;&quot;&quot;\n    pass\n\ndef transform(self, x):\n    &quot;&quot;&quot; NOTE assumes 1st in pipeline and passed pandas dataframe \n    otherwise cannot match numpy array columns to unordered\n    feature weights dict.\n    sklearn does not allow pass **params or set class variables after init\n    &quot;&quot;&quot;\n    cols = x.columns\n    x = x.values.copy()\n    for i, col in enumerate(cols):\n        x[:,i] = x[:,i] * self.__dict__[col]\n    return x\n\ndef fit(self, x, y=None):\n    return self\n\nclass FBFeatureWeights(FeatureWeights):\n&quot;&quot;&quot; weights features for models such as KNN &quot;&quot;&quot;\n# copy features into __init__ parameters \ndef __init__(self, x, y, time, accuracy, dayofweek, hour, month):\n    self.__dict__ = locals().copy()\n    del self.__dict__[&quot;self&quot;]\n</code></pre>",
      "rawMarkdown": "Would be useful to run gridearch to help optimise feature weights prior to KNN. I have got this to work...sort of....but does anyone have a better way?\r\n\r\nThe current solution requires pasting the feature list into the class every time the features change. Also it has to be first in the pipeline so I can pass a dataframe in to match up the column names with the weights. After the first pipeline step it becomes a np.array.\r\n\r\n\r\n    class FeatureWeights(TransformerMixin, BaseEstimator):\r\n    \"\"\" base class for applying weights to features \"\"\"    \r\n    \r\n    def __init__(self):\r\n        \"\"\" Override to explicitly list params \r\n            BaseEstimator does not allow *args, **kwargs \"\"\"\r\n        pass\r\n    \r\n    def transform(self, x):\r\n        \"\"\" NOTE assumes 1st in pipeline and passed pandas dataframe \r\n        otherwise cannot match numpy array columns to unordered\r\n        feature weights dict.\r\n        sklearn does not allow pass **params or set class variables after init\r\n        \"\"\"\r\n        cols = x.columns\r\n        x = x.values.copy()\r\n        for i, col in enumerate(cols):\r\n            x[:,i] = x[:,i] * self.__dict__[col]\r\n        return x\r\n\r\n    def fit(self, x, y=None):\r\n        return self\r\n\r\n    class FBFeatureWeights(FeatureWeights):\r\n    \"\"\" weights features for models such as KNN \"\"\"\r\n    # copy features into __init__ parameters \r\n    def __init__(self, x, y, time, accuracy, dayofweek, hour, month):\r\n        self.__dict__ = locals().copy()\r\n        del self.__dict__[\"self\"]"
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "125440": "Would be useful to run gridearch to help optimise feature weights prior to KNN. I have got this to work...sort of....but does anyone have a better way?\r\n\r\nThe current solution requires pasting the feature list into the class every time the features change. Also it has to be first in the pipeline so I can pass a dataframe in to match up the column names with the weights. After the first pipeline step it becomes a np.array.\r\n\r\n\r\n    class FeatureWeights(TransformerMixin, BaseEstimator):\r\n    \"\"\" base class for applying weights to features \"\"\"    \r\n    \r\n    def __init__(self):\r\n        \"\"\" Override to explicitly list params \r\n            BaseEstimator does not allow *args, **kwargs \"\"\"\r\n        pass\r\n    \r\n    def transform(self, x):\r\n        \"\"\" NOTE assumes 1st in pipeline and passed pandas dataframe \r\n        otherwise cannot match numpy array columns to unordered\r\n        feature weights dict.\r\n        sklearn does not allow pass **params or set class variables after init\r\n        \"\"\"\r\n        cols = x.columns\r\n        x = x.values.copy()\r\n        for i, col in enumerate(cols):\r\n            x[:,i] = x[:,i] * self.__dict__[col]\r\n        return x\r\n\r\n    def fit(self, x, y=None):\r\n        return self\r\n\r\n    class FBFeatureWeights(FeatureWeights):\r\n    \"\"\" weights features for models such as KNN \"\"\"\r\n    # copy features into __init__ parameters \r\n    def __init__(self, x, y, time, accuracy, dayofweek, hour, month):\r\n        self.__dict__ = locals().copy()\r\n        del self.__dict__[\"self\"]"
  }
}