{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from sklearn.base import clone\n", "from sklearn.datasets import load_breast_cancer, load_iris\n", "from sklearn.ensemble import RandomForestClassifier\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.feature_selection import RFE as skRFE" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "class RFE():\n", " def __init__(self, estimator):\n", " self.estimator = estimator\n", "\n", " def fit(self, X, y):\n", " n_features_to_select = X.shape[1] / 2\n", " support = np.ones(X.shape[1], dtype=np.bool)\n", " ranking = np.ones(X.shape[1], dtype=np.int)\n", " while np.sum(support) > n_features_to_select:\n", " est = clone(self.estimator)\n", " est.fit(X[:, support], y)\n", " if hasattr(est, \"feature_importances_\"):\n", " importances = est.feature_importances_\n", " elif hasattr(est, \"coef_\"):\n", " if est.coef_.ndim == 1:\n", " importances = np.abs(est.coef_)\n", " else:\n", " importances = np.linalg.norm(est.coef_, ord=1, axis=0)\n", " cur_feature = np.arange(X.shape[1])[support][np.argmin(importances)]\n", " support[cur_feature] = False\n", " ranking[~support] += 1\n", " self.support_ = support\n", " self.ranking_ = ranking\n", " self.estimator_ = clone(self.estimator)\n", " self.estimator_.fit(X[:, support], y)\n", " return self\n", "\n", " def transform(self, X):\n", " return X[:, self.support_]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "X, y = load_breast_cancer(return_X_y=True)\n", "clf = RandomForestClassifier(random_state=0)\n", "est1 = RFE(estimator=clf).fit(X, y)\n", "est2 = skRFE(estimator=clf).fit(X, y)\n", "assert np.array_equal(est1.support_, est2.support_)\n", "assert np.array_equal(est1.ranking_, est2.ranking_)\n", "Xt1 = est1.transform(X)\n", "Xt2 = est2.transform(X)\n", "assert np.allclose(Xt1, Xt2)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "X, y = load_breast_cancer(return_X_y=True)\n", "clf = LogisticRegression(max_iter=15000, random_state=0)\n", "est1 = RFE(estimator=clf).fit(X, y)\n", "est2 = skRFE(estimator=clf).fit(X, y)\n", "assert np.array_equal(est1.support_, est2.support_)\n", "assert np.array_equal(est1.ranking_, est2.ranking_)\n", "Xt1 = est1.transform(X)\n", "Xt2 = est2.transform(X)\n", "assert np.allclose(Xt1, Xt2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "X, y = load_iris(return_X_y=True)\n", "clf = LogisticRegression(max_iter=15000, random_state=0)\n", "est1 = RFE(estimator=clf).fit(X, y)\n", "est2 = skRFE(estimator=clf).fit(X, y)\n", "assert np.array_equal(est1.support_, est2.support_)\n", "assert np.array_equal(est1.ranking_, est2.ranking_)\n", "Xt1 = est1.transform(X)\n", "Xt2 = est2.transform(X)\n", "assert np.allclose(Xt1, Xt2)" ] } ], "metadata": { "kernelspec": { "display_name": "dev", "language": "python", "name": "dev" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }