{ "cells": [ { "cell_type": "markdown", "source": [ "# Example 3:Ridge Regression\n", "This example try to show how to use our PLQ Composite Decomposition tool to perform Ridge Regression." ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "markdown", "source": [ "# 1. Data Generation" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "source": [ "from plqcom import PLQLoss, plq_to_rehloss, affine_transformation\n", "import numpy as np\n", "from rehline import ReHLine\n", "\n", "n_samples, n_features = 1000, 5\n", "rng = np.random.RandomState(0)\n", "X = rng.randn(n_samples, n_features)\n", "beta = rng.randn(n_features)\n", "y = np.dot(X, beta) + rng.normal(scale=0.1, size=n_samples)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" }, "ExecuteTime": { "end_time": "2025-01-03T09:26:29.875018Z", "start_time": "2025-01-03T09:26:29.357142Z" } }, "outputs": [], "execution_count": 1 }, { "cell_type": "markdown", "source": [ "# 2.Create and Decompose the PLQ Loss" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "source": [ "plqloss = PLQLoss(quad_coef={'a': np.array([1.]), 'b': np.array([0.]), 'c': np.array([0.])},\n", " cutpoints=np.array([]))" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" }, "ExecuteTime": { "end_time": "2025-01-03T09:26:31.934888Z", "start_time": "2025-01-03T09:26:31.931230Z" } }, "outputs": [], "execution_count": 2 }, { "cell_type": "code", "source": [ "rehloss = plq_to_rehloss(plqloss)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" }, "ExecuteTime": { "end_time": "2025-01-03T09:26:32.245900Z", "start_time": "2025-01-03T09:26:32.242660Z" } }, "outputs": [], "execution_count": 3 }, { "cell_type": "code", "source": [ "rehloss.rehu_cut, rehloss.rehu_coef, rehloss.rehu_intercept" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" }, "ExecuteTime": { "end_time": "2025-01-03T09:26:33.249899Z", "start_time": "2025-01-03T09:26:33.244088Z" } }, "outputs": [ { "data": { "text/plain": [ "(array([[inf],\n", " [inf]]),\n", " array([[-1.41421356],\n", " [ 1.41421356]]),\n", " array([[-0.],\n", " [ 0.]]))" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 4 }, { "cell_type": "markdown", "source": [ "# 3. Broadcast to all samples" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "source": [ "rehloss = affine_transformation(rehloss, n=X.shape[0], c=1, p=-1, q=y)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" }, "ExecuteTime": { "end_time": "2025-01-03T09:26:38.134329Z", "start_time": "2025-01-03T09:26:38.131467Z" } }, "outputs": [], "execution_count": 5 }, { "cell_type": "markdown", "source": [ "# 4. Use the ReHLine to solve the problem" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "source": [ "clf = ReHLine()\n", "C = 1\n", "clf.Tau, clf.S, clf.T, clf.C = rehloss.rehu_cut, rehloss.rehu_coef, rehloss.rehu_intercept, C\n", "clf.fit(X=X)\n", "print('sol privided by rehline: %s' % clf.coef_)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" }, "ExecuteTime": { "end_time": "2025-01-03T09:27:10.594222Z", "start_time": "2025-01-03T09:27:10.579926Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sol privided by rehline: [ 0.31029299 -0.73855141 -1.53883908 -0.56076169 -1.6047202 ]\n" ] } ], "execution_count": 6 }, { "cell_type": "markdown", "source": [ "Compare with the solution provided by sklearn" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "source": [ "from sklearn.linear_model import Ridge\n", "clf1 = Ridge(alpha=0.5)\n", "clf1.fit(X, y)\n", "print('sol privided by sklearn: %s' % clf1.coef_)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" }, "ExecuteTime": { "end_time": "2025-01-03T09:27:16.507328Z", "start_time": "2025-01-03T09:27:16.078233Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sol privided by sklearn: [ 0.31017419 -0.73849146 -1.53893293 -0.56084128 -1.60476756]\n" ] } ], "execution_count": 7 } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }