what makes a model linear
play

What makes a model linear IN TR OD U C TION TO L IN E AR MOD E L - PowerPoint PPT Presentation

What makes a model linear IN TR OD U C TION TO L IN E AR MOD E L IN G IN P YTH ON Jason Vest u to Data Scientist Ta y lor Series Things to kno w: 1. appro x imate an y c u r v e 2. pol y nomial form : y = a0 + a1*x + a2*x**2 + a3*x**3 + ....


  1. What makes a model linear IN TR OD U C TION TO L IN E AR MOD E L IN G IN P YTH ON Jason Vest u to Data Scientist

  2. Ta y lor Series Things to kno w: 1. appro x imate an y c u r v e 2. pol y nomial form : y = a0 + a1*x + a2*x**2 + a3*x**3 + .... + an*x**n 3. o � en , � rst order is eno u gh : y = a0 + a1*x INTRODUCTION TO LINEAR MODELING IN PYTHON

  3. Series Terms : a 0=1 INTRODUCTION TO LINEAR MODELING IN PYTHON

  4. Series Terms : a 1=1 INTRODUCTION TO LINEAR MODELING IN PYTHON

  5. Series Terms : a 2=1 INTRODUCTION TO LINEAR MODELING IN PYTHON

  6. Combining all Terms INTRODUCTION TO LINEAR MODELING IN PYTHON

  7. Real Data INTRODUCTION TO LINEAR MODELING IN PYTHON

  8. Zeroth Order INTRODUCTION TO LINEAR MODELING IN PYTHON

  9. First Order INTRODUCTION TO LINEAR MODELING IN PYTHON

  10. Higher Order INTRODUCTION TO LINEAR MODELING IN PYTHON

  11. O v er - fitting INTRODUCTION TO LINEAR MODELING IN PYTHON

  12. Let ' s practice ! IN TR OD U C TION TO L IN E AR MOD E L IN G IN P YTH ON

  13. Interpreting Slope and Intercept IN TR OD U C TION TO L IN E AR MOD E L IN G IN P YTH ON Jason Vest u to Data Scientist

  14. Reminder : Terminolog y Re v ie w: y = a0 + a1*x x = independent v ariable , e . g . time y = dependent v ariable , e . g . distance tra v eled xp = 10; yp = a0 + a1*xp , " model prediction " INTRODUCTION TO LINEAR MODELING IN PYTHON

  15. Intercept x0 = 0 print(y(x0)) 100 INTRODUCTION TO LINEAR MODELING IN PYTHON

  16. Slope slope = (225 - 100) / (5 - 0) print(slope) 25 INTRODUCTION TO LINEAR MODELING IN PYTHON

  17. A v erage Slope slope = (350 - 100) / (10 - 0) print(slope) 25 INTRODUCTION TO LINEAR MODELING IN PYTHON

  18. Rescaling v ers u s Dependenc y slope = (212-32)/(100-0) # 180/100 = 9/5 intercept = 32 INTRODUCTION TO LINEAR MODELING IN PYTHON

  19. Let ' s practice ! IN TR OD U C TION TO L IN E AR MOD E L IN G IN P YTH ON

  20. Model Optimi z ation IN TR OD U C TION TO L IN E AR MOD E L IN G IN P YTH ON Jason Vest u to Data Scientist

  21. Resid u als residuals = y_model - y_data len(residuals) == len(y_data) True INTRODUCTION TO LINEAR MODELING IN PYTHON

  22. Resid u als S u mmed residuals = y_model - y_data print(np.sum(residuals)) 0.0 INTRODUCTION TO LINEAR MODELING IN PYTHON

  23. Resid u als Sq u ared residuals_squared = np.square(y_model - y_ print(np.sum(residuals_squared)) 65.1 INTRODUCTION TO LINEAR MODELING IN PYTHON

  24. RSS resid_squared = np.square(y_model - y_data RSS = np.sum(resid_squared) INTRODUCTION TO LINEAR MODELING IN PYTHON

  25. RSS RSS = np.sum(np.square(y_model - y_data)) print(RSS) 5.9 INTRODUCTION TO LINEAR MODELING IN PYTHON

  26. Variation of RSS Minim u m v al u e of RSS gi v es minim u m resid u als Minim u m resid u als gi v e the best model INTRODUCTION TO LINEAR MODELING IN PYTHON

  27. Let ' s practice ! IN TR OD U C TION TO L IN E AR MOD E L IN G IN P YTH ON

  28. Least - Sq u ares Optimi z ation IN TR OD U C TION TO L IN E AR MOD E L IN G IN P YTH ON Jason Vest u to Data Scientist

  29. Minima of RSS Se � ing RSS slope = z ero , and some calc u l u s , y ields : a = covariance ( x , y )/ variance ( x ) 1 a = mean ( y ) − a × mean ( x ) 0 1 INTRODUCTION TO LINEAR MODELING IN PYTHON

  30. Optimi z ed b y N u mp y N u mp y e x pressions of optimal slope and intercept x_mean = np.mean(x) y_mean = np.mean(y) x_dev = x - x_mean y_dev = y - y_mean a1 = np.sum( x_dev * y_dev ) / np.sum( x_dev**2 ) a0 = y_mean - (a1*x_mean) INTRODUCTION TO LINEAR MODELING IN PYTHON

  31. Optimi z ed b y Scip y from scipy import optimize x_data, y_data = load_data() def model_func(x, a0, a1): return a0 + (a1*x) param_opt, param_cov = optimize.curve_fit(model_func, x_data, y_data) a0 = param_opt[0] # a0 is the intercept in y = a0 + a1*x a1 = param_opt[1] # a1 is the slope in y = a0 + a1*x INTRODUCTION TO LINEAR MODELING IN PYTHON

  32. Optimi z ed b y Statsmodels from statsmodels.formula.api import ols x_data, y_data = load_data() df = pd.DataFrame(dict(x_name=x_data, y_name=y_data)) model_fit = ols(formula="y_name ~ x_name", data=df).fit() y_model = model_fit.predict(df) x_model = x_data a0 = model_fit.params['Intercept'] a1 = model_fit.params['x_name'] INTRODUCTION TO LINEAR MODELING IN PYTHON

  33. Let ' s practice ! IN TR OD U C TION TO L IN E AR MOD E L IN G IN P YTH ON

Recommend


More recommend