modeling real data
play

Modeling Real Data IN TR OD U C TION TO L IN E AR MOD E L IN G IN - PowerPoint PPT Presentation

Modeling Real Data 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 Scikit - Learn from sklearn.linear_model import LinearRegression # Initialize a general model model =


  1. Modeling Real Data 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. Scikit - Learn from sklearn.linear_model import LinearRegression # Initialize a general model model = LinearRegression(fit_intercept=True) # Load and shape the data x_raw, y_raw = load_data() x_data = x_raw.reshape(len(y_raw),1) y_data = y_raw.reshape(len(y_raw),1) # Fit the model to the data model_fit = model.fit(x_data, y_data) INTRODUCTION TO LINEAR MODELING IN PYTHON

  3. Predictions and Parameters # Extract the linear model parameters intercept = model.intercept_[0] slope = model.coef_[0,0] # Use the model to make predictions future_x = 2100 future_y = model.predict(future_x) INTRODUCTION TO LINEAR MODELING IN PYTHON

  4. statsmodels x, y = load_data() df = pd.DataFrame(dict(times=x_data, distances=y_data)) fig = df.plot('times', 'distances') model_fit = ols(formula="distances ~ times", data=df).fit() INTRODUCTION TO LINEAR MODELING IN PYTHON

  5. Uncertaint y a0 = model_fit.params['Intercept'] a1 = model_fit.params['times'] e0 = model_fit.bse['Intercept'] e1 = model_fit.bse['times'] intercept = a0 slope = a1 uncertainty_in_intercept = e0 uncertainty_in_slope = e1 INTRODUCTION TO LINEAR MODELING IN PYTHON

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

  7. The Limits of Prediction 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

  8. Interpolation INTRODUCTION TO LINEAR MODELING IN PYTHON

  9. Interpolation INTRODUCTION TO LINEAR MODELING IN PYTHON

  10. Interpolation INTRODUCTION TO LINEAR MODELING IN PYTHON

  11. Interpolation INTRODUCTION TO LINEAR MODELING IN PYTHON

  12. Interpolation INTRODUCTION TO LINEAR MODELING IN PYTHON

  13. Domain of Validit y z oom in : data looks linear model ass u mption : a2*x**2 + a3*x**3 + ... = z ero . b u ild a linear model : a0 + a1*x z oom o u t : y o u r model breaks INTRODUCTION TO LINEAR MODELING IN PYTHON

  14. E x trapolating Too Far INTRODUCTION TO LINEAR MODELING IN PYTHON

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

  16. Goodness - of - Fit 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

  17. 3 Different R ' s B u ilding Models : RSS E v al u ating Models : RMSE R - sq u ared INTRODUCTION TO LINEAR MODELING IN PYTHON

  18. RMSE residuals = y_model - y_data RSS = np.sum( np.square(residuals) ) mean_squared_residuals = np.sum( np.square(residuals) ) / len(residuals) MSE = np.mean( np.square(residuals) ) RMSE = np.sqrt(np.mean( np.square(residuals))) RMSE = np.std(residuals) INTRODUCTION TO LINEAR MODELING IN PYTHON

  19. R - Sq u ared in Code De v iations : R - sq u ared : deviations = np.mean(y_data) - y_data r_squared = 1 - (RSS / VAR) VAR = np.sum(np.square(deviations)) r = correlation(y_data, y_model) Resid u als : residuals = y_model - y_data RSS = np.sum(np.square(residuals)) INTRODUCTION TO LINEAR MODELING IN PYTHON

  20. R - Sq u ared in Data INTRODUCTION TO LINEAR MODELING IN PYTHON

  21. R - Sq u ared in Data INTRODUCTION TO LINEAR MODELING IN PYTHON

  22. R - Sq u ared in Data INTRODUCTION TO LINEAR MODELING IN PYTHON

  23. R - Sq u ared in Data INTRODUCTION TO LINEAR MODELING IN PYTHON

  24. RMSE v s R - Sq u ared RMSE : ho w m u ch v ariation is resid u al R - sq u ared : w hat fraction of v ariation is linear INTRODUCTION TO LINEAR MODELING IN PYTHON

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

  26. Standard Error 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

  27. Uncertaint y in Predictions Model Predictions and RMSE : predictions compared to data gi v es resid u als resid u als ha v e spread RMSE , meas u res resid u al spread RMSE , q u anti � es prediction goodness INTRODUCTION TO LINEAR MODELING IN PYTHON

  28. Uncertaint y in Parameters Model Parameters and Standard Error : Parameter v al u e as center Parameter standard error as spread Standard Error , meas u res parameter u ncertaint y INTRODUCTION TO LINEAR MODELING IN PYTHON

  29. Comp u ting Standard Errors df = pd.DataFrame(dict(times=x_data, distances=y_data)) model_fit = ols(formula="distances ~ times", data=df).fit() a1 = model_fit.params['times'] a0 = model_fit.params['Intercept'] slope = a1 intercept = a0 INTRODUCTION TO LINEAR MODELING IN PYTHON

  30. Comp u ting Standard Errors e0 = model_fit.bse['Intercept'] e1 = model_fit.bse['times'] standard_error_of_intercept = e0 standard_error_of_slope = e1 INTRODUCTION TO LINEAR MODELING IN PYTHON

  31. 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