10.5 Computer Examples
from sie import *
data=load_data('data/shoesize.xls')
data.head()
Show expected output
Index Gender Size Height
0 1 F 5.5 60
1 2 F 6.0 60
2 3 F 7.0 60
3 4 F 8.0 60
4 5 F 8.0 60import random
random.seed(102)
rows = random.sample(data.index, 10)
newdata=data.ix[rows]
data=newdata
data
Show expected output
Index Gender Size Height
60 61 F 7.0 64
251 252 M 9.0 70
69 70 F 8.0 64
290 291 M 11.0 71
247 248 M 12.0 69
156 157 F 9.5 68
231 232 M 10.0 69
17 18 F 6.5 61
216 217 M 10.0 68
252 253 M 9.0 70plot(data['Height'],data['Size'],'o')
gca().set_xlim([60,72])
gca().set_ylim([4,14])
xlabel('Height [inches]')
ylabel('Shoe Size')
Show expected output
<matplotlib.text.Text at 0x10adae0d0>
result=regression('Size ~ Height',data)
Show expected output
<matplotlib.figure.Figure at 0x10d200710>
Show expected output
<matplotlib.figure.Figure at 0x10d702610>
plot(data['Height'],data['Size'],'o')
h=linspace(60,72,10)
plot(h,result['_Predict'](Height=h),'-')
gca().set_xlim([60,72])
gca().set_ylim([4,14])
xlabel('Height [inches]')
ylabel('Shoe Size')
b=result.Intercept.mean()
m=result.Height.mean()
if b>0:
text(62,12,'$y=%.3f x + %.3f$' % (m,b),fontsize=30)
else:
text(62,12,'$y=%.3f x %.3f$' % (m,b),fontsize=30)

data=load_data('data/sat.csv')
result=regression('total ~ expenditure',data)
Show expected output
<matplotlib.figure.Figure at 0x1109156d0>
Show expected output
<matplotlib.figure.Figure at 0x110953110>
plot(data['expenditure'],data['total'],'o')
xlabel('Expenditure [per pupil, thousands]')
ylabel('SAT Total')
h=linspace(3,10,10)
plot(h,result['_Predict'](expenditure=h),'-')
b=result.Intercept.mean()
m=result.expenditure.mean()
if b>0:
text(4.5,1125,'$y=%.3f x + %.3f$' % (m,b),fontsize=30)
else:
text(4.5,1125,'$y=%.3f x %.3f$' % (m,b),fontsize=30)

result=regression('percent_taking ~ expenditure',data)
Show expected output
<matplotlib.figure.Figure at 0x111cfff10>
Show expected output
<matplotlib.figure.Figure at 0x111867750>
plot(data['expenditure'],data['percent_taking'],'o')
xlabel('Expenditure [per pupil, thousands]')
ylabel('SAT Total')
h=linspace(3,10,10)
plot(h,result['_Predict'](expenditure=h),'-')
b=result.Intercept.mean()
m=result.expenditure.mean()
if b>0:
text(4.5,85,'$y=%.3f x + %.3f$' % (m,b),fontsize=30)
else:
text(4.5,85,'$y=%.3f x %.3f$' % (m,b),fontsize=30)

result=regression('total ~ expenditure + percent_taking',data)
Show expected output
<matplotlib.figure.Figure at 0x1107f5fd0>
Show expected output
<matplotlib.figure.Figure at 0x10d70a690>
Show expected output
<matplotlib.figure.Figure at 0x110fbcf90>
Adapted from Statistical Inference for Everyone, by Brian Blais (Bryant University), licensed under CC BY-SA 4.0 (dual-licensed under the GNU FDL 1.2 or later; this adaptation uses the CC BY-SA grant). Changes were made; this adaptation is distributed under the same license. License: CC-BY-SA-4.0.