{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from sklearn.datasets import load_iris\n", "from sklearn.tree import DecisionTreeClassifier\n", "from sklearn.ensemble import RandomForestClassifier as skRandomForestClassifier" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def accuracy_score(y_true, y_pred):\n", " return np.mean(y_true == y_pred)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "class RandomForestClassifier():\n", " def __init__(self, n_estimators=100, max_depth=None, max_features=\"auto\",\n", " oob_score=False, random_state=0):\n", " self.n_estimators = n_estimators\n", " self.max_depth = max_depth\n", " self.max_features = max_features\n", " self.oob_score = oob_score\n", " self.random_state = random_state\n", "\n", " def fit(self, X, y):\n", " self.n_features_ = X.shape[1]\n", " self.classes_, y_train = np.unique(y, return_inverse=True)\n", " self.n_classes_ = len(self.classes_)\n", " MAX_INT = np.iinfo(np.int32).max\n", " rng = np.random.RandomState(self.random_state)\n", " self.estimators_ = []\n", " for i in range(self.n_estimators):\n", " est = DecisionTreeClassifier(max_depth=self.max_depth,\n", " max_features=self.max_features,\n", " random_state=rng.randint(MAX_INT))\n", " sample_rng = np.random.RandomState(est.random_state)\n", " sample_indices = sample_rng.randint(0, X.shape[0], X.shape[0])\n", " sample_counts = np.bincount(sample_indices, minlength=X.shape[0])\n", " est.fit(X, y_train, sample_weight=sample_counts)\n", " self.estimators_.append(est)\n", " if self.oob_score:\n", " self._set_oob_score(X, y_train)\n", " return self\n", "\n", " def _set_oob_score(self, X, y):\n", " predictions = np.zeros((X.shape[0], self.n_classes_))\n", " for i in range(self.n_estimators):\n", " sample_rng = np.random.RandomState(self.estimators_[i].random_state)\n", " sample_indices = sample_rng.randint(0, X.shape[0], X.shape[0])\n", " mask = np.ones(X.shape[0], dtype=bool)\n", " mask[sample_indices] = False\n", " predictions[mask] += self.estimators_[i].predict_proba(X[mask])\n", " self.oob_decision_function_ = predictions / np.sum(predictions, axis=1)[:, np.newaxis]\n", " self.oob_score_ = accuracy_score(y, np.argmax(predictions, axis=1)) \n", "\n", " def predict_proba(self, X):\n", " proba = np.zeros((X.shape[0], self.n_classes_))\n", " for i in range(self.n_estimators):\n", " proba += self.estimators_[i].predict_proba(X)\n", " proba /= self.n_estimators\n", " return proba\n", "\n", " def predict(self, X):\n", " proba = self.predict_proba(X)\n", " return self.classes_[np.argmax(proba, axis=1)]\n", "\n", " @property\n", " def feature_importances_(self):\n", " all_importances = np.zeros(self.n_features_)\n", " for i in range(self.n_estimators):\n", " all_importances += self.estimators_[i].feature_importances_\n", " all_importances /= self.n_estimators \n", " return all_importances / np.sum(all_importances)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "X, y = load_iris(return_X_y=True)\n", "clf1 = RandomForestClassifier(random_state=0, oob_score=True).fit(X, y)\n", "clf2 = skRandomForestClassifier(random_state=0, oob_score=True).fit(X, y)\n", "pred1 = clf1.predict(X)\n", "pred2 = clf2.predict(X)\n", "assert np.array_equal(pred1, pred2)\n", "prob1 = clf1.predict_proba(X)\n", "prob2 = clf2.predict_proba(X)\n", "assert np.allclose(prob1, prob2)\n", "assert np.allclose(clf1.oob_decision_function_, clf2.oob_decision_function_)\n", "assert np.allclose(clf1.oob_score_, clf2.oob_score_)\n", "assert np.allclose(clf1.feature_importances_, clf2.feature_importances_)" ] } ], "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 }