diff --git a/data/collegeAdmissions.csv b/data/collegeAdmissions.csv new file mode 100644 index 00000000000..2b16b704372 --- /dev/null +++ b/data/collegeAdmissions.csv @@ -0,0 +1,401 @@ +admit,gre,gpa,rank +0,380,3.61,3 +1,660,3.67,3 +1,800,4,1 +1,640,3.19,4 +0,520,2.93,4 +1,760,3,2 +1,560,2.98,1 +0,400,3.08,2 +1,540,3.39,3 +0,700,3.92,2 +0,800,4,4 +0,440,3.22,1 +1,760,4,1 +0,700,3.08,2 +1,700,4,1 +0,480,3.44,3 +0,780,3.87,4 +0,360,2.56,3 +0,800,3.75,2 +1,540,3.81,1 +0,500,3.17,3 +1,660,3.63,2 +0,600,2.82,4 +0,680,3.19,4 +1,760,3.35,2 +1,800,3.66,1 +1,620,3.61,1 +1,520,3.74,4 +1,780,3.22,2 +0,520,3.29,1 +0,540,3.78,4 +0,760,3.35,3 +0,600,3.4,3 +1,800,4,3 +0,360,3.14,1 +0,400,3.05,2 +0,580,3.25,1 +0,520,2.9,3 +1,500,3.13,2 +1,520,2.68,3 +0,560,2.42,2 +1,580,3.32,2 +1,600,3.15,2 +0,500,3.31,3 +0,700,2.94,2 +1,460,3.45,3 +1,580,3.46,2 +0,500,2.97,4 +0,440,2.48,4 +0,400,3.35,3 +0,640,3.86,3 +0,440,3.13,4 +0,740,3.37,4 +1,680,3.27,2 +0,660,3.34,3 +1,740,4,3 +0,560,3.19,3 +0,380,2.94,3 +0,400,3.65,2 +0,600,2.82,4 +1,620,3.18,2 +0,560,3.32,4 +0,640,3.67,3 +1,680,3.85,3 +0,580,4,3 +0,600,3.59,2 +0,740,3.62,4 +0,620,3.3,1 +0,580,3.69,1 +0,800,3.73,1 +0,640,4,3 +0,300,2.92,4 +0,480,3.39,4 +0,580,4,2 +0,720,3.45,4 +0,720,4,3 +0,560,3.36,3 +1,800,4,3 +0,540,3.12,1 +1,620,4,1 +0,700,2.9,4 +0,620,3.07,2 +0,500,2.71,2 +0,380,2.91,4 +1,500,3.6,3 +0,520,2.98,2 +0,600,3.32,2 +0,600,3.48,2 +0,700,3.28,1 +1,660,4,2 +0,700,3.83,2 +1,720,3.64,1 +0,800,3.9,2 +0,580,2.93,2 +1,660,3.44,2 +0,660,3.33,2 +0,640,3.52,4 +0,480,3.57,2 +0,700,2.88,2 +0,400,3.31,3 +0,340,3.15,3 +0,580,3.57,3 +0,380,3.33,4 +0,540,3.94,3 +1,660,3.95,2 +1,740,2.97,2 +1,700,3.56,1 +0,480,3.13,2 +0,400,2.93,3 +0,480,3.45,2 +0,680,3.08,4 +0,420,3.41,4 +0,360,3,3 +0,600,3.22,1 +0,720,3.84,3 +0,620,3.99,3 +1,440,3.45,2 +0,700,3.72,2 +1,800,3.7,1 +0,340,2.92,3 +1,520,3.74,2 +1,480,2.67,2 +0,520,2.85,3 +0,500,2.98,3 +0,720,3.88,3 +0,540,3.38,4 +1,600,3.54,1 +0,740,3.74,4 +0,540,3.19,2 +0,460,3.15,4 +1,620,3.17,2 +0,640,2.79,2 +0,580,3.4,2 +0,500,3.08,3 +0,560,2.95,2 +0,500,3.57,3 +0,560,3.33,4 +0,700,4,3 +0,620,3.4,2 +1,600,3.58,1 +0,640,3.93,2 +1,700,3.52,4 +0,620,3.94,4 +0,580,3.4,3 +0,580,3.4,4 +0,380,3.43,3 +0,480,3.4,2 +0,560,2.71,3 +1,480,2.91,1 +0,740,3.31,1 +1,800,3.74,1 +0,400,3.38,2 +1,640,3.94,2 +0,580,3.46,3 +0,620,3.69,3 +1,580,2.86,4 +0,560,2.52,2 +1,480,3.58,1 +0,660,3.49,2 +0,700,3.82,3 +0,600,3.13,2 +0,640,3.5,2 +1,700,3.56,2 +0,520,2.73,2 +0,580,3.3,2 +0,700,4,1 +0,440,3.24,4 +0,720,3.77,3 +0,500,4,3 +0,600,3.62,3 +0,400,3.51,3 +0,540,2.81,3 +0,680,3.48,3 +1,800,3.43,2 +0,500,3.53,4 +1,620,3.37,2 +0,520,2.62,2 +1,620,3.23,3 +0,620,3.33,3 +0,300,3.01,3 +0,620,3.78,3 +0,500,3.88,4 +0,700,4,2 +1,540,3.84,2 +0,500,2.79,4 +0,800,3.6,2 +0,560,3.61,3 +0,580,2.88,2 +0,560,3.07,2 +0,500,3.35,2 +1,640,2.94,2 +0,800,3.54,3 +0,640,3.76,3 +0,380,3.59,4 +1,600,3.47,2 +0,560,3.59,2 +0,660,3.07,3 +1,400,3.23,4 +0,600,3.63,3 +0,580,3.77,4 +0,800,3.31,3 +1,580,3.2,2 +1,700,4,1 +0,420,3.92,4 +1,600,3.89,1 +1,780,3.8,3 +0,740,3.54,1 +1,640,3.63,1 +0,540,3.16,3 +0,580,3.5,2 +0,740,3.34,4 +0,580,3.02,2 +0,460,2.87,2 +0,640,3.38,3 +1,600,3.56,2 +1,660,2.91,3 +0,340,2.9,1 +1,460,3.64,1 +0,460,2.98,1 +1,560,3.59,2 +0,540,3.28,3 +0,680,3.99,3 +1,480,3.02,1 +0,800,3.47,3 +0,800,2.9,2 +1,720,3.5,3 +0,620,3.58,2 +0,540,3.02,4 +0,480,3.43,2 +1,720,3.42,2 +0,580,3.29,4 +0,600,3.28,3 +0,380,3.38,2 +0,420,2.67,3 +1,800,3.53,1 +0,620,3.05,2 +1,660,3.49,2 +0,480,4,2 +0,500,2.86,4 +0,700,3.45,3 +0,440,2.76,2 +1,520,3.81,1 +1,680,2.96,3 +0,620,3.22,2 +0,540,3.04,1 +0,800,3.91,3 +0,680,3.34,2 +0,440,3.17,2 +0,680,3.64,3 +0,640,3.73,3 +0,660,3.31,4 +0,620,3.21,4 +1,520,4,2 +1,540,3.55,4 +1,740,3.52,4 +0,640,3.35,3 +1,520,3.3,2 +1,620,3.95,3 +0,520,3.51,2 +0,640,3.81,2 +0,680,3.11,2 +0,440,3.15,2 +1,520,3.19,3 +1,620,3.95,3 +1,520,3.9,3 +0,380,3.34,3 +0,560,3.24,4 +1,600,3.64,3 +1,680,3.46,2 +0,500,2.81,3 +1,640,3.95,2 +0,540,3.33,3 +1,680,3.67,2 +0,660,3.32,1 +0,520,3.12,2 +1,600,2.98,2 +0,460,3.77,3 +1,580,3.58,1 +1,680,3,4 +1,660,3.14,2 +0,660,3.94,2 +0,360,3.27,3 +0,660,3.45,4 +0,520,3.1,4 +1,440,3.39,2 +0,600,3.31,4 +1,800,3.22,1 +1,660,3.7,4 +0,800,3.15,4 +0,420,2.26,4 +1,620,3.45,2 +0,800,2.78,2 +0,680,3.7,2 +0,800,3.97,1 +0,480,2.55,1 +0,520,3.25,3 +0,560,3.16,1 +0,460,3.07,2 +0,540,3.5,2 +0,720,3.4,3 +0,640,3.3,2 +1,660,3.6,3 +1,400,3.15,2 +1,680,3.98,2 +0,220,2.83,3 +0,580,3.46,4 +1,540,3.17,1 +0,580,3.51,2 +0,540,3.13,2 +0,440,2.98,3 +0,560,4,3 +0,660,3.67,2 +0,660,3.77,3 +1,520,3.65,4 +0,540,3.46,4 +1,300,2.84,2 +1,340,3,2 +1,780,3.63,4 +1,480,3.71,4 +0,540,3.28,1 +0,460,3.14,3 +0,460,3.58,2 +0,500,3.01,4 +0,420,2.69,2 +0,520,2.7,3 +0,680,3.9,1 +0,680,3.31,2 +1,560,3.48,2 +0,580,3.34,2 +0,500,2.93,4 +0,740,4,3 +0,660,3.59,3 +0,420,2.96,1 +0,560,3.43,3 +1,460,3.64,3 +1,620,3.71,1 +0,520,3.15,3 +0,620,3.09,4 +0,540,3.2,1 +1,660,3.47,3 +0,500,3.23,4 +1,560,2.65,3 +0,500,3.95,4 +0,580,3.06,2 +0,520,3.35,3 +0,500,3.03,3 +0,600,3.35,2 +0,580,3.8,2 +0,400,3.36,2 +0,620,2.85,2 +1,780,4,2 +0,620,3.43,3 +1,580,3.12,3 +0,700,3.52,2 +1,540,3.78,2 +1,760,2.81,1 +0,700,3.27,2 +0,720,3.31,1 +1,560,3.69,3 +0,720,3.94,3 +1,520,4,1 +1,540,3.49,1 +0,680,3.14,2 +0,460,3.44,2 +1,560,3.36,1 +0,480,2.78,3 +0,460,2.93,3 +0,620,3.63,3 +0,580,4,1 +0,800,3.89,2 +1,540,3.77,2 +1,680,3.76,3 +1,680,2.42,1 +1,620,3.37,1 +0,560,3.78,2 +0,560,3.49,4 +0,620,3.63,2 +1,800,4,2 +0,640,3.12,3 +0,540,2.7,2 +0,700,3.65,2 +1,540,3.49,2 +0,540,3.51,2 +0,660,4,1 +1,480,2.62,2 +0,420,3.02,1 +1,740,3.86,2 +0,580,3.36,2 +0,640,3.17,2 +0,640,3.51,2 +1,800,3.05,2 +1,660,3.88,2 +1,600,3.38,3 +1,620,3.75,2 +1,460,3.99,3 +0,620,4,2 +0,560,3.04,3 +0,460,2.63,2 +0,700,3.65,2 +0,600,3.89,3 diff --git a/examples/linregProstate.py b/examples/linregProstate.py index 32a8f9d2842..e8bc43f6ac2 100644 --- a/examples/linregProstate.py +++ b/examples/linregProstate.py @@ -19,7 +19,8 @@ pd.set_option('display.width', 1000) # Get data -url = 'http://statweb.stanford.edu/~tibs/ElemStatLearn/datasets/prostate.data' +#url = 'http://statweb.stanford.edu/~tibs/ElemStatLearn/datasets/prostate.data' +url = 'https://web.stanford.edu/~hastie/ElemStatLearn/datasets/prostate.data' df = pd.read_csv(url, sep='\t', header=0) # skip the column of indices df = df.drop('Unnamed: 0', axis=1) @@ -45,7 +46,7 @@ def L2loss(yhat, ytest): stderr = np.std(sqerr) / np.sqrt(ntest) return (mse, stderr) -def fitAndPredict(X, y, train_mask): +def trainAndTest(X, y, train_mask): # partition into train/test set Xtrain = X[train_mask] ytrain = y[train_mask] @@ -95,9 +96,10 @@ def fitAndPredict(X, y, train_mask): ''' print('vanilla model on scaled X') -fitAndPredict(X, y, istrain) +trainAndTest(X, y, istrain) -# Matches HTF book +# Matches HTF Table 3.2 book +# https://web.stanford.edu/~hastie/ElemStatLearn/ #['intercept:2.465', 'lcavol:0.676', 'lweight:0.262', 'age:-0.141', 'lbph:0.209', #'svi:0.304', 'lcp:-0.287', 'gleason:-0.021', 'pgg45:0.266'] @@ -147,8 +149,34 @@ def fitAndPredict(X, y, train_mask): #3 1 0 0 0 0 -2.178174 -0.812191 -0.791989 -1.030029 -0.867655 -0.868957 #4 1 0 0 0 0 -0.510513 -0.461218 -0.251933 -1.030029 -0.867655 -0.868957 +pd.set_option('precision', 1) +X.head() +pd.set_option('precision', 3) + + +# Intercept svi[T.1] gleason[T.7] gleason[T.8] gleason[T.9] lcavol lweight age lbph lcp pgg45 +# 0 1.0 0.0 0.0 0.0 0.0 -1.6 -2.0 -1.9 -1.0 -0.9 -0.9 +# 1 1.0 0.0 0.0 0.0 0.0 -2.0 -0.7 -0.8 -1.0 -0.9 -0.9 +# 2 1.0 0.0 1.0 0.0 0.0 -1.6 -2.2 1.4 -1.0 -0.9 -0.2 +# 3 1.0 0.0 0.0 0.0 0.0 -2.2 -0.8 -0.8 -1.0 -0.9 -0.9 +# 4 1.0 0.0 0.0 0.0 0.0 -0.5 -0.5 -0.3 -1.0 -0.9 -0.9 + +# print summary statistics of each feature +X.describe() + + +# Intercept svi[T.1] gleason[T.7] gleason[T.8] gleason[T.9] lcavol lweight age lbph lcp pgg45 +# count 97.0 97.000000 97.000000 97.000000 97.000000 9.700000e+01 9.700000e+01 9.700000e+01 9.700000e+01 9.700000e+01 9.700000e+01 +# mean 1.0 0.216495 0.577320 0.010309 0.051546 3.204767e-17 -3.170431e-16 4.131861e-16 -2.432190e-17 3.662591e-17 5.636957e-17 +# std 0.0 0.413995 0.496552 0.101535 0.222258 1.005195e+00 1.005195e+00 1.005195e+00 1.005195e+00 1.005195e+00 1.005195e+00 +# min 1.0 0.000000 0.000000 0.000000 0.000000 -2.300218e+00 -2.942386e+00 -3.087227e+00 -1.030029e+00 -8.676552e-01 -8.689573e-01 +# 25% 1.0 0.000000 0.000000 0.000000 0.000000 -7.139973e-01 -5.937689e-01 -5.219612e-01 -1.030029e+00 -8.676552e-01 -8.689573e-01 +# 50% 1.0 0.000000 1.000000 0.000000 0.000000 8.264956e-02 -1.392703e-02 1.531086e-01 1.383966e-01 -4.450983e-01 -3.343557e-01 +# 75% 1.0 0.000000 1.000000 0.000000 0.000000 6.626939e-01 5.806076e-01 5.581506e-01 1.010033e+00 9.762744e-01 5.566470e-01 +# max + print('categorical') -fitAndPredict(X, y, istrain) +trainAndTest(X, y, istrain) #['Intercept:2.218', 'svi[T.1]:0.707', 'gleason[T.7]:0.183', 'gleason[T.8]:0.727', 'gleason[T.9]:-0.497', #'lcavol:0.666', 'lweight:0.275', 'age:-0.164', 'lbph:0.19', 'lcp:-0.341', 'pgg45:0.31'] @@ -179,7 +207,7 @@ def fitAndPredict(X, y, train_mask): ''' print('interaction ') -fitAndPredict(X, y, istrain) +trainAndTest(X, y, istrain) diff --git a/examples/logregDemoCollege.py b/examples/logregDemoCollege.py index 3538f87c0e6..2fe27027da9 100644 --- a/examples/logregDemoCollege.py +++ b/examples/logregDemoCollege.py @@ -3,7 +3,8 @@ # But we modified it to use scikit-learn intsead of statsmodels for fitting. # We also borrowed some code from # https://github.com/justmarkham/gadsdc1/blob/master/logistic_assignment/kevin_logistic_sklearn.ipynb -# For details on how to do this analysis in R, see http://www.ats.ucla.edu/stat/r/dae/logit.htm +# For details on how to do this analysis in R, see +# https://stats.idre.ucla.edu/r/dae/logit-regression/ import matplotlib.pyplot as plt import numpy as np @@ -20,8 +21,10 @@ # Prevent numpy from printing too many digits np.set_printoptions(precision=3) -# read the data in -df = pd.read_csv("http://www.ats.ucla.edu/stat/data/binary.csv") +# read the data +#fname = "http://www.ats.ucla.edu/stat/data/binary.csv" +fname = "https://stats.idre.ucla.edu/stat/data/binary.csv" +df = pd.read_csv(fname) #df.to_csv('/Users/kpmurphy/github/pmtk3/data/collegeAdmissions.csv') @@ -59,6 +62,7 @@ df.hist() plt.show() +pd.set_option('precision', 3) df.groupby('prestige').mean() # admit gre gpa @@ -108,7 +112,7 @@ # Let's use patsy notation to handle dummy variables. # This adds a column of 1s y, X = dmatrices('admit ~ gre + gpa + C(prestige)', df, return_type="dataframe") -y = np.ravel(y) +y = np.ravel(y) # convert from pandas series to numpy array X.head() # Intercept C(prestige)[T.2] C(prestige)[T.3] C(prestige)[T.4] gre gpa #0 1 0 1 0 380 3.61