{"action":"closed","issue":{"url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/4674","labels_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/4674/labels{/name}","comments_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/4674/comments","events_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/4674/events","html_url":"https://github.com/scikit-learn/scikit-learn/issues/4674","id":73282584,"number":4674,"title":"Q: combine linear steps in one for memory issue?","user":{"login":"kingjr","id":4881164,"avatar_url":"https://avatars.githubusercontent.com/u/4881164?v=3","gravatar_id":"","url":"https://api.github.com/users/kingjr","html_url":"https://github.com/kingjr","followers_url":"https://api.github.com/users/kingjr/followers","following_url":"https://api.github.com/users/kingjr/following{/other_user}","gists_url":"https://api.github.com/users/kingjr/gists{/gist_id}","starred_url":"https://api.github.com/users/kingjr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kingjr/subscriptions","organizations_url":"https://api.github.com/users/kingjr/orgs","repos_url":"https://api.github.com/users/kingjr/repos","events_url":"https://api.github.com/users/kingjr/events{/privacy}","received_events_url":"https://api.github.com/users/kingjr/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-05-05T11:07:11Z","updated_at":"2015-05-05T21:46:29Z","closed_at":"2015-05-05T21:46:29Z","body":"Dear all,\r\n\r\nI'm trying to combine some linear steps of a fitted pipeline to minimize memory storage. Typically, I would like to rescale the coef of an SVC with the inverse_transform of a StandardScaler:\r\n\r\n```\r\npipeline = Pipeline([('scaler', StandardScaler()), ('svc', SVC())])\r\npipeline.fit(X, y)\r\n\r\nscaler = pipeline.steps[0][1]\r\nsvc = pipeline.steps[1][1]\r\nSV = svc.__getattribute__('support_vectors_')\r\nfor i, sv in enumerate(SV):\r\n    SV[i] = scaler.inverse_transform(sv)\r\n```\r\n\r\nHowever, this produces some weird results. Typically the predictions of the classic pipeline (scaler then svc) differs from the combined pipeline (rescaled svc).\r\n\r\nHere is a full example\r\n\r\n```\r\nimport numpy as np\r\nfrom matplotlib import pyplot as plt\r\nfrom sklearn.svm import SVC\r\nfrom sklearn.preprocessing import StandardScaler\r\nfrom sklearn.pipeline import Pipeline\r\n\r\n# setup dataset\r\nsnr = .1\r\nX = np.random.randn(100, 10)\r\ny = np.random.randint(0, 2, 100)\r\nX[y == 1, :] += snr\r\n# unwhiten data\r\nX += 500\r\nX *= 10\r\n\r\n# set classic classifier\r\npipeline = Pipeline([('scaler', StandardScaler()), ('svc', SVC())])\r\npipeline.fit(X, y)\r\ny_pred = pipeline.decision_function(X)\r\n\r\n# rescale SVC coef\r\nscaler = pipeline.steps[0][1]\r\nsvc = pipeline.steps[1][1]\r\nSV = svc.__getattribute__('support_vectors_')\r\n# combine the scaler and support vector steps\r\nfor i, sv in enumerate(SV):\r\n    SV[i] = scaler.inverse_transform(sv)\r\n# test predictions\r\ny_pred_scaled = svc.decision_function(X)\r\n\r\n# plot both predictions\r\nfig, ax = plt.subplots(1, 2)\r\nax[0].scatter(y_pred, y)\r\nax[0].scatter(y_pred_scaled, y + .1, color='r')\r\nax[0].set_ylim([-1, 3])\r\nax[0].set_xlabel('distance from hyperplan')\r\nax[0].set_ylabel('category')\r\nax[0].legend(['normal pipeline', 'combined pipeline'])\r\n# plot correlation between classic and rescaled clf\r\nax[1].scatter(y_pred, y_pred_scaled, color='k')\r\nax[1].set_xlabel('normal pipeline distance')\r\nax[1].set_ylabel('combined pipeline distance')\r\nplt.show()\r\n```\r\n\r\n![precision](https://cloud.githubusercontent.com/assets/4881164/7471442/0d47b69e-f2f5-11e4-91b1-2f3157afdbf1.png)\r\n\r\nI think the issue is due to a precision error. \r\n\r\nHas anyone got any idea on how to circumvent this?\r\n\r\nThanks!"}}