{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from scipy.sparse.linalg import lsqr\n", "from sklearn.datasets import load_iris, load_breast_cancer\n", "from sklearn.linear_model import RidgeClassifier as skRidgeClassifier" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Implementation 1\n", "- convert classification problem to regression problem through binarizing labels in a one-vs-all fashion\n", "- based on scipy.sparse.linalg.lsqr (see the implementation of linear_model.Ridge)\n", "- center the dataset and calculate the intercept manually\n", "- similar to scikit-learn solver='lsqr'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "class RidgeClassifier:\n", " def __init__(self, alpha=1.0):\n", " self.alpha = alpha\n", "\n", " def _encode(self, y):\n", " classes = np.unique(y)\n", " y_train = np.full((y.shape[0], len(classes)), -1)\n", " for i, c in enumerate(classes):\n", " y_train[y == c, i] = 1\n", " if len(classes) == 2:\n", " y_train = y_train[:, 1].reshape(-1, 1)\n", " return classes, y_train\n", "\n", " def _solve_lsqr(self, X, y):\n", " coefs = np.zeros((y.shape[1], X.shape[1]))\n", " for i in range(y.shape[1]):\n", " cur_y = y[:, i]\n", " info = lsqr(X, cur_y, np.sqrt(self.alpha))\n", " coefs[i] = info[0]\n", " return coefs\n", "\n", " def fit(self, X, y):\n", " self.classes_, y_train = self._encode(y)\n", " X_mean = np.mean(X, axis=0)\n", " y_mean = np.mean(y_train, axis=0)\n", " X_train = X - X_mean\n", " y_train = y_train - y_mean\n", " self.coef_ = self._solve_lsqr(X_train, y_train)\n", " self.intercept_ = y_mean - np.dot(X_mean, self.coef_.T)\n", " return self\n", "\n", " def decision_function(self, X):\n", " scores = np.dot(X, self.coef_.T) + self.intercept_\n", " if scores.shape[1] == 1:\n", " return scores.ravel()\n", " else:\n", " return scores\n", "\n", " def predict(self, X):\n", " scores = self.decision_function(X)\n", " if len(scores.shape) == 1:\n", " indices = (scores > 0).astype(int)\n", " else:\n", " indices = np.argmax(scores, axis=1)\n", " return self.classes_[indices]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# binary classification\n", "for alpha in [0.1, 1, 10]:\n", " X, y = load_breast_cancer(return_X_y = True)\n", " clf1 = RidgeClassifier(alpha=alpha).fit(X, y)\n", " clf2 = skRidgeClassifier(alpha=alpha, solver='lsqr',\n", " # keep consisent with scipy default\n", " tol=1e-8).fit(X, y)\n", " assert clf1.coef_.shape == (1, X.shape[1])\n", " assert np.allclose(clf1.coef_, clf2.coef_)\n", " assert np.allclose(clf1.intercept_, clf2.intercept_)\n", " prob1 = clf1.decision_function(X)\n", " prob2 = clf2.decision_function(X)\n", " pred1 = clf1.predict(X)\n", " pred2 = clf2.predict(X)\n", " assert np.allclose(prob1, prob2)\n", " assert np.array_equal(pred1, pred2)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# multiclass classification\n", "for alpha in [0.1, 1, 10]:\n", " X, y = load_iris(return_X_y = True)\n", " clf1 = RidgeClassifier(alpha=alpha).fit(X, y)\n", " clf2 = skRidgeClassifier(alpha=alpha, solver='lsqr',\n", " # keep consisent with scipy default\n", " tol=1e-8).fit(X, y)\n", " assert clf1.coef_.shape == (len(np.unique(y)), X.shape[1])\n", " assert np.allclose(clf1.coef_, clf2.coef_)\n", " assert np.allclose(clf1.intercept_, clf2.intercept_)\n", " prob1 = clf1.decision_function(X)\n", " prob2 = clf2.decision_function(X)\n", " pred1 = clf1.predict(X)\n", " pred2 = clf2.predict(X)\n", " assert np.allclose(prob1, prob2)\n", " assert np.array_equal(pred1, pred2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }