Skip to content

Commit ad32c82

Browse files
committed
removed temp cython files
1 parent f5e08cd commit ad32c82

6 files changed

Lines changed: 57 additions & 7154 deletions

File tree

benchmarks/.ipynb_checkpoints/cython_least_squares-checkpoint.ipynb

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:a091baac3cb82ac71775c03f73be1925fefe2d659bb7359de4bb88887fbe7d24"
4+
"signature": "sha256:379319350ffd7e7539cf8f11b3a7c15b6f1d1013bcb445b55714c76bf0855fa2"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -714,7 +714,30 @@
714714
"language": "python",
715715
"metadata": {},
716716
"outputs": [],
717-
"prompt_number": 15
717+
"prompt_number": 2
718+
},
719+
{
720+
"cell_type": "code",
721+
"collapsed": false,
722+
"input": [
723+
"%%cython\n",
724+
"import numpy as np\n",
725+
"import scipy.stats\n",
726+
"cimport numpy as np\n",
727+
"\n",
728+
"def cy_lin_lstsqr_mat2(x, y):\n",
729+
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
730+
" cdef double x_avg = sum(x)/len(x)\n",
731+
" cdef double y_avg = sum(y)/len(y)\n",
732+
" cdef double var_x = sum([(x_i - x_avg)**2 for x_i in x])\n",
733+
" cdef double cov_xy = sum([(x_i - x_avg)*(y_i - y_avg) for x_i,y_i in zip(x,y)])\n",
734+
" cdef double slope = cov_xy / var_x\n",
735+
" cdef double y_interc = y_avg - slope*x_avg\n",
736+
" return (slope, y_interc)"
737+
],
738+
"language": "python",
739+
"metadata": {},
740+
"outputs": []
718741
},
719742
{
720743
"cell_type": "code",
@@ -740,6 +763,16 @@
740763
" y_interc = y_avg - slope*x_avg\n",
741764
" return (slope, y_interc)\n",
742765
"\n",
766+
"def cy_lin_lstsqr_mat2(x, y):\n",
767+
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
768+
" cdef double x_avg = sum(x)/len(x)\n",
769+
" cdef double y_avg = sum(y)/len(y)\n",
770+
" cdef double var_x = sum([(x_i - x_avg)**2 for x_i in x])\n",
771+
" cdef double cov_xy = sum([(x_i - x_avg)*(y_i - y_avg) for x_i,y_i in zip(x,y)])\n",
772+
" cdef double slope = cov_xy / var_x\n",
773+
" cdef double y_interc = y_avg - slope*x_avg\n",
774+
" return (slope, y_interc)\n",
775+
"\n",
743776
"def cy_numpy_lstsqr(x, y):\n",
744777
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
745778
" X = np.vstack([x, np.ones(len(x))]).T\n",
@@ -752,7 +785,27 @@
752785
"language": "python",
753786
"metadata": {},
754787
"outputs": [],
755-
"prompt_number": 16
788+
"prompt_number": 3
789+
},
790+
{
791+
"cell_type": "code",
792+
"collapsed": false,
793+
"input": [
794+
"import timeit\n",
795+
"\n",
796+
"for lab,appr in zip([\"matrix approach\",\"classic approach\",\n",
797+
" \"numpy function\",\"scipy function\"],\n",
798+
" [(lin_lstsqr_mat, cy_lin_lstsqr_mat), \n",
799+
" (classic_lstsqr, cy_classic_lstsqr),\n",
800+
" (cy_lin_lstsqr_mat2, cy_lin_lstsqr_mat2),\n",
801+
" (scipy_lstsqr, cy_scipy_lstsqr)]):\n",
802+
" print(\"\\n\\n{}: \".format(lab))\n",
803+
" %timeit appr[0](x, y)\n",
804+
" %timeit appr[1](x, y)"
805+
],
806+
"language": "python",
807+
"metadata": {},
808+
"outputs": []
756809
},
757810
{
758811
"cell_type": "markdown",

0 commit comments

Comments
 (0)