Top Banner
Regression(1) February 7, 2019 0.1 Linear Regression: y = mx + c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000) y = 100-(x+np.random.normal(0,0.1,1000))*3 scatter(x,y) def predit(x): return -3*x+100 fitline = predit(x) plt.xlabel('Time') plt.ylabel('Speed') plt.plot(x,fitline,c='b') plt.show() 1
18

Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x =...

Aug 03, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

Regression(1)

February 7, 2019

0.1 Linear Regression:

y = mx + c

In [16]: import numpy as npfrom matplotlib.pyplot import *import matplotlib.pyplot as plt

x = np.random.normal(3,1,1000)y = 100-(x+np.random.normal(0,0.1,1000))*3

scatter(x,y)

def predit(x):return -3*x+100

fitline = predit(x)

plt.xlabel('Time')plt.ylabel('Speed')plt.plot(x,fitline,c='b')plt.show()

1

Page 2: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

In [ ]:

In [15]: %matplotlib inlineimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltplt.rcParams['figure.figsize'] = (20.0,10)

# Readind data

data = pd.read_csv('/home/ubuntu/potential-enigma-master/headbrain.csv')

data.head()

Out[15]: Gender Age Range Head Size(cmˆ3) Brain Weight(grams)0 1 1 4512 15301 1 1 3738 12972 1 1 4261 13353 1 1 3777 12824 1 1 4177 1590

0.2 Collecting x and y

In [16]: X = data['Head Size(cm^3)'].valuesY = data['Brain Weight(grams)'].values

2

Page 3: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

1 Calculating Mean X and Y

In [17]: mean_x=np.mean(X)mean_y=np.mean(Y)

n=len(X)

number = 0denom = 0for i in range(n):

number += (X[i]-mean_x) * (Y[i] - mean_y)denom += (X[i]-mean_x) ** 2

b1 = number / denomb0 = mean_y - (b1 * mean_x)

print(b1,b0)

0.28146265041713114 260.04052131790420.27944720086046 267.364647998834130.21375579817788407 506.086650989486430.20999399289007883 519.75701966017830.28035789953905893 264.055176686026470.27934490207246887 267.736400931112940.2868130790498729 240.597108817817120.28712689411350895 239.456707524792140.2860485884126271 243.37526134217070.2844763439317853 249.088784517663950.2717976737725513 295.162964883322960.2717081005564223 295.488473194843660.27194237715992337 294.637113994738570.294239971714564 213.607843548529440.28212866077126314 257.62024531132920.28795896441154306 236.432971083426540.28099537273250413 261.738604480567230.27684883344479927 276.807093260193940.23361861275900037 433.905350420398240.2169540767774588 494.464133548324750.21854175193534553 488.694535422666950.2082100682197233 526.23978685803380.2060808674661902 533.97728442843410.20545411005461087 536.25491557302140.23469201606689213 430.004611857775330.24017027603760743 410.09666135423790.2482765438943977 380.638552369985750.2555620705353283 354.163010038081440.25323936786980883 362.60369192371286

3

Page 4: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

0.253250438600284 362.563460982589850.2531577842310536 362.90016617847970.25607260276216254 352.30774023405470.25313810336145964 362.9716862925010.25222454028063923 366.29156681880920.2535349877874303 361.529411637759270.25032482067760187 373.1951318248580.24846079372565047 379.968990038064130.24910768164977767 377.61820478075560.24790147279455826 382.001557581645330.24785392986192992 382.17432819761050.24719513358191483 384.56838831972310.2459364560371903 389.14241189550060.25268135387029095 364.631510088981940.2581684026802432 344.69162101782470.2581083924691333 344.909697618582870.25995376782428736 338.20361915074080.2638769516510518 323.946802231315360.26077605658878733 335.21542871969360.2607978639059909 335.13618111300380.26081505338682087 335.07371468472660.2570162463824043 348.878547281333450.2575049718168502 347.102523176822160.24956908558925858 375.94146675838610.24957187324221985 375.931336451049330.2505005592590964 372.556499302732730.25076130715696654 371.60894364227690.24974861033473372 375.289075348306260.25769800897057943 346.40102778917570.2567576793563458 349.81817767202970.25650673458150103 350.730108866138270.2582258341297552 344.48291561495180.25934162626540963 340.4281364099510.246855694207316 385.801908142547860.24965810994363632 375.617953005837760.2510563407760134 370.53679396039580.2530340806040338 363.349704115157240.25139738662318406 369.297436229801630.25288292305091026 363.899009387617070.2546333476737726 357.53798107965110.2511067782592215 370.35350457205040.25384814557645113 360.39139887513930.2547493040856606 357.11659645738530.2537188332698067 360.861318706242060.2550884666212322 355.884082665249140.2537478730339442 360.755788448427670.25424572332322204 358.94660469846030.2544709156516549 358.12825767729237

4

Page 5: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

0.25500433887018686 356.189802202609030.25261410980083476 364.87587446992520.2523303467639307 365.90706695140980.2510384485157346 370.60181428325950.2525640918271337 365.05763936426230.24989125474300602 374.7707067723950.24999138237945326 374.406843786504740.24891765108642608 378.308774244341860.250115710873326 373.95503508895670.25205218566790416 366.91790202701920.25214188627729417 366.591930769463030.2518684480445171 367.585602999879370.2536120941838143 361.249207643986670.25188725063152156 367.51727455737670.2521380326410079 366.605934851207170.252019921169 367.03515094376260.2519519980662558 367.281982925944360.25190808744226056 367.44155376298920.25034330251594894 373.127968980269540.2503721695267258 373.023066506709940.25152391382678396 368.83763743965970.2514599307857189 369.070151270948940.2511740633710245 370.10899104356520.2514632829232464 369.057969631462130.2528045798162629 364.183708041201160.25687673484771895 349.385531021067440.2569329719856605 349.18116573636280.256920579792085 349.22619886324070.25689500280218264 349.31914542870630.25797792078619286 345.38383061336150.2579180375027814 345.60144595993460.2580017612057671 345.297194729813670.2648006884820674 320.58995038265220.26484407022282935 320.432301502813860.26487073064358285 320.335417758778140.2647532293582622 320.762416438061450.26470736761244373 320.929077635346740.26351688792382116 325.25527077755910.26327609192872686 326.1303213916980.26333701099639006 325.908942013894940.26192311518574396 331.04702745817260.2605102480955331 336.18137454106990.2593340457514167 340.455683933830640.26005367645765043 337.84055202021020.2540089174045731 359.807155408468360.2510098358165885 370.70579259049910.2511247178086617 370.28831240077330.24914677301382462 377.47614709369395

5

Page 6: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

0.24940671607675025 376.53151619663460.25077424972604156 371.561910455478260.2504829874156042 372.620355233697860.2486898661286105 379.136542858807840.24860168801672575 379.45698137327790.24856406649977714 379.5936976483880.2481184570279222 381.213038708691330.2496120176678089 375.78545194723030.24964683873855542 375.658912469986150.2522455110401543 366.21535925569990.2522234235210775 366.295625113632350.2516952508204127 368.215000250694630.25230501465262106 365.999123630135840.25423780090850495 358.97539468668640.2551591776302298 355.627119455269170.25514966186481763 355.66169966647520.25517317838339804 355.576240836405650.25436297367631266 358.520517904783450.2537885387286637 360.60800965698750.25355218315276684 361.466923825234860.25395724562855476 359.994930206470260.25495034371072356 356.386020156443350.2539537937413776 360.00747433534220.25299662762602354 363.485807921187640.2523808475626163 365.72354747515330.25134007719946444 369.505698191974940.25065978261669114 371.977882964890570.2507202064653808 371.75830320865830.2511237417899473 370.2918592445450.25150206572851586 368.91703324439390.2513313970244848 369.537241874600450.25010724903360043 373.98578534311150.24987394551492922 374.83360836115660.2485556559708946 379.624261439372160.24770532429679554 382.71435956725350.24943571456200198 376.426135945942750.25080349024848536 371.455650643673040.2500017957419118 374.36900171520690.2519547867404979 367.271848907281540.2517526833389123 368.00629096312970.250836824185641 371.334515397348350.24970982698311503 375.43001372080290.25001065403224443 374.336810762891560.2503859934712209 372.972830409072460.25026196916037713 373.423533708060060.24934986912812762 376.73809752820810.24940993525985136 376.51981771241130.2492718052762659 377.02178090710686

6

Page 7: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

0.24916955345439326 377.393363164907560.24910830022953998 377.61595686711950.24902215853141504 377.928995071171360.2490963005365572 377.659563650155630.24954354789705563 376.03427051634380.2488567840718407 378.52996446169950.24907011511974345 377.754721233882950.2490541157329612 377.81286287043360.250395249824641 372.93919289885650.24884821329828832 378.561110580461560.24875335175110386 378.90583664241060.24931672577659297 376.85854018799430.24928915206249247 376.958742832345930.249299400967132 376.92149839937460.25218295642189165 366.44268221057930.25197274948067655 367.20657246105670.25211831924351624 366.67757317133410.25069219784487606 371.860086299212750.2507940024859373 371.49012909270710.25167849269960096 368.27589912030580.2518068238010439 367.8095449806250.2518493216258088 367.65510824406080.2515064732062162 368.90101650762470.2519154598562986 367.41476247258950.2517994419715709 367.836370486636040.25184025728537485 367.68804798070550.2515504840148999 368.741081600267530.25282092419966384 364.12431268984950.2519596801465236 367.25406631107890.2531694563280585 362.85774987646280.2537303780910826 360.819364923150030.25349358922414367 361.67985366738790.2535099479671172 361.62040613347060.25354567469353645 361.490575511154360.2524776419211379 365.371797593115840.252811340896116 364.15913833407060.2560361442441639 352.440230180794970.25689526584257805 349.31818954212920.2567966697414657 349.676486941536840.2578829076241608 345.729107642387360.2575817503985224 346.82351045894610.25761589809639884 346.699418013029230.2565422390893954 350.60108578406620.25658657202972274 350.439980253034260.2563629381868898 351.25266375068360.2569455256840363 349.135545702403650.2576894107719317 346.432273570502960.2579679794103413 345.41995748931276

7

Page 8: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

0.25980543568775377 338.742656883155860.259698967789669 339.12956032633270.26041298551918673 336.534825922731330.26046523702265717 336.344944400060740.2604537836662183 336.386565800706760.2598295902158195 338.65487953200080.25888497956532025 342.087586664517060.25901948463537605 341.59879637499820.2605670669142441 335.97489543335780.26065579869979044 335.652444873473770.2604723196620546 336.31920614825960.2613287472626384 333.20695547497520.2613684297674717 333.06274958728430.26231587724921734 329.61973343395810.26266494505241406 328.351223982861140.26342933948939945 325.57342104944223

1.1 PLotting values and Regression Line

In [18]: import numpy as np

max_x = np.max(X) + 100min_x = np.min(X) - 100

x = np.linspace(min_x, max_x,500)

y = b0 + b1 * x

plt.plot(x,y,color='#58b970',label='Regression Line')plt.scatter(X,Y,c='#ef5423',label=' Scatter Plot')

plt.xlabel('Head Size in cm3')plt.ylabel('Brain Weight in grams')plt.legend()plt.show()

8

Page 9: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

In [19]: ss_t =0ss_r=0for i in range(n):

y_pred = b0+b1*X[i]ss_t += (Y[i]-mean_y) ** 2ss_r += (Y[i] - y_pred) ** 2

r2 = 1 - (ss_r/ss_t)print(r2)

0.6393117199570003

In [20]: from sklearn.linear_model import LinearRegressionfrom sklearn.metrics import mean_squared_error

X = X.reshape((n,1))

reg = LinearRegression()

reg = reg.fit(X,Y)Y_pred = reg.predict(X)

r2_score = reg.score(X,Y)print(r2_score)

0.639311719957

In [ ]:

9

Page 10: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

1.2 Logistic Regression

This Is Sigmoid Function:

y = 1/(1 + e−x)

In [25]: import matplotlib.pyplot as pltimport numpy as np%matplotlib inline%config InlineBackend.figure_format = 'retina'

def inverse_log_odds(z):return 1.0 / (1.0 + np.exp(-z))

z = np.arange(-7, 7, 0.1)phi_z = inverse_log_odds(z)

plt.plot(z, phi_z)plt.axvline(0.0, color='k')plt.ylim(-0.1, 1.1)plt.xlabel('z')plt.ylabel('$\phi (z)$')

plt.yticks([0.0, 0.5, 1.0])ax = plt.gca()ax.yaxis.grid(True)

plt.tight_layout()plt.show()

10

Page 11: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

In [ ]:

In [ ]:

In [2]: %matplotlib inlineimport pandas as pdimport numpy as npimport seaborn as snsimport matplotlib.pyplot as pltimport math

titanic_data = pd.read_table('titanic.txt')

titanic_data.head(10)

Out[2]: PassengerId Survived Pclass \0 1 0 31 2 1 12 3 1 33 4 1 14 5 0 35 6 0 36 7 0 1

11

Page 12: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

7 8 0 38 9 1 39 10 1 2

Name Sex Age SibSp \0 Braund, Mr. Owen Harris male 22.0 11 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 12 Heikkinen, Miss. Laina female 26.0 03 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 14 Allen, Mr. William Henry male 35.0 05 Moran, Mr. James male NaN 06 McCarthy, Mr. Timothy J male 54.0 07 Palsson, Master. Gosta Leonard male 2.0 38 Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) female 27.0 09 Nasser, Mrs. Nicholas (Adele Achem) female 14.0 1

Parch Ticket Fare Cabin Embarked0 0 A/5 21171 7.2500 NaN S1 0 PC 17599 71.2833 C85 C2 0 STON/O2. 3101282 7.9250 NaN S3 0 113803 53.1000 C123 S4 0 373450 8.0500 NaN S5 0 330877 8.4583 NaN Q6 0 17463 51.8625 E46 S7 1 349909 21.0750 NaN S8 2 347742 11.1333 NaN S9 0 237736 30.0708 NaN C

In [3]: sns.countplot(x= "Survived", hue="Sex", data = titanic_data)

Out[3]: <matplotlib.axes._subplots.AxesSubplot at 0x7f2e31868f60>

12

Page 13: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

In [14]: sns.countplot(x= "Survived", hue = "Name", data=titanic_data)

Out[14]: <matplotlib.axes._subplots.AxesSubplot at 0x7fa9347bf828>

13

Page 14: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

14

Page 15: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

In [18]: sns.countplot(x="Survived" , hue = "Ticket" , data = titanic_data)

Out[18]: <matplotlib.axes._subplots.AxesSubplot at 0x7fa9326b29e8>

15

Page 16: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

16

Page 17: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

In [19]: sns.countplot(x = "Survived", data=titanic_data)

Out[19]: <matplotlib.axes._subplots.AxesSubplot at 0x7fa931e25b38>

In [23]: sns.countplot(x = "Survived" , hue= "Parch",data = titanic_data)

Out[23]: <matplotlib.axes._subplots.AxesSubplot at 0x7fa9324282e8>

17

Page 18: Regression(1) · Regression(1) February 7, 2019 0.1 Linear Regression: y = mx +c In [16]: import numpy as np from matplotlib.pyplot import * import matplotlib.pyplot as plt x = np.random.normal(3,1,1000)

In [ ]:

18