Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": null,
  6. "metadata": {
  7. "collapsed": true
  8. },
  9. "outputs": [],
  10. "source": [
  11. "#Linear_Regression_Plot_Channing7June2015\n",
  12. "\n",
  13. "import pylab as pl\n",
  14. "import numpy as np\n",
  15. "from sklearn import linear_model\n",
  16. "\n",
  17. "#Load data\n",
  18. "BSA=[[50,1.22],[25,0.958],[12.5,0.792],[6.25,0.758]]\n",
  19. "BSA=np.array(BSA)\n",
  20. "\n",
  21. "#Create linear regression object\n",
  22. "regr=linear_model.LinearRegression()\n",
  23. "\n",
  24. "#Define X and Y, use only one feature\n",
  25. "X=BSA[:,[0]]\n",
  26. "Y=BSA[:,[1]]\n",
  27. "\n",
  28. "#Fit\n",
  29. "regr.fit(X,Y)\n",
  30. "\n",
  31. "#Plotting\n",
  32. "pl.scatter(X,Y,color='black')\n",
  33. "pl.plot(X,regr.predict(X),color='red',linewidth=1)\n",
  34. "\n",
  35. "#Fomula\n",
  36. "k=regr.coef_\n",
  37. "b=regr.intercept_\n",
  38. "R_square=1-np.mean((regr.predict(X)-Y)**2)\n",
  39. "\n",
  40. "pl.title(\"y=%.4lf x + %.4lf \\n R^2=%.4lf\" %(k,b,R_square))\n",
  41. "\n",
  42. "pl.show()\n",
  43. "\n"
  44. ]
  45. },
  46. {
  47. "cell_type": "code",
  48. "execution_count": null,
  49. "metadata": {
  50. "collapsed": true
  51. },
  52. "outputs": [],
  53. "source": []
  54. }
  55. ],
  56. "metadata": {
  57. "kernelspec": {
  58. "display_name": "Python 2",
  59. "language": "python",
  60. "name": "python2"
  61. },
  62. "language_info": {
  63. "codemirror_mode": {
  64. "name": "ipython",
  65. "version": 2
  66. },
  67. "file_extension": ".py",
  68. "mimetype": "text/x-python",
  69. "name": "python",
  70. "nbconvert_exporter": "python",
  71. "pygments_lexer": "ipython2",
  72. "version": "2.7.10"
  73. }
  74. },
  75. "nbformat": 4,
  76. "nbformat_minor": 0
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement