Skip to content

Instantly share code, notes, and snippets.

@Saurabh7
Created June 7, 2016 07:47
Show Gist options
  • Save Saurabh7/b0dbd19e9ab7ae06108f9e9b37430f69 to your computer and use it in GitHub Desktop.
Save Saurabh7/b0dbd19e9ab7ae06108f9e9b37430f69 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from modshogun import RealFeatures, RegressionLabels, LeastAngleRegression\n",
"import numpy as np\n",
"from time import time\n",
"from sklearn.linear_model import LassoLars\n",
"from sklearn.metrics import mean_squared_error as mse"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ran1=[30]\n",
"ran2=[300]#,100000]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"num_feat=10\n",
"num_vec=100\n",
"timesg=[]\n",
"timesk=[]\n",
"outsg=[]\n",
"outsk=[]\n",
"\n",
"\n",
"def gen_data(num_feat, num_vec):\n",
" X=np.random.rand(num_vec, num_feat)\n",
" xtest=np.random.rand(num_vec/10, num_feat)\n",
" \n",
" w=np.zeros((1, num_feat))\n",
"\n",
" var=np.array(range(0, num_feat))\n",
" var=np.random.permutation(var)\n",
"\n",
" for i in range(num_feat/2):\n",
" w[0][var[i]]=np.random.randint(100)\n",
"\n",
" y=np.dot(w,X.T)\n",
" ytest=np.dot(w, xtest.T)\n",
" y=np.array(y)\n",
" y=np.reshape(y,(num_vec,))\n",
" ytest=np.reshape(ytest,(num_vec/10,))\n",
" return X, y, xtest, ytest\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\n",
"def run():\n",
" for f in ran1:\n",
" for v in ran2:\n",
" X,y, xtest, ytest =gen_data(f,v)\n",
" feat = RealFeatures(X.T)\n",
" lab = RegressionLabels(y) \n",
" ftest = RealFeatures(xtest.T)\n",
" ltest = RegressionLabels(ytest) \n",
" \n",
" t1=time()\n",
" lambda1=0.01\n",
" modelsg = LeastAngleRegression()\n",
" modelsg.set_max_l1_norm(lambda1)\n",
" modelsg.set_labels(lab)\n",
" modelsg.train(feat)\n",
" t2=time()\n",
"\n",
" timesg.append(t2-t1)\n",
" t3=time()\n",
" modelsk = LassoLars(alpha=lambda1)\n",
" modelsk.fit(X, y)\n",
" #out = model.coef\n",
" t4=time()\n",
" timesk.append(t4-t3)\n",
" sgout=modelsg.apply(ftest)\n",
" skout=modelsk.predict(xtest)\n",
" \n",
" sgout1=sgout.get_labels()\n",
" print sgout1, skout, ytest\n",
" outsg.append(mse(ytest, sgout1))\n",
" outsk.append(mse(ytest, skout))\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 385.72652834 385.72484367 385.72638108 385.72121201 385.71863408\n",
" 385.72195451 385.71981821 385.72221152 385.72321434 385.72273265\n",
" 385.72188016 385.71937371 385.7275607 385.71864802 385.71961235\n",
" 385.72395046 385.71860946 385.72377739 385.71821185 385.71892004\n",
" 385.72427833 385.72588812 385.72738081 385.72476188 385.72621216\n",
" 385.72158667 385.72294017 385.72199666 385.7268188 385.72685648] [ 425.75474947 442.53869976 321.77677581 353.07133606 363.24071599\n",
" 437.0867815 411.56535414 409.15885936 364.21720537 431.41260143\n",
" 407.19071325 351.41136834 333.21237432 370.62953131 382.19169385\n",
" 447.80262874 428.96436776 408.35301406 428.3309812 513.57059017\n",
" 344.16988754 507.08600343 429.48442884 384.75214797 508.03107014\n",
" 493.9912634 289.45732391 294.71884688 381.75522255 409.03580245] [ 426.13074984 442.87998496 321.17438232 352.66419854 363.22309136\n",
" 437.54651105 412.32216736 409.4405144 363.91208429 431.76318783\n",
" 407.45448377 351.28938453 332.62279038 370.52934846 381.84746235\n",
" 448.20390181 428.99022733 408.80286699 429.48047462 514.83201116\n",
" 343.59235217 508.02597633 429.99167192 384.5365857 509.06755368\n",
" 494.97747608 288.04436562 294.06963151 381.54572513 408.85673744]\n"
]
}
],
"source": [
"run()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------\n",
"\n",
"N: 300 D: 30 shogun: 0.000226 , mse: 3705.699514 | scikit: 0.003624 , mse: 0.392170 \n"
]
}
],
"source": [
"count=0\n",
"for i in ran1:\n",
" for j in ran2:\n",
" print \"------------\\n\"\n",
" print 'N:', j , 'D:', i, 'shogun: %.6f , mse: %.6f | scikit: %.6f , mse: %.6f ' % (timesg[count], outsg[count]\\\n",
" ,timesk[count], outsk[count])\n",
" count+=1"
]
},
{
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment