|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | 3 | "name": "", |
4 | | - "signature": "sha256:a091baac3cb82ac71775c03f73be1925fefe2d659bb7359de4bb88887fbe7d24" |
| 4 | + "signature": "sha256:379319350ffd7e7539cf8f11b3a7c15b6f1d1013bcb445b55714c76bf0855fa2" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
7 | 7 | "nbformat_minor": 0, |
|
714 | 714 | "language": "python", |
715 | 715 | "metadata": {}, |
716 | 716 | "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": [] |
718 | 741 | }, |
719 | 742 | { |
720 | 743 | "cell_type": "code", |
|
740 | 763 | " y_interc = y_avg - slope*x_avg\n", |
741 | 764 | " return (slope, y_interc)\n", |
742 | 765 | "\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", |
743 | 776 | "def cy_numpy_lstsqr(x, y):\n", |
744 | 777 | " \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n", |
745 | 778 | " X = np.vstack([x, np.ones(len(x))]).T\n", |
|
752 | 785 | "language": "python", |
753 | 786 | "metadata": {}, |
754 | 787 | "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": [] |
756 | 809 | }, |
757 | 810 | { |
758 | 811 | "cell_type": "markdown", |
|
0 commit comments