Skip to content

Instantly share code, notes, and snippets.

@jonathan-taylor
Created October 20, 2015 22:33
Show Gist options
  • Save jonathan-taylor/f62b82088d6b3946299d to your computer and use it in GitHub Desktop.
Save jonathan-taylor/f62b82088d6b3946299d to your computer and use it in GitHub Desktop.
variability of active set in cross validation
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/jonathantaylor/anaconda/lib/python2.7/site-packages/selection/algorithms/lasso.py:32: UserWarning: cvx not available\n",
" warnings.warn('cvx not available')\n"
]
},
{
"data": {
"text/plain": [
"Loading required package: Matrix\n",
"Loading required package: foreach\n",
"foreach: simple, scalable parallel programming from Revolution Analytics\n",
"Use Revolution R for scalability, fault tolerance and more.\n",
"http://www.revolutionanalytics.com\n",
"Loaded glmnet 2.0-2\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"%load_ext rpy2.ipython\n",
"from selection.algorithms.lasso import instance\n",
"%R library(glmnet)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def active_set(X, Y ):\n",
" \n",
" %R -i X,Y\n",
" %R lam.min = cv.glmnet(X,as.matrix(Y))$lambda.min\n",
" %R -o B B = as.matrix(glmnet(X, as.matrix(Y), lambda=lam.min)$beta)\n",
" return tuple(np.nonzero(np.squeeze(B) != 0)[0])\n",
"\n",
"X, Y = instance(n=100, p=200, rho=0.3)[:2]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of different active sets in 50 assignments to folds: 4\n"
]
}
],
"source": [
"X, Y = instance(n=100, p=200, rho=0.3)[:2]\n",
"active_sets = set([active_set(X, Y) for _ in range(50)])\n",
"print 'Number of different active sets in 50 assignments to folds:', len(active_sets)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of different active sets in 50 assignments to folds: 7\n"
]
}
],
"source": [
"X, Y = instance(n=100, p=200, rho=0.3)[:2]\n",
"active_sets = set([active_set(X, Y) for _ in range(50)])\n",
"print 'Number of different active sets in 50 assignments to folds:', len(active_sets)\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of different active sets in 50 assignments to folds: 2\n"
]
}
],
"source": [
"X, Y = instance(n=100, p=200, rho=0.3)[:2]\n",
"active_sets = set([active_set(X, Y) for _ in range(50)])\n",
"print 'Number of different active sets in 50 assignments to folds:', len(active_sets)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment