Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "#Linear_Regression_Plot_Channing7June2015\n",
- "\n",
- "import pylab as pl\n",
- "import numpy as np\n",
- "from sklearn import linear_model\n",
- "\n",
- "#Load data\n",
- "BSA=[[50,1.22],[25,0.958],[12.5,0.792],[6.25,0.758]]\n",
- "BSA=np.array(BSA)\n",
- "\n",
- "#Create linear regression object\n",
- "regr=linear_model.LinearRegression()\n",
- "\n",
- "#Define X and Y, use only one feature\n",
- "X=BSA[:,[0]]\n",
- "Y=BSA[:,[1]]\n",
- "\n",
- "#Fit\n",
- "regr.fit(X,Y)\n",
- "\n",
- "#Plotting\n",
- "pl.scatter(X,Y,color='black')\n",
- "pl.plot(X,regr.predict(X),color='red',linewidth=1)\n",
- "\n",
- "#Fomula\n",
- "k=regr.coef_\n",
- "b=regr.intercept_\n",
- "R_square=1-np.mean((regr.predict(X)-Y)**2)\n",
- "\n",
- "pl.title(\"y=%.4lf x + %.4lf \\n R^2=%.4lf\" %(k,b,R_square))\n",
- "\n",
- "pl.show()\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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement